Skip to content

Commit

Permalink
fix: Fix empty perm and function call conflict
Browse files Browse the repository at this point in the history
Fix empty perm and function call conflict.

Begin adding line continuations.
  • Loading branch information
reiniscirpons committed May 26, 2024
1 parent d72bc06 commit 3da543f
Show file tree
Hide file tree
Showing 4 changed files with 7,005 additions and 4,766 deletions.
46 changes: 33 additions & 13 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = grammar({

extras: $ => [
$.comment,
/\s/
/\s/,
$.line_continuation
],

inline: $ => [
Expand Down Expand Up @@ -266,7 +267,16 @@ module.exports = grammar({
$.escape_sequence
)),

escape_sequence: $ => token(seq(
// TODO: Properly handle line continuation characters in strings.
// Currently the test
// "abc\
// def"
// Gets parsed as
// (string (escape_sequence))
// instead of
// (string) (line_continuation) (string)
// Likely easiest to fix when implementing proper line continuation logic.
escape_sequence: _ => token(seq(
'\\',
choice(
/[^0-7]/, // single character
Expand Down Expand Up @@ -313,7 +323,7 @@ module.exports = grammar({
)
),

ellipsis: $ => '...',
ellipsis: _ => '...',

locals: $ => seq(
"local", commaSep1($.identifier), ";"
Expand All @@ -331,11 +341,17 @@ module.exports = grammar({
field('arguments', $.argument_list)
)),

argument_list: $ => seq(
'(',
optional(commaSep1($._expression)),
// TODO: handle options: `f(1,2 : opt)` or `f(1,2 : opt1 := true, opt2: = b)`
')'
argument_list: $ => choice(
// Need to have the empty call separately to disambiguate with empty
// permutation. This is possibly due to the "Match Specificity" rule in the
// tree-sitter spec.
'()',
seq(
'(',
commaSep1($._expression),
// TODO: handle options: `f(1,2 : opt)` or `f(1,2 : opt1 := true, opt2: = b)`
')'
)
),

// TODO: add special rules for calls to Declare{GlobalFunction,Operation,...},
Expand Down Expand Up @@ -372,9 +388,7 @@ module.exports = grammar({
),

permutation_expression: $ => choice(
// TODO: fix conflict with empty argument list,
// i.e. want f() to parse as a call and an identifier and permuation
// '()',
'()',
prec.right(repeat1($.permutation_cycle_expression))
),

Expand All @@ -397,9 +411,15 @@ module.exports = grammar({
// \[\]
// \+
// multi\ word\ identifier
identifier: $ => /[a-zA-Z_@][a-zA-Z_@0-9]*/,
identifier: _ => /[a-zA-Z_@][a-zA-Z_@0-9]*/,

comment: _ => token(seq('#', /.*/)),

comment: $ => token(seq('#', /.*/))
// TODO: make line continuations seamless, i.e. parse
// 1234\
// 5678
// as just (integer) instead of ((integer) (line_continuation) (integer))
line_continuation: _ => token(seq('\\', choice(seq(optional('\r'), '\n'), '\0'))),

}
});
Expand Down
67 changes: 59 additions & 8 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1286,15 +1286,19 @@
}
},
"argument_list": {
"type": "SEQ",
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "("
"value": "()"
},
{
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SEQ",
"members": [
Expand All @@ -1321,13 +1325,10 @@
]
},
{
"type": "BLANK"
"type": "STRING",
"value": ")"
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
Expand Down Expand Up @@ -1560,6 +1561,10 @@
"permutation_expression": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "()"
},
{
"type": "PREC_RIGHT",
"value": 0,
Expand Down Expand Up @@ -1660,6 +1665,48 @@
}
]
}
},
"line_continuation": {
"type": "TOKEN",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\\"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "\r"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "\n"
}
]
},
{
"type": "STRING",
"value": "\u0000"
}
]
}
]
}
}
},
"extras": [
Expand All @@ -1670,6 +1717,10 @@
{
"type": "PATTERN",
"value": "\\s"
},
{
"type": "SYMBOL",
"name": "line_continuation"
}
],
"conflicts": [
Expand Down
10 changes: 9 additions & 1 deletion src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@
"fields": {},
"children": {
"multiple": true,
"required": true,
"required": false,
"types": [
{
"type": "permutation_cycle_expression",
Expand Down Expand Up @@ -2197,6 +2197,10 @@
"type": "(",
"named": false
},
{
"type": "()",
"named": false
},
{
"type": ")",
"named": false
Expand Down Expand Up @@ -2345,6 +2349,10 @@
"type": "integer",
"named": true
},
{
"type": "line_continuation",
"named": true
},
{
"type": "local",
"named": false
Expand Down
Loading

0 comments on commit 3da543f

Please sign in to comment.