Skip to content

Commit 8d34758

Browse files
author
José Valim
committed
Group arrow-like ops together
1 parent 68c890e commit 8d34758

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

lib/elixir/src/elixir_parser.yrl

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ Nonterminals
22
grammar expr_list
33
expr paren_expr block_expr fn_expr bracket_expr call_expr bracket_at_expr max_expr
44
base_expr matched_expr matched_op_expr unmatched_expr op_expr
5-
mult_op two_op right_op bin_concat_op
6-
match_op send_op default_op when_op pipe_op in_op inc_op range_op
5+
mult_op two_op bin_concat_op
6+
match_op default_op when_op pipe_op in_op inc_op range_op
77
colon_colon_op three_op
88
comp_op_eol at_op_eol unary_op_eol dual_op_eol and_op_eol or_op_eol
9+
arrow_op_eol
910
open_paren close_paren empty_paren
1011
open_bracket close_bracket
1112
open_curly close_curly
@@ -28,14 +29,14 @@ Terminals
2829
fn 'end' aliases
2930
number signed_number atom bin_string list_string sigil
3031
dot_call_op op_identifier
31-
comp_op at_op unary_op dual_op and_op or_op
32+
comp_op at_op unary_op dual_op and_op or_op arrow_op
3233
'when' 'in' 'inlist' 'inbits' 'do'
3334
'true' 'false' 'nil'
3435
'=' '*' '/' '++' '--' '**' '//'
3536
'(' ')' '[' ']' '{' '}' '<<' '>>' '::'
36-
eol ',' '&' '|' '.' '<-' '<>' '->' '|>'
37+
eol ',' '&' '|' '.' '<>' '->'
3738
'...' '..'
38-
'<<<' '>>>' '&&&' '|||' '^^^'
39+
'^^^'
3940
.
4041

4142
Rootsymbol grammar.
@@ -49,12 +50,11 @@ Right 50 default_op.
4950
Left 60 pipe_op.
5051
Left 70 inc_op.
5152
Right 80 match_op.
52-
Right 110 send_op.
53-
Left 140 or_op_eol. %% ||, or, xor
54-
Left 150 and_op_eol. %% &&, and
55-
Left 160 comp_op_eol. %% < (op), > (op), <=, >=, ==, !=, =~, ===, !===
53+
Left 130 or_op_eol. %% ||, |||, or, xor
54+
Left 140 and_op_eol. %% &&, &&&, and
55+
Left 150 comp_op_eol. %% <, >, <=, >=, ==, !=, =~, ===, !===
56+
Right 160 arrow_op_eol. %% < (op), (op) >
5657
Left 170 in_op.
57-
Right 190 right_op.
5858
Left 200 range_op.
5959
Left 210 three_op.
6060
Left 220 dual_op_eol. %% +, -
@@ -103,7 +103,6 @@ op_expr -> match_op expr : { '$1', '$2' }.
103103
op_expr -> dual_op_eol expr : { '$1', '$2' }.
104104
op_expr -> mult_op expr : { '$1', '$2' }.
105105
op_expr -> two_op expr : { '$1', '$2' }.
106-
op_expr -> right_op expr : { '$1', '$2' }.
107106
op_expr -> and_op_eol expr : { '$1', '$2' }.
108107
op_expr -> or_op_eol expr : { '$1', '$2' }.
109108
op_expr -> three_op expr : { '$1', '$2' }.
@@ -112,17 +111,16 @@ op_expr -> bin_concat_op expr : { '$1', '$2' }.
112111
op_expr -> in_op expr : { '$1', '$2' }.
113112
op_expr -> inc_op expr : { '$1', '$2' }.
114113
op_expr -> when_op expr : { '$1', '$2' }.
115-
op_expr -> send_op expr : { '$1', '$2' }.
116114
op_expr -> range_op expr : { '$1', '$2' }.
117115
op_expr -> default_op expr : { '$1', '$2' }.
118116
op_expr -> colon_colon_op expr : { '$1', '$2' }.
119117
op_expr -> comp_op_eol expr : { '$1', '$2' }.
118+
op_expr -> arrow_op_eol expr : { '$1', '$2' }.
120119

121120
matched_op_expr -> match_op matched_expr : { '$1', '$2' }.
122121
matched_op_expr -> dual_op_eol matched_expr : { '$1', '$2' }.
123122
matched_op_expr -> mult_op matched_expr : { '$1', '$2' }.
124123
matched_op_expr -> two_op matched_expr : { '$1', '$2' }.
125-
matched_op_expr -> right_op matched_expr : { '$1', '$2' }.
126124
matched_op_expr -> and_op_eol matched_expr : { '$1', '$2' }.
127125
matched_op_expr -> or_op_eol matched_expr : { '$1', '$2' }.
128126
matched_op_expr -> three_op matched_expr : { '$1', '$2' }.
@@ -131,11 +129,11 @@ matched_op_expr -> bin_concat_op matched_expr : { '$1', '$2' }.
131129
matched_op_expr -> in_op matched_expr : { '$1', '$2' }.
132130
matched_op_expr -> inc_op matched_expr : { '$1', '$2' }.
133131
matched_op_expr -> when_op matched_expr : { '$1', '$2' }.
134-
matched_op_expr -> send_op matched_expr : { '$1', '$2' }.
135132
matched_op_expr -> range_op matched_expr : { '$1', '$2' }.
136133
matched_op_expr -> default_op matched_expr : { '$1', '$2' }.
137134
matched_op_expr -> colon_colon_op matched_expr : { '$1', '$2' }.
138135
matched_op_expr -> comp_op_eol matched_expr : { '$1', '$2' }.
136+
matched_op_expr -> arrow_op_eol matched_expr : { '$1', '$2' }.
139137

