Add lvalue expressions

This commit is contained in:
Bruno BELANYI 2022-06-01 20:07:00 +02:00
parent 8921953bbb
commit 6a3e4663ed
6 changed files with 1788 additions and 1150 deletions

View file

@ -20,6 +20,10 @@ module.exports = grammar({
// Ensure we don't extract keywords from tokens // Ensure we don't extract keywords from tokens
word: ($) => $.identifier, word: ($) => $.identifier,
conflicts: ($) => [
[$._lvalue, $.array_expression],
],
rules: { rules: {
source_file: ($) => choice( source_file: ($) => choice(
$._expr, $._expr,
@ -33,6 +37,8 @@ module.exports = grammar({
$.array_expression, $.array_expression,
$.record_expression, $.record_expression,
$._lvalue,
$.function_call, $.function_call,
$.unary_expression, $.unary_expression,
@ -69,6 +75,25 @@ module.exports = grammar({
) )
), ),
_lvalue: ($) => choice(
$.identifier,
$.record_value,
$.array_value,
),
record_value: ($) => seq(
field("record", $._lvalue),
".",
field("field", $.identifier),
),
array_value: ($) => seq(
field("array", $._lvalue),
"[",
field("index", $._expr),
"]",
),
function_call: ($) => seq( function_call: ($) => seq(
field("function", $.identifier), field("function", $.identifier),
"(", "(",

View file

@ -34,6 +34,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "record_expression" "name": "record_expression"
}, },
{
"type": "SYMBOL",
"name": "_lvalue"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "function_call" "name": "function_call"
@ -173,6 +177,77 @@
] ]
} }
}, },
"_lvalue": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "record_value"
},
{
"type": "SYMBOL",
"name": "array_value"
}
]
},
"record_value": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "record",
"content": {
"type": "SYMBOL",
"name": "_lvalue"
}
},
{
"type": "STRING",
"value": "."
},
{
"type": "FIELD",
"name": "field",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
}
]
},
"array_value": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "array",
"content": {
"type": "SYMBOL",
"name": "_lvalue"
}
},
{
"type": "STRING",
"value": "["
},
{
"type": "FIELD",
"name": "index",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
},
{
"type": "STRING",
"value": "]"
}
]
},
"function_call": { "function_call": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -685,7 +760,12 @@
"value": "\\s" "value": "\\s"
} }
], ],
"conflicts": [], "conflicts": [
[
"_lvalue",
"array_expression"
]
],
"precedences": [], "precedences": [],
"externals": [], "externals": [],
"inline": [], "inline": [],

View file

