@@ -44,6 +44,7 @@ module.exports = grammar({
44
44
[ $ . _type_specifier , $ . _expression , $ . macro_type_specifier ] ,
45
45
[ $ . _type_specifier , $ . macro_type_specifier ] ,
46
46
[ $ . sized_type_specifier ] ,
47
+ [ $ . preproc_expression ] ,
47
48
] ,
48
49
49
50
word : $ => $ . identifier ,
@@ -104,6 +105,77 @@ module.exports = grammar({
104
105
preproc_directive : $ => / # [ \t ] * [ a - z A - Z ] \w * / ,
105
106
preproc_arg : $ => token ( prec ( - 1 , repeat1 ( / .| \\ \r ? \n / ) ) ) ,
106
107
108
+ line_continuation : $ => token (
109
+ / \\ \r ? \n /
110
+ ) ,
111
+
112
+ preproc_condition : $ => seq (
113
+ $ . preproc_expression ,
114
+ '\n' ,
115
+ ) ,
116
+
117
+ preproc_expression : $ => seq (
118
+ optional ( $ . line_continuation ) ,
119
+ choice (
120
+ $ . identifier ,
121
+ prec ( PREC . CALL , seq ( $ . identifier , '(' , commaSep ( $ . preproc_expression ) , ')' ) ) ,
122
+ $ . number_literal ,
123
+ $ . char_literal ,
124
+ $ . preproc_unary_expression ,
125
+ $ . preproc_binary_expression ,
126
+ $ . preproc_defined ,
127
+ $ . preproc_parenthesized_expression ,
128
+ ) ,
129
+ optional ( $ . line_continuation ) ,
130
+ ) ,
131
+
132
+ preproc_parenthesized_expression : $ => seq (
133
+ '(' ,
134
+ $ . preproc_expression ,
135
+ ')'
136
+ ) ,
137
+
138
+ preproc_defined : $ => choice (
139
+ prec ( PREC . CALL , seq ( 'defined' , '(' , $ . identifier , ')' ) ) ,
140
+ seq ( 'defined' , $ . identifier ) ,
141
+ ) ,
142
+
143
+ preproc_unary_expression : $ => prec . left ( PREC . UNARY , seq (
144
+ field ( 'operator' , choice ( '!' , '~' , '-' , '+' ) ) ,
145
+ field ( 'argument' , $ . preproc_expression )
146
+ ) ) ,
147
+
148
+ preproc_binary_expression : $ => {
149
+ const table = [
150
+ [ '+' , PREC . ADD ] ,
151
+ [ '-' , PREC . ADD ] ,
152
+ [ '*' , PREC . MULTIPLY ] ,
153
+ [ '/' , PREC . MULTIPLY ] ,
154
+ [ '%' , PREC . MULTIPLY ] ,
155
+ [ '||' , PREC . LOGICAL_OR ] ,
156
+ [ '&&' , PREC . LOGICAL_AND ] ,
157
+ [ '|' , PREC . INCLUSIVE_OR ] ,
158
+ [ '^' , PREC . EXCLUSIVE_OR ] ,
159
+ [ '&' , PREC . BITWISE_AND ] ,
160
+ [ '==' , PREC . EQUAL ] ,
161
+ [ '!=' , PREC . EQUAL ] ,
162
+ [ '>' , PREC . RELATIONAL ] ,
163
+ [ '>=' , PREC . RELATIONAL ] ,
164
+ [ '<=' , PREC . RELATIONAL ] ,
165
+ [ '<' , PREC . RELATIONAL ] ,
166
+ [ '<<' , PREC . SHIFT ] ,
167
+ [ '>>' , PREC . SHIFT ] ,
168
+ ] ;
169
+
170
+ return choice ( ...table . map ( ( [ operator , precedence ] ) => {
171
+ return prec . left ( precedence , seq (
172
+ field ( 'left' , $ . preproc_expression ) ,
173
+ field ( 'operator' , operator ) ,
174
+ field ( 'right' , $ . preproc_expression )
175
+ ) )
176
+ } ) ) ;
177
+ } ,
178
+
107
179
// Main Grammar
108
180
109
181
function_definition : $ => seq (
@@ -868,7 +940,7 @@ function preprocIf (suffix, content) {
868
940
return {
869
941
[ 'preproc_if' + suffix ] : $ => seq (
870
942
preprocessor ( 'if' ) ,
871
- field ( 'condition' , $ . preproc_arg ) ,
943
+ field ( 'condition' , $ . preproc_condition ) ,
872
944
repeat ( content ( $ ) ) ,
873
945
field ( 'alternative' , optional ( elseBlock ( $ ) ) ) ,
874
946
preprocessor ( 'endif' )
@@ -889,7 +961,7 @@ function preprocIf (suffix, content) {
889
961
890
962
[ 'preproc_elif' + suffix ] : $ => seq (
891
963
preprocessor ( 'elif' ) ,
892
- field ( 'condition' , $ . preproc_arg ) ,
964
+ field ( 'condition' , $ . preproc_condition ) ,
893
965
repeat ( content ( $ ) ) ,
894
966
field ( 'alternative' , optional ( elseBlock ( $ ) ) ) ,
895
967
)
0 commit comments