140138
block_expr -> parens_call call_args_parens do_block : build_identifier('$1', '$2' ++ '$3').
141139
block_expr -> parens_call call_args_parens call_args_parens do_block : build_nested_parens('$1', '$2', '$3' ++ '$4').
@@ -273,17 +271,6 @@ two_op -> '--' eol : '$1'.
273271
two_op -> '**' : '$1'.
274272
two_op -> '**' eol : '$1'.
275273

276-
right_op -> '|>' : '$1'.
277-
right_op -> '|>' eol : '$1'.
278-
279-
three_op -> '&&&' : '$1'.
280-
three_op -> '&&&' eol : '$1'.
281-
three_op -> '|||' : '$1'.
282-
three_op -> '|||' eol : '$1'.
283-
three_op -> '<<<' : '$1'.
284-
three_op -> '<<<' eol : '$1'.
285-
three_op -> '>>>' : '$1'.
286-
three_op -> '>>>' eol : '$1'.
287274
three_op -> '^^^' : '$1'.
288275
three_op -> '^^^' eol : '$1'.
289276

@@ -327,9 +314,6 @@ when_op -> 'when' eol : '$1'.
327314
stab_op -> '->' : '$1'.
328315
stab_op -> '->' eol : '$1'.
329316

330-
send_op -> '<-' : '$1'.
331-
send_op -> '<-' eol : '$1'.
332-
333317
range_op -> '..' : '$1'.
334318
range_op -> '..' eol : '$1'.
335319

@@ -339,6 +323,9 @@ at_op_eol -> at_op eol : '$1'.
339323
comp_op_eol -> comp_op : '$1'.
340324
comp_op_eol -> comp_op eol : '$1'.
341325

326+
arrow_op_eol -> arrow_op : '$1'.
327+
arrow_op_eol -> arrow_op eol : '$1'.
328+
342329
% Dot operator
343330

344331
dot_op -> '.' : '$1'.

lib/elixir/src/elixir_tokenizer.erl

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,18 @@
1616
).
1717

1818
-define(op3(T1, T2, T3),
19-
T1 == $<, T2 == $<, T3 == $<;
20-
T1 == $>, T2 == $>, T3 == $>;
21-
T1 == $^, T2 == $^, T3 == $^;
22-
T1 == $~, T2 == $~, T3 == $~;
23-
T1 == $&, T2 == $&, T3 == $&;
24-
T1 == $|, T2 == $|, T3 == $|
19+
T1 == $^, T2 == $^, T3 == $^
2520
).
2621

2722
-define(op2(T1, T2),
28-
T1 == $&, T2 == $&;
29-
T1 == $|, T2 == $|;
3023
T1 == $<, T2 == $>;
3124
T1 == $+, T2 == $+;
3225
T1 == $-, T2 == $-;
3326
T1 == $*, T2 == $*;
3427
T1 == $/, T2 == $/;
3528
T1 == $:, T2 == $:;
36-
T1 == $<, T2 == $-;
3729
T1 == $-, T2 == $>;
38-
T1 == $., T2 == $.;
39-
T1 == $|, T2 == $>
30+
T1 == $., T2 == $.
4031
).
4132

