From 0827093fe76b9f2e2abe4711ca63719e1f3506fe Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 3 Jun 2022 10:31:30 +0200 Subject: [PATCH] Add 'field_identifier' alias This will be useful for high-lighting. --- grammar.js | 7 +++++-- src/grammar.json | 18 ++++++++++++++---- src/node-types.json | 8 ++++++-- src/parser.c | 19 +++++++++++++++++-- test/corpus/expressions.txt | 10 +++++----- test/corpus/lvalues.txt | 2 +- 6 files changed, 48 insertions(+), 16 deletions(-) diff --git a/grammar.js b/grammar.js index 94100c4..1ff2838 100644 --- a/grammar.js +++ b/grammar.js @@ -23,6 +23,7 @@ module.exports = grammar({ inline: ($) => [ $._type_identifier, + $._field_identifier, ], conflicts: ($) => [ @@ -88,6 +89,8 @@ module.exports = grammar({ _type_identifier: ($) => alias($.identifier, $.type_identifier), + _field_identifier: ($) => alias($.identifier, $.field_identifier), + escape_sequence: (_) => token.immediate( seq( "\\", @@ -113,7 +116,7 @@ module.exports = grammar({ record_value: ($) => seq( field("record", $._lvalue), ".", - field("field", $.identifier), + field("field", $._field_identifier), ), array_value: ($) => seq( @@ -177,7 +180,7 @@ module.exports = grammar({ sepBy( ",", seq( - field("field", $.identifier), + field("field", $._field_identifier), "=", field("init", $._expr), ), diff --git a/src/grammar.json b/src/grammar.json index b8cccfd..11518af 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -142,6 +142,15 @@ "named": true, "value": "type_identifier" }, + "_field_identifier": { + "type": "ALIAS", + "content": { + "type": "SYMBOL", + "name": "identifier" + }, + "named": true, + "value": "field_identifier" + }, "escape_sequence": { "type": "IMMEDIATE_TOKEN", "content": { @@ -259,7 +268,7 @@ "name": "field", "content": { "type": "SYMBOL", - "name": "identifier" + "name": "_field_identifier" } } ] @@ -731,7 +740,7 @@ "name": "field", "content": { "type": "SYMBOL", - "name": "identifier" + "name": "_field_identifier" } }, { @@ -765,7 +774,7 @@ "name": "field", "content": { "type": "SYMBOL", - "name": "identifier" + "name": "_field_identifier" } }, { @@ -1460,7 +1469,8 @@ } ], "inline": [ - "_type_identifier" + "_type_identifier", + "_field_identifier" ], "supertypes": [] } diff --git a/src/node-types.json b/src/node-types.json index 3e2e9d8..b6918cd 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -1466,7 +1466,7 @@ "required": false, "types": [ { - "type": "identifier", + "type": "field_identifier", "named": true } ] @@ -1596,7 +1596,7 @@ "required": true, "types": [ { - "type": "identifier", + "type": "field_identifier", "named": true } ] @@ -2303,6 +2303,10 @@ "type": "escape_sequence", "named": true }, + { + "type": "field_identifier", + "named": true + }, { "type": "for", "named": false diff --git a/src/parser.c b/src/parser.c index 688af3c..8e9c799 100644 --- a/src/parser.c +++ b/src/parser.c @@ -9,7 +9,7 @@ #define STATE_COUNT 148 #define LARGE_STATE_COUNT 2 #define SYMBOL_COUNT 87 -#define ALIAS_COUNT 1 +#define ALIAS_COUNT 2 #define TOKEN_COUNT 49 #define EXTERNAL_TOKEN_COUNT 1 #define FIELD_COUNT 26 @@ -103,7 +103,8 @@ enum { aux_sym__declaration_chunk_repeat1 = 84, aux_sym__declaration_chunk_repeat2 = 85, aux_sym_record_type_repeat1 = 86, - alias_sym_type_identifier = 87, + alias_sym_field_identifier = 87, + alias_sym_type_identifier = 88, }; static const char * const ts_symbol_names[] = { @@ -194,6 +195,7 @@ static const char * const ts_symbol_names[] = { [aux_sym__declaration_chunk_repeat1] = "_declaration_chunk_repeat1", [aux_sym__declaration_chunk_repeat2] = "_declaration_chunk_repeat2", [aux_sym_record_type_repeat1] = "record_type_repeat1", + [alias_sym_field_identifier] = "field_identifier", [alias_sym_type_identifier] = "type_identifier", }; @@ -285,6 +287,7 @@ static const TSSymbol ts_symbol_map[] = { [aux_sym__declaration_chunk_repeat1] = aux_sym__declaration_chunk_repeat1, [aux_sym__declaration_chunk_repeat2] = aux_sym__declaration_chunk_repeat2, [aux_sym_record_type_repeat1] = aux_sym_record_type_repeat1, + [alias_sym_field_identifier] = alias_sym_field_identifier, [alias_sym_type_identifier] = alias_sym_type_identifier, }; @@ -637,6 +640,10 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { .visible = false, .named = false, }, + [alias_sym_field_identifier] = { + .visible = true, + .named = true, + }, [alias_sym_type_identifier] = { .visible = true, .named = true, @@ -872,6 +879,9 @@ static const TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE [7] = { [1] = anon_sym_DASH, }, + [8] = { + [2] = alias_sym_field_identifier, + }, [15] = { [0] = alias_sym_type_identifier, }, @@ -883,12 +893,17 @@ static const TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE }, [25] = { [0] = alias_sym_type_identifier, + [2] = alias_sym_field_identifier, }, [32] = { [3] = alias_sym_type_identifier, }, [33] = { [0] = alias_sym_type_identifier, + [2] = alias_sym_field_identifier, + }, + [36] = { + [1] = alias_sym_field_identifier, }, }; diff --git a/test/corpus/expressions.txt b/test/corpus/expressions.txt index 11ccb61..8d2520a 100644 --- a/test/corpus/expressions.txt +++ b/test/corpus/expressions.txt @@ -40,9 +40,9 @@ record_type { a = 12, b = "27" } (source_file (record_expression type: (type_identifier) - field: (identifier) + field: (field_identifier) init: (integer_literal) - field: (identifier) + field: (field_identifier) init: (string_literal))) ================================================================================ @@ -56,7 +56,7 @@ record_type { a = 12 } (source_file (record_expression type: (type_identifier) - field: (identifier) + field: (field_identifier) init: (integer_literal))) ================================================================================ @@ -82,7 +82,7 @@ record_type { a = 12, } (source_file (record_expression type: (type_identifier) - field: (identifier) + field: (field_identifier) init: (integer_literal) (ERROR))) @@ -156,4 +156,4 @@ a := array[12] := record.field index: (integer_literal)) right: (record_value record: (identifier) - field: (identifier))))) + field: (field_identifier))))) diff --git a/test/corpus/lvalues.txt b/test/corpus/lvalues.txt index 2c4cc3a..6f50485 100644 --- a/test/corpus/lvalues.txt +++ b/test/corpus/lvalues.txt @@ -33,4 +33,4 @@ a_record.field (source_file (record_value record: (identifier) - field: (identifier))) + field: (field_identifier)))