Skip to content

Commit

Permalink
Merge pull request #7 from starkware-libs/lucas/range
Browse files Browse the repository at this point in the history
feat(grammar): add range
  • Loading branch information
0xLucqs authored Sep 19, 2024
2 parents d5733e0 + f2c08a6 commit 9ee027f
Show file tree
Hide file tree
Showing 4 changed files with 42,178 additions and 40,431 deletions.
38 changes: 36 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const PREC = {
comparative: 4,
and: 3,
or: 2,
range: 1,
assign: 0,
closure: -1,
};
Expand Down Expand Up @@ -95,6 +96,7 @@ module.exports = grammar({
],
supertypes: ($) => [
$.expression,
$.expression_except_range,
$._type,
$._literal,
$._literal_pattern,
Expand Down Expand Up @@ -587,11 +589,29 @@ module.exports = grammar({
$.slice_pattern,
$.macro_invocation,
$.tuple_enum_pattern,
$.range_pattern,
'_',
),
// for example in let (a, b) = c; it would be `(a, b)`
tuple_pattern: ($) => seq('(', sepBy(',', choice($._pattern, $.closure_expression)), optional(','), ')'),

range_pattern: $ => seq(
choice(
$._literal_pattern,
$._path,
),
choice(
seq(
'..',
choice(
$._literal_pattern,
$._path,
),
),
'..',
),
),

// for example in let [a, b] = c; it would be `[a, b]`
slice_pattern: ($) => seq('[', sepBy(',', $._pattern), optional(','), ']'),

Expand Down Expand Up @@ -780,7 +800,13 @@ module.exports = grammar({
expression_statement: ($) =>
choice(seq($.expression, ';'), prec(1, $._expression_ending_with_block)),

expression: ($) =>

expression: $ => choice(
$.expression_except_range,
$.range_expression,
),

expression_except_range: ($) =>
choice(
prec.left($.identifier),
alias(choice(...primitiveTypes), $.identifier),
Expand Down Expand Up @@ -918,6 +944,14 @@ module.exports = grammar({
unary_expression: ($) =>
prec(PREC.unary, seq(choice('-', '*', '!', '~', '@'), $.expression)),

range_expression: $ => prec.left(PREC.range, choice(
seq($.expression, '..', $.expression),
seq($.expression, '..'),
seq('..', $.expression),
'..',
)),


// for example in let a = try_smth?; it would be `try_smth?`
try_expression: ($) => prec(PREC.try, seq($.expression, '?')),

Expand Down Expand Up @@ -1106,7 +1140,7 @@ module.exports = grammar({
call_expression: ($) =>
prec(
PREC.call,
seq(field('function', $.expression), field('arguments', $.arguments)),
seq(field('function', $.expression_except_range), field('arguments', $.arguments)),
),
// for example in foo(a, b, c) it would be `(a, b, c)`
arguments: ($) =>
Expand Down
84 changes: 71 additions & 13 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 86 additions & 1 deletion src/node-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9ee027f

Please sign in to comment.