Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
scip-ctags: add support for C
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnugget committed Nov 6, 2023
1 parent 38dd49e commit e0c3393
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
; Make use of @local

(translation_unit (declaration (init_declarator declarator: (_) @descriptor.term)))
(function_definition body: (_) @local)

(enum_specifier name: (_) @descriptor.type body: (enumerator_list (enumerator name: (_) @descriptor.term)) @descriptor.scope)
(parameter_list) @local

(field_declaration declarator: [
(pointer_declarator (field_identifier) @descriptor.term)
(field_identifier) @descriptor.term
])
(function_definition (function_declarator declarator: (_) @descriptor.method))
(function_declarator declarator: (_) @descriptor.method @kind.function)

(pointer_declarator declarator: ((identifier) @descriptor.term @kind.variable))

(init_declarator
declarator: (identifier) @descriptor.term @kind.variable
value: (_))

(array_declarator
declarator: (identifier) @descriptor.term @kind.variable
size: (_))

(declaration
(type_qualifier)?
declarator: ((identifier) @descriptor.term @kind.variable))

;; Enums
(enum_specifier
name: (_)?
body: (enumerator_list
(enumerator name: (_) @descriptor.term @kind.enummember))) ;; <-- capture enum kind

(enum_specifier
name: (type_identifier) @descriptor.type @kind.enum ;; <-- capture enum kind
body: (_))

;; fields (inside structs, or unions, ...)
(field_declaration_list
(field_declaration declarator: [
(pointer_declarator (field_identifier) @descriptor.term @kind.field)
(field_identifier) @descriptor.term @kind.field]))

;; structs
(struct_specifier
name: (type_identifier) @descriptor.type @kind.struct ;; <-- capture struct kidn
body: (_)) @scope

;; typedefs
(type_definition
type: (_)
declarator: (type_identifier) @descriptor.term @kind.typealias)

;; macros
(preproc_def
name: (identifier) @descriptor.term @kind.macro)