4233
-define(op1(T),
@@ -83,6 +74,20 @@
8374
-define(or_op(T1, T2),
8475
T1 == $|, T2 == $|).
8576

77+
-define(and_op3(T1, T2, T3),
78+
T1 == $&, T2 == $&, T3 == $&).
79+
80+
-define(or_op3(T1, T2, T3),
81+
T1 == $|, T2 == $|, T3 == $|).
82+
83+
-define(arrow_op3(T1, T2, T3),
84+
T1 == $<, T2 == $<, T3 == $<;
85+
T1 == $>, T2 == $>, T3 == $>).
86+
87+
-define(arrow_op(T1, T2),
88+
T1 == $<, T2 == $-;
89+
T1 == $|, T2 == $>).
90+
8691
tokenize(String, Line, Opts) ->
8792
File = case lists:keyfind(file, 1, Opts) of
8893
{ file, V1 } -> V1;
@@ -230,15 +235,16 @@ tokenize([$.,T1,T2|Rest], Line, Scope, Tokens) when ?container2(T1, T2) ->
230235

231236
% ## Three Token Operators
232237
tokenize([$.,T1,T2,T3|Rest], Line, Scope, Tokens) when
233-
?unary_op3(T1, T2, T3); ?comp_op3(T1, T2, T3) ->
238+
?unary_op3(T1, T2, T3); ?comp_op3(T1, T2, T3); ?and_op3(T1, T2, T3); ?or_op3(T1, T2, T3);
239+
?arrow_op3(T1, T2, T3) ->
234240
handle_call_identifier(Rest, Line, list_to_atom([T1, T2, T3]), Scope, Tokens);
235241

236242
tokenize([$.,T1,T2,T3|Rest], Line, Scope, Tokens) when ?op3(T1, T2, T3) ->
237243
handle_call_identifier(Rest, Line, list_to_atom([T1, T2, T3]), Scope, Tokens);
238244

239245
% ## Two Token Operators
240246
tokenize([$.,T1,T2|Rest], Line, Scope, Tokens) when
241-
?comp_op2(T1, T2); ?and_op(T1, T2); ?or_op(T1, T2) ->
247+
?comp_op2(T1, T2); ?and_op(T1, T2); ?or_op(T1, T2); ?arrow_op(T1, T2) ->
242248
handle_call_identifier(Rest, Line, list_to_atom([T1, T2]), Scope, Tokens);
243249

244250
tokenize([$.,T1,T2|Rest], Line, Scope, Tokens) when ?op2(T1, T2) ->
@@ -315,15 +321,16 @@ tokenize([$:,T1,T2|Rest], Line, Scope, Tokens) when ?container2(T1, T2) ->
315321

316322
% ## Three Token Operators
317323
tokenize([$:,T1,T2,T3|Rest], Line, Scope, Tokens) when
318-
?unary_op3(T1, T2, T3); ?comp_op3(T1, T2, T3) ->
324+
?unary_op3(T1, T2, T3); ?comp_op3(T1, T2, T3); ?and_op3(T1, T2, T3); ?or_op3(T1, T2, T3);
325+
?arrow_op3(T1, T2, T3) ->
319326
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2,T3]) }|Tokens]);
320327

321328
tokenize([$:,T1,T2,T3|Rest], Line, Scope, Tokens) when ?op3(T1, T2, T3) ->
322329
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2,T3]) }|Tokens]);
323330

324331
% ## Two Token Operators
325332
tokenize([$:,T1,T2|Rest], Line, Scope, Tokens) when
326-
?comp_op2(T1, T2); ?and_op(T1, T2); ?or_op(T1, T2) ->
333+
?comp_op2(T1, T2); ?and_op(T1, T2); ?or_op(T1, T2); ?arrow_op(T1, T2) ->
327334
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2]) }|Tokens]);
328335

329336
tokenize([$:,T1,T2|Rest], Line, Scope, Tokens) when ?op2(T1, T2) ->
@@ -370,6 +377,15 @@ tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?unary_op3(T1, T2, T3) ->
370377
tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?comp_op3(T1, T2, T3) ->
371378
handle_op(Rest, Line, comp_op, list_to_atom([T1,T2,T3]), Scope, Tokens);
372379

380+
tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?and_op3(T1, T2, T3) ->
381+
handle_op(Rest, Line, and_op, list_to_atom([T1,T2,T3]), Scope, Tokens);
382+
383+
tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?or_op3(T1, T2, T3) ->
384+
handle_op(Rest, Line, or_op, list_to_atom([T1,T2,T3]), Scope, Tokens);
385+
386+
tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?arrow_op3(T1, T2, T3) ->
387+
handle_op(Rest, Line, arrow_op, list_to_atom([T1,T2,T3]), Scope, Tokens);
388+
373389
tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?op3(T1, T2, T3) ->
374390
handle_op(Rest, Line, list_to_atom([T1,T2,T3]), Scope, Tokens);
375391

@@ -384,6 +400,9 @@ tokenize([T|Rest], Line, Scope, Tokens) when T == $(;
384400
handle_terminator(Rest, Line, Scope, Token, Tokens);
385401

386402
% ## Two Token Operators
403+
tokenize([T1,T2|Rest], Line, Scope, Tokens) when ?arrow_op(T1, T2) ->
404+
handle_op(Rest, Line, arrow_op, list_to_atom([T1, T2]), Scope, Tokens);
405+
387406
tokenize([T1,T2|Rest], Line, Scope, Tokens) when ?comp_op2(T1, T2) ->
388407
handle_op(Rest, Line, comp_op, list_to_atom([T1, T2]), Scope, Tokens);
389408

lib/elixir/test/elixir/bitwise_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,22 @@ defmodule Bitwise.OperatorsTest do
3838
end
3939

4040
test :band do
41-
assert 1 &&& 1 == 1
41+
assert (1 &&& 1) == 1
4242
end
4343

4444
test :bor do
45-
assert 0 ||| 1 == 1
45+
assert (0 ||| 1) == 1
4646
end
4747

4848
test :bxor do
4949
assert 1 ^^^ 1 == 0
5050
end
5151

5252
test :bsl do
53-
assert 1 <<< 1 == 2
53+
assert (1 <<< 1) == 2
5454
end
5555

5656
test :bsr do
57-
assert 1 >>> 1 == 0
57+
assert (1 >>> 1) == 0
5858
end
5959
end

0 commit comments

Comments
 (0)