@ -11,6 +11,10 @@
"type": "array_expression", "type": "array_expression",
"named": true "named": true
}, },
{
"type": "array_value",
"named": true
},
{ {
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
@ -19,6 +23,10 @@
"type": "function_call", "type": "function_call",
"named": true "named": true
}, },
{
"type": "identifier",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -31,6 +39,10 @@
"type": "record_expression", "type": "record_expression",
"named": true "named": true
}, },
{
"type": "record_value",
"named": true
},
{ {
"type": "sequence_expression", "type": "sequence_expression",
"named": true "named": true
@ -53,6 +65,10 @@
"type": "array_expression", "type": "array_expression",
"named": true "named": true
}, },
{
"type": "array_value",
"named": true
},
{ {
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
@ -61,6 +77,10 @@
"type": "function_call", "type": "function_call",
"named": true "named": true
}, },
{
"type": "identifier",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -73,6 +93,10 @@
"type": "record_expression", "type": "record_expression",
"named": true "named": true
}, },
{
"type": "record_value",
"named": true
},
{ {
"type": "sequence_expression", "type": "sequence_expression",
"named": true "named": true
@ -100,10 +124,28 @@
} }
}, },
{ {
"type": "binary_expression", "type": "array_value",
"named": true, "named": true,
"fields": { "fields": {
"left": { "array": {
"multiple": false,
"required": true,
"types": [
{
"type": "array_value",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "record_value",
"named": true
}
]
},
"index": {
"multiple": false, "multiple": false,
"required": true, "required": true,
"types": [ "types": [
@ -111,6 +153,10 @@
"type": "array_expression", "type": "array_expression",
"named": true "named": true
}, },
{
"type": "array_value",
"named": true
},
{ {
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
@ -119,6 +165,10 @@
"type": "function_call", "type": "function_call",
"named": true "named": true
}, },
{
"type": "identifier",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -131,6 +181,70 @@
"type": "record_expression", "type": "record_expression",
"named": true "named": true
}, },
{
"type": "record_value",
"named": true
},
{
"type": "sequence_expression",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_expression",
"named": true
}
]
}
}
},
{
"type": "binary_expression",
"named": true,
"fields": {
"left": {
"multiple": false,
"required": true,
"types": [
{
"type": "array_expression",
"named": true
},
{
"type": "array_value",
"named": true
},
{
"type": "binary_expression",
"named": true
},
{
"type": "function_call",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "nil_literal",
"named": true
},
{
"type": "record_expression",
"named": true
},
{
"type": "record_value",
"named": true
},
{ {
"type": "sequence_expression", "type": "sequence_expression",
"named": true "named": true
@ -163,6 +277,10 @@
"type": "array_expression", "type": "array_expression",
"named": true "named": true
}, },
{
"type": "array_value",
"named": true
},
{ {
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
@ -171,6 +289,10 @@
"type": "function_call", "type": "function_call",
"named": true "named": true
}, },
{
"type": "identifier",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -183,6 +305,10 @@
"type": "record_expression", "type": "record_expression",
"named": true "named": true
}, },
{
"type": "record_value",
"named": true
},
{ {
"type": "sequence_expression", "type": "sequence_expression",
"named": true "named": true
@ -215,6 +341,10 @@
"type": "array_expression", "type": "array_expression",
"named": true "named": true
}, },
{
"type": "array_value",
"named": true
},
{ {
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
@ -223,6 +353,10 @@
"type": "function_call", "type": "function_call",
"named": true "named": true
}, },
{
"type": "identifier",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -235,6 +369,10 @@
"type": "record_expression", "type": "record_expression",
"named": true "named": true
}, },
{
"type": "record_value",
"named": true
},
{ {
"type": "sequence_expression", "type": "sequence_expression",
"named": true "named": true
@ -283,6 +421,10 @@
"type": "array_expression", "type": "array_expression",
"named": true "named": true
}, },
{
"type": "array_value",
"named": true
},
{ {
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
@ -291,6 +433,10 @@
"type": "function_call", "type": "function_call",
"named": true "named": true
}, },
{
"type": "identifier",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -303,6 +449,10 @@
"type": "record_expression", "type": "record_expression",
"named": true "named": true
}, },
{
"type": "record_value",
"named": true
},
{ {
"type": "sequence_expression", "type": "sequence_expression",
"named": true "named": true
@ -329,6 +479,40 @@
} }
} }
}, },
{
"type": "record_value",
"named": true,
"fields": {
"field": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"record": {
"multiple": false,
"required": true,
"types": [
{
"type": "array_value",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "record_value",
"named": true
}
]
}
}
},
{ {
"type": "sequence_expression", "type": "sequence_expression",
"named": true, "named": true,
@ -341,6 +525,10 @@
"type": "array_expression", "type": "array_expression",
"named": true "named": true
}, },
{
"type": "array_value",
"named": true
},
{ {
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
@ -349,6 +537,10 @@
"type": "function_call", "type": "function_call",
"named": true "named": true
}, },
{
"type": "identifier",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -361,6 +553,10 @@
"type": "record_expression", "type": "record_expression",
"named": true "named": true
}, },
{
"type": "record_value",
"named": true
},
{ {
"type": "sequence_expression", "type": "sequence_expression",
"named": true "named": true
@ -388,6 +584,10 @@
"type": "array_expression", "type": "array_expression",
"named": true "named": true
}, },
{
"type": "array_value",
"named": true
},
{ {
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
@ -396,6 +596,10 @@
"type": "function_call", "type": "function_call",
"named": true "named": true
}, },
{
"type": "identifier",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -408,6 +612,10 @@
"type": "record_expression", "type": "record_expression",
"named": true "named": true
}, },
{
"type": "record_value",
"named": true
},
{ {
"type": "sequence_expression", "type": "sequence_expression",
"named": true "named": true
@ -450,6 +658,10 @@
"type": "array_expression", "type": "array_expression",
"named": true "named": true
}, },
{
"type": "array_value",
"named": true
},
{ {
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
@ -458,6 +670,10 @@
"type": "function_call", "type": "function_call",
"named": true "named": true
}, },
{
"type": "identifier",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -470,6 +686,10 @@
"type": "record_expression", "type": "record_expression",
"named": true "named": true
}, },
{
"type": "record_value",
"named": true
},
{ {
"type": "sequence_expression", "type": "sequence_expression",
"named": true "named": true
@ -512,6 +732,10 @@
"type": ",", "type": ",",
"named": false "named": false
}, },
{
"type": ".",
"named": false
},
{ {
"type": ";", "type": ";",
"named": false "named": false

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@ _main
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(ERROR (source_file
(identifier)) (identifier))
================================================================================ ================================================================================
@ -17,5 +17,5 @@ my_1st_variable
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(ERROR (source_file
(identifier)) (identifier))

36
test/corpus/lvalues.txt Normal file
View file

@ -0,0 +1,36 @@
================================================================================
Variable
================================================================================
a_variable
--------------------------------------------------------------------------------
(source_file
(identifier))
================================================================================
Array indexing
================================================================================
an_array[12]
--------------------------------------------------------------------------------
(source_file
(array_value
array: (identifier)
index: (integer_literal)))
================================================================================
Record field
================================================================================
a_record.field
--------------------------------------------------------------------------------
(source_file
(record_value
record: (identifier)
field: (identifier)))