Merge pull request #237 from susliko/soft-keyword-identifiers

Allow soft keywords as identifiers
text_sliders
eugene yokota 2023-05-30 22:23:43 +07:00 committed by GitHub
commit 305504c509
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 13 deletions

@ -1615,3 +1615,46 @@ addSbtPlugin("foo" % "bar" % "0.1")
(string)) (string))
(operator_identifier) (operator_identifier)
(string))))) (string)))))
================================================================================
Soft keywords used as identifiers (Scala 3)
================================================================================
infix()
opaque()
open()
transparent()
as()
derives()
end()
throws()
using()
--------------------------------------------------------------------------------
(compilation_unit
(call_expression
(identifier)
(arguments))
(call_expression
(identifier)
(arguments))
(call_expression
(identifier)
(arguments))
(call_expression
(identifier)
(arguments))
(call_expression
(identifier)
(arguments))
(call_expression
(identifier)
(arguments))
(call_expression
(identifier)
(arguments))
(call_expression
(identifier)
(arguments))
(call_expression
(identifier)
(arguments)))

@ -17,7 +17,6 @@ const PREC = {
compound: 7, compound: 7,
call: 8, call: 8,
field: 8, field: 8,
end_marker: 9,
macro: 10, macro: 10,
binding: 10, binding: 10,
} }
@ -63,6 +62,13 @@ module.exports = grammar({
$.literal, $.literal,
], ],
// Doc: https://tree-sitter.github.io/tree-sitter/creating-parsers, search "precedences"
// These names can be used in the prec functions to define precedence relative only to other names in the array, rather than globally.
precedences: $ => [
['mod', 'soft_id'],
['end', 'soft_id'],
],
conflicts: $ => [ conflicts: $ => [
[$.tuple_type, $.parameter_types], [$.tuple_type, $.parameter_types],
[$.binding, $._simple_expression], [$.binding, $._simple_expression],
@ -402,7 +408,7 @@ module.exports = grammar({
), ),
), ),
_end_marker: $ => prec.left(PREC.end_marker, seq( _end_marker: $ => prec.left('end', seq(
'end', 'end',
choice( choice(
'if', 'if',
@ -524,7 +530,7 @@ module.exports = grammar({
optional(seq(':', field('return_type', $._type))), optional(seq(':', field('return_type', $._type))),
)), )),
opaque_modifier: $ => 'opaque', opaque_modifier: $ => prec('mod', 'opaque'),
/** /**
* Extension ::= 'extension' [DefTypeParamClause] {UsingParamClause} * Extension ::= 'extension' [DefTypeParamClause] {UsingParamClause}
@ -600,7 +606,7 @@ module.exports = grammar({
sep1('with', $._constructor_application), sep1('with', $._constructor_application),
)), )),
modifiers: $ => repeat1(choice( modifiers: $ => prec.left(repeat1(choice(
'abstract', 'abstract',
'final', 'final',
'sealed', 'sealed',
@ -612,7 +618,7 @@ module.exports = grammar({
$.infix_modifier, $.infix_modifier,
$.open_modifier, $.open_modifier,
$.transparent_modifier $.transparent_modifier
)), ))),
access_modifier: $ => prec.left(seq( access_modifier: $ => prec.left(seq(
choice('private', 'protected'), choice('private', 'protected'),
@ -625,10 +631,10 @@ module.exports = grammar({
']', ']',
), ),
inline_modifier: $ => 'inline', inline_modifier: $ => prec('mod', 'inline'),
infix_modifier: $ => 'infix', infix_modifier: $ => prec('mod', 'infix'),
open_modifier: $ => 'open', open_modifier: $ => prec('mod', 'open'),
transparent_modifier: $ => 'transparent', transparent_modifier: $ => prec('mod', 'transparent'),
/** /**
* InheritClauses ::= ['extends' ConstrApps] ['derives' QualId {',' QualId}] * InheritClauses ::= ['extends' ConstrApps] ['derives' QualId {',' QualId}]
@ -732,13 +738,13 @@ module.exports = grammar({
optional($._end_marker), optional($._end_marker),
)), )),
indented_cases: $ => prec.left(PREC.end_marker, seq( indented_cases: $ => prec.left(seq(
$._indent, $._indent,
repeat1($.case_clause), repeat1($.case_clause),
$._outdent, $._outdent,
)), )),
_indented_type_cases: $ => prec.left(PREC.end_marker, seq( _indented_type_cases: $ => prec.left(seq(
$._indent, $._indent,
repeat1($.type_case_clause), repeat1($.type_case_clause),
$._outdent, $._outdent,
@ -1311,7 +1317,18 @@ module.exports = grammar({
identifier: $ => prec.left(choice( identifier: $ => prec.left(choice(
$._alpha_identifier, $._alpha_identifier,
$._backquoted_id, $._backquoted_id,
)), $._soft_identifier,
)),
// https://docs.scala-lang.org/scala3/reference/soft-modifier.html
_soft_identifier: $ => prec('soft_id', choice(
'infix',
'inline',
'opaque',
'open',
'transparent',
'end',
)),
/** /**
* alphaid ::= upper idrest * alphaid ::= upper idrest

@ -3,7 +3,7 @@
# 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=95
SCALA_SCALA_COMPILER_EXPECTED=86 SCALA_SCALA_COMPILER_EXPECTED=87
DOTTY_COMPILER_EXPECTED=81 DOTTY_COMPILER_EXPECTED=81
SYNTAX_COMPLEXITY_CEILING=2500 SYNTAX_COMPLEXITY_CEILING=2500