@willbooster/tree-sitter-c 5.0.2 → 5.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/grammar.js CHANGED
@@ -35,6 +35,28 @@ const PREC = {
35
35
  SUBSCRIPT: 17,
36
36
  };
37
37
 
38
+ /** @type {[string, number][]} */
39
+ const BINARY_OPERATORS = [
40
+ ['+', PREC.ADD],
41
+ ['-', PREC.ADD],
42
+ ['*', PREC.MULTIPLY],
43
+ ['/', PREC.MULTIPLY],
44
+ ['%', PREC.MULTIPLY],
45
+ ['||', PREC.LOGICAL_OR],
46
+ ['&&', PREC.LOGICAL_AND],
47
+ ['|', PREC.INCLUSIVE_OR],
48
+ ['^', PREC.EXCLUSIVE_OR],
49
+ ['&', PREC.BITWISE_AND],
50
+ ['==', PREC.EQUAL],
51
+ ['!=', PREC.EQUAL],
52
+ ['>', PREC.RELATIONAL],
53
+ ['>=', PREC.RELATIONAL],
54
+ ['<=', PREC.RELATIONAL],
55
+ ['<', PREC.RELATIONAL],
56
+ ['<<', PREC.SHIFT],
57
+ ['>>', PREC.SHIFT],
58
+ ];
59
+
38
60
  const LINE_COMMENT = seq('//', /(\\+(.|\r?\n)|[^\\\n])*/);
39
61
  const PRAGMA_SPACING = repeat(choice(/\s/, /\\(?:\r\n?|\n\r?)/));
40
62
  const PREPROC_ARGUMENT = /\S([^/\n]|\/[^*]|\\\r?\n)*/;
@@ -113,6 +135,7 @@ module.exports = Object.assign(
113
135
  $._single_noncompound_statement,
114
136
  $.type_specifier,
115
137
  $._declarator,
138
+ $._old_style_declarator,
116
139
  $._field_declarator,
117
140
  $._type_declarator,
118
141
  $._abstract_declarator,
@@ -123,7 +146,6 @@ module.exports = Object.assign(
123
146
  rules: {
124
147
  translation_unit: ($) => repeat($._top_level_item),
125
148
 
126
- // Top level items are block items with the exception of the expression statement
127
149
  _top_level_item: ($) =>
128
150
  choice(
129
151
  $.function_definition,
@@ -177,7 +199,6 @@ module.exports = Object.assign(
177
199
  ')'
178
200
  )
179
201
  ),
180
- // Preprocesser
181
202
 
182
203
  preproc_include: ($) =>
183
204
  seq(
@@ -268,30 +289,8 @@ module.exports = Object.assign(
268
289
  seq('(', commaSep(choice($._preproc_expression, $.system_lib_string, $.string_literal)), ')'),
269
290
 
270
291
  preproc_binary_expression: ($) => {
271
- /** @type {[string, number][]} */
272
- const table = [
273
- ['+', PREC.ADD],
274
- ['-', PREC.ADD],
275
- ['*', PREC.MULTIPLY],
276
- ['/', PREC.MULTIPLY],
277
- ['%', PREC.MULTIPLY],
278
- ['||', PREC.LOGICAL_OR],
279
- ['&&', PREC.LOGICAL_AND],
280
- ['|', PREC.INCLUSIVE_OR],
281
- ['^', PREC.EXCLUSIVE_OR],
282
- ['&', PREC.BITWISE_AND],
283
- ['==', PREC.EQUAL],
284
- ['!=', PREC.EQUAL],
285
- ['>', PREC.RELATIONAL],
286
- ['>=', PREC.RELATIONAL],
287
- ['<=', PREC.RELATIONAL],
288
- ['<', PREC.RELATIONAL],
289
- ['<<', PREC.SHIFT],
290
- ['>>', PREC.SHIFT],
291
- ];
292
-
293
292
  return choice(
294
- ...table.map(([operator, precedence]) => {
293
+ ...BINARY_OPERATORS.map(([operator, precedence]) => {
295
294
  return prec.left(
296
295
  precedence,
297
296
  seq(
@@ -304,8 +303,6 @@ module.exports = Object.assign(
304
303
  );
305
304
  },
306
305
 
307
- // Main Grammar
308
-
309
306
  function_definition: ($) =>
310
307
  seq(
311
308
  optional($.ms_call_modifier),
@@ -319,11 +316,32 @@ module.exports = Object.assign(
319
316
  seq(
320
317
  optional($.ms_call_modifier),
321
318
  $._declaration_specifiers,
322
- field('declarator', alias($._old_style_function_declarator, $.function_declarator)),
319
+ field('declarator', alias($._old_style_declarator, $._declarator)),
323
320
  repeat1($.declaration),
324
321
  field('body', $.compound_statement)
325
322
  ),
326
323
 
324
+ _old_style_declarator: ($) =>
325
+ choice(
326
+ alias($._old_style_function_declarator, $.function_declarator),
327
+ alias($._old_style_pointer_declarator, $.pointer_declarator)
328
+ ),
329
+
330
+ _old_style_pointer_declarator: ($) =>
331
+ prec.dynamic(
332
+ 1,
333
+ prec.right(
334
+ seq(
335
+ optional($.ms_based_modifier),
336
+ '*',
337
+ repeat($.attribute_declaration),
338
+ repeat($.ms_pointer_modifier),
339
+ repeat(choice($.type_qualifier, $.attribute_specifier)),
340
+ field('declarator', alias($._old_style_declarator, $._declarator))
341
+ )
342
+ )
343
+ ),
344
+
327
345
  declaration: ($) =>
328
346
  seq(
329
347
  choice(
@@ -833,7 +851,7 @@ module.exports = Object.assign(
833
851
  parameter_list: ($) =>
834
852
  seq('(', choice(commaSep(choice($.parameter_declaration, $.variadic_parameter)), $.compound_statement), ')'),
835
853
  _old_style_parameter_list: ($) =>
836
- seq('(', commaSep(choice($.identifier, alias(VA_ARG_KEYWORDS, $.identifier), $.variadic_parameter)), ')'),
854
+ seq('(', commaSep1(choice($.identifier, alias(VA_ARG_KEYWORDS, $.identifier), $.variadic_parameter)), ')'),
837
855
 
838
856
  parameter_declaration: ($) =>
839
857
  seq(
@@ -842,8 +860,6 @@ module.exports = Object.assign(
842
860
  repeat($.attribute_specifier)
843
861
  ),
844
862
 
845
- // Statements
846
-
847
863
  attributed_statement: ($) => seq(repeat1($.attribute_declaration), $.statement),
848
864
 
849
865
  statement: ($) => choice($.case_statement, $._non_case_statement),
@@ -1021,8 +1037,6 @@ module.exports = Object.assign(
1021
1037
 
1022
1038
  seh_leave_statement: () => seq('__leave', ';'),
1023
1039
 
1024
- // Expressions
1025
-
1026
1040
  expression: ($) => choice($._expression_not_binary, $.binary_expression),
1027
1041
 
1028
1042
  _expression_not_binary: ($) =>
@@ -1102,34 +1116,10 @@ module.exports = Object.assign(
1102
1116
  prec.left(PREC.UNARY, seq(field('operator', choice('!', '~', '-', '+')), field('argument', $.expression))),
1103
1117
 
1104
1118
  binary_expression: ($) => {
1105
- /** @type {[string, number][]} */
1106
- const table = [
1107
- ['+', PREC.ADD],
1108
- ['-', PREC.ADD],
1109
- ['*', PREC.MULTIPLY],
1110
- ['/', PREC.MULTIPLY],
1111
- ['%', PREC.MULTIPLY],
1112
- ['||', PREC.LOGICAL_OR],
1113
- ['&&', PREC.LOGICAL_AND],
1114
- ['|', PREC.INCLUSIVE_OR],
1115
- ['^', PREC.EXCLUSIVE_OR],
1116
- ['&', PREC.BITWISE_AND],
1117
- ['==', PREC.EQUAL],
1118
- ['!=', PREC.EQUAL],
1119
- ['>', PREC.RELATIONAL],
1120
- ['>=', PREC.RELATIONAL],
1121
- ['<=', PREC.RELATIONAL],
1122
- ['<', PREC.RELATIONAL],
1123
- ['<<', PREC.SHIFT],
1124
- ['>>', PREC.SHIFT],
1125
- ];
1126
-
1127
1119
  return choice(
1128
- ...table.map(([operator, precedence]) => {
1129
- return prec.left(
1130
- precedence,
1131
- seq(field('left', $.expression), field('operator', operator), field('right', $.expression))
1132
- );
1120
+ ...BINARY_OPERATORS.map(([operator, precedence]) => {
1121
+ const body = seq(field('left', $.expression), field('operator', operator), field('right', $.expression));
1122
+ return prec.left(precedence, operator === '&&' ? prec.dynamic(1, body) : body);
1133
1123
  })
1134
1124
  );
1135
1125
  },
@@ -1384,7 +1374,6 @@ module.exports = Object.assign(
1384
1374
  "'"
1385
1375
  ),
1386
1376
 
1387
- // Must concatenate at least 2 nodes, one of which must be a string_literal.
1388
1377
  // Identifier is added to parse macros that are strings, like PRIu64.
1389
1378
  concatenated_string: ($) =>
1390
1379
  prec.right(
@@ -1450,7 +1439,6 @@ module.exports = Object.assign(
1450
1439
  )
1451
1440
  ),
1452
1441
 
1453
- // http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890
1454
1442
  comment: () => token(choice(LINE_COMMENT, seq('/*', /[^*]*\*+([^/*][^*]*\*+)*/, '/'))),
1455
1443
  },
1456
1444
  }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "5.0.2",
3
+ "version": "5.0.4",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
Binary file
package/tree-sitter.json CHANGED
@@ -16,7 +16,7 @@
16
16
  }
17
17
  ],
18
18
  "metadata": {
19
- "version": "5.0.2",
19
+ "version": "5.0.4",
20
20
  "license": "MIT",
21
21
  "description": "C grammar for tree-sitter",
22
22
  "authors": [