Skip to content

Commit

Permalink
wip: added _rhs_expression allowing expr function_definition with opt…
Browse files Browse the repository at this point in the history
… name

modified rules:
- {augmented,_variable{_inferred_type,_typed,}}_assignment
- parenthesized_expression
- assignment
- pair
- array
- {typed_default,default}_parameter
- function_definition
- argument_list

Anonymous functions are parsing correctly, but the current body rule does
not allow the following:

    call(func hello(): pass) # ERROR MISSING newline

The body rule should allow being a single statement with no following
newline.
  • Loading branch information
PrestonKnopp committed Feb 7, 2023
1 parent 5d43d78 commit 86e8d44
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 12 deletions.
70 changes: 70 additions & 0 deletions corpus/source.txt
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,76 @@ func test():
(argument_list (string)))))))

=====================================
Functions Anonymous
=====================================

var x = func hello():
pass

var x = func():
pass

var x = func hello(): pass

func hello(p = func(): pass):
pass

hello(func(): pass)
hello(func():
pass
)

---

(source
(variable_statement
(name)
(function_definition
(name)
(parameters)
(body
(pass_statement))))
(variable_statement
(name)
(function_definition
(parameters)
(body
(pass_statement))))
(variable_statement
(name)
(function_definition
(name)
(parameters)
(body
(pass_statement))))
(function_definition
(name)
(parameters
(default_parameter
(identifier)
(function_definition
(parameters)
(body
(pass_statement)))))
(body
(pass_statement)))
(expression_statement
(call
(identifier)
(argument_list
(function_definition
(parameters)
(body
(pass_statement))))))
(expression_statement
(call
(identifier)
(argument_list
(function_definition
(parameters)
(body
(pass_statement)))))))
=====================================
Constructor Definitions
=====================================

Expand Down
32 changes: 20 additions & 12 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ module.exports = grammar({

inferred_type: ($) => choice(":=", seq(":", "=")),

_variable_assignment: ($) => seq("=", $._expression),
_variable_assignment: ($) => seq("=", $._rhs_expression),
_variable_inferred_type_assignment: ($) =>
seq($.inferred_type, $._expression),
_variable_typed_assignment: ($) => seq(":", $.type, "=", $._expression),
seq($.inferred_type, $._rhs_expression),
_variable_typed_assignment: ($) => seq(":", $.type, "=", $._rhs_expression),

_variable_typed_definition: ($) =>
choice(seq(":", $.type), $._variable_typed_assignment),
Expand Down Expand Up @@ -436,6 +436,14 @@ module.exports = grammar({
$.parenthesized_expression
),

_rhs_expression: ($) =>
choice(
$.comparison_operator,
$.conditional_expression,
$._primary_expression,
$.function_definition
),

// This makes an attribute's ast linear
// When attribute is used inside $.attribute it becomes recursive spaghetti
_attribute_expression: ($) =>
Expand Down Expand Up @@ -571,19 +579,19 @@ module.exports = grammar({
),

parenthesized_expression: ($) =>
prec(PREC.parenthesized_expression, seq("(", $._expression, ")")),
prec(PREC.parenthesized_expression, seq("(", $._rhs_expression, ")")),

// -----------------------------------------------------------------------------
// - Assignment -
// -----------------------------------------------------------------------------

assignment: ($) => seq($._expression, "=", $._expression),
assignment: ($) => seq($._expression, "=", $._rhs_expression),

augmented_assignment: ($) =>
seq(
$._expression,
choice("+=", "-=", "*=", "/=", "%=", ">>=", "<<=", "&=", "^=", "|="),
$._expression
$._rhs_expression
),

// -----------------------------------------------------------------------------
Expand All @@ -593,12 +601,12 @@ module.exports = grammar({
pair: ($) =>
seq(
choice(seq($._expression, ":"), seq($.identifier, "=")),
$._expression
$._rhs_expression
),

dictionary: ($) => seq("{", optional(trailCommaSep1($.pair)), "}"),

array: ($) => seq("[", optional(trailCommaSep1($._expression)), "]"),
array: ($) => seq("[", optional(trailCommaSep1($._rhs_expression)), "]"),

// -----------------------------------------------------------------------------
// - Function Definition -
Expand All @@ -607,12 +615,12 @@ module.exports = grammar({
typed_parameter: ($) =>
prec(PREC.typed_parameter, seq($.identifier, ":", $.type)),

default_parameter: ($) => seq($.identifier, "=", $._expression),
default_parameter: ($) => seq($.identifier, "=", $._rhs_expression),

typed_default_parameter: ($) =>
prec(
PREC.typed_parameter,
seq($.identifier, ":", $.type, "=", $._expression)
seq($.identifier, ":", $.type, "=", $._rhs_expression)
),

_parameters: ($) =>
Expand All @@ -634,7 +642,7 @@ module.exports = grammar({
optional(choice($.static_keyword, $.remote_keyword)),
optional($.annotations),
"func",
$.name,
optional($.name),
$.parameters,
optional($.return_type),
":",
Expand All @@ -656,7 +664,7 @@ module.exports = grammar({
// - Function Call -
// -----------------------------------------------------------------------------

argument_list: ($) => seq("(", optional(commaSep1($._expression)), ")"),
argument_list: ($) => seq("(", optional(commaSep1($._rhs_expression)), ")"),

base_call: ($) => prec(PREC.call, seq(".", $.identifier, $.argument_list)),

Expand Down

0 comments on commit 86e8d44

Please sign in to comment.