@willbooster/tree-sitter-c 18.3.0 → 19.0.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/README.md +6 -3
- package/grammar.js +61 -22
- package/package.json +2 -1
- package/queries/highlights.scm +1 -0
- package/src/conditionalMacro.h +2 -2
- package/src/functionMacro.h +83 -29
- package/src/gnuRawString.h +70 -0
- package/src/node-types.json +53 -2
- package/src/pragma.h +51 -6
- package/src/scanner.c +14 -3
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +3 -2
package/README.md
CHANGED
|
@@ -59,11 +59,14 @@ parser.setLanguage(await Language.load(c));
|
|
|
59
59
|
|
|
60
60
|
The package also ships `grammar.js`, scanner sources under `src/`, the queries in `queries/`,
|
|
61
61
|
and the node types in `src/node-types.json` for grammars extending C (such as C++).
|
|
62
|
-
The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, `functionMacro.h`,
|
|
63
|
-
`conditionalMacro.h`.
|
|
62
|
+
The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, `functionMacro.h`,
|
|
63
|
+
`conditionalMacro.h`, and `gnuRawString.h`.
|
|
64
64
|
|
|
65
65
|
Derived grammars must port the inherited external-token dispatch from `src/scanner.c`.
|
|
66
66
|
Keep preprocessor argument/newline handling ahead of annotation lookahead.
|
|
67
|
+
Port `preproc_argument_mode` selection together with `scan_preproc_arg` so tokenized pragmas retain raw bodies.
|
|
68
|
+
Append `_gnu_raw_string_body` and `_raw_macro_type_tail` in their inherited enum slots and port their dispatch from `src/scanner.c`.
|
|
69
|
+
A derived grammar overriding `raw_string_literal` can keep its own body scanner, but must retain the inherited enum slots.
|
|
67
70
|
Port `_conditional_marker_ifdef`, `_conditional_marker_ifndef`, `_leading_conditional_marker_ifdef`, and
|
|
68
71
|
`_leading_conditional_marker_ifndef` with `scan_conditional_macro`. The leading pair checks a following type prefix;
|
|
69
72
|
when both contexts are valid, choose the leading role if its type proof succeeds, otherwise use the valid
|
|
@@ -112,7 +115,7 @@ modifier groups need separate annotation support.
|
|
|
112
115
|
|
|
113
116
|
Trailing object annotations support one complete `primitive_type` token, plain pointer stars and an identifier,
|
|
114
117
|
for example `int *value ATTR((1));`. The first argument token after initial parentheses must be numeric,
|
|
115
|
-
a string/character quote or a comma. Comments and whitespace may separate the prefix tokens; the remaining
|
|
118
|
+
a string/character quote, a GNU raw-string encoding prefix or a comma. Comments and whitespace may separate the prefix tokens; the remaining
|
|
116
119
|
arguments use the structural annotation-argument grammar. Sized-only starts such as `unsigned` or `long`,
|
|
117
120
|
qualified/multiword/unknown-type prefixes,
|
|
118
121
|
arrays, parenthesized/function declarators, initializers and multiple declarators need separate support.
|
package/grammar.js
CHANGED
|
@@ -60,6 +60,7 @@ const BINARY_OPERATORS = [
|
|
|
60
60
|
['>>', PREC.SHIFT],
|
|
61
61
|
];
|
|
62
62
|
|
|
63
|
+
const LINE_SPLICE = /\\(?:\r\n?|\n\r?)/;
|
|
63
64
|
const LINE_COMMENT = seq('//', /(\\+(.|\r?\n)|[^\\\n])*/);
|
|
64
65
|
const PRAGMA_SPACING = repeat(choice(/\s/, /\\(?:\r\n?|\n\r?)/));
|
|
65
66
|
const PREPROC_ARGUMENT = /\S([^/\n]|\/[^*]|\\\r?\n)*/;
|
|
@@ -71,6 +72,7 @@ module.exports = Object.assign(
|
|
|
71
72
|
name: 'c',
|
|
72
73
|
|
|
73
74
|
conflicts: ($) => [
|
|
75
|
+
[$._string, $._raw_string_type_argument],
|
|
74
76
|
[
|
|
75
77
|
$._conditional_function_specifiers,
|
|
76
78
|
$._declaration_specifiers,
|
|
@@ -235,6 +237,8 @@ module.exports = Object.assign(
|
|
|
235
237
|
sym('_conditional_marker_ifndef'),
|
|
236
238
|
sym('_leading_conditional_marker_ifdef'),
|
|
237
239
|
sym('_leading_conditional_marker_ifndef'),
|
|
240
|
+
sym('_gnu_raw_string_body'),
|
|
241
|
+
sym('_raw_macro_type_tail'),
|
|
238
242
|
],
|
|
239
243
|
|
|
240
244
|
extras: ($) => [$.pragma_operator, /\s|\\(?:\r\n?|\n\r?)/, $.comment],
|
|
@@ -424,16 +428,9 @@ module.exports = Object.assign(
|
|
|
424
428
|
preproc_params: ($) => seq(alias($._preproc_lparen, '('), commaSep(choice($.identifier, '...')), ')'),
|
|
425
429
|
|
|
426
430
|
preproc_call: ($) =>
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
optional(
|
|
431
|
-
seq(
|
|
432
|
-
field('argument', alias($._preproc_directive_arg, $.preproc_arg)),
|
|
433
|
-
repeat(choice($.comment, $.pragma_operator))
|
|
434
|
-
)
|
|
435
|
-
),
|
|
436
|
-
sym('_preproc_newline')
|
|
431
|
+
choice(
|
|
432
|
+
preprocCall($, alias(/#[ \t]*pragma/, $.preproc_directive), choice($._preproc_directive_arg, $.preproc_arg)),
|
|
433
|
+
preprocCall($, $.preproc_directive, $._preproc_directive_arg)
|
|
437
434
|
),
|
|
438
435
|
|
|
439
436
|
...preprocIf('_in_header', () => sym('_header_preproc_item')),
|
|
@@ -494,7 +491,11 @@ module.exports = Object.assign(
|
|
|
494
491
|
),
|
|
495
492
|
|
|
496
493
|
preproc_argument_list: ($) =>
|
|
497
|
-
seq(
|
|
494
|
+
seq(
|
|
495
|
+
'(',
|
|
496
|
+
commaSep(choice($._preproc_expression, $.system_lib_string, $.string_literal, $.raw_string_literal)),
|
|
497
|
+
')'
|
|
498
|
+
),
|
|
498
499
|
|
|
499
500
|
preproc_binary_expression: ($) => {
|
|
500
501
|
return choice(
|
|
@@ -667,7 +668,7 @@ module.exports = Object.assign(
|
|
|
667
668
|
linkage_specification: ($) =>
|
|
668
669
|
seq(
|
|
669
670
|
'extern',
|
|
670
|
-
field('value', $.string_literal),
|
|
671
|
+
field('value', choice($.string_literal, $.raw_string_literal)),
|
|
671
672
|
field('body', choice($.function_definition, $.declaration, $.declaration_list))
|
|
672
673
|
),
|
|
673
674
|
|
|
@@ -1441,7 +1442,21 @@ module.exports = Object.assign(
|
|
|
1441
1442
|
$.builtin_available_expression
|
|
1442
1443
|
),
|
|
1443
1444
|
|
|
1444
|
-
_string: ($) => prec.left(choice($.string_literal, $.concatenated_string)),
|
|
1445
|
+
_string: ($) => prec.left(choice($.string_literal, $.raw_string_literal, $.concatenated_string)),
|
|
1446
|
+
|
|
1447
|
+
raw_string_literal: () =>
|
|
1448
|
+
seq(
|
|
1449
|
+
token(
|
|
1450
|
+
seq(
|
|
1451
|
+
optional(seq(choice('L', 'U', 'u', seq('u', repeat(LINE_SPLICE), '8')), repeat(LINE_SPLICE))),
|
|
1452
|
+
'R',
|
|
1453
|
+
repeat(LINE_SPLICE),
|
|
1454
|
+
'"'
|
|
1455
|
+
)
|
|
1456
|
+
),
|
|
1457
|
+
sym('_gnu_raw_string_body'),
|
|
1458
|
+
'"'
|
|
1459
|
+
),
|
|
1445
1460
|
|
|
1446
1461
|
comma_expression: ($) =>
|
|
1447
1462
|
seq(field('left', $.expression), ',', field('right', choice($.expression, $.comma_expression))),
|
|
@@ -1613,7 +1628,7 @@ module.exports = Object.assign(
|
|
|
1613
1628
|
gnu_asm_output_operand: ($) =>
|
|
1614
1629
|
seq(
|
|
1615
1630
|
optional(seq('[', field('symbol', $.identifier), ']')),
|
|
1616
|
-
field('constraint', $.string_literal),
|
|
1631
|
+
field('constraint', choice($.string_literal, $.raw_string_literal)),
|
|
1617
1632
|
'(',
|
|
1618
1633
|
field('value', $.expression),
|
|
1619
1634
|
')'
|
|
@@ -1624,7 +1639,7 @@ module.exports = Object.assign(
|
|
|
1624
1639
|
gnu_asm_input_operand: ($) =>
|
|
1625
1640
|
seq(
|
|
1626
1641
|
optional(seq('[', field('symbol', $.identifier), ']')),
|
|
1627
|
-
field('constraint', $.string_literal),
|
|
1642
|
+
field('constraint', choice($.string_literal, $.raw_string_literal)),
|
|
1628
1643
|
'(',
|
|
1629
1644
|
field('value', $.expression),
|
|
1630
1645
|
')'
|
|
@@ -1768,11 +1783,11 @@ module.exports = Object.assign(
|
|
|
1768
1783
|
prec.right(
|
|
1769
1784
|
seq(
|
|
1770
1785
|
choice(
|
|
1771
|
-
seq($.identifier, $.string_literal),
|
|
1772
|
-
seq($.string_literal, $.string_literal),
|
|
1773
|
-
seq($.string_literal, $.identifier)
|
|
1786
|
+
seq($.identifier, choice($.string_literal, $.raw_string_literal)),
|
|
1787
|
+
seq(choice($.string_literal, $.raw_string_literal), choice($.string_literal, $.raw_string_literal)),
|
|
1788
|
+
seq(choice($.string_literal, $.raw_string_literal), $.identifier)
|
|
1774
1789
|
),
|
|
1775
|
-
repeat(choice($.string_literal, $.identifier))
|
|
1790
|
+
repeat(choice($.string_literal, $.raw_string_literal, $.identifier))
|
|
1776
1791
|
)
|
|
1777
1792
|
),
|
|
1778
1793
|
|
|
@@ -1868,12 +1883,22 @@ module.exports = Object.assign(
|
|
|
1868
1883
|
-1,
|
|
1869
1884
|
seq(
|
|
1870
1885
|
field('name', choice($.identifier, alias(VA_ARG_KEYWORDS, $.identifier))),
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1886
|
+
choice(
|
|
1887
|
+
seq('(', field('type', $.type_descriptor), ')'),
|
|
1888
|
+
field('arguments', alias($._raw_string_macro_type_arguments, $.argument_list))
|
|
1889
|
+
)
|
|
1874
1890
|
)
|
|
1875
1891
|
),
|
|
1876
1892
|
|
|
1893
|
+
_raw_string_macro_type_arguments: ($) =>
|
|
1894
|
+
seq('(', $._raw_string_type_argument, optional(alias(sym('_raw_macro_type_tail'), $.preproc_arg)), ')'),
|
|
1895
|
+
|
|
1896
|
+
_raw_string_type_argument: ($) =>
|
|
1897
|
+
choice($.raw_string_literal, alias($._parenthesized_raw_string_type_argument, $.parenthesized_expression)),
|
|
1898
|
+
|
|
1899
|
+
_parenthesized_raw_string_type_argument: ($) =>
|
|
1900
|
+
seq('(', $._raw_string_type_argument, optional(alias(sym('_raw_macro_type_tail'), $.preproc_arg)), ')'),
|
|
1901
|
+
|
|
1877
1902
|
_preproc_number: () =>
|
|
1878
1903
|
/(\d|\.\d)([\p{XID_Continue}.]|[eEpP][+-]|'[_a-zA-Z0-9]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})*/u,
|
|
1879
1904
|
|
|
@@ -1883,6 +1908,20 @@ module.exports = Object.assign(
|
|
|
1883
1908
|
{ PREC, preprocIf, preprocessor, commaSep, commaSep1 }
|
|
1884
1909
|
);
|
|
1885
1910
|
|
|
1911
|
+
/**
|
|
1912
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'comment' | 'pragma_operator' | 'preproc_arg'>} $
|
|
1913
|
+
* @param {import('./types/treeSitterDsl').Rule} directive
|
|
1914
|
+
* @param {import('./types/treeSitterDsl').Rule} argument
|
|
1915
|
+
*/
|
|
1916
|
+
function preprocCall($, directive, argument) {
|
|
1917
|
+
return seq(
|
|
1918
|
+
field('directive', directive),
|
|
1919
|
+
repeat(choice($.comment, $.pragma_operator)),
|
|
1920
|
+
optional(seq(field('argument', alias(argument, $.preproc_arg)), repeat(choice($.comment, $.pragma_operator)))),
|
|
1921
|
+
sym('_preproc_newline')
|
|
1922
|
+
);
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1886
1925
|
/**
|
|
1887
1926
|
* @param {import('./types/treeSitterDsl').GrammarSymbols<'identifier' | 'macro_specifier'>} $
|
|
1888
1927
|
* @param {import('./types/treeSitterDsl').Rule} ifdef
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-c",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "19.0.0",
|
|
4
4
|
"description": "C grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"c",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"src/keywords.h",
|
|
28
28
|
"src/functionMacro.h",
|
|
29
29
|
"src/conditionalMacro.h",
|
|
30
|
+
"src/gnuRawString.h",
|
|
30
31
|
"src/scanner.c"
|
|
31
32
|
],
|
|
32
33
|
"publishConfig": {
|
package/queries/highlights.scm
CHANGED
package/src/conditionalMacro.h
CHANGED
|
@@ -134,14 +134,14 @@ static bool conditional_macro_modifier_group(TSLexer *lexer, const char *word, b
|
|
|
134
134
|
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
|
|
135
135
|
if (!grouped || lexer->lookahead != '(') return true;
|
|
136
136
|
bool literal = false;
|
|
137
|
-
return function_macro_balanced(lexer, '(', ')', &literal) && conditional_macro_skip_lines(lexer, escaped);
|
|
137
|
+
return function_macro_balanced(lexer, '(', ')', &literal, NULL) && conditional_macro_skip_lines(lexer, escaped);
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
static bool conditional_macro_attribute_declaration(TSLexer *lexer, bool *escaped) {
|
|
141
141
|
lexer->advance(lexer, false);
|
|
142
142
|
if (!conditional_macro_skip_lines(lexer, escaped) || lexer->lookahead != '[') return false;
|
|
143
143
|
bool literal = false;
|
|
144
|
-
if (!function_macro_balanced(lexer, '[', ']', &literal) ||
|
|
144
|
+
if (!function_macro_balanced(lexer, '[', ']', &literal, NULL) ||
|
|
145
145
|
!conditional_macro_skip_lines(lexer, escaped) || lexer->lookahead != ']') return false;
|
|
146
146
|
lexer->advance(lexer, false);
|
|
147
147
|
return conditional_macro_skip_lines(lexer, escaped);
|
package/src/functionMacro.h
CHANGED
|
@@ -8,14 +8,22 @@ typedef struct {
|
|
|
8
8
|
bool identifier;
|
|
9
9
|
bool number;
|
|
10
10
|
int32_t last;
|
|
11
|
+
GnuRawStringPrefix raw_prefix;
|
|
11
12
|
} MacroArgumentWord;
|
|
12
13
|
|
|
14
|
+
typedef enum {
|
|
15
|
+
MACRO_ARGUMENT_PLAIN,
|
|
16
|
+
MACRO_ARGUMENT_CONSUMED,
|
|
17
|
+
MACRO_ARGUMENT_INVALID,
|
|
18
|
+
} MacroArgumentToken;
|
|
19
|
+
|
|
13
20
|
typedef struct {
|
|
14
21
|
bool post_type;
|
|
15
22
|
bool declarator_group;
|
|
16
23
|
bool explicit_type;
|
|
17
24
|
bool modifier;
|
|
18
25
|
bool declarator_name;
|
|
26
|
+
bool unterminated_raw;
|
|
19
27
|
} MacroPrototypeTail;
|
|
20
28
|
|
|
21
29
|
typedef struct {
|
|
@@ -25,13 +33,15 @@ typedef struct {
|
|
|
25
33
|
bool allow_object_start;
|
|
26
34
|
} MacroAttributeSymbols;
|
|
27
35
|
|
|
28
|
-
static
|
|
36
|
+
static MacroArgumentToken function_macro_argument_token(TSLexer *lexer, MacroArgumentWord *word, bool *unterminated_raw);
|
|
37
|
+
|
|
38
|
+
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw);
|
|
29
39
|
static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested);
|
|
30
40
|
static void function_macro_note_literal(bool *literal, unsigned depth, unsigned nested, const MacroArgumentWord *word, int32_t c);
|
|
31
41
|
static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototypeTail *tail);
|
|
32
42
|
static bool function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail);
|
|
33
43
|
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type);
|
|
34
|
-
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote);
|
|
44
|
+
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote, bool raw, bool *unterminated_raw);
|
|
35
45
|
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word);
|
|
36
46
|
static bool function_macro_argument_slash(TSLexer *lexer, bool allow_division);
|
|
37
47
|
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped);
|
|
@@ -48,6 +58,24 @@ static bool function_macro_attribute_keyword(const char *word, bool trailing);
|
|
|
48
58
|
static bool function_macro_keyword(const char *word);
|
|
49
59
|
static bool function_macro_word_in(const char *word, const char *const *words, unsigned count);
|
|
50
60
|
|
|
61
|
+
static inline bool scan_raw_macro_type_tail(TSLexer *lexer) {
|
|
62
|
+
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
63
|
+
if (lexer->lookahead == ')' || lexer->eof(lexer)) return false;
|
|
64
|
+
unsigned depth = 1;
|
|
65
|
+
MacroArgumentWord word = {0};
|
|
66
|
+
while (!lexer->eof(lexer)) {
|
|
67
|
+
int32_t c = lexer->lookahead;
|
|
68
|
+
if (c == ')' && depth == 1) break;
|
|
69
|
+
if (function_macro_argument_token(lexer, &word, NULL) != MACRO_ARGUMENT_PLAIN) continue;
|
|
70
|
+
if (c == '(') depth++;
|
|
71
|
+
else if (c == ')') depth--;
|
|
72
|
+
function_macro_argument_word(&word, c);
|
|
73
|
+
lexer->advance(lexer, false);
|
|
74
|
+
}
|
|
75
|
+
lexer->mark_end(lexer);
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
51
79
|
static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool require_literal, bool trailing, const MacroAttributeSymbols *symbols) {
|
|
52
80
|
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
53
81
|
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = !trailing};
|
|
@@ -65,8 +93,10 @@ static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool r
|
|
|
65
93
|
if (!scan_pragma_spacing(lexer) || lexer->lookahead != '(') return false;
|
|
66
94
|
if (trailing) return function_macro_trailing_argument_prefix(lexer);
|
|
67
95
|
bool literal = false;
|
|
68
|
-
|
|
69
|
-
|
|
96
|
+
bool unterminated_raw = false;
|
|
97
|
+
bool balanced = function_macro_balanced(lexer, '(', ')', &literal, &unterminated_raw);
|
|
98
|
+
return unterminated_raw || (balanced && (!require_literal || literal) &&
|
|
99
|
+
(!prototype || function_macro_declaration_continues(lexer, require_literal)));
|
|
70
100
|
}
|
|
71
101
|
|
|
72
102
|
static bool function_macro_trailing_argument_prefix(TSLexer *lexer) {
|
|
@@ -80,6 +110,15 @@ static bool function_macro_trailing_argument_prefix(TSLexer *lexer) {
|
|
|
80
110
|
first = lexer->lookahead;
|
|
81
111
|
return first >= '0' && first <= '9';
|
|
82
112
|
}
|
|
113
|
+
if (first == 'R' || first == 'L' || first == 'u' || first == 'U') {
|
|
114
|
+
GnuRawStringPrefix prefix = {0};
|
|
115
|
+
while (lexer->lookahead != '"' && prefix.length < sizeof(prefix.value)) {
|
|
116
|
+
gnu_raw_string_prefix_character(&prefix, lexer->lookahead);
|
|
117
|
+
lexer->advance(lexer, false);
|
|
118
|
+
if (!scan_preproc_splices(lexer)) return false;
|
|
119
|
+
}
|
|
120
|
+
return lexer->lookahead == '"' && gnu_raw_string_prefix(&prefix);
|
|
121
|
+
}
|
|
83
122
|
return first == ',' || first == '"' || first == '\'' || (first >= '0' && first <= '9');
|
|
84
123
|
}
|
|
85
124
|
|
|
@@ -133,7 +172,7 @@ static bool function_macro_attribute_keyword(const char *word, bool trailing) {
|
|
|
133
172
|
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
|
|
134
173
|
}
|
|
135
174
|
|
|
136
|
-
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal) {
|
|
175
|
+
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw) {
|
|
137
176
|
unsigned depth = 1;
|
|
138
177
|
unsigned nested = 0;
|
|
139
178
|
MacroArgumentWord word = {0};
|
|
@@ -143,25 +182,9 @@ static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close,
|
|
|
143
182
|
int32_t c = lexer->lookahead;
|
|
144
183
|
if (c == ';' || c == '{' || c == '#') return false;
|
|
145
184
|
function_macro_note_literal(literal, depth, nested, &word, c);
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
if (c == '/') {
|
|
152
|
-
if (!function_macro_argument_slash(lexer, true)) return false;
|
|
153
|
-
word = (MacroArgumentWord){0};
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
if (c == '\\') {
|
|
157
|
-
if (!function_macro_argument_escape(lexer, &word)) return false;
|
|
158
|
-
continue;
|
|
159
|
-
}
|
|
160
|
-
if (c == '"' || (c == '\'' && !word.number)) {
|
|
161
|
-
if (!function_macro_argument_quote(lexer, c)) return false;
|
|
162
|
-
word = (MacroArgumentWord){0};
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
185
|
+
MacroArgumentToken token = function_macro_argument_token(lexer, &word, unterminated_raw);
|
|
186
|
+
if (token == MACRO_ARGUMENT_INVALID) return false;
|
|
187
|
+
if (token == MACRO_ARGUMENT_CONSUMED) continue;
|
|
165
188
|
function_macro_argument_word(&word, c);
|
|
166
189
|
if (!function_macro_group(c, open, close, &depth, &nested)) return false;
|
|
167
190
|
lexer->advance(lexer, false);
|
|
@@ -198,9 +221,9 @@ static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type)
|
|
|
198
221
|
} else if (c <= 127 && strchr("{}#)]", c)) {
|
|
199
222
|
return false;
|
|
200
223
|
} else if (c == '(' || c == '[') {
|
|
201
|
-
if (!function_macro_tail_group(lexer, c, &tail)) return
|
|
224
|
+
if (!function_macro_tail_group(lexer, c, &tail)) return tail.unterminated_raw;
|
|
202
225
|
} else if (c == '"' || c == '\'') {
|
|
203
|
-
if (!function_macro_argument_quote(lexer, c)) return false;
|
|
226
|
+
if (!function_macro_argument_quote(lexer, c, false, NULL)) return false;
|
|
204
227
|
} else {
|
|
205
228
|
if (c == '*' && tail.post_type && tail.declarator_name && !tail.declarator_group) return false;
|
|
206
229
|
lexer->advance(lexer, false);
|
|
@@ -211,7 +234,7 @@ static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type)
|
|
|
211
234
|
|
|
212
235
|
static bool function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail) {
|
|
213
236
|
bool literal = false;
|
|
214
|
-
if (!function_macro_balanced(lexer, c, c == '(' ? ')' : ']', &literal)) return false;
|
|
237
|
+
if (!function_macro_balanced(lexer, c, c == '(' ? ')' : ']', &literal, &tail->unterminated_raw)) return false;
|
|
215
238
|
if (c == '(' && !tail->modifier) {
|
|
216
239
|
if (literal && tail->post_type && !tail->declarator_group) tail->declarator_name = false;
|
|
217
240
|
else tail->declarator_group = true;
|
|
@@ -262,13 +285,39 @@ static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototyp
|
|
|
262
285
|
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word) {
|
|
263
286
|
lexer->advance(lexer, false);
|
|
264
287
|
if (scan_preproc_newline(lexer, false)) return true;
|
|
288
|
+
gnu_raw_string_prefix_invalidate(&word->raw_prefix);
|
|
265
289
|
if (!scan_preproc_ucn(lexer, &word->number)) return false;
|
|
266
290
|
word->identifier = !word->number;
|
|
267
291
|
return true;
|
|
268
292
|
}
|
|
269
293
|
|
|
270
|
-
static
|
|
294
|
+
static MacroArgumentToken function_macro_argument_token(TSLexer *lexer, MacroArgumentWord *word, bool *unterminated_raw) {
|
|
295
|
+
int32_t c = lexer->lookahead;
|
|
296
|
+
if (pragma_space(c)) {
|
|
297
|
+
lexer->advance(lexer, false);
|
|
298
|
+
*word = (MacroArgumentWord){0};
|
|
299
|
+
} else if (c == '/') {
|
|
300
|
+
if (!function_macro_argument_slash(lexer, true)) return MACRO_ARGUMENT_INVALID;
|
|
301
|
+
*word = (MacroArgumentWord){0};
|
|
302
|
+
} else if (c == '\\') {
|
|
303
|
+
if (!function_macro_argument_escape(lexer, word)) return MACRO_ARGUMENT_INVALID;
|
|
304
|
+
} else if (c == '"' || (c == '\'' && !word->number)) {
|
|
305
|
+
if (!function_macro_argument_quote(lexer, c,
|
|
306
|
+
c == '"' && gnu_raw_string_prefix(&word->raw_prefix), unterminated_raw)) return MACRO_ARGUMENT_INVALID;
|
|
307
|
+
*word = (MacroArgumentWord){0};
|
|
308
|
+
} else {
|
|
309
|
+
return MACRO_ARGUMENT_PLAIN;
|
|
310
|
+
}
|
|
311
|
+
return MACRO_ARGUMENT_CONSUMED;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote, bool raw, bool *unterminated_raw) {
|
|
271
315
|
lexer->advance(lexer, false);
|
|
316
|
+
if (raw) {
|
|
317
|
+
bool terminated = scan_gnu_raw_string_end(lexer);
|
|
318
|
+
if (!terminated && unterminated_raw) *unterminated_raw = lexer->eof(lexer);
|
|
319
|
+
return terminated;
|
|
320
|
+
}
|
|
272
321
|
while (!lexer->eof(lexer)) {
|
|
273
322
|
int32_t c = lexer->lookahead;
|
|
274
323
|
lexer->advance(lexer, false);
|
|
@@ -328,7 +377,12 @@ static void function_macro_argument_word(MacroArgumentWord *word, int32_t c) {
|
|
|
328
377
|
if (!member && !digit && c != '.' && c != '\'' && !exponent_sign) *word = (MacroArgumentWord){0};
|
|
329
378
|
if (digit && !word->identifier) word->number = true;
|
|
330
379
|
if (member && !word->number) word->identifier = true;
|
|
331
|
-
if (word->number)
|
|
380
|
+
if (word->number) {
|
|
381
|
+
word->last = c;
|
|
382
|
+
gnu_raw_string_prefix_invalidate(&word->raw_prefix);
|
|
383
|
+
}
|
|
384
|
+
else if (member || digit || exponent_sign) gnu_raw_string_prefix_character(&word->raw_prefix, c);
|
|
385
|
+
else word->raw_prefix = (GnuRawStringPrefix){0};
|
|
332
386
|
}
|
|
333
387
|
|
|
334
388
|
static bool function_macro_keyword(const char *word) {
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#ifndef TREE_SITTER_C_GNU_RAW_STRING_H_
|
|
2
|
+
#define TREE_SITTER_C_GNU_RAW_STRING_H_
|
|
3
|
+
|
|
4
|
+
#include "tree_sitter/parser.h"
|
|
5
|
+
#include <string.h>
|
|
6
|
+
|
|
7
|
+
typedef struct {
|
|
8
|
+
char value[4];
|
|
9
|
+
unsigned length;
|
|
10
|
+
} GnuRawStringPrefix;
|
|
11
|
+
|
|
12
|
+
static bool scan_gnu_raw_string_body(TSLexer *lexer);
|
|
13
|
+
static void gnu_raw_string_prefix_invalidate(GnuRawStringPrefix *prefix);
|
|
14
|
+
|
|
15
|
+
static inline bool scan_gnu_raw_string_token(TSLexer *lexer) {
|
|
16
|
+
if (!scan_gnu_raw_string_body(lexer)) return false;
|
|
17
|
+
lexer->mark_end(lexer);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static bool scan_gnu_raw_string_end(TSLexer *lexer) {
|
|
22
|
+
if (!scan_gnu_raw_string_body(lexer) || lexer->lookahead != '"') return false;
|
|
23
|
+
lexer->advance(lexer, false);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static void gnu_raw_string_prefix_character(GnuRawStringPrefix *prefix, int32_t c) {
|
|
28
|
+
if (prefix->length < sizeof(prefix->value) - 1 && c > 0 && c < 128) {
|
|
29
|
+
prefix->value[prefix->length++] = (char)c;
|
|
30
|
+
prefix->value[prefix->length] = 0;
|
|
31
|
+
} else {
|
|
32
|
+
gnu_raw_string_prefix_invalidate(prefix);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static void gnu_raw_string_prefix_invalidate(GnuRawStringPrefix *prefix) {
|
|
37
|
+
prefix->length = sizeof(prefix->value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static bool gnu_raw_string_prefix(const GnuRawStringPrefix *prefix) {
|
|
41
|
+
return prefix->length < sizeof(prefix->value) &&
|
|
42
|
+
(strcmp(prefix->value, "R") == 0 || strcmp(prefix->value, "LR") == 0 ||
|
|
43
|
+
strcmp(prefix->value, "uR") == 0 || strcmp(prefix->value, "UR") == 0 ||
|
|
44
|
+
strcmp(prefix->value, "u8R") == 0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static bool scan_gnu_raw_string_body(TSLexer *lexer) {
|
|
48
|
+
char delimiter[16];
|
|
49
|
+
unsigned length = 0;
|
|
50
|
+
while (lexer->lookahead != '(') {
|
|
51
|
+
int32_t c = lexer->lookahead;
|
|
52
|
+
if (lexer->eof(lexer) || length == sizeof delimiter || c < 0x21 || c > 0x7e || c == ')' || c == '\\') return false;
|
|
53
|
+
delimiter[length++] = (char)c;
|
|
54
|
+
lexer->advance(lexer, false);
|
|
55
|
+
}
|
|
56
|
+
lexer->advance(lexer, false);
|
|
57
|
+
for (int delimiter_index = -1; !lexer->eof(lexer); lexer->advance(lexer, false)) {
|
|
58
|
+
if (delimiter_index >= 0 && (unsigned)delimiter_index == length && lexer->lookahead == '"') break;
|
|
59
|
+
if (delimiter_index >= 0 && (unsigned)delimiter_index < length &&
|
|
60
|
+
lexer->lookahead == delimiter[delimiter_index]) {
|
|
61
|
+
delimiter_index++;
|
|
62
|
+
} else {
|
|
63
|
+
delimiter_index = -1;
|
|
64
|
+
}
|
|
65
|
+
if (lexer->lookahead == ')') delimiter_index = 0;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#endif
|
package/src/node-types.json
CHANGED
|
@@ -207,6 +207,10 @@
|
|
|
207
207
|
"type": "pointer_expression",
|
|
208
208
|
"named": true
|
|
209
209
|
},
|
|
210
|
+
{
|
|
211
|
+
"type": "raw_string_literal",
|
|
212
|
+
"named": true
|
|
213
|
+
},
|
|
210
214
|
{
|
|
211
215
|
"type": "sizeof_expression",
|
|
212
216
|
"named": true
|
|
@@ -550,6 +554,10 @@
|
|
|
550
554
|
"type": "initializer_pair",
|
|
551
555
|
"named": true
|
|
552
556
|
},
|
|
557
|
+
{
|
|
558
|
+
"type": "preproc_arg",
|
|
559
|
+
"named": true
|
|
560
|
+
},
|
|
553
561
|
{
|
|
554
562
|
"type": "preproc_defined",
|
|
555
563
|
"named": true
|
|
@@ -1320,6 +1328,10 @@
|
|
|
1320
1328
|
"type": "identifier",
|
|
1321
1329
|
"named": true
|
|
1322
1330
|
},
|
|
1331
|
+
{
|
|
1332
|
+
"type": "raw_string_literal",
|
|
1333
|
+
"named": true
|
|
1334
|
+
},
|
|
1323
1335
|
{
|
|
1324
1336
|
"type": "string_literal",
|
|
1325
1337
|
"named": true
|
|
@@ -2216,6 +2228,10 @@
|
|
|
2216
2228
|
"type": "concatenated_string",
|
|
2217
2229
|
"named": true
|
|
2218
2230
|
},
|
|
2231
|
+
{
|
|
2232
|
+
"type": "raw_string_literal",
|
|
2233
|
+
"named": true
|
|
2234
|
+
},
|
|
2219
2235
|
{
|
|
2220
2236
|
"type": "string_literal",
|
|
2221
2237
|
"named": true
|
|
@@ -2236,6 +2252,10 @@
|
|
|
2236
2252
|
"type": "concatenated_string",
|
|
2237
2253
|
"named": true
|
|
2238
2254
|
},
|
|
2255
|
+
{
|
|
2256
|
+
"type": "raw_string_literal",
|
|
2257
|
+
"named": true
|
|
2258
|
+
},
|
|
2239
2259
|
{
|
|
2240
2260
|
"type": "string_literal",
|
|
2241
2261
|
"named": true
|
|
@@ -2318,6 +2338,10 @@
|
|
|
2318
2338
|
"multiple": false,
|
|
2319
2339
|
"required": true,
|
|
2320
2340
|
"types": [
|
|
2341
|
+
{
|
|
2342
|
+
"type": "raw_string_literal",
|
|
2343
|
+
"named": true
|
|
2344
|
+
},
|
|
2321
2345
|
{
|
|
2322
2346
|
"type": "string_literal",
|
|
2323
2347
|
"named": true
|
|
@@ -2370,6 +2394,10 @@
|
|
|
2370
2394
|
"multiple": false,
|
|
2371
2395
|
"required": true,
|
|
2372
2396
|
"types": [
|
|
2397
|
+
{
|
|
2398
|
+
"type": "raw_string_literal",
|
|
2399
|
+
"named": true
|
|
2400
|
+
},
|
|
2373
2401
|
{
|
|
2374
2402
|
"type": "string_literal",
|
|
2375
2403
|
"named": true
|
|
@@ -2654,6 +2682,10 @@
|
|
|
2654
2682
|
"multiple": false,
|
|
2655
2683
|
"required": true,
|
|
2656
2684
|
"types": [
|
|
2685
|
+
{
|
|
2686
|
+
"type": "raw_string_literal",
|
|
2687
|
+
"named": true
|
|
2688
|
+
},
|
|
2657
2689
|
{
|
|
2658
2690
|
"type": "string_literal",
|
|
2659
2691
|
"named": true
|
|
@@ -2682,6 +2714,16 @@
|
|
|
2682
2714
|
"type": "macro_type_specifier",
|
|
2683
2715
|
"named": true,
|
|
2684
2716
|
"fields": {
|
|
2717
|
+
"arguments": {
|
|
2718
|
+
"multiple": false,
|
|
2719
|
+
"required": false,
|
|
2720
|
+
"types": [
|
|
2721
|
+
{
|
|
2722
|
+
"type": "argument_list",
|
|
2723
|
+
"named": true
|
|
2724
|
+
}
|
|
2725
|
+
]
|
|
2726
|
+
},
|
|
2685
2727
|
"name": {
|
|
2686
2728
|
"multiple": false,
|
|
2687
2729
|
"required": true,
|
|
@@ -2694,7 +2736,7 @@
|
|
|
2694
2736
|
},
|
|
2695
2737
|
"type": {
|
|
2696
2738
|
"multiple": false,
|
|
2697
|
-
"required":
|
|
2739
|
+
"required": false,
|
|
2698
2740
|
"types": [
|
|
2699
2741
|
{
|
|
2700
2742
|
"type": "type_descriptor",
|
|
@@ -2945,7 +2987,7 @@
|
|
|
2945
2987
|
"named": true,
|
|
2946
2988
|
"fields": {},
|
|
2947
2989
|
"children": {
|
|
2948
|
-
"multiple":
|
|
2990
|
+
"multiple": true,
|
|
2949
2991
|
"required": true,
|
|
2950
2992
|
"types": [
|
|
2951
2993
|
{
|
|
@@ -2960,6 +3002,10 @@
|
|
|
2960
3002
|
"type": "expression",
|
|
2961
3003
|
"named": true
|
|
2962
3004
|
},
|
|
3005
|
+
{
|
|
3006
|
+
"type": "preproc_arg",
|
|
3007
|
+
"named": true
|
|
3008
|
+
},
|
|
2963
3009
|
{
|
|
2964
3010
|
"type": "preproc_defined",
|
|
2965
3011
|
"named": true
|
|
@@ -3977,6 +4023,11 @@
|
|
|
3977
4023
|
]
|
|
3978
4024
|
}
|
|
3979
4025
|
},
|
|
4026
|
+
{
|
|
4027
|
+
"type": "raw_string_literal",
|
|
4028
|
+
"named": true,
|
|
4029
|
+
"fields": {}
|
|
4030
|
+
},
|
|
3980
4031
|
{
|
|
3981
4032
|
"type": "return_statement",
|
|
3982
4033
|
"named": true,
|
package/src/pragma.h
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
#include "tree_sitter/parser.h"
|
|
5
5
|
#include "identifier.h"
|
|
6
|
+
#include "gnuRawString.h"
|
|
6
7
|
#include <string.h>
|
|
7
8
|
|
|
8
9
|
static const char pragma_word[] = "_Pragma";
|
|
@@ -15,6 +16,12 @@ typedef struct {
|
|
|
15
16
|
bool lookahead_only;
|
|
16
17
|
} MacroName;
|
|
17
18
|
|
|
19
|
+
typedef enum {
|
|
20
|
+
PREPROC_ARGUMENT_TOKENS,
|
|
21
|
+
PREPROC_ARGUMENT_TEXT,
|
|
22
|
+
PREPROC_ARGUMENT_PRAGMA,
|
|
23
|
+
} PreprocArgumentMode;
|
|
24
|
+
|
|
18
25
|
typedef enum {
|
|
19
26
|
MACRO_NAME_END,
|
|
20
27
|
MACRO_NAME_CHARACTER,
|
|
@@ -27,6 +34,7 @@ static void scan_macro_name_start(TSLexer *lexer, bool lookahead_only);
|
|
|
27
34
|
static MacroNameCharacter scan_macro_name_character(TSLexer *lexer, bool continuation, MacroName *name);
|
|
28
35
|
static bool scan_macro_name_ucn(TSLexer *lexer);
|
|
29
36
|
|
|
37
|
+
static bool scan_pragma_argument(TSLexer *lexer);
|
|
30
38
|
static bool scan_pragma_spacing(TSLexer *lexer);
|
|
31
39
|
static bool scan_pragma_suffix(TSLexer *lexer);
|
|
32
40
|
static bool pragma_space(int32_t c);
|
|
@@ -36,6 +44,7 @@ static bool scan_preproc_splices(TSLexer *lexer);
|
|
|
36
44
|
static bool scan_preproc_ucn(TSLexer *lexer, bool *in_number);
|
|
37
45
|
static bool preproc_word(int32_t c, bool continuation);
|
|
38
46
|
static bool scan_preproc_quote(TSLexer *lexer, int32_t quote, bool escaped, bool directive_text);
|
|
47
|
+
static bool scan_preproc_raw_quote(TSLexer *lexer);
|
|
39
48
|
|
|
40
49
|
static bool scan_pragma(TSLexer *lexer) {
|
|
41
50
|
while (pragma_space(lexer->lookahead)) {
|
|
@@ -49,13 +58,27 @@ static bool scan_pragma_suffix(TSLexer *lexer) {
|
|
|
49
58
|
if (!scan_pragma_spacing(lexer) || lexer->lookahead != '(') return false;
|
|
50
59
|
lexer->advance(lexer, false);
|
|
51
60
|
if (!scan_pragma_spacing(lexer)) return false;
|
|
61
|
+
if (!scan_pragma_argument(lexer)) return false;
|
|
62
|
+
if (!scan_pragma_spacing(lexer) || lexer->lookahead != ')') return false;
|
|
63
|
+
lexer->advance(lexer, false);
|
|
64
|
+
lexer->mark_end(lexer);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static bool scan_pragma_argument(TSLexer *lexer) {
|
|
52
69
|
if (lexer->lookahead == 'L' || lexer->lookahead == 'u' || lexer->lookahead == 'U') {
|
|
53
70
|
bool utf8 = lexer->lookahead == 'u';
|
|
54
71
|
lexer->advance(lexer, false);
|
|
72
|
+
if (!scan_preproc_splices(lexer)) return false;
|
|
55
73
|
if (utf8 && lexer->lookahead == '8') lexer->advance(lexer, false);
|
|
74
|
+
if (!scan_preproc_splices(lexer)) return false;
|
|
56
75
|
}
|
|
76
|
+
bool raw = lexer->lookahead == 'R';
|
|
77
|
+
if (raw) lexer->advance(lexer, false);
|
|
78
|
+
if (!scan_preproc_splices(lexer)) return false;
|
|
57
79
|
if (lexer->lookahead != '"') return false;
|
|
58
80
|
lexer->advance(lexer, false);
|
|
81
|
+
if (raw) return scan_gnu_raw_string_end(lexer);
|
|
59
82
|
while (lexer->lookahead != '"') {
|
|
60
83
|
if (lexer->eof(lexer) || lexer->lookahead == '\n' || lexer->lookahead == '\r') return false;
|
|
61
84
|
if (lexer->lookahead == '\\') {
|
|
@@ -66,9 +89,6 @@ static bool scan_pragma_suffix(TSLexer *lexer) {
|
|
|
66
89
|
lexer->advance(lexer, false);
|
|
67
90
|
}
|
|
68
91
|
lexer->advance(lexer, false);
|
|
69
|
-
if (!scan_pragma_spacing(lexer) || lexer->lookahead != ')') return false;
|
|
70
|
-
lexer->advance(lexer, false);
|
|
71
|
-
lexer->mark_end(lexer);
|
|
72
92
|
return true;
|
|
73
93
|
}
|
|
74
94
|
|
|
@@ -114,7 +134,14 @@ static bool scan_pragma_spacing(TSLexer *lexer) {
|
|
|
114
134
|
}
|
|
115
135
|
}
|
|
116
136
|
|
|
117
|
-
static
|
|
137
|
+
static PreprocArgumentMode preproc_argument_mode(bool tokens, bool directive, bool lparen) {
|
|
138
|
+
if (tokens && directive && !lparen) return PREPROC_ARGUMENT_PRAGMA;
|
|
139
|
+
return !tokens && directive ? PREPROC_ARGUMENT_TEXT : PREPROC_ARGUMENT_TOKENS;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static bool scan_preproc_arg(TSLexer *lexer, bool consumed_backslash, PreprocArgumentMode mode) {
|
|
143
|
+
bool directive_text = mode != PREPROC_ARGUMENT_TOKENS;
|
|
144
|
+
bool raw_allowed = mode != PREPROC_ARGUMENT_TEXT;
|
|
118
145
|
bool has_content = consumed_backslash;
|
|
119
146
|
while (!has_content && pragma_space(lexer->lookahead) && lexer->lookahead != '\n' && lexer->lookahead != '\r') {
|
|
120
147
|
lexer->advance(lexer, true);
|
|
@@ -122,6 +149,8 @@ static bool scan_preproc_arg(TSLexer *lexer, bool consumed_backslash, bool direc
|
|
|
122
149
|
bool after_comment = false;
|
|
123
150
|
bool in_number = false;
|
|
124
151
|
bool in_identifier = consumed_backslash && scan_preproc_ucn(lexer, &in_number);
|
|
152
|
+
GnuRawStringPrefix raw_prefix = {0};
|
|
153
|
+
if (consumed_backslash) gnu_raw_string_prefix_invalidate(&raw_prefix);
|
|
125
154
|
if (consumed_backslash) lexer->mark_end(lexer);
|
|
126
155
|
int32_t number_last = 0;
|
|
127
156
|
while (!lexer->eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '\r') {
|
|
@@ -138,6 +167,11 @@ static bool scan_preproc_arg(TSLexer *lexer, bool consumed_backslash, bool direc
|
|
|
138
167
|
if (digit && !in_identifier) in_number = true;
|
|
139
168
|
if (word && !in_number) in_identifier = true;
|
|
140
169
|
if (in_number && c != '\\') number_last = c;
|
|
170
|
+
if (c != '"' && c != '\\') {
|
|
171
|
+
if (in_number) gnu_raw_string_prefix_invalidate(&raw_prefix);
|
|
172
|
+
else if (word || digit || exponent_sign) gnu_raw_string_prefix_character(&raw_prefix, c);
|
|
173
|
+
else raw_prefix = (GnuRawStringPrefix){0};
|
|
174
|
+
}
|
|
141
175
|
if (lexer->lookahead == '/') {
|
|
142
176
|
lexer->advance(lexer, false);
|
|
143
177
|
bool split_opener = lexer->lookahead == '\\';
|
|
@@ -187,6 +221,7 @@ static bool scan_preproc_arg(TSLexer *lexer, bool consumed_backslash, bool direc
|
|
|
187
221
|
if (!after_comment && has_content) lexer->mark_end(lexer);
|
|
188
222
|
continue;
|
|
189
223
|
}
|
|
224
|
+
gnu_raw_string_prefix_invalidate(&raw_prefix);
|
|
190
225
|
if (scan_preproc_ucn(lexer, &in_number)) {
|
|
191
226
|
if (!in_number) in_identifier = true;
|
|
192
227
|
number_last = 0;
|
|
@@ -196,8 +231,9 @@ static bool scan_preproc_arg(TSLexer *lexer, bool consumed_backslash, bool direc
|
|
|
196
231
|
}
|
|
197
232
|
has_content = true;
|
|
198
233
|
after_comment = false;
|
|
199
|
-
} else if (lexer->lookahead == '"' || (
|
|
234
|
+
} else if (lexer->lookahead == '"' || (raw_allowed && lexer->lookahead == '\'')) {
|
|
200
235
|
int32_t quote = lexer->lookahead;
|
|
236
|
+
bool raw = raw_allowed && quote == '"' && gnu_raw_string_prefix(&raw_prefix);
|
|
201
237
|
lexer->advance(lexer, false);
|
|
202
238
|
bool escaped = false;
|
|
203
239
|
if (quote == '\'' && in_number) {
|
|
@@ -216,7 +252,9 @@ static bool scan_preproc_arg(TSLexer *lexer, bool consumed_backslash, bool direc
|
|
|
216
252
|
}
|
|
217
253
|
in_number = false;
|
|
218
254
|
in_identifier = false;
|
|
219
|
-
|
|
255
|
+
bool quoted = raw ? scan_preproc_raw_quote(lexer) : scan_preproc_quote(lexer, quote, escaped, directive_text);
|
|
256
|
+
if (!quoted) return true;
|
|
257
|
+
raw_prefix = (GnuRawStringPrefix){0};
|
|
220
258
|
has_content = true;
|
|
221
259
|
after_comment = false;
|
|
222
260
|
} else {
|
|
@@ -231,6 +269,13 @@ static bool scan_preproc_arg(TSLexer *lexer, bool consumed_backslash, bool direc
|
|
|
231
269
|
return has_content;
|
|
232
270
|
}
|
|
233
271
|
|
|
272
|
+
static bool scan_preproc_raw_quote(TSLexer *lexer) {
|
|
273
|
+
if (scan_gnu_raw_string_end(lexer)) return true;
|
|
274
|
+
while (!lexer->eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '\r') lexer->advance(lexer, false);
|
|
275
|
+
lexer->mark_end(lexer);
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
|
|
234
279
|
static bool scan_preproc_quote(TSLexer *lexer, int32_t quote, bool escaped, bool directive_text) {
|
|
235
280
|
lexer->mark_end(lexer);
|
|
236
281
|
bool literal = true;
|
package/src/scanner.c
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#include "conditionalMacro.h"
|
|
2
|
+
#include "gnuRawString.h"
|
|
2
3
|
|
|
3
|
-
enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, CONDITIONAL_MARKER_IFDEF, CONDITIONAL_MARKER_IFNDEF, LEADING_CONDITIONAL_MARKER_IFDEF, LEADING_CONDITIONAL_MARKER_IFNDEF };
|
|
4
|
+
enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, CONDITIONAL_MARKER_IFDEF, CONDITIONAL_MARKER_IFNDEF, LEADING_CONDITIONAL_MARKER_IFDEF, LEADING_CONDITIONAL_MARKER_IFNDEF, GNU_RAW_STRING_BODY, RAW_MACRO_TYPE_TAIL };
|
|
4
5
|
|
|
5
6
|
static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols);
|
|
6
7
|
|
|
@@ -26,6 +27,14 @@ void tree_sitter_c_external_scanner_deserialize(void *payload, const char *buffe
|
|
|
26
27
|
|
|
27
28
|
bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
|
|
28
29
|
(void)payload;
|
|
30
|
+
if (valid_symbols[GNU_RAW_STRING_BODY] && !valid_symbols[PREPROC_LPAREN]) {
|
|
31
|
+
lexer->result_symbol = GNU_RAW_STRING_BODY;
|
|
32
|
+
return scan_gnu_raw_string_token(lexer);
|
|
33
|
+
}
|
|
34
|
+
if (valid_symbols[RAW_MACRO_TYPE_TAIL] && !valid_symbols[PREPROC_LPAREN]) {
|
|
35
|
+
lexer->result_symbol = RAW_MACRO_TYPE_TAIL;
|
|
36
|
+
return scan_raw_macro_type_tail(lexer);
|
|
37
|
+
}
|
|
29
38
|
if (valid_symbols[PREPROC_FUNCTION_NAME] && !valid_symbols[PREPROC_LPAREN]) {
|
|
30
39
|
lexer->result_symbol = PREPROC_FUNCTION_NAME;
|
|
31
40
|
return scan_function_macro_name(lexer, valid_symbols[PRAGMA_OPERATOR], PRAGMA_OPERATOR);
|
|
@@ -67,6 +76,8 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
|
|
|
67
76
|
|
|
68
77
|
static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols) {
|
|
69
78
|
bool directive_text = !valid_symbols[PREPROC_ARG] && valid_symbols[PREPROC_DIRECTIVE_ARG];
|
|
79
|
+
PreprocArgumentMode argument_mode = preproc_argument_mode(
|
|
80
|
+
valid_symbols[PREPROC_ARG], valid_symbols[PREPROC_DIRECTIVE_ARG], valid_symbols[PREPROC_LPAREN]);
|
|
70
81
|
TSSymbol argument_symbol = directive_text ? PREPROC_DIRECTIVE_ARG : PREPROC_ARG;
|
|
71
82
|
bool argument_valid = valid_symbols[PREPROC_ARG] || directive_text;
|
|
72
83
|
while (valid_symbols[PREPROC_LPAREN] && lexer->lookahead == '\\') {
|
|
@@ -89,7 +100,7 @@ static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols) {
|
|
|
89
100
|
lexer->mark_end(lexer);
|
|
90
101
|
if (!scan_preproc_newline(lexer, true)) {
|
|
91
102
|
lexer->result_symbol = argument_symbol;
|
|
92
|
-
return argument_valid && scan_preproc_arg(lexer, true,
|
|
103
|
+
return argument_valid && scan_preproc_arg(lexer, true, argument_mode);
|
|
93
104
|
}
|
|
94
105
|
}
|
|
95
106
|
if (lexer->eof(lexer) || scan_preproc_newline(lexer, false)) {
|
|
@@ -100,7 +111,7 @@ static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols) {
|
|
|
100
111
|
}
|
|
101
112
|
if (argument_valid) {
|
|
102
113
|
lexer->result_symbol = argument_symbol;
|
|
103
|
-
return scan_preproc_arg(lexer, false,
|
|
114
|
+
return scan_preproc_arg(lexer, false, argument_mode);
|
|
104
115
|
}
|
|
105
116
|
lexer->result_symbol = PRAGMA_OPERATOR;
|
|
106
117
|
return valid_symbols[PRAGMA_OPERATOR] && scan_pragma(lexer);
|
package/tree-sitter-c.wasm
CHANGED
|
Binary file
|
package/tree-sitter.json
CHANGED
|
@@ -18,12 +18,13 @@
|
|
|
18
18
|
"src/identifier.h",
|
|
19
19
|
"src/keywords.h",
|
|
20
20
|
"src/functionMacro.h",
|
|
21
|
-
"src/conditionalMacro.h"
|
|
21
|
+
"src/conditionalMacro.h",
|
|
22
|
+
"src/gnuRawString.h"
|
|
22
23
|
]
|
|
23
24
|
}
|
|
24
25
|
],
|
|
25
26
|
"metadata": {
|
|
26
|
-
"version": "
|
|
27
|
+
"version": "19.0.0",
|
|
27
28
|
"license": "MIT",
|
|
28
29
|
"description": "C grammar for tree-sitter",
|
|
29
30
|
"authors": [
|