;; union
(union_specifier
name: (type_identifier) @descriptor.type @kind.union
body: (_)) @scope
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ mod test {
generate_tags_and_snapshot!(Scip, test_scip_javascript, "globals.js");
generate_tags_and_snapshot!(Scip, test_scip_javascript_object, "javascript-object.js");

generate_tags_and_snapshot!(All, test_tags_c_example, test_scip_c_example, "example.c");

// Test to make sure that kinds are the override behavior
generate_tags_and_snapshot!(All, test_tags_go_diff, test_scip_go_diff, "go-diff.go");
generate_tags_and_snapshot!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ expression: dumped
use walkdir::WalkDir;

static LANGUAGE: &str = "Rust";
// ^^^^^^^^ definition scip-ctags LANGUAGE.
// ^^^^^^^^ definition(Variable) scip-ctags LANGUAGE.
const THRESHOLD: i32 = 10;
// ^^^^^^^^^ definition(Constant) scip-ctags THRESHOLD.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
---
source: crates/scip-syntax/src/lib.rs
expression: dumped
---
#include <stdlib.h>
#include <sys/types.h>

#define BUFSIZE 4096
// ^^^^^^^ definition(Macro) scip-ctags BUFSIZE.

const char *name = "com.horsegraph.connection";
// ^^^^ definition(Variable) scip-ctags name.
const char *author = "Petri & Thorsten";
// ^^^^^^ definition(Variable) scip-ctags author.
const int count;
// ^^^^^ definition(Variable) scip-ctags count.
int count2;
// ^^^^^^ definition(Variable) scip-ctags count2.
const int age = 28;
// ^^^ definition(Field) scip-ctags age.
static uint sweet_sweet_numbers[5] = {23, 420, 69, 42, 7};
// ^^^^^^^^^^^^^^^^^^^ definition(Variable) scip-ctags sweet_sweet_numbers.

const int *ptr1 = &count;
// ^^^^ definition(Variable) scip-ctags ptr1.
const int **ptrptr1 = &ptr1;
// ^^^^^^^ definition(Variable) scip-ctags ptrptr1.
const int ***ptrptrptr1 = &ptrptr1;
// ^^^^^^^^^^ definition(Variable) scip-ctags ptrptrptr1.

enum { BLACK, RED };
// ^^^^^ definition(EnumMember) scip-ctags BLACK.
// ^^^ definition(EnumMember) scip-ctags RED.

enum animal {
// ^^^^^^ definition(Enum) scip-ctags animal#
ANIMAL_TOUCAN,
// ^^^^^^^^^^^^^ definition(EnumMember) scip-ctags ANIMAL_TOUCAN.
ANIMAL_TIGER = 1,
// ^^^^^^^^^^^^ definition(EnumMember) scip-ctags ANIMAL_TIGER.
ANIMAL_TIGGER = ANIMAL_TIGER,
// ^^^^^^^^^^^^^ definition(EnumMember) scip-ctags ANIMAL_TIGGER.
ANIMAL_HORSE,
// ^^^^^^^^^^^^ definition(EnumMember) scip-ctags ANIMAL_HORSE.
ANIMAL_GIRAFFE,
// ^^^^^^^^^^^^^^ definition(EnumMember) scip-ctags ANIMAL_GIRAFFE.
ANIMAL_GOPHER = 99,
// ^^^^^^^^^^^^^ definition(EnumMember) scip-ctags ANIMAL_GOPHER.
ANIMAL_ORANGUTAN
// ^^^^^^^^^^^^^^^^ definition(EnumMember) scip-ctags ANIMAL_ORANGUTAN.
};

typedef enum instrument {
// ^^^^^^^^^^ definition(Enum) scip-ctags instrument#
INSTRUMENT_GUITAR,
// ^^^^^^^^^^^^^^^^^ definition(EnumMember) scip-ctags INSTRUMENT_GUITAR.
INSTRUMENT_KEYTAR,
// ^^^^^^^^^^^^^^^^^ definition(EnumMember) scip-ctags INSTRUMENT_KEYTAR.
INSTRUMENT_SITAR
// ^^^^^^^^^^^^^^^^ definition(EnumMember) scip-ctags INSTRUMENT_SITAR.
} Instrument;
// ^^^^^^^^^^ definition(TypeAlias) scip-ctags Instrument.

union {
char *hobby;
// ^^^^^ definition(Field) scip-ctags hobby.
int age; // <-- TODO: this will be tagged as a Constant because it's the same
// ^^^ definition(Field) scip-ctags age.
// symbol as the `const int age` above.
} person;
// ^^^^^^ definition(Variable) scip-ctags person.

union object {
// ^^^^^^ definition(Union) scip-ctags object#
char *name;
// ^^^^ definition(Field) scip-ctags object#name.
int value;
// ^^^^^ definition(Field) scip-ctags object#value.
int age;
// ^^^ definition(Field) scip-ctags object#age.
} obj1, obj2, *obj3;
// ^^^^ definition(Variable) scip-ctags obj1.
// ^^^^ definition(Variable) scip-ctags obj2.
// ^^^^ definition(Variable) scip-ctags obj3.

const union object2 {
// ^^^^^^^ definition(Union) scip-ctags object2#
char name;
// ^^^^ definition(Field) scip-ctags object2#name.
} obj4;
// ^^^^ definition(Variable) scip-ctags obj4.

struct connection {
// ^^^^^^^^^^ definition(Struct) scip-ctags connection#
int complete;
// ^^^^^^^^ definition(Field) scip-ctags connection#complete.
int fd;
// ^^ definition(Field) scip-ctags connection#fd.
int bufsize;
// ^^^^^^^ definition(Field) scip-ctags connection#bufsize.
char *buffer;
// ^^^^^^ definition(Field) scip-ctags connection#buffer.
char *url;
// ^^^ definition(Field) scip-ctags connection#url.
};

typedef struct connection Connection;
// ^^^^^^^^^^ definition(TypeAlias) scip-ctags Connection.

typedef struct computer {
// ^^^^^^^^ definition(Struct) scip-ctags computer#
int cores;
// ^^^^^ definition(Field) scip-ctags computer#cores.
} Computer;
// ^^^^^^^^ definition(TypeAlias) scip-ctags Computer.

typedef struct {
int number;
// ^^^^^^ definition(Field) scip-ctags number.
} NoName;
// ^^^^^^ definition(TypeAlias) scip-ctags NoName.

struct outer {
// ^^^^^ definition(Struct) scip-ctags outer#
struct inner {
// ^^^^^ definition(Struct) scip-ctags outer#inner#
int x;
// ^ definition(Field) scip-ctags outer#inner#x.
int y;
// ^ definition(Field) scip-ctags outer#inner#y.
} b;
// ^ definition(Field) scip-ctags outer#b.
};

// Prototype
struct connection *new_connection(int fd);
// ^^^^^^^^^^^^^^ definition(Function) scip-ctags new_connection().
// Implementation
struct connection *new_connection(int fd) {
// ^^^^^^^^^^^^^^ definition(Function) scip-ctags new_connection().
struct connection *c = malloc(sizeof(struct connection));
if (c == NULL) {
return NULL;
}

c->url = NULL;
c->complete = 0;
c->fd = fd;

c->buffer = calloc(BUFSIZE, sizeof(char));
if (c->buffer == NULL) {
free(c);
return NULL;
}
c->bufsize = BUFSIZE;

return c;
}

static void free_connection(struct connection *c) {
// ^^^^^^^^^^^^^^^ definition(Function) scip-ctags free_connection().
if (c->buffer) {
free(c->buffer);
}

if (c->url) {
free(c->url);
}

free(c);
}

// Prototype
int returns_int();
// ^^^^^^^^^^^ definition(Function) scip-ctags returns_int().
// Implementation
int returns_int() { return 12; }
// ^^^^^^^^^^^ definition(Function) scip-ctags returns_int().

int main() { return 0; }
// ^^^^ definition(Function) scip-ctags main().

int k_and_r(s, f)
// ^^^^^^^ definition(Function) scip-ctags k_and_r().
char *s;
// ^ definition(Variable) scip-ctags s.
float f;
// ^ definition(Variable) scip-ctags f.
{ return 5; }

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ expression: dumped
---
// Traditional variable declaration
var traditionalVar = "Hello, I'm an old-style variable";
// ^^^^^^^^^^^^^^ definition scip-ctags traditionalVar.
// ^^^^^^^^^^^^^^ definition(Variable) scip-ctags traditionalVar.

// Let variable declaration
let scopedLetVar = "Hello, I'm a block-scoped variable";
// ^^^^^^^^^^^^ definition scip-ctags scopedLetVar.
// ^^^^^^^^^^^^ definition(Variable) scip-ctags scopedLetVar.

// Constant variable declaration
const constantVar = "Hello, I'm a constant variable";
// ^^^^^^^^^^^ definition scip-ctags constantVar.
// ^^^^^^^^^^^ definition(Variable) scip-ctags constantVar.

// Function declaration
function functionDeclaration() {
Expand All @@ -22,13 +22,13 @@ expression: dumped

// Anonymous function declaration
var anonymousFunction = function() {
// ^^^^^^^^^^^^^^^^^ definition scip-ctags anonymousFunction.
// ^^^^^^^^^^^^^^^^^ definition(Variable) scip-ctags anonymousFunction.
return "Hello, I'm an anonymous function";
};

// ES6 arrow function declaration
const arrowFunction = () => {
// ^^^^^^^^^^^^^ definition scip-ctags arrowFunction.
// ^^^^^^^^^^^^^ definition(Variable) scip-ctags arrowFunction.
return "Hello, I'm an arrow function";
};

Expand All @@ -43,7 +43,7 @@ expression: dumped

// Object declaration
var objectDeclaration = {
// ^^^^^^^^^^^^^^^^^ definition scip-ctags objectDeclaration.
// ^^^^^^^^^^^^^^^^^ definition(Variable) scip-ctags objectDeclaration.
message: "Hello, I'm an object declaration"
// ^^^^^^^ definition(Property) scip-ctags objectDeclaration.message.
};
Expand All @@ -54,11 +54,11 @@ expression: dumped
this.message = "Hello, I'm an object constructor";
}
var objectConstructed = new ObjectConstructor();
// ^^^^^^^^^^^^^^^^^ definition scip-ctags objectConstructed.
// ^^^^^^^^^^^^^^^^^ definition(Variable) scip-ctags objectConstructed.

// ES6 method shorthand in object declaration
var objectWithMethods = {
// ^^^^^^^^^^^^^^^^^ definition scip-ctags objectWithMethods.
// ^^^^^^^^^^^^^^^^^ definition(Variable) scip-ctags objectWithMethods.
method() {
return "Hello, I'm a method in an object";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ expression: dumped
# TODO: Deal with duplicates (bruh = 10; bruh = 10;) being marked as definitions

bruh = 10
//^^^^ definition scip-ctags bruh.
//^^^^ definition(Variable) scip-ctags bruh.

class Bruh(object):
// ^^^^ definition(Class) scip-ctags Bruh#
a: int
// ^ definition scip-ctags Bruh#a.
// ^ definition(Variable) scip-ctags Bruh#a.

def __init__(self) -> None:
// ^^^^^^^^ definition(Method) scip-ctags Bruh#__init__().
Expand All @@ -26,7 +26,7 @@ expression: dumped

if 1 == 1:
should_show_ifs = False
// ^^^^^^^^^^^^^^^ definition scip-ctags should_show_ifs.
// ^^^^^^^^^^^^^^^ definition(Variable) scip-ctags should_show_ifs.

# Don't show from whiles / fors
while False:
Expand Down Expand Up @@ -98,12 +98,12 @@ expression: dumped


foo, bar, baz = 1, 2, 3
//^^^ definition scip-ctags foo.
// ^^^ definition scip-ctags bar.
// ^^^ definition scip-ctags baz.
//^^^ definition(Variable) scip-ctags foo.
// ^^^ definition(Variable) scip-ctags bar.
// ^^^ definition(Variable) scip-ctags baz.

# semi-colons haha
foo = 1; bar = foo
//^^^ definition scip-ctags foo.
// ^^^ definition scip-ctags bar.
//^^^ definition(Variable) scip-ctags foo.
// ^^^ definition(Variable) scip-ctags bar.

Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ expression: dumped
}

var global1 = 0;
// ^^^^^^^ definition scip-ctags global1.
// ^^^^^^^ definition(Variable) scip-ctags global1.
var global2;
// ^^^^^^^ definition scip-ctags global2.
// ^^^^^^^ definition(Variable) scip-ctags global2.

function func() {
// ^^^^ definition(Function) scip-ctags func().
Expand All @@ -54,7 +54,7 @@ expression: dumped
}

var myObject = {
// ^^^^^^^^ definition scip-ctags myObject.
// ^^^^^^^^ definition(Variable) scip-ctags myObject.
myProperty: "value",
// ^^^^^^^^^^ definition(Property) scip-ctags myObject.myProperty.

Expand Down
Loading

0 comments on commit e0c3393

Please sign in to comment.