parse nullary range operator (#25)

Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
This commit is contained in:
Michael Davis 2022-02-13 13:19:53 -06:00 committed by GitHub
parent de20391afe
commit 08dbe8978c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 2 deletions

View File

@ -16,6 +16,7 @@ const PREC = {
XOR_OP: 140, XOR_OP: 140,
TERNARY_OP: 150, TERNARY_OP: 150,
CONCAT_OPS: 160, CONCAT_OPS: 160,
RANGE_OP: 160,
ADD_OPS: 170, ADD_OPS: 170,
MULT_OPS: 180, MULT_OPS: 180,
POWER_OP: 190, POWER_OP: 190,
@ -33,13 +34,13 @@ const COMP_OPS = ["==", "!=", "=~", "===", "!=="];
const REL_OPS = ["<", ">", "<=", ">="]; const REL_OPS = ["<", ">", "<=", ">="];
const ARROW_OPS = ["|>", "<<<", ">>>", "<<~", "~>>", "<~", "~>", "<~>", "<|>"]; const ARROW_OPS = ["|>", "<<<", ">>>", "<<~", "~>>", "<~", "~>", "<~>", "<|>"];
const IN_OPS = ["in", "not in"]; const IN_OPS = ["in", "not in"];
const CONCAT_OPS = ["++", "--", "+++", "---", "..", "<>"]; const CONCAT_OPS = ["++", "--", "+++", "---", "<>"];
const ADD_OPS = ["+", "-"]; const ADD_OPS = ["+", "-"];
const MULT_OPS = ["*", "/"]; const MULT_OPS = ["*", "/"];
const UNARY_OPS = ["+", "-", "!", "^", "~~~", "not"]; const UNARY_OPS = ["+", "-", "!", "^", "~~~", "not"];
const ALL_OPS = [ const ALL_OPS = [
["->", "when", "::", "|", "=>", "&", "=", "^^^", "//", "**", ".", "@"], ["->", "when", "::", "|", "=>", "&", "=", "^^^", "//", "..", "**", ".", "@"],
IN_MATCH_OPS, IN_MATCH_OPS,
OR_OPS, OR_OPS,
AND_OPS, AND_OPS,
@ -213,6 +214,7 @@ module.exports = grammar({
$.tuple, $.tuple,
$.bitstring, $.bitstring,
$.map, $.map,
$._nullary_operator,
$.unary_operator, $.unary_operator,
$.binary_operator, $.binary_operator,
$.dot, $.dot,
@ -426,6 +428,11 @@ module.exports = grammar({
) )
), ),
_nullary_operator: ($) =>
// Nullary operators don't have any child nodes, so we reuse the
// operator_identifier node
alias(prec(PREC.RANGE_OP, ".."), $.operator_identifier),
unary_operator: ($) => unary_operator: ($) =>
choice( choice(
unaryOp($, prec, PREC.CAPTURE_OP, "&", $._capture_expression), unaryOp($, prec, PREC.CAPTURE_OP, "&", $._capture_expression),
@ -480,6 +487,7 @@ module.exports = grammar({
binaryOp($, prec.left, PREC.XOR_OP, "^^^"), binaryOp($, prec.left, PREC.XOR_OP, "^^^"),
binaryOp($, prec.right, PREC.TERNARY_OP, "//"), binaryOp($, prec.right, PREC.TERNARY_OP, "//"),
binaryOp($, prec.right, PREC.CONCAT_OPS, choice(...CONCAT_OPS)), binaryOp($, prec.right, PREC.CONCAT_OPS, choice(...CONCAT_OPS)),
binaryOp($, prec.right, PREC.RANGE_OP, ".."),
binaryOp($, prec.left, PREC.ADD_OPS, choice(...ADD_OPS)), binaryOp($, prec.left, PREC.ADD_OPS, choice(...ADD_OPS)),
binaryOp($, prec.left, PREC.MULT_OPS, choice(...MULT_OPS)), binaryOp($, prec.left, PREC.MULT_OPS, choice(...MULT_OPS)),
binaryOp($, prec.left, PREC.POWER_OP, "**"), binaryOp($, prec.left, PREC.POWER_OP, "**"),
@ -524,6 +532,10 @@ module.exports = grammar({
alias($._not_in, "not in"), alias($._not_in, "not in"),
"^^", "^^",
...CONCAT_OPS, ...CONCAT_OPS,
// The range operator has both a binary and a nullary version.
// The nullary version is already parsed as operator_identifier,
// so it covers this case
// ".."
...MULT_OPS, ...MULT_OPS,
"**", "**",
"->", "->",

View File

@ -656,6 +656,31 @@ x +y +z
(unary_operator (unary_operator
(identifier)))))))) (identifier))))))))
=====================================
nullary range
=====================================
..
Enum.to_list(..)
not ..
range = ..
---
(source
(operator_identifier)
(call
(dot
(alias)
(identifier))
(arguments
(operator_identifier)))
(unary_operator
(operator_identifier))
(binary_operator
(identifier)
(operator_identifier)))
===================================== =====================================
stepped range stepped range
===================================== =====================================

View File

@ -82,3 +82,8 @@ y = true and false
# ^ module # ^ module
# ^ operator # ^ operator
# ^ function # ^ function
range = ..
# <- variable
# ^ operator
# ^ operator