Skip to content

Commit 08dbe89

Browse files
parse nullary range operator (#25)
Co-authored-by: Jonatan Kłosko <[email protected]>
1 parent de20391 commit 08dbe89

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

grammar.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const PREC = {
1616
XOR_OP: 140,
1717
TERNARY_OP: 150,
1818
CONCAT_OPS: 160,
19+
RANGE_OP: 160,
1920
ADD_OPS: 170,
2021
MULT_OPS: 180,
2122
POWER_OP: 190,
@@ -33,13 +34,13 @@ const COMP_OPS = ["==", "!=", "=~", "===", "!=="];
3334
const REL_OPS = ["<", ">", "<=", ">="];
3435
const ARROW_OPS = ["|>", "<<<", ">>>", "<<~", "~>>", "<~", "~>", "<~>", "<|>"];
3536
const IN_OPS = ["in", "not in"];
36-
const CONCAT_OPS = ["++", "--", "+++", "---", "..", "<>"];
37+
const CONCAT_OPS = ["++", "--", "+++", "---", "<>"];
3738
const ADD_OPS = ["+", "-"];
3839
const MULT_OPS = ["*", "/"];
3940
const UNARY_OPS = ["+", "-", "!", "^", "~~~", "not"];
4041

4142
const ALL_OPS = [
42-
["->", "when", "::", "|", "=>", "&", "=", "^^^", "//", "**", ".", "@"],
43+
["->", "when", "::", "|", "=>", "&", "=", "^^^", "//", "..", "**", ".", "@"],
4344
IN_MATCH_OPS,
4445
OR_OPS,
4546
AND_OPS,
@@ -213,6 +214,7 @@ module.exports = grammar({
213214
$.tuple,
214215
$.bitstring,
215216
$.map,
217+
$._nullary_operator,
216218
$.unary_operator,
217219
$.binary_operator,
218220
$.dot,
@@ -426,6 +428,11 @@ module.exports = grammar({
426428
)
427429
),
428430

431+
_nullary_operator: ($) =>
432+
// Nullary operators don't have any child nodes, so we reuse the
433+
// operator_identifier node
434+
alias(prec(PREC.RANGE_OP, ".."), $.operator_identifier),
435+
429436
unary_operator: ($) =>
430437
choice(
431438
unaryOp($, prec, PREC.CAPTURE_OP, "&", $._capture_expression),
@@ -480,6 +487,7 @@ module.exports = grammar({
480487
binaryOp($, prec.left, PREC.XOR_OP, "^^^"),
481488
binaryOp($, prec.right, PREC.TERNARY_OP, "//"),
482489
binaryOp($, prec.right, PREC.CONCAT_OPS, choice(...CONCAT_OPS)),
490+
binaryOp($, prec.right, PREC.RANGE_OP, ".."),
483491
binaryOp($, prec.left, PREC.ADD_OPS, choice(...ADD_OPS)),
484492
binaryOp($, prec.left, PREC.MULT_OPS, choice(...MULT_OPS)),
485493
binaryOp($, prec.left, PREC.POWER_OP, "**"),
@@ -524,6 +532,10 @@ module.exports = grammar({
524532
alias($._not_in, "not in"),
525533
"^^",
526534
...CONCAT_OPS,
535+
// The range operator has both a binary and a nullary version.
536+
// The nullary version is already parsed as operator_identifier,
537+
// so it covers this case
538+
// ".."
527539
...MULT_OPS,
528540
"**",
529541
"->",

test/corpus/expression/operator.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,31 @@ x +y +z
656656
(unary_operator
657657
(identifier))))))))
658658

659+
=====================================
660+
nullary range
661+
=====================================
662+
663+
..
664+
Enum.to_list(..)
665+
not ..
666+
range = ..
667+
668+
---
669+
670+
(source
671+
(operator_identifier)
672+
(call
673+
(dot
674+
(alias)
675+
(identifier))
676+
(arguments
677+
(operator_identifier)))
678+
(unary_operator
679+
(operator_identifier))
680+
(binary_operator
681+
(identifier)
682+
(operator_identifier)))
683+
659684
=====================================
660685
stepped range
661686
=====================================

test/highlight/operators.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,8 @@ y = true and false
8282
# ^ module
8383
# ^ operator
8484
# ^ function
85+
86+
range = ..
87+
# <- variable
88+
# ^ operator
89+
# ^ operator

0 commit comments

Comments
 (0)