Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor $.compilation_unit, optimize grammar #269

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,19 @@ module.exports = grammar({
[$.self_type, $._simple_expression],
// 'package' package_identifier '{' operator_identifier '=>' • 'enum' …
[$.self_type, $.lambda_expression],
// 'class' _class_constructor • _automatic_semicolon …
[$._class_definition],
// 'class' operator_identifier • _automatic_semicolon …
[$._class_constructor],
// 'enum' _class_constructor '{' 'case' operator_identifier _full_enum_def_repeat1 • _automatic_semicolon …
[$._full_enum_def],
],

word: $ => $._alpha_identifier,

rules: {
compilation_unit: $ => repeat($._top_level_definition),
// TopStats ::= TopStat {semi TopStat}
compilation_unit: $ => optional(trailingSep1($._semicolon, $._top_level_definition)),

_top_level_definition: $ => choice(
$.package_clause,
Expand Down Expand Up @@ -161,7 +168,7 @@ module.exports = grammar({
)
),

simple_enum_case: $ => seq(field('name', $._identifier), field('extend', optional($.extends_clause))),
simple_enum_case: $ => prec.left(seq(field('name', $._identifier), field('extend', optional($.extends_clause)))),

full_enum_case: $ => seq(field('name', $._identifier), $._full_enum_def),

Expand All @@ -171,16 +178,15 @@ module.exports = grammar({
field('extend', optional($.extends_clause))
),

package_clause: $ => seq(
package_clause: $ => prec.right(seq(
'package',
field('name', $.package_identifier),
optional($._semicolon),
// This is slightly more permissive than the EBNF in that it allows any
// kind of delcaration inside of the package blocks. As we're more
// concerned with the structure rather than the validity of the program
// we'll allow it.
field('body', optional($.template_body))
),
)),

package_identifier: $ => prec.right(sep1(
'.', $._identifier
Expand Down Expand Up @@ -263,43 +269,48 @@ module.exports = grammar({
field('name', $._identifier),
field('extend', optional($.extends_clause)),
field('derive', optional($.derives_clause)),
field('body', optional($.template_body)),
field('body', optional($._definition_body)),
)),

class_definition: $ => prec.left(seq(
class_definition: $ => seq(
repeat($.annotation),
optional($.modifiers),
optional('case'),
'class',
$._class_definition,
)),
),

_class_definition: $ => seq(
$._class_constructor,
field('extend', optional($.extends_clause)),
field('derive', optional($.derives_clause)),
field('body', optional($.template_body))
optional($._definition_body),
),

_definition_body: $ => seq(
optional($._automatic_semicolon),
field('body', $.template_body)
),

/**
* ClassConstr ::= [ClsTypeParamClause] [ConstrMods] ClsParamClauses
* ConstrMods ::= {Annotation} [AccessModifier]
*/
_class_constructor: $ => prec.right(seq(
_class_constructor: $ => seq(
field('name', $._identifier),
field('type_parameters', optional($.type_parameters)),
optional($.annotation),
optional($.access_modifier),
field('class_parameters', repeat($.class_parameters)),
)),
),

trait_definition: $ => prec.left(seq(
repeat($.annotation),
optional($.modifiers),
'trait',
$._class_constructor,
field('extend', optional($.extends_clause)),
field('body', optional($.template_body))
field('body', optional($._definition_body))
)),

// The EBNF makes a distinction between function type parameters and other
Expand Down Expand Up @@ -633,6 +644,7 @@ module.exports = grammar({
)),

class_parameters: $ => prec(1, seq(
optional($._automatic_semicolon),
'(',
optional(choice('implicit', 'using')),
trailingCommaSep($.class_parameter),
Expand Down
4 changes: 2 additions & 2 deletions script/smoke_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# This is an integration test to generally check the quality of parsing.

SCALA_SCALA_LIBRARY_EXPECTED=95
SCALA_SCALA_COMPILER_EXPECTED=87
SCALA_SCALA_COMPILER_EXPECTED=86
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a degradation in a single file:

StdAttachments.scala#L214 5 ms (ERROR [213, 13] - [214, 5]), which is minimized to

package x

trait A {
  self: A =>
  import a._
}

trait B {
  self: B =>

  class A() 
  val x = 1
}

It seams feasible to me to investigate the issue individually after this PR gets merged

DOTTY_COMPILER_EXPECTED=81
SYNTAX_COMPLEXITY_CEILING=2800
SYNTAX_COMPLEXITY_CEILING=2500

if [ ! -d "$SCALA_SCALA_DIR" ]; then
echo "\$SCALA_SCALA_DIR must be set"
Expand Down