-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueries.py
152 lines (130 loc) · 3.47 KB
/
queries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
Contains all tree-sitter queries necessary to find functions, types, typedefs and dependencies.
"""
# Example:
#
# `typedef struct name {//members} alias;`
# `typedef union name {// members} alias;`
# Will match against arbitrary number of aliases, with or without arbitrary number of pointers.
TYPEDEF_SPECIFIER_QUERY = """
(type_definition
type: (_
name: (type_identifier)@name
body: (_)@fields )@specifier
declarator: (_)@alias) @node
"""
# Example:
# `typedef struct name alias;`
TYPEDEF_QUERY = """
(type_definition
type: (_
name: (type_identifier)@name)@specifier
declarator: (_)@alias)@node
"""
# Example:
# `typedef long name;`
TYPEDEF_SIZED = """
(type_definition
type: (sized_type_specifier)
declarator: (type_identifier)@name)@node
"""
# Example:
# `typedef int name;`
TYPEDEF_PRIMITIVE = """
(type_definition
type: (primitive_type)
declarator: (type_identifier)@name)@node
"""
# Matches against the field containining the name (without pointers) when used against a TYPEDEF matched node.
ALIAS_NAME_QUERY = """
declarator: (type_identifier)@alias_name
"""
# Example: ` struct name { // Member definition };`
STRUCT_SPECIFIER_QUERY = """
(struct_specifier
name: (type_identifier)@name
body: (field_declaration_list))@node
"""
# Example: `return_type foo(const a *, char b);`
FUNCTION_DECLARATION = """
(declaration
type: (_) @return_type) @node
"""
FUNCTION_DEFINITION = """
(function_definition
type: (_) @return_type)@node
"""
# Example: `return_type name( // Parameters ); `
FUNCTION_NAME = """
declarator: (function_declarator
declarator: (identifier)@name)@node
"""
# Example: `enum name { // Members };`
ENUM_SPECIFIER_QUERY = """
(enum_specifier
name: (type_identifier)@name
body: (enumerator_list)) @node
"""
# Matches against a typed field in a struct node.
# Example:
# `struct foo { enum name; };`
# `struct foo { struct name; };`
SPECIFIER_FIELD_DECLARATION = """
(field_declaration
type: (_
name: (type_identifier)@name))
"""
# Example: `struct foo { name foo; };`
# A field declaration is one instance of `name foo` within a field list. Does not match
FIELD_DECLARATION = """
(field_declaration
type: (type_identifier)@name)
"""
# Example: `void foo(name a, name b)`
# A Parameter declaration is one instance of `name a` within a parameter list
# `(name a, name b)`
PARAMETER_DECLARATION = """
(parameter_declaration
type: (type_identifier)@name)
"""
# Matches against a typed parameter in a function node. Example:
# `void foo(enum name)`
# `void foo(struct name)`
SPECIFIER_PARAMETER_DECLARATION = """
(parameter_declaration
type: (_
name: (type_identifier)@name)@type)
"""
# Example: ``void foo(struct name)`
# Catches any error nodes in the tree
ERROR_NODE = """
(ERROR) @error
"""
QUERY = " ".join(
f"({s})"
for s in (
FUNCTION_DECLARATION,
FUNCTION_DEFINITION,
ERROR_NODE,
STRUCT_SPECIFIER_QUERY,
ENUM_SPECIFIER_QUERY,
)
)
TYPEDEF_QUERIES = " ".join(
f"({s})"
for s in (
TYPEDEF_SPECIFIER_QUERY,
TYPEDEF_QUERY,
TYPEDEF_SIZED,
TYPEDEF_PRIMITIVE,
)
)
DEPENDENCY_QUERY = " ".join(
f"({s})"
for s in (
FIELD_DECLARATION,
SPECIFIER_FIELD_DECLARATION,
PARAMETER_DECLARATION,
SPECIFIER_PARAMETER_DECLARATION,
)
)