Skip to content

Commit 3a85528

Browse files
committed
Import CSS test grammar
1 parent 9ac17cb commit 3a85528

File tree

8 files changed

+392
-0
lines changed

8 files changed

+392
-0
lines changed

test-grammars/css/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Max Brunsfeld
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

test-grammars/css/highlights.scm

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
(comment) @comment
2+
3+
[
4+
(tag_name)
5+
(nesting_selector)
6+
(universal_selector)
7+
] @tag
8+
9+
[
10+
"~"
11+
">"
12+
"+"
13+
"-"
14+
"*"
15+
"/"
16+
"="
17+
"^="
18+
"|="
19+
"~="
20+
"$="
21+
"*="
22+
] @operator
23+
24+
[
25+
"and"
26+
"not"
27+
"only"
28+
"or"
29+
] @keyword.operator
30+
31+
(property_name) @variable.other.member
32+
(plain_value) @constant
33+
34+
((property_name) @variable
35+
(#match? @variable "^--"))
36+
((plain_value) @variable
37+
(#match? @variable "^--"))
38+
39+
(attribute_name) @attribute
40+
(class_name) @label
41+
(feature_name) @variable.other.member
42+
(function_name) @function
43+
(id_name) @label
44+
(namespace_name) @namespace
45+
46+
[
47+
"@charset"
48+
"@import"
49+
"@keyframes"
50+
"@media"
51+
"@namespace"
52+
"@supports"
53+
(at_keyword)
54+
(from)
55+
(important)
56+
(to)
57+
(keyword_query)
58+
(keyframes_name)
59+
(unit)
60+
] @keyword
61+
62+
[
63+
"#"
64+
"."
65+
] @punctuation
66+
67+
(string_value) @string
68+
((color_value) "#") @string.special
69+
(color_value) @string.special
70+
71+
(integer_value) @constant.numeric.integer
72+
(float_value) @constant.numeric.float
73+
74+
[
75+
")"
76+
"("
77+
"["
78+
"]"
79+
"{"
80+
"}"
81+
] @punctuation.bracket
82+
83+
[
84+
","
85+
";"
86+
":"
87+
"::"
88+
] @punctuation.delimiter

test-grammars/css/injections.scm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
((comment) @injection.content
2+
(#set! injection.language "comment"))

test-grammars/css/metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"repo": "https://github.com/tree-sitter/tree-sitter-css",
3+
"rev": "769203d0f9abe1a9a691ac2b9fe4bb4397a73c51",
4+
"license": "MIT",
5+
"compressed": true
6+
}

test-grammars/css/src/grammar.json

2.17 KB
Binary file not shown.

test-grammars/css/src/parser.c

14.5 KB
Binary file not shown.

test-grammars/css/src/scanner.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <tree_sitter/parser.h>
2+
#include <wctype.h>
3+
4+
enum TokenType {
5+
DESCENDANT_OP,
6+
};
7+
8+
void *tree_sitter_css_external_scanner_create() { return NULL; }
9+
void tree_sitter_css_external_scanner_destroy(void *p) {}
10+
void tree_sitter_css_external_scanner_reset(void *p) {}
11+
unsigned tree_sitter_css_external_scanner_serialize(void *p, char *buffer) { return 0; }
12+
void tree_sitter_css_external_scanner_deserialize(void *p, const char *b, unsigned n) {}
13+
14+
bool tree_sitter_css_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
15+
if (iswspace(lexer->lookahead) && valid_symbols[DESCENDANT_OP]) {
16+
lexer->result_symbol = DESCENDANT_OP;
17+
18+
lexer->advance(lexer, true);
19+
while (iswspace(lexer->lookahead)) {
20+
lexer->advance(lexer, true);
21+
}
22+
lexer->mark_end(lexer);
23+
24+
if (
25+
lexer->lookahead == '#' ||
26+
lexer->lookahead == '.' ||
27+
lexer->lookahead == '[' ||
28+
lexer->lookahead == '-' ||
29+
iswalnum(lexer->lookahead)
30+
) {
31+
return true;
32+
}
33+
34+
if (lexer->lookahead == ':') {
35+
lexer->advance(lexer, false);
36+
if (iswspace(lexer->lookahead)) return false;
37+
for (;;) {
38+
if (
39+
lexer->lookahead == ';' ||
40+
lexer->lookahead == '}' ||
41+
lexer->eof(lexer)
42+
) return false;
43+
if (lexer->lookahead == '{') {
44+
return true;
45+
}
46+
lexer->advance(lexer, false);
47+
}
48+
}
49+
}
50+
51+
return false;
52+
}
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
#ifndef TREE_SITTER_PARSER_H_
2+
#define TREE_SITTER_PARSER_H_
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <stdbool.h>
9+
#include <stdint.h>
10+
#include <stdlib.h>
11+
12+
#define ts_builtin_sym_error ((TSSymbol)-1)
13+
#define ts_builtin_sym_end 0
14+
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
15+
16+
typedef uint16_t TSStateId;
17+
18+
#ifndef TREE_SITTER_API_H_
19+
typedef uint16_t TSSymbol;
20+
typedef uint16_t TSFieldId;
21+
typedef struct TSLanguage TSLanguage;
22+
#endif
23+
24+
typedef struct {
25+
TSFieldId field_id;
26+
uint8_t child_index;
27+
bool inherited;
28+
} TSFieldMapEntry;
29+
30+
typedef struct {
31+
uint16_t index;
32+
uint16_t length;
33+
} TSFieldMapSlice;
34+
35+
typedef struct {
36+
bool visible;
37+
bool named;
38+
bool supertype;
39+
} TSSymbolMetadata;
40+
41+
typedef struct TSLexer TSLexer;
42+
43+
struct TSLexer {
44+
int32_t lookahead;
45+
TSSymbol result_symbol;
46+
void (*advance)(TSLexer *, bool);
47+
void (*mark_end)(TSLexer *);
48+
uint32_t (*get_column)(TSLexer *);
49+
bool (*is_at_included_range_start)(const TSLexer *);
50+
bool (*eof)(const TSLexer *);
51+
};
52+
53+
typedef enum {
54+
TSParseActionTypeShift,
55+
TSParseActionTypeReduce,
56+
TSParseActionTypeAccept,
57+
TSParseActionTypeRecover,
58+
} TSParseActionType;
59+
60+
typedef union {
61+
struct {
62+
uint8_t type;
63+
TSStateId state;
64+
bool extra;
65+
bool repetition;
66+
} shift;
67+
struct {
68+
uint8_t type;
69+
uint8_t child_count;
70+
TSSymbol symbol;
71+
int16_t dynamic_precedence;
72+
uint16_t production_id;
73+
} reduce;
74+
uint8_t type;
75+
} TSParseAction;
76+
77+
typedef struct {
78+
uint16_t lex_state;
79+
uint16_t external_lex_state;
80+
} TSLexMode;
81+
82+
typedef union {
83+
TSParseAction action;
84+
struct {
85+
uint8_t count;
86+
bool reusable;
87+
} entry;
88+
} TSParseActionEntry;
89+
90+
struct TSLanguage {
91+
uint32_t version;
92+
uint32_t symbol_count;
93+
uint32_t alias_count;
94+
uint32_t token_count;
95+
uint32_t external_token_count;
96+
uint32_t state_count;
97+
uint32_t large_state_count;
98+
uint32_t production_id_count;
99+
uint32_t field_count;
100+
uint16_t max_alias_sequence_length;
101+
const uint16_t *parse_table;
102+
const uint16_t *small_parse_table;
103+
const uint32_t *small_parse_table_map;
104+
const TSParseActionEntry *parse_actions;
105+
const char * const *symbol_names;
106+
const char * const *field_names;
107+
const TSFieldMapSlice *field_map_slices;
108+
const TSFieldMapEntry *field_map_entries;
109+
const TSSymbolMetadata *symbol_metadata;
110+
const TSSymbol *public_symbol_map;
111+
const uint16_t *alias_map;
112+
const TSSymbol *alias_sequences;
113+
const TSLexMode *lex_modes;
114+
bool (*lex_fn)(TSLexer *, TSStateId);
115+
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
116+
TSSymbol keyword_capture_token;
117+
struct {
118+
const bool *states;
119+
const TSSymbol *symbol_map;
120+
void *(*create)(void);
121+
void (*destroy)(void *);
122+
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
123+
unsigned (*serialize)(void *, char *);
124+
void (*deserialize)(void *, const char *, unsigned);
125+
} external_scanner;
126+
};
127+
128+
/*
129+
* Lexer Macros
130+
*/
131+
132+
#define START_LEXER() \
133+
bool result = false; \
134+
bool skip = false; \
135+
bool eof = false; \
136+
int32_t lookahead; \
137+
goto start; \
138+
next_state: \
139+
lexer->advance(lexer, skip); \
140+
start: \
141+
skip = false; \
142+
lookahead = lexer->lookahead;
143+
144+
#define ADVANCE(state_value) \
145+
{ \
146+
state = state_value; \
147+
goto next_state; \
148+
}
149+
150+
#define SKIP(state_value) \
151+
{ \
152+
skip = true; \
153+
state = state_value; \
154+
goto next_state; \
155+
}
156+
157+
#define ACCEPT_TOKEN(symbol_value) \
158+
result = true; \
159+
lexer->result_symbol = symbol_value; \
160+
lexer->mark_end(lexer);
161+
162+
#define END_STATE() return result;
163+
164+
/*
165+
* Parse Table Macros
166+
*/
167+
168+
#define SMALL_STATE(id) id - LARGE_STATE_COUNT
169+
170+
#define STATE(id) id
171+
172+
#define ACTIONS(id) id
173+
174+
#define SHIFT(state_value) \
175+
{{ \
176+
.shift = { \
177+
.type = TSParseActionTypeShift, \
178+
.state = state_value \
179+
} \
180+
}}
181+
182+
#define SHIFT_REPEAT(state_value) \
183+
{{ \
184+
.shift = { \
185+
.type = TSParseActionTypeShift, \
186+
.state = state_value, \
187+
.repetition = true \
188+
} \
189+
}}
190+
191+
#define SHIFT_EXTRA() \
192+
{{ \
193+
.shift = { \
194+
.type = TSParseActionTypeShift, \
195+
.extra = true \
196+
} \
197+
}}
198+
199+
#define REDUCE(symbol_val, child_count_val, ...) \
200+
{{ \
201+
.reduce = { \
202+
.type = TSParseActionTypeReduce, \
203+
.symbol = symbol_val, \
204+
.child_count = child_count_val, \
205+
__VA_ARGS__ \
206+
}, \
207+
}}
208+
209+
#define RECOVER() \
210+
{{ \
211+
.type = TSParseActionTypeRecover \
212+
}}
213+
214+
#define ACCEPT_INPUT() \
215+
{{ \
216+
.type = TSParseActionTypeAccept \
217+
}}
218+
219+
#ifdef __cplusplus
220+
}
221+
#endif
222+
223+
#endif // TREE_SITTER_PARSER_H_

0 commit comments

Comments
 (0)