Multi-val/var definitions

Problem
-------
Definitions like the following are not supported:
```scala
val a,b,c: Int = 1
var d,e = ""
```

Solution
-------
Add `$.identifiers` as a choice option along with `$._pattern` to
`$.var_definition`.
Remove `prec(-1)` in `$.identifiers` definitions so that parser wouldn't
on declarations with more than two identifiers.
Resolve a conflict between `$.identifiers` and `$.val_declaration`
text_sliders
susliko 2023-06-08 23:47:13 +07:00
parent d24467932b
commit 9d53ab25cb
3 changed files with 37 additions and 41 deletions

@ -868,7 +868,8 @@ Value definitions
class A { class A {
val b = 1 val b = 1
val c : String = "d" val c : String = "d"
val a1, a2 = pair val a, b, c: T3 = triple
var a, b, c = triple
} }
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -886,6 +887,14 @@ class A {
(string)) (string))
(val_definition (val_definition
(identifiers (identifiers
(identifier)
(identifier)
(identifier))
(type_identifier)
(identifier))
(var_definition
(identifiers
(identifier)
(identifier) (identifier)
(identifier)) (identifier))
(identifier))))) (identifier)))))

@ -3,9 +3,7 @@ const PREC = {
using_directive: 1, using_directive: 1,
control: 1, control: 1,
stable_type_id: 2, stable_type_id: 2,
binding_decl: 2,
while: 2, while: 2,
binding_def: 3,
assign: 3, assign: 3,
case: 3, case: 3,
stable_id: 4, stable_id: 4,
@ -91,6 +89,8 @@ module.exports = grammar({
[$._class_constructor], [$._class_constructor],
// 'enum' _class_constructor '{' 'case' operator_identifier _full_enum_def_repeat1 • _automatic_semicolon … // 'enum' _class_constructor '{' 'case' operator_identifier _full_enum_def_repeat1 • _automatic_semicolon …
[$._full_enum_def], [$._full_enum_def],
// _start_val identifier ',' identifier • ':' …
[$.identifiers, $.val_declaration],
], ],
word: $ => $._alpha_identifier, word: $ => $._alpha_identifier,
@ -456,51 +456,39 @@ module.exports = grammar({
), ),
val_definition: $ => val_definition: $ =>
prec( seq(
PREC.binding_def, $._start_val,
seq( field("pattern", choice($._pattern, $.identifiers)),
$._start_val, optional(seq(":", field("type", $._type))),
field("pattern", choice($._pattern, $.identifiers)), "=",
optional(seq(":", field("type", $._type))), field("value", $._indentable_expression),
"=",
field("value", $._indentable_expression),
),
), ),
val_declaration: $ => val_declaration: $ =>
prec( seq(
PREC.binding_decl, $._start_val,
seq( commaSep1(field("name", $._identifier)),
$._start_val, ":",
commaSep1(field("name", $._identifier)), field("type", $._type),
":",
field("type", $._type),
),
), ),
_start_val: $ => seq(repeat($.annotation), optional($.modifiers), "val"), _start_val: $ => seq(repeat($.annotation), optional($.modifiers), "val"),
var_declaration: $ => var_declaration: $ =>
prec( seq(
PREC.binding_decl, $._start_var,
seq( commaSep1(field("name", $._identifier)),
$._start_var, ":",
commaSep1(field("name", $._identifier)), field("type", $._type),
":",
field("type", $._type),
),
), ),
var_definition: $ => var_definition: $ =>
prec( seq(
PREC.binding_def, $._start_var,
seq( field("pattern", choice($._pattern, $.identifiers)),
$._start_var, optional(seq(":", field("type", $._type))),
field("pattern", $._pattern), "=",
optional(seq(":", field("type", $._type))), field("value", $._indentable_expression),
"=",
field("value", $._indentable_expression),
),
), ),
_start_var: $ => seq(repeat($.annotation), optional($.modifiers), "var"), _start_var: $ => seq(repeat($.annotation), optional($.modifiers), "var"),
@ -1384,8 +1372,7 @@ module.exports = grammar({
_identifier: $ => choice($.identifier, $.operator_identifier), _identifier: $ => choice($.identifier, $.operator_identifier),
identifiers: $ => identifiers: $ => seq($.identifier, ",", commaSep1($.identifier)),
prec.left(-1, seq($.identifier, ",", commaSep1($.identifier))),
wildcard: $ => "_", wildcard: $ => "_",

@ -2,8 +2,8 @@
# This is an integration test to generally check the quality of parsing. # This is an integration test to generally check the quality of parsing.
SCALA_SCALA_LIBRARY_EXPECTED=95 SCALA_SCALA_LIBRARY_EXPECTED=97
SCALA_SCALA_COMPILER_EXPECTED=89 SCALA_SCALA_COMPILER_EXPECTED=93
DOTTY_COMPILER_EXPECTED=82 DOTTY_COMPILER_EXPECTED=82
SYNTAX_COMPLEXITY_CEILING=2500 SYNTAX_COMPLEXITY_CEILING=2500