Fixes extends clause

Problem
-------
1. In Scala 3, extends clause can be comma separated.
2. In Scala 2 or 3 extends clause can ctor with parameters:
   `class D with E(x) with F(y)`.

Solution
--------
`extends` is now followed by *constructor applications* separated by
either `with` or `,` as opposed to treating them as a compound type.
pull/481/head
Eugene Yokota 2023-01-13 02:36:04 +07:00
parent 63adddac00
commit b5fd12eaab
4 changed files with 41 additions and 9 deletions

@ -333,6 +333,8 @@ object C {
with C[T] with C[T]
} }
class D(c: String) extends E(c) with F
--- ---
(compilation_unit (compilation_unit
@ -358,9 +360,31 @@ object C {
(identifier) (identifier)
(template_body (template_body
(class_definition (identifier) (extends_clause (class_definition (identifier) (extends_clause
(compound_type (generic_type (type_identifier) (type_arguments (type_identifier)))
(generic_type (type_identifier) (type_arguments (type_identifier))) (generic_type (type_identifier) (type_arguments (type_identifier)))))))
(generic_type (type_identifier) (type_arguments (type_identifier))))))))) (class_definition
(identifier)
(class_parameters
(class_parameter (identifier) (type_identifier)))
(extends_clause
(type_identifier) (arguments (identifier))
(type_identifier))))
=======================================
Subclass definitions (Scala 3 syntax)
=======================================
class A extends B, C:
1
end A
---
(compilation_unit
(class_definition
(identifier)
(extends_clause (type_identifier) (type_identifier))
(template_body (integer_literal))))
======================================= =======================================
Class definitions with parameters Class definitions with parameters
@ -449,7 +473,7 @@ trait T[U] extends V.W[U] {
(extends_clause (type_identifier))) (extends_clause (type_identifier)))
(trait_definition (trait_definition
(identifier) (identifier)
(extends_clause (compound_type (type_identifier) (type_identifier)))) (extends_clause (type_identifier) (type_identifier)))
(trait_definition (trait_definition
(identifier) (identifier)
(type_parameters (identifier)) (type_parameters (identifier))

@ -114,7 +114,7 @@ class F extends Cloneable with Resetable with Serializable {}
(type_identifier) (block)) (type_identifier) (block))
(class_definition (identifier) (class_definition (identifier)
(extends_clause (extends_clause
(compound_type (type_identifier) (type_identifier) (type_identifier))) (type_identifier) (type_identifier) (type_identifier))
(template_body))) (template_body)))
================================== ==================================

@ -515,11 +515,11 @@ module.exports = grammar({
/** /**
* StructuralInstance ::= ConstrApp {'with' ConstrApp} ['with' WithTemplateBody] * StructuralInstance ::= ConstrApp {'with' ConstrApp} ['with' WithTemplateBody]
*/ */
_structural_instance: $ => seq( _structural_instance: $ => prec.left(PREC.compound, seq(
$._constructor_application, $._constructor_application,
'with', 'with',
field('body', $.with_template_body), field('body', $.with_template_body),
), )),
/** /**
* ConstrApp ::= SimpleType1 {Annotation} {ParArgumentExprs} * ConstrApp ::= SimpleType1 {Annotation} {ParArgumentExprs}
@ -545,6 +545,11 @@ module.exports = grammar({
), ),
)), )),
_constructor_applications: $ => prec.left(choice(
commaSep1($._constructor_application),
sep1('with', $._constructor_application),
)),
modifiers: $ => repeat1(choice( modifiers: $ => repeat1(choice(
'abstract', 'abstract',
'final', 'final',
@ -575,9 +580,12 @@ module.exports = grammar({
open_modifier: $ => 'open', open_modifier: $ => 'open',
transparent_modifier: $ => 'transparent', transparent_modifier: $ => 'transparent',
/**
* InheritClauses ::= ['extends' ConstrApps] ['derives' QualId {',' QualId}]
*/
extends_clause: $ => prec.left(seq( extends_clause: $ => prec.left(seq(
'extends', 'extends',
field('type', $._type), field('type', $._constructor_applications),
optional($.arguments) optional($.arguments)
)), )),

@ -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=100 SCALA_SCALA_LIBRARY_EXPECTED=100
SCALA_SCALA_COMPILER_EXPECTED=66 SCALA_SCALA_COMPILER_EXPECTED=67
DOTTY_COMPILER_EXPECTED=66 DOTTY_COMPILER_EXPECTED=66
if [ ! -d "$SCALA_SCALA_DIR" ]; then if [ ! -d "$SCALA_SCALA_DIR" ]; then