Skip to content

add while/do loops and for-comprehension #30

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 1 commit into from
Aug 8, 2021
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
90 changes: 90 additions & 0 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,93 @@ def g(a: A): B = { f(); return null.asInstanceOf[B] }
)
)
)

===============================
While loops
===============================

def f = {
while(a) g()
while(b < c) {
d
}
}

---

(compilation_unit
(function_definition
(identifier)
(block
(while_expression
(parenthesized_expression (identifier))
(call_expression (identifier) (arguments)))
(while_expression
(parenthesized_expression (infix_expression (identifier) (operator_identifier) (identifier)))
(block (identifier))))))

===============================
Do-while loops
===============================

def f() = {
do g(a, b) while(c && d)
do {
g(a, b)
h(a, b)
} while (c < d)
}

---

(compilation_unit
(function_definition
(identifier)
(parameters)
(block
(do_while_expression
(call_expression (identifier) (arguments (identifier) (identifier)))
(parenthesized_expression (infix_expression (identifier) (operator_identifier) (identifier))))
(do_while_expression
(block
(call_expression (identifier) (arguments (identifier) (identifier)))
(call_expression (identifier) (arguments (identifier) (identifier))))
(parenthesized_expression (infix_expression (identifier) (operator_identifier) (identifier)))))))

===============================
For comprehensions
===============================

def f() = {
for (n <- nums) yield n + 1
for (x <- xs; y <- ys) g(x, y)
for {
x <- xs
a = b + c
y <- g() if d
} yield {
h(x, y)
}
}

---

