Add '+=' assignment

This commit is contained in:
Bruno BELANYI 2024-04-07 21:40:17 +01:00
parent 38bf9532ad
commit bc06816b57
5 changed files with 137 additions and 52 deletions

View file

@ -16,7 +16,7 @@ module.exports = grammar({
assignment: ($) => seq( assignment: ($) => seq(
field("left", $.identifier), field("left", $.identifier),
field("operator", "="), field("operator", choice("=", "+=")),
field("right", $._expr), field("right", $._expr),
), ),

13
src/grammar.json generated
View file

@ -32,8 +32,17 @@
"type": "FIELD", "type": "FIELD",
"name": "operator", "name": "operator",
"content": { "content": {
"type": "STRING", "type": "CHOICE",
"value": "=" "members": [
{
"type": "STRING",
"value": "="
},
{
"type": "STRING",
"value": "+="
}
]
} }
}, },
{ {

8
src/node-types.json generated
View file

@ -17,6 +17,10 @@
"multiple": false, "multiple": false,
"required": true, "required": true,
"types": [ "types": [
{
"type": "+=",
"named": false
},
{ {
"type": "=", "type": "=",
"named": false "named": false
@ -54,6 +58,10 @@
] ]
} }
}, },
{
"type": "+=",
"named": false
},
{ {
"type": "=", "type": "=",
"named": false "named": false

120
src/parser.c generated
View file

@ -7,10 +7,10 @@
#define LANGUAGE_VERSION 14 #define LANGUAGE_VERSION 14
#define STATE_COUNT 8 #define STATE_COUNT 8
#define LARGE_STATE_COUNT 4 #define LARGE_STATE_COUNT 2
#define SYMBOL_COUNT 9 #define SYMBOL_COUNT 10
#define ALIAS_COUNT 0 #define ALIAS_COUNT 0
#define TOKEN_COUNT 4 #define TOKEN_COUNT 5
#define EXTERNAL_TOKEN_COUNT 0 #define EXTERNAL_TOKEN_COUNT 0
#define FIELD_COUNT 3 #define FIELD_COUNT 3
#define MAX_ALIAS_SEQUENCE_LENGTH 3 #define MAX_ALIAS_SEQUENCE_LENGTH 3
@ -18,18 +18,20 @@
enum ts_symbol_identifiers { enum ts_symbol_identifiers {
anon_sym_EQ = 1, anon_sym_EQ = 1,
sym_identifier = 2, anon_sym_PLUS_EQ = 2,
sym_integer_literal = 3, sym_identifier = 3,
sym_source_file = 4, sym_integer_literal = 4,
sym__definition = 5, sym_source_file = 5,
sym_assignment = 6, sym__definition = 6,
sym__expr = 7, sym_assignment = 7,
aux_sym_source_file_repeat1 = 8, sym__expr = 8,
aux_sym_source_file_repeat1 = 9,
}; };
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_EQ] = "=", [anon_sym_EQ] = "=",
[anon_sym_PLUS_EQ] = "+=",
[sym_identifier] = "identifier", [sym_identifier] = "identifier",
[sym_integer_literal] = "integer_literal", [sym_integer_literal] = "integer_literal",
[sym_source_file] = "source_file", [sym_source_file] = "source_file",
@ -42,6 +44,7 @@ static const char * const ts_symbol_names[] = {
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_EQ] = anon_sym_EQ, [anon_sym_EQ] = anon_sym_EQ,
[anon_sym_PLUS_EQ] = anon_sym_PLUS_EQ,
[sym_identifier] = sym_identifier, [sym_identifier] = sym_identifier,
[sym_integer_literal] = sym_integer_literal, [sym_integer_literal] = sym_integer_literal,
[sym_source_file] = sym_source_file, [sym_source_file] = sym_source_file,
@ -60,6 +63,10 @@ static const TSSymbolMetadata ts_symbol_metadata[] = {
.visible = true, .visible = true,
.named = false, .named = false,
}, },
[anon_sym_PLUS_EQ] = {
.visible = true,
.named = false,
},
[sym_identifier] = { [sym_identifier] = {
.visible = true, .visible = true,
.named = true, .named = true,
@ -1960,26 +1967,33 @@ 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(1); if (eof) ADVANCE(2);
if (lookahead == '=') ADVANCE(2); if (lookahead == '+') ADVANCE(1);
if (lookahead == '=') ADVANCE(3);
if (('\t' <= lookahead && lookahead <= '\r') || if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(0) lookahead == ' ') SKIP(0)
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(4); if (('0' <= lookahead && lookahead <= '9')) ADVANCE(6);
if (sym_identifier_character_set_1(lookahead)) ADVANCE(3); if (sym_identifier_character_set_1(lookahead)) ADVANCE(5);
END_STATE(); END_STATE();
case 1: case 1:
ACCEPT_TOKEN(ts_builtin_sym_end); if (lookahead == '=') ADVANCE(4);
END_STATE(); END_STATE();
case 2: case 2:
ACCEPT_TOKEN(anon_sym_EQ); ACCEPT_TOKEN(ts_builtin_sym_end);
END_STATE(); END_STATE();
case 3: case 3:
ACCEPT_TOKEN(sym_identifier); ACCEPT_TOKEN(anon_sym_EQ);
if (sym_identifier_character_set_2(lookahead)) ADVANCE(3);
END_STATE(); END_STATE();
case 4: case 4:
ACCEPT_TOKEN(anon_sym_PLUS_EQ);
END_STATE();
case 5:
ACCEPT_TOKEN(sym_identifier);
if (sym_identifier_character_set_2(lookahead)) ADVANCE(5);
END_STATE();
case 6:
ACCEPT_TOKEN(sym_integer_literal); ACCEPT_TOKEN(sym_integer_literal);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(4); if (('0' <= lookahead && lookahead <= '9')) ADVANCE(6);
END_STATE(); END_STATE();
default: default:
return false; return false;
@ -2001,6 +2015,7 @@ static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = {
[0] = { [0] = {
[ts_builtin_sym_end] = ACTIONS(1), [ts_builtin_sym_end] = ACTIONS(1),
[anon_sym_EQ] = ACTIONS(1), [anon_sym_EQ] = ACTIONS(1),
[anon_sym_PLUS_EQ] = ACTIONS(1),
[sym_identifier] = ACTIONS(1), [sym_identifier] = ACTIONS(1),
[sym_integer_literal] = ACTIONS(1), [sym_integer_literal] = ACTIONS(1),
}, },
@ -2012,59 +2027,66 @@ static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = {
[ts_builtin_sym_end] = ACTIONS(3), [ts_builtin_sym_end] = ACTIONS(3),
[sym_identifier] = ACTIONS(5), [sym_identifier] = ACTIONS(5),
}, },
[2] = {
[sym__definition] = STATE(3),
[sym_assignment] = STATE(3),
[aux_sym_source_file_repeat1] = STATE(3),
[ts_builtin_sym_end] = ACTIONS(7),
[sym_identifier] = ACTIONS(5),
},
[3] = {
[sym__definition] = STATE(3),
[sym_assignment] = STATE(3),
[aux_sym_source_file_repeat1] = STATE(3),
[ts_builtin_sym_end] = ACTIONS(9),
[sym_identifier] = ACTIONS(11),
},
}; };
static const uint16_t ts_small_parse_table[] = { static const uint16_t ts_small_parse_table[] = {
[0] = 2, [0] = 3,
STATE(5), 1, ACTIONS(5), 1,
sym_identifier,
ACTIONS(7), 1,
ts_builtin_sym_end,
STATE(3), 3,
sym__definition,
sym_assignment,
aux_sym_source_file_repeat1,
[12] = 3,
ACTIONS(9), 1,
ts_builtin_sym_end,
ACTIONS(11), 1,
sym_identifier,
STATE(3), 3,
sym__definition,
sym_assignment,
aux_sym_source_file_repeat1,
[24] = 2,
STATE(6), 1,
sym__expr, sym__expr,
ACTIONS(14), 2, ACTIONS(14), 2,
sym_identifier, sym_identifier,
sym_integer_literal, sym_integer_literal,
[8] = 1, [32] = 1,
ACTIONS(16), 2, ACTIONS(16), 2,
anon_sym_EQ,
anon_sym_PLUS_EQ,
[37] = 1,
ACTIONS(18), 2,
ts_builtin_sym_end, ts_builtin_sym_end,
sym_identifier, sym_identifier,
[13] = 1, [42] = 1,
ACTIONS(18), 1,
anon_sym_EQ,
[17] = 1,
ACTIONS(20), 1, ACTIONS(20), 1,
ts_builtin_sym_end, ts_builtin_sym_end,
}; };
static const uint32_t ts_small_parse_table_map[] = { static const uint32_t ts_small_parse_table_map[] = {
[SMALL_STATE(4)] = 0, [SMALL_STATE(2)] = 0,
[SMALL_STATE(5)] = 8, [SMALL_STATE(3)] = 12,
[SMALL_STATE(6)] = 13, [SMALL_STATE(4)] = 24,
[SMALL_STATE(7)] = 17, [SMALL_STATE(5)] = 32,
[SMALL_STATE(6)] = 37,
[SMALL_STATE(7)] = 42,
}; };
static const TSParseActionEntry ts_parse_actions[] = { 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}}, REDUCE(sym_source_file, 0), [3] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 0),
[5] = {.entry = {.count = 1, .reusable = true}}, SHIFT(6), [5] = {.entry = {.count = 1, .reusable = true}}, SHIFT(5),
[7] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1), [7] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1),
[9] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2), [9] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2),
[11] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2), SHIFT_REPEAT(6), [11] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2), SHIFT_REPEAT(5),
[14] = {.entry = {.count = 1, .reusable = true}}, SHIFT(5), [14] = {.entry = {.count = 1, .reusable = true}}, SHIFT(6),
[16] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_assignment, 3, .production_id = 1), [16] = {.entry = {.count = 1, .reusable = true}}, SHIFT(4),
[18] = {.entry = {.count = 1, .reusable = true}}, SHIFT(4), [18] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_assignment, 3, .production_id = 1),
[20] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(), [20] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(),
}; };

View file

@ -32,6 +32,19 @@ foo = bar
(identifier) (identifier)
(identifier))) (identifier)))
================================================================================
Addition assignement
================================================================================
foo += 12
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(integer_literal)))
================================================================================ ================================================================================
Multiple assignments Multiple assignments
================================================================================ ================================================================================
@ -48,3 +61,36 @@ bar = 27
(assignment (assignment
(identifier) (identifier)
(integer_literal))) (integer_literal)))
================================================================================
Various assignements
================================================================================
foo = 0
foo += 12
bar = 27
baz = 42
qux = 1
qux += 1
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal)))