@willbooster/tree-sitter-c 18.0.1 → 18.1.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "18.0.1",
3
+ "version": "18.1.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -3,22 +3,27 @@
3
3
 
4
4
  #include "functionMacro.h"
5
5
 
6
- static bool conditional_macro_body(TSLexer *lexer, bool *escaped);
6
+ typedef struct {
7
+ TSSymbol ifdef, ifndef, leading_ifdef, leading_ifndef;
8
+ // Optional, unordered ASCII words of at most FUNCTION_MACRO_KEYWORD_MAX_LENGTH bytes.
9
+ // Excludes unknown type aliases and marker names; recognized C type/modifier prefixes are unchanged.
10
+ const char *const *reserved_words;
11
+ unsigned reserved_word_count;
12
+ } ConditionalMacroSymbols;
13
+
14
+ static bool conditional_macro_body(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
7
15
  static bool conditional_macro_word(TSLexer *lexer, MacroName *name, bool escaped);
8
16
  static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped);
9
17
  static bool conditional_macro_next_line(TSLexer *lexer, bool *escaped);
10
18
  static bool conditional_macro_directive(TSLexer *lexer, MacroName *name);
11
- static bool conditional_macro_marker_line(TSLexer *lexer, bool *escaped);
19
+ static bool conditional_macro_marker_line(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
12
20
  static bool conditional_macro_closing(TSLexer *lexer);
13
- static bool conditional_macro_type_prefix(TSLexer *lexer, bool escaped);
21
+ static bool conditional_macro_type_prefix(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool escaped);
14
22
  static bool conditional_macro_skip_lines(TSLexer *lexer, bool *escaped);
23
+ static bool conditional_macro_reserved_word(const ConditionalMacroSymbols *symbols, const char *word);
15
24
  static bool conditional_macro_modifier_group(TSLexer *lexer, const char *word, bool *escaped);
16
25
  static bool conditional_macro_attribute_declaration(TSLexer *lexer, bool *escaped);
17
26
 
18
- typedef struct {
19
- TSSymbol ifdef, ifndef, leading_ifdef, leading_ifndef;
20
- } ConditionalMacroSymbols;
21
-
22
27
  static bool scan_conditional_macro(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const bool *valid_symbols) {
23
28
  char directive[7] = {0};
24
29
  MacroName command = {.word = directive, .capacity = sizeof(directive) - 1, .ascii = true};
@@ -33,8 +38,8 @@ static bool scan_conditional_macro(TSLexer *lexer, const ConditionalMacroSymbols
33
38
  lexer->mark_end(lexer);
34
39
 
35
40
  bool escaped = false;
36
- if (!conditional_macro_body(lexer, &escaped)) return false;
37
- if (leading && conditional_macro_type_prefix(lexer, escaped)) {
41
+ if (!conditional_macro_body(lexer, symbols, &escaped)) return false;
42
+ if (leading && conditional_macro_type_prefix(lexer, symbols, escaped)) {
38
43
  lexer->result_symbol = leading_symbol;
39
44
  return true;
40
45
  }
@@ -42,12 +47,12 @@ static bool scan_conditional_macro(TSLexer *lexer, const ConditionalMacroSymbols
42
47
  return generic;
43
48
  }
44
49
 
45
- static bool conditional_macro_body(TSLexer *lexer, bool *escaped) {
50
+ static bool conditional_macro_body(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
46
51
  bool separated = false;
47
52
  MacroName condition = {.ascii = true};
48
53
  if (!conditional_macro_spacing(lexer, &separated, escaped) || !separated ||
49
54
  !conditional_macro_word(lexer, &condition, *escaped) || !conditional_macro_next_line(lexer, escaped)) return false;
50
- if (!conditional_macro_marker_line(lexer, escaped) || !conditional_macro_closing(lexer)) return false;
55
+ if (!conditional_macro_marker_line(lexer, symbols, escaped) || !conditional_macro_closing(lexer)) return false;
51
56
  return conditional_macro_skip_lines(lexer, escaped) && !lexer->eof(lexer) && lexer->lookahead != '#';
52
57
  }
53
58
 
@@ -68,14 +73,14 @@ typedef enum {
68
73
  CONDITIONAL_INVALID,
69
74
  } ConditionalMacroPrefix;
70
75
 
71
- static ConditionalMacroPrefix conditional_macro_prefix_kind(const MacroName *name, const char *word);
76
+ static ConditionalMacroPrefix conditional_macro_prefix_kind(const ConditionalMacroSymbols *symbols, const MacroName *name, const char *word);
72
77
 
73
- static ConditionalMacroPrefix conditional_macro_prefix(TSLexer *lexer, bool *escaped);
78
+ static ConditionalMacroPrefix conditional_macro_prefix(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
74
79
 
75
- static bool conditional_macro_type_prefix(TSLexer *lexer, bool escaped) {
80
+ static bool conditional_macro_type_prefix(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool escaped) {
76
81
  ConditionalMacroPrefix kind;
77
82
  do {
78
- kind = conditional_macro_prefix(lexer, &escaped);
83
+ kind = conditional_macro_prefix(lexer, symbols, &escaped);
79
84
  } while (kind == CONDITIONAL_MODIFIER);
80
85
  if (kind == CONDITIONAL_TYPE) return true;
81
86
  if (kind != CONDITIONAL_ALIAS) return false;
@@ -84,14 +89,14 @@ static bool conditional_macro_type_prefix(TSLexer *lexer, bool escaped) {
84
89
  return conditional_macro_word(lexer, &declarator, escaped);
85
90
  }
86
91
 
87
- static ConditionalMacroPrefix conditional_macro_prefix(TSLexer *lexer, bool *escaped) {
92
+ static ConditionalMacroPrefix conditional_macro_prefix(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
88
93
  if (lexer->lookahead == '[') {
89
94
  return conditional_macro_attribute_declaration(lexer, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
90
95
  }
91
96
  char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
92
97
  MacroName type = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
93
98
  if (!conditional_macro_word(lexer, &type, *escaped)) return CONDITIONAL_INVALID;
94
- ConditionalMacroPrefix kind = conditional_macro_prefix_kind(&type, word);
99
+ ConditionalMacroPrefix kind = conditional_macro_prefix_kind(symbols, &type, word);
95
100
  if (kind == CONDITIONAL_TYPE) return kind;
96
101
  if (!conditional_macro_skip_lines(lexer, escaped)) return CONDITIONAL_INVALID;
97
102
  if (kind == CONDITIONAL_TYPE_MODIFIER && lexer->lookahead == '(') return CONDITIONAL_TYPE;
@@ -101,9 +106,12 @@ static ConditionalMacroPrefix conditional_macro_prefix(TSLexer *lexer, bool *esc
101
106
  return kind;
102
107
  }
103
108
 
104
- static ConditionalMacroPrefix conditional_macro_prefix_kind(const MacroName *name, const char *word) {
109
+ static ConditionalMacroPrefix conditional_macro_prefix_kind(const ConditionalMacroSymbols *symbols, const MacroName *name, const char *word) {
105
110
  if (name->ascii && strcmp(word, pragma_word) == 0) return CONDITIONAL_MODIFIER;
106
- if (!name->ascii || !function_macro_keyword(word)) return CONDITIONAL_ALIAS;
111
+ if (!name->ascii) return CONDITIONAL_ALIAS;
112
+ if (!function_macro_keyword(word)) {
113
+ return conditional_macro_reserved_word(symbols, word) ? CONDITIONAL_RESERVED : CONDITIONAL_ALIAS;
114
+ }
107
115
  bool type = function_macro_word_in(word, function_macro_type_keywords,
108
116
  sizeof(function_macro_type_keywords) / sizeof(function_macro_type_keywords[0]));
109
117
  bool modifier = (!type && function_macro_object_prefix_keyword(word)) ||
@@ -113,6 +121,14 @@ static ConditionalMacroPrefix conditional_macro_prefix_kind(const MacroName *nam
113
121
  return modifier ? CONDITIONAL_MODIFIER : CONDITIONAL_RESERVED;
114
122
  }
115
123
 
124
+ static bool conditional_macro_reserved_word(const ConditionalMacroSymbols *symbols, const char *word) {
125
+ if (!symbols->reserved_words) return false;
126
+ for (unsigned i = 0; i < symbols->reserved_word_count; i++) {
127
+ if (strcmp(word, symbols->reserved_words[i]) == 0) return true;
128
+ }
129
+ return false;
130
+ }
131
+
116
132
  static bool conditional_macro_modifier_group(TSLexer *lexer, const char *word, bool *escaped) {
117
133
  bool grouped = strcmp(word, pragma_word) == 0 || function_macro_word_in(word, function_macro_group_modifiers,
118
134
  sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
@@ -140,11 +156,11 @@ static bool conditional_macro_skip_lines(TSLexer *lexer, bool *escaped) {
140
156
  }
141
157
  }
142
158
 
143
- static bool conditional_macro_marker_line(TSLexer *lexer, bool *escaped) {
159
+ static bool conditional_macro_marker_line(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
144
160
  char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
145
161
  MacroName marker = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
146
162
  if (!conditional_macro_word(lexer, &marker, *escaped) ||
147
- (marker.ascii && function_macro_keyword(word))) return false;
163
+ (marker.ascii && (function_macro_keyword(word) || conditional_macro_reserved_word(symbols, word)))) return false;
148
164
  return conditional_macro_next_line(lexer, escaped);
149
165
  }
150
166
 
Binary file
package/tree-sitter.json CHANGED
@@ -12,11 +12,18 @@
12
12
  ],
13
13
  "highlights": "queries/highlights.scm",
14
14
  "tags": "queries/tags.scm",
15
- "injection-regex": "^(c|h)$"
15
+ "injection-regex": "^(c|h)$",
16
+ "external-files": [
17
+ "src/pragma.h",
18
+ "src/identifier.h",
19
+ "src/keywords.h",
20
+ "src/functionMacro.h",
21
+ "src/conditionalMacro.h"
22
+ ]
16
23
  }
17
24
  ],
18
25
  "metadata": {
19
- "version": "18.0.1",
26
+ "version": "18.1.0",
20
27
  "license": "MIT",
21
28
  "description": "C grammar for tree-sitter",
22
29
  "authors": [