(compilation_unit
(function_definition
(identifier)
(parameters)
(block
(for_expression
(enumerators (enumerator (identifier) (identifier)))
(infix_expression (identifier) (operator_identifier) (integer_literal)))
(for_expression
(enumerators
(enumerator (identifier) (identifier))
(enumerator (identifier) (identifier)))
(call_expression (identifier) (arguments (identifier) (identifier))))
(for_expression
(enumerators
(enumerator (identifier) (identifier))
(assignment_expression (identifier) (infix_expression (identifier) (operator_identifier) (identifier)))
(enumerator (identifier) (call_expression (identifier) (arguments)) (guard (identifier))))
(block (call_expression (identifier) (arguments (identifier) (identifier))))))))
103 changes: 72 additions & 31 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = grammar({
],

supertypes: $ => [
$._expression,
$.expression,
$._definition,
$._pattern,
],
Expand Down Expand Up @@ -218,7 +218,7 @@ module.exports = grammar({
field('pattern', $._pattern),
optional(seq(':', field('type', $._type))),
'=',
field('value', $._expression)
field('value', $.expression)
),

val_declaration: $ => seq(
Expand Down Expand Up @@ -246,7 +246,7 @@ module.exports = grammar({
field('pattern', $._pattern),
optional(seq(':', field('type', $._type))),
'=',
field('value', $._expression)
field('value', $.expression)
),

type_definition: $ => seq(
Expand All @@ -268,7 +268,7 @@ module.exports = grammar({
field('parameters', repeat($.parameters)),
optional(seq(':', field('return_type', $._type))),
choice(
seq('=', field('body', $._expression)),
seq('=', field('body', $.expression)),
field('body', $.block)
)
),
Expand Down Expand Up @@ -321,19 +321,19 @@ module.exports = grammar({
optional(choice('val', 'var')),
field('name', $.identifier),
optional(seq(':', field('type', $._type))),
optional(seq('=', field('default_value', $._expression)))
optional(seq('=', field('default_value', $.expression)))
),

parameter: $ => seq(
repeat($.annotation),
field('name', $.identifier),
optional(seq(':', field('type', $._param_type))),
optional(seq('=', field('default_value', $._expression)))
optional(seq('=', field('default_value', $.expression)))
),

_block: $ => prec.left(seq(
sep1($._semicolon, choice(
$._expression,
$.expression,
$._definition
)),
optional($._semicolon),
Expand Down Expand Up @@ -500,7 +500,7 @@ module.exports = grammar({
// ---------------------------------------------------------------
// Expressions

_expression: $ => choice(
expression: $ => choice(
$.if_expression,
$.match_expression,
$.try_expression,
Expand All @@ -521,35 +521,38 @@ module.exports = grammar({
$.literal,
$.unit,
$.return_expression,
$.throw_expression
$.throw_expression,
$.while_expression,
$.do_while_expression,
$.for_expression,
),

if_expression: $ => prec.right(seq(
'if',
field('condition', $.parenthesized_expression),
field('consequence', $._expression),
field('consequence', $.expression),
optional(seq(
'else',
field('alternative', $._expression)
field('alternative', $.expression)
))
)),

match_expression: $ => seq(
field('value', $._expression),
field('value', $.expression),
'match',
field('body', $.case_block)
),

try_expression: $ => prec.right(seq(
'try',
field('body', $._expression),
field('body', $.expression),
optional($.catch_clause),
optional($.finally_clause)
)),

catch_clause: $ => prec.right(seq('catch', $.case_block)),

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

case_block: $ => choice(
prec(-1, seq('{', '}')),
Expand All @@ -566,58 +569,58 @@ module.exports = grammar({

guard: $ => seq(
'if',
field('condition', $._expression)
field('condition', $.expression)
),

assignment_expression: $ => prec.right(PREC.assign, seq(
field('left', $._expression),
field('left', $.expression),
'=',
field('right', $._expression)
field('right', $.expression)
)),

generic_function: $ => prec(PREC.call, seq(
field('function', $._expression),
field('function', $.expression),
field('type_arguments', $.type_arguments)
)),

call_expression: $ => prec(PREC.call, seq(
field('function', $._expression),
field('function', $.expression),
field('arguments', $.arguments),
field('body', optional(choice($.block, $.case_block)))
)),

field_expression: $ => prec(PREC.field, seq(
field('value', $._expression),
field('value', $.expression),
'.',
field('field', $.identifier)
)),

instance_expression: $ => prec(PREC.new, seq(
'new',
$._expression
$.expression
)),

infix_expression: $ => prec.left(PREC.infix, seq(
field('left', $._expression),
field('left', $.expression),
field('operator', choice($.identifier, $.operator_identifier)),
field('right', $._expression)
field('right', $.expression)
)),

prefix_expression: $ => prec(PREC.prefix, seq(
choice('+', '-', '!', '~'),
$._expression
$.expression
)),

tuple_expression: $ => seq(
'(',
$._expression,
repeat1(seq(',', $._expression)),
$.expression,
repeat1(seq(',', $.expression)),
')'
),

parenthesized_expression: $ => seq(
'(',
$._expression,
$.expression,
')'
),

Expand All @@ -629,7 +632,7 @@ module.exports = grammar({

arguments: $ => seq(
'(',
commaSep($._expression),
commaSep($.expression),
')'
),

Expand Down Expand Up @@ -759,9 +762,47 @@ module.exports = grammar({

unit: $ => seq('(', ')'),

return_expression: $ => prec.left(seq('return', optional($._expression))),
return_expression: $ => prec.left(seq('return', optional($.expression))),

throw_expression: $ => prec.left(seq('throw', $._expression)),
throw_expression: $ => prec.left(seq('throw', $.expression)),

while_expression: $ => prec.right(seq(
'while',
field('condition', $.parenthesized_expression),
field('body', $.expression)
)),

do_while_expression: $ => prec.right(seq(
'do',
field('body', $.expression),
'while',
field('condition', $.parenthesized_expression)
)),

for_expression: $ => prec.right(seq(
'for',
field('enumerators', choice(
seq("(", $.enumerators, ")"),
seq("{", $.enumerators, "}")
)),
optional('yield'),
field('body', $.expression)
)),

enumerators: $ => seq(
sep1($._semicolon, choice(
$.enumerator,
$.assignment_expression
)),
optional($._automatic_semicolon)
),

enumerator: $ => seq(
$.identifier,
'<-',
$.expression,
optional($.guard)
),

comment: $ => token(choice(
seq('//', /.*/),
Expand All @@ -779,7 +820,7 @@ function commaSep(rule) {
}

function commaSep1(rule) {
return seq(rule, repeat(seq(',', rule)))
return sep1(',', rule)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO this is the best change in the PR

}

function sep(delimiter, rule) {
Expand Down
Loading