Debian Codegraph @

Xtree-house/test-grammars/comment/src/tree_sitter_comment/parser.c

raw symbols SCIP
1 #include "parser.h"
2
3 #include "chars.c"
4 #include "tokens.h"
5 #include <stdbool.h>
6 #include <stdio.h>
7
8 /// Parse the name of the tag.
9 ///
10 /// They can be of the form:
11 /// - TODO:
12 /// - TODO: text
13 /// - TODO(stsewd):
14 /// - TODO(stsewd): text
15 /// - TODO (stsewd): text
16 bool parse_tagname(TSLexer* lexer, const bool* valid_symbols)
17 {
18 if (!is_upper(lexer->lookahead) || !valid_symbols[T_TAGNAME]) {
19 return false;
20 }
21
22 int32_t previous = lexer->lookahead;
23 lexer->advance(lexer, false);
24
25 while (is_upper(lexer->lookahead)
26 || is_digit(lexer->lookahead)
27 || is_internal_char(lexer->lookahead)) {
28 previous = lexer->lookahead;
29 lexer->advance(lexer, false);
30 }
31 // The tag name ends here.
32 // But we keep parsing to see if it's a valid tag name.
33 lexer->mark_end(lexer);
34
35 // It can't end with an internal char.
36 if (is_internal_char(previous)) {
37 return false;
38 }
39
40 // For the user component this is `\s*(`.
41 // We don't parse that part, we just need to be sure it ends with `:\s`.
42 if ((is_space(lexer->lookahead) && !is_newline(lexer->lookahead))
43 || lexer->lookahead == '(') {
44 // Skip white spaces.
45 while (is_space(lexer->lookahead) && !is_newline(lexer->lookahead)) {
46 lexer->advance(lexer, false);
47 }
48 // Checking aperture.
49 if (lexer->lookahead != '(') {
50 return false;
51 }
52 lexer->advance(lexer, false);
53
54 // Checking closure.
55 int user_length = 0;
56 while (lexer->lookahead != ')') {
57 if (is_newline(lexer->lookahead)) {
58 return false;
59 }
60 lexer->advance(lexer, false);
61 user_length++;
62 }
63 if (user_length <= 0) {
64 return false;
65 }
66 lexer->advance(lexer, false);
67 }
68
69 // It should end with `:`...
70 if (lexer->lookahead != ':') {
71 return false;
72 }
73
74 // ... and be followed by one space.
75 lexer->advance(lexer, false);
76 if (!is_space(lexer->lookahead)) {
77 return false;
78 }
79
80 lexer->result_symbol = T_TAGNAME;
81 return true;
82 }
83
84 bool parse(TSLexer* lexer, const bool* valid_symbols)
85 {
86 // If all valid symbols are true, tree-sitter is in correction mode.
87 // We don't want to parse anything in that case.
88 if (valid_symbols[T_INVALID_TOKEN]) {
89 return false;
90 }
91
92 if (is_upper(lexer->lookahead) && valid_symbols[T_TAGNAME]) {
93 return parse_tagname(lexer, valid_symbols);
94 }
95
96 return false;
97 }