Skip to content

Commit

Permalink
Merge pull request #18 from ananthakumaran/stepped-range
Browse files Browse the repository at this point in the history
add support for stepped ranges
  • Loading branch information
ananthakumaran committed May 15, 2021
2 parents b13a0ff + c0b43ab commit 0c46616
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elixir 1.12.0-rc.1-otp-23
erlang 23.1
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ parse-all:
./node_modules/.bin/tree-sitter parse '../elixir/**/*.ex*' --quiet --stat

parse:
./node_modules/.bin/tree-sitter parse 'test.ex'
./node_modules/.bin/tree-sitter parse -x 'test.ex'

debug-graph:
./node_modules/.bin/tree-sitter parse 'test.ex' --debug-graph
Expand Down
43 changes: 22 additions & 21 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ function atleastOnce(rule) {
return seq(rule, repeat(rule));
}

function binaryOp($, assoc, precedence, operator, bare_keyword) {
const right = bare_keyword
? choice($._expression, $.keyword_list)
: $._expression;
function binaryOp($, assoc, precedence, operator, right) {
return assoc(
precedence,
seq(
field("left", $._expression),
field("operator", operator),
optional($._terminator),
field("right", right)
field("right", right || $._expression)
)
);
}
Expand Down Expand Up @@ -107,7 +104,8 @@ const OPERATORS = [
"|",
"::",
"<-",
"\\\\"
"\\\\",
"..//"
];

const PREC = {
Expand Down Expand Up @@ -263,34 +261,37 @@ module.exports = grammar({
binary_op: $ =>
choice(
binaryOp($, prec.left, 40, choice("\\\\", "<-")),
binaryOp($, prec.right, 50, alias($._when, "when"), true),
binaryOp(
$,
prec.right,
50,
alias($._when, "when"),
choice($._expression, $.keyword_list)
),
binaryOp($, prec.right, 60, "::"),
binaryOp($, prec.right, 70, "|", true),
binaryOp($, prec.right, 70, "|", choice($._expression, $.keyword_list)),
binaryOp($, prec.right, 80, "=>"),
binaryOp($, prec.right, 100, "="),
binaryOp($, prec.left, 130, choice("||", "|||", alias($._or, "or"))),
binaryOp($, prec.left, 140, choice("&&", "&&&", alias($._and, "and"))),
binaryOp($, prec.left, 150, choice("==", "!=", "=~", "===", "!==")),
binaryOp($, prec.left, 160, choice("<", ">", "<=", ">=")),
binaryOp($, prec.left, 120, choice("||", "|||", alias($._or, "or"))),
binaryOp($, prec.left, 130, choice("&&", "&&&", alias($._and, "and"))),
binaryOp($, prec.left, 140, choice("==", "!=", "=~", "===", "!==")),
binaryOp($, prec.left, 150, choice("<", ">", "<=", ">=")),
binaryOp(
$,
prec.left,
170,
160,
choice("|>", "<<<", ">>>", "<<~", "~>>", "<~", "~>", "<~>", "<|>")
),
binaryOp(
$,
prec.left,
180,
170,
choice(alias($._in, "in"), alias($._not_in, "not in"))
),
binaryOp($, prec.left, 190, choice("^^^")),
binaryOp(
$,
prec.right,
200,
choice("++", "--", "..", "<>", "+++", "---")
),
binaryOp($, prec.left, 180, choice("^^^")),
binaryOp($, prec.right, 200, choice("++", "--", "<>", "+++", "---")),
binaryOp($, prec.right, 190, choice("//")),
binaryOp($, prec.right, 200, ".."),
binaryOp($, prec.left, 210, choice("+", "-")),
binaryOp($, prec.left, 220, choice("*", "/")),
$._op_capture
Expand Down
30 changes: 27 additions & 3 deletions src/scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,16 @@ struct Scanner {
if (lexer->lookahead != '.') return true;
advance(lexer);
lexer->mark_end(lexer);
if (lexer->lookahead != '.') return true;
advance(lexer);
lexer->mark_end(lexer);
if (lexer->lookahead == '/') {
advance(lexer);
if (lexer->lookahead == '/') {
advance(lexer);
lexer->mark_end(lexer);
}
} else if (lexer->lookahead == '.') {
advance(lexer);
lexer->mark_end(lexer);
}
return true;
case ':':
advance(lexer);
Expand Down Expand Up @@ -584,6 +591,7 @@ struct Scanner {
is_whitespace(lexer->lookahead)) {
return is_valid(lexer, valid_symbols, KEYWORD_LITERAL);
}
return false;
}
if (lexer->lookahead != '.') return false;
advance(lexer);
Expand All @@ -593,7 +601,22 @@ struct Scanner {
is_whitespace(lexer->lookahead)) {
return is_valid(lexer, valid_symbols, KEYWORD_LITERAL);
}
return false;
}
if (lexer->lookahead == '/') {
advance(lexer);
if (lexer->lookahead != '/') return false;
advance(lexer);
if (lexer->lookahead == ':') {
advance(lexer);
if (is_newline(lexer->lookahead) ||
is_whitespace(lexer->lookahead)) {
return is_valid(lexer, valid_symbols, KEYWORD_LITERAL);
}
}
return false;
}

if (lexer->lookahead != '.') return false;
advance(lexer);
lexer->mark_end(lexer);
Expand All @@ -604,6 +627,7 @@ struct Scanner {
is_whitespace(lexer->lookahead)) {
return is_valid(lexer, valid_symbols, KEYWORD_LITERAL);
}
return false;
}

return is_valid(lexer, valid_symbols, IDENTIFIER, false);
Expand Down
12 changes: 11 additions & 1 deletion test/corpus/data_types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ atoms
operator atom
================================================================================

[:@, :., :+, :-, :!, :^, :not, :~~~, :*, :/, :+, :-, :++, :--, :.., :..., :<>, :+++, :---, :^^^, :in, :not, :|>, :<<<, :>>>, :<<~, :~>>, :<~, :~>, :<~>, :<|>, :<, :>, :<=, :>=, :==, :!=, :=~, :===, :!==, :&&, :&&&, :and, :||, :|||, :=, :&, :=>, :|, :::, :when, :<-, :\\, :%, :%{}, :{}, :->, :<<>>]
[:@, :., :+, :-, :!, :^, :not, :~~~, :*, :/, :+, :-, :++, :--, :.., :..., :<>, :+++, :---, :^^^, :in, :not, :|>, :<<<, :>>>, :<<~, :~>>, :<~, :~>, :<~>, :<|>, :<, :>, :<=, :>=, :==, :!=, :=~, :===, :!==, :&&, :&&&, :and, :||, :|||, :=, :&, :=>, :|, :::, :when, :<-, :\\, :%, :%{}, :{}, :->, :<<>>, :..//]

--------------------------------------------------------------------------------

Expand Down Expand Up @@ -295,6 +295,8 @@ operator atom
(atom_literal))
(atom
(atom_literal))
(atom
(atom_literal))
(atom
(atom_literal))))

Expand Down Expand Up @@ -861,6 +863,9 @@ linebreak followed by operator keyword
[
...: 1]

[
..//: 1]

[
<>: 1]

Expand Down Expand Up @@ -1214,6 +1219,11 @@ in: 1]
(keyword
(keyword_literal))
(integer)))
(list
(keyword_list
(keyword
(keyword_literal))
(integer)))
(list
(keyword_list
(keyword
Expand Down
16 changes: 16 additions & 0 deletions test/corpus/expression.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,19 @@ Unary operator with newline
(program
(unary_op
(integer)))

================================================================================
stepped range
================================================================================

1..2
// 4

--------------------------------------------------------------------------------

(program
(binary_op
(binary_op
(integer)
(integer))
(integer)))
95 changes: 95 additions & 0 deletions test/corpus/operator.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,98 @@ a
(binary_op
(identifier)
(identifier)))

================================================================================
stepped ranges
================================================================================

1 .. 2 // 3
(1..2//3).step
1..2//3
0..1//-1
--------------------------------------------------------------------------------

(program
(binary_op
(binary_op
(integer)
(integer))
(integer))
(dot_call
(paren_expr
(binary_op
(binary_op
(integer)
(integer))
(integer)))
(function_identifier))
(binary_op
(binary_op
(integer)
(integer))
(integer))
(binary_op
(binary_op
(integer)
(integer))
(unary_op
(integer))))

================================================================================
stepped blocks
================================================================================

foo do end..bar do end//baz do end
1..(2//3)
(1..2)//3
(1..2)//(3)

--------------------------------------------------------------------------------

(program
(binary_op
(binary_op
(call
(function_identifier)
(do_block))
(call
(function_identifier)
(do_block)))
(call
(function_identifier)
(do_block)))
(binary_op
(integer)
(block
(binary_op
(integer)
(integer))))
(binary_op
(block
(binary_op
(integer)
(integer)))
(integer))
(binary_op
(block
(binary_op
(integer)
(integer)))
(block
(integer))))

================================================================================
multiline stepped blocks
================================================================================

1..2
// 4

--------------------------------------------------------------------------------

(program
(binary_op
(binary_op
(integer)
(integer))
(integer)))
4 changes: 4 additions & 0 deletions test/highlight/sandbox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,10 @@ use Bitwise
2 <<< 3
# ^ operator

1..10//3
#^ operator
# ^ operator

# Protocols
defprotocol Useless do
#<- keyword
Expand Down

0 comments on commit 0c46616

Please sign in to comment.