Skip to content

Fixes instance_expression #131

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

Merged
merged 2 commits into from
Jan 12, 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
17 changes: 2 additions & 15 deletions corpus/definitions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1015,28 +1015,15 @@ Inline given (Scala 3)
=======================================

inline given Test =
new Test:
def apply(i: Int) = 2
new Test

---

(compilation_unit
(given_definition
(modifiers (inline_modifier))
(type_identifier)
(assignment_expression
(instance_expression
(ascription_expression
(identifier)
(infix_type
(type_identifier)
(identifier)
(tuple_type
(infix_type
(type_identifier)
(operator_identifier)
(type_identifier))))))
(integer_literal))))
(instance_expression (type_identifier))))


=======================================
Expand Down
42 changes: 36 additions & 6 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def main() {
(case_class_pattern
(type_identifier)
(identifier))
(throw_expression (instance_expression (call_expression (identifier) (arguments (identifier))))
(throw_expression (instance_expression (type_identifier) (arguments (identifier)))
)
)
)
Expand Down Expand Up @@ -314,7 +314,7 @@ def main(): Unit =
(case_class_pattern
(type_identifier)
(identifier))
(throw_expression (instance_expression (call_expression (identifier) (arguments (identifier))))
(throw_expression (instance_expression (type_identifier) (arguments (identifier)))
)
)
)
Expand Down Expand Up @@ -428,8 +428,17 @@ Instance expressions

class C {
def main() {
a = new B
c = new D(e, f)
val a = new B
val c = new D(e, f)
val e = new E:
Copy link
Collaborator

Choose a reason for hiding this comment

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

For posterity, can you also add an example of the quiet syntax new:

scala> trait Hello:
     |   def x: Int
     |
// defined trait Hello

scala> val x: Hello = new:
     |   def x = 25
     |
val x: Hello = anon$1@6bd92538

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done - 14a22e7

def app = 1
val f = new:
def app = 1
val g = new G {}
val i = new {
"ok"
}
new Array: Array
}
}

Expand All @@ -443,8 +452,29 @@ class C {
(identifier)
(parameters)
(block
(assignment_expression (identifier) (instance_expression (identifier)))
(assignment_expression (identifier) (instance_expression (call_expression (identifier) (arguments (identifier) (identifier))))))))))
(val_definition
(identifier)
(instance_expression (type_identifier)))
(val_definition
(identifier)
(instance_expression (type_identifier) (arguments (identifier) (identifier))))
(val_definition
(identifier)
(instance_expression (type_identifier) (template_body
(function_definition (identifier) (integer_literal)))))
(val_definition
(identifier)
(instance_expression (template_body
(function_definition (identifier) (integer_literal)))))
(val_definition
(identifier)
(instance_expression (type_identifier) (template_body)))
(val_definition
(identifier)
(instance_expression (template_body (string))))
(ascription_expression
(instance_expression (type_identifier))
(type_identifier)))))))

===============================
Infix expressions
Expand Down
67 changes: 54 additions & 13 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ const PREC = {
ascription: 4,
postfix: 5,
infix: 6,
new: 7,
constructor_app: 7,
prefix: 7,
compound: 7,
call: 8,
field: 8,
end_marker: 9,
macro: 10,
binding: 10,
}

module.exports = grammar({
Expand Down Expand Up @@ -63,13 +64,15 @@ module.exports = grammar({
conflicts: $ => [
[$.tuple_type, $.parameter_types],
[$.binding, $._simple_expression],
[$.binding, $.ascription_expression],
[$.if_expression, $.expression],
[$.while_expression, $._simple_expression],
[$.for_expression, $.infix_expression],
[$._indentable_expression, $.do_while_expression],
[$.if_expression],
[$.match_expression],
[$._type_identifier, $.identifier],
[$.instance_expression],
],

word: $ => $._alpha_identifier,
Expand Down Expand Up @@ -323,12 +326,12 @@ module.exports = grammar({
$._block,
$._outdent,
)),
seq(
prec.left(PREC.control, seq(
'{',
optional($.self_type),
optional($._block),
'}',
),
)),
),

/*
Expand Down Expand Up @@ -511,14 +514,35 @@ module.exports = grammar({
* StructuralInstance ::= ConstrApp {'with' ConstrApp} ['with' WithTemplateBody]
*/
_structural_instance: $ => seq(
choice(
$._annotated_type,
$.compound_type,
),
$._constructor_application,
'with',
field('body', $.with_template_body),
),

/**
* ConstrApp ::= SimpleType1 {Annotation} {ParArgumentExprs}
*
* Note: It would look more elegant if we could make seq(choice(), optional(arguments)),
* but that doesn't seem to work.
*/
_constructor_application: $ => prec.left(PREC.constructor_app, choice(
$._annotated_type,
$.compound_type,
// This adds _simple_type, but not the above intentionall/y.
seq(
$._simple_type,
field('arguments', $.arguments),
),
seq(
$._annotated_type,
field('arguments', $.arguments),
),
seq(
$.compound_type,
field('arguments', $.arguments),
),
)),

modifiers: $ => repeat1(choice(
'abstract',
'final',
Expand Down Expand Up @@ -887,7 +911,7 @@ module.exports = grammar({

finally_clause: $ => prec.right(seq('finally', $._indentable_expression)),

binding: $ => prec.dynamic(PREC.binding_decl, seq(
binding: $ => prec.dynamic(PREC.binding, seq(
field('name', $._identifier),
optional(seq(':', field('type', $._param_type))),
)),
Expand Down Expand Up @@ -941,15 +965,32 @@ module.exports = grammar({
field('field', $._identifier)
)),

instance_expression: $ => prec(PREC.new, seq(
'new',
$.expression
)),
/**
* SimpleExpr ::= SimpleRef
* | 'new' ConstrApp {'with' ConstrApp} [TemplateBody]
* | 'new' TemplateBody
*/
instance_expression: $ => choice(
// This is weakened so ascription wins for new Array: Array
prec.dynamic(0, seq(
'new',
$._constructor_application,
$.template_body,
)),
seq(
'new',
$.template_body,
),
seq(
'new',
$._constructor_application,
),
),

/**
* PostfixExpr [Ascription]
*/
ascription_expression: $ => prec.right(PREC.ascription, seq(
ascription_expression: $ => prec.dynamic(PREC.ascription, seq(
$._postfix_expression_choice,
':',
choice(
Expand Down
2 changes: 1 addition & 1 deletion script/smoke_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

SCALA_SCALA_LIBRARY_EXPECTED=100
SCALA_SCALA_COMPILER_EXPECTED=66
DOTTY_COMPILER_EXPECTED=73
DOTTY_COMPILER_EXPECTED=74

if [ ! -d "$SCALA_SCALA_DIR" ]; then
echo "\$SCALA_SCALA_DIR must be set"
Expand Down
4 changes: 4 additions & 0 deletions test/highlight/basics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ object Hello {
// ^string
// ^conditional

val y = new lower_case_intentionally
// ^keyword.operator
// ^type

trait Test {
// ^ keyword
// ^ type.definition
Expand Down