feat: add integer

This commit is contained in:
2025-10-18 12:50:41 +08:00
parent 104742ba31
commit a8d1d2c1dc
4 changed files with 28 additions and 40 deletions
+2 -3
View File
@@ -11,7 +11,6 @@ module.exports = grammar({
name: "emm", name: "emm",
rules: { rules: {
// TODO: add the actual grammar rules integer: ($) => /0|[1-9]\d*/,
source_file: $ => "hello" },
}
}); });
+3 -3
View File
@@ -2,9 +2,9 @@
"$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/grammar.schema.json", "$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/grammar.schema.json",
"name": "emm", "name": "emm",
"rules": { "rules": {
"source_file": { "integer": {
"type": "STRING", "type": "PATTERN",
"value": "hello" "value": "0|[1-9]\\d*"
} }
}, },
"extras": [ "extras": [
+1 -5
View File
@@ -1,12 +1,8 @@
[ [
{ {
"type": "source_file", "type": "integer",
"named": true, "named": true,
"root": true, "root": true,
"fields": {} "fields": {}
},
{
"type": "hello",
"named": false
} }
] ]
Generated
+22 -29
View File
@@ -20,20 +20,20 @@
#define SUPERTYPE_COUNT 0 #define SUPERTYPE_COUNT 0
enum ts_symbol_identifiers { enum ts_symbol_identifiers {
anon_sym_hello = 1, aux_sym_integer_token1 = 1,
sym_source_file = 2, sym_integer = 2,
}; };
static const char * const ts_symbol_names[] = { static const char * const ts_symbol_names[] = {
[ts_builtin_sym_end] = "end", [ts_builtin_sym_end] = "end",
[anon_sym_hello] = "hello", [aux_sym_integer_token1] = "integer_token1",
[sym_source_file] = "source_file", [sym_integer] = "integer",
}; };
static const TSSymbol ts_symbol_map[] = { static const TSSymbol ts_symbol_map[] = {
[ts_builtin_sym_end] = ts_builtin_sym_end, [ts_builtin_sym_end] = ts_builtin_sym_end,
[anon_sym_hello] = anon_sym_hello, [aux_sym_integer_token1] = aux_sym_integer_token1,
[sym_source_file] = sym_source_file, [sym_integer] = sym_integer,
}; };
static const TSSymbolMetadata ts_symbol_metadata[] = { static const TSSymbolMetadata ts_symbol_metadata[] = {
@@ -41,11 +41,11 @@ static const TSSymbolMetadata ts_symbol_metadata[] = {
.visible = false, .visible = false,
.named = true, .named = true,
}, },
[anon_sym_hello] = { [aux_sym_integer_token1] = {
.visible = true, .visible = false,
.named = false, .named = false,
}, },
[sym_source_file] = { [sym_integer] = {
.visible = true, .visible = true,
.named = true, .named = true,
}, },
@@ -71,28 +71,21 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
eof = lexer->eof(lexer); eof = lexer->eof(lexer);
switch (state) { switch (state) {
case 0: case 0:
if (eof) ADVANCE(5); if (eof) ADVANCE(1);
if (lookahead == 'h') ADVANCE(1); if (lookahead == '0') ADVANCE(2);
if (('\t' <= lookahead && lookahead <= '\r') || if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(0); lookahead == ' ') SKIP(0);
if (('1' <= lookahead && lookahead <= '9')) ADVANCE(3);
END_STATE(); END_STATE();
case 1: case 1:
if (lookahead == 'e') ADVANCE(3);
END_STATE();
case 2:
if (lookahead == 'l') ADVANCE(4);
END_STATE();
case 3:
if (lookahead == 'l') ADVANCE(2);
END_STATE();
case 4:
if (lookahead == 'o') ADVANCE(6);
END_STATE();
case 5:
ACCEPT_TOKEN(ts_builtin_sym_end); ACCEPT_TOKEN(ts_builtin_sym_end);
END_STATE(); END_STATE();
case 6: case 2:
ACCEPT_TOKEN(anon_sym_hello); ACCEPT_TOKEN(aux_sym_integer_token1);
END_STATE();
case 3:
ACCEPT_TOKEN(aux_sym_integer_token1);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(3);
END_STATE(); END_STATE();
default: default:
return false; return false;
@@ -109,11 +102,11 @@ static const TSLexerMode ts_lex_modes[STATE_COUNT] = {
static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = {
[STATE(0)] = { [STATE(0)] = {
[ts_builtin_sym_end] = ACTIONS(1), [ts_builtin_sym_end] = ACTIONS(1),
[anon_sym_hello] = ACTIONS(1), [aux_sym_integer_token1] = ACTIONS(1),
}, },
[STATE(1)] = { [STATE(1)] = {
[sym_source_file] = STATE(3), [sym_integer] = STATE(3),
[anon_sym_hello] = ACTIONS(3), [aux_sym_integer_token1] = ACTIONS(3),
}, },
}; };
@@ -135,7 +128,7 @@ static const TSParseActionEntry ts_parse_actions[] = {
[0] = {.entry = {.count = 0, .reusable = false}}, [0] = {.entry = {.count = 0, .reusable = false}},
[1] = {.entry = {.count = 1, .reusable = false}}, RECOVER(), [1] = {.entry = {.count = 1, .reusable = false}}, RECOVER(),
[3] = {.entry = {.count = 1, .reusable = true}}, SHIFT(2), [3] = {.entry = {.count = 1, .reusable = true}}, SHIFT(2),
[5] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1, 0, 0), [5] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_integer, 1, 0, 0),
[7] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(), [7] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(),
}; };