Skip to content

Commit 68c890e

Browse files
author
José Valim
committed
Move and and or operators to the new table
1 parent 6f73d7d commit 68c890e

File tree

3 files changed

+44
-46
lines changed

3 files changed

+44
-46
lines changed

lib/elixir/src/elixir_parser.yrl

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Nonterminals
44
base_expr matched_expr matched_op_expr unmatched_expr op_expr
55
mult_op two_op right_op bin_concat_op
66
match_op send_op default_op when_op pipe_op in_op inc_op range_op
7-
andand_op oror_op and_op or_op colon_colon_op three_op
8-
comp_op_eol at_op_eol unary_op_eol dual_op_eol
7+
colon_colon_op three_op
8+
comp_op_eol at_op_eol unary_op_eol dual_op_eol and_op_eol or_op_eol
99
open_paren close_paren empty_paren
1010
open_bracket close_bracket
1111
open_curly close_curly
@@ -28,13 +28,13 @@ Terminals
2828
fn 'end' aliases
2929
number signed_number atom bin_string list_string sigil
3030
dot_call_op op_identifier
31-
comp_op at_op unary_op dual_op
32-
'and' 'or' 'xor' 'when' 'in' 'inlist' 'inbits' 'do'
31+
comp_op at_op unary_op dual_op and_op or_op
32+
'when' 'in' 'inlist' 'inbits' 'do'
3333
'true' 'false' 'nil'
3434
'=' '*' '/' '++' '--' '**' '//'
3535
'(' ')' '[' ']' '{' '}' '<<' '>>' '::'
3636
eol ',' '&' '|' '.' '<-' '<>' '->' '|>'
37-
'&&' '||' '...' '..'
37+
'...' '..'
3838
'<<<' '>>>' '&&&' '|||' '^^^'
3939
.
4040

@@ -50,23 +50,21 @@ Left 60 pipe_op.
5050
Left 70 inc_op.
5151
Right 80 match_op.
5252
Right 110 send_op.
53-
Left 120 oror_op.
54-
Left 130 andand_op.
55-
Left 140 or_op.
56-
Left 150 and_op.
57-
Left 160 comp_op_eol. %% < (op), > (op), <=, >=, ==, !=, =~, ===, !===
53+
Left 140 or_op_eol. %% ||, or, xor
54+
Left 150 and_op_eol. %% &&, and
55+
Left 160 comp_op_eol. %% < (op), > (op), <=, >=, ==, !=, =~, ===, !===
5856
Left 170 in_op.
5957
Right 190 right_op.
6058
Left 200 range_op.
6159
Left 210 three_op.
62-
Left 220 dual_op_eol. %% +, -
60+
Left 220 dual_op_eol. %% +, -
6361
Left 230 mult_op.
6462
Right 240 bin_concat_op.
6563
Right 250 two_op.
66-
Nonassoc 300 unary_op_eol. %% +, -, !, ^, not, ~~~
64+
Nonassoc 300 unary_op_eol. %% +, -, !, ^, not, ~~~
6765
Left 310 dot_call_op.
6866
Left 310 dot_op.
69-
Nonassoc 320 at_op_eol. %% @ (op)
67+
Nonassoc 320 at_op_eol. %% @ (op)
7068
Nonassoc 330 var.
7169

7270
%%% MAIN FLOW OF EXPRESSIONS
@@ -106,11 +104,9 @@ op_expr -> dual_op_eol expr : { '$1', '$2' }.
106104
op_expr -> mult_op expr : { '$1', '$2' }.
107105
op_expr -> two_op expr : { '$1', '$2' }.
108106
op_expr -> right_op expr : { '$1', '$2' }.
109-
op_expr -> andand_op expr : { '$1', '$2' }.
107+
op_expr -> and_op_eol expr : { '$1', '$2' }.
108+
op_expr -> or_op_eol expr : { '$1', '$2' }.
110109
op_expr -> three_op expr : { '$1', '$2' }.
111-
op_expr -> oror_op expr : { '$1', '$2' }.
112-
op_expr -> and_op expr : { '$1', '$2' }.
113-
op_expr -> or_op expr : { '$1', '$2' }.
114110
op_expr -> pipe_op expr : { '$1', '$2' }.
115111
op_expr -> bin_concat_op expr : { '$1', '$2' }.
116112
op_expr -> in_op expr : { '$1', '$2' }.
@@ -127,11 +123,9 @@ matched_op_expr -> dual_op_eol matched_expr : { '$1', '$2' }.
127123
matched_op_expr -> mult_op matched_expr : { '$1', '$2' }.
128124
matched_op_expr -> two_op matched_expr : { '$1', '$2' }.
129125
matched_op_expr -> right_op matched_expr : { '$1', '$2' }.
130-
matched_op_expr -> andand_op matched_expr : { '$1', '$2' }.
126+
matched_op_expr -> and_op_eol matched_expr : { '$1', '$2' }.
127+
matched_op_expr -> or_op_eol matched_expr : { '$1', '$2' }.
131128
matched_op_expr -> three_op matched_expr : { '$1', '$2' }.
132-
matched_op_expr -> oror_op matched_expr : { '$1', '$2' }.
133-
matched_op_expr -> and_op matched_expr : { '$1', '$2' }.
134-
matched_op_expr -> or_op matched_expr : { '$1', '$2' }.
135129
matched_op_expr -> pipe_op matched_expr : { '$1', '$2' }.
136130
matched_op_expr -> bin_concat_op matched_expr : { '$1', '$2' }.
137131
matched_op_expr -> in_op matched_expr : { '$1', '$2' }.
@@ -307,19 +301,11 @@ unary_op_eol -> dual_op eol : '$1'.
307301
match_op -> '=' : '$1'.
308302
match_op -> '=' eol : '$1'.
309303

310-
andand_op -> '&&' : '$1'.
311-
andand_op -> '&&' eol : '$1'.
304+
and_op_eol -> and_op : '$1'.
305+
and_op_eol -> and_op eol : '$1'.
312306

313-
oror_op -> '||' : '$1'.
314-
oror_op -> '||' eol : '$1'.
315-
316-
and_op -> 'and' : '$1'.
317-
and_op -> 'and' eol : '$1'.
318-
319-
or_op -> 'or' : '$1'.
320-
or_op -> 'or' eol : '$1'.
321-
or_op -> 'xor' : '$1'.
322-
or_op -> 'xor' eol : '$1'.
307+
or_op_eol -> or_op : '$1'.
308+
or_op_eol -> or_op eol : '$1'.
323309

324310
pipe_op -> '|' : '$1'.
325311
pipe_op -> '|' eol : '$1'.

lib/elixir/src/elixir_tokenizer.erl

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@
7777
T1 == $=, T2 == $=, T3 == $=;
7878
T1 == $!, T2 == $=, T3 == $=).
7979

80+
-define(and_op(T1, T2),
81+
T1 == $&, T2 == $&).
82+
83+
-define(or_op(T1, T2),
84+
T1 == $|, T2 == $|).
85+
8086
tokenize(String, Line, Opts) ->
8187
File = case lists:keyfind(file, 1, Opts) of
8288
{ file, V1 } -> V1;
@@ -232,7 +238,7 @@ tokenize([$.,T1,T2,T3|Rest], Line, Scope, Tokens) when ?op3(T1, T2, T3) ->
232238

233239
% ## Two Token Operators
234240
tokenize([$.,T1,T2|Rest], Line, Scope, Tokens) when
235-
?comp_op2(T1, T2) ->
241+
?comp_op2(T1, T2); ?and_op(T1, T2); ?or_op(T1, T2) ->
236242
handle_call_identifier(Rest, Line, list_to_atom([T1, T2]), Scope, Tokens);
237243

238244
tokenize([$.,T1,T2|Rest], Line, Scope, Tokens) when ?op2(T1, T2) ->
@@ -317,7 +323,7 @@ tokenize([$:,T1,T2,T3|Rest], Line, Scope, Tokens) when ?op3(T1, T2, T3) ->
317323

318324
% ## Two Token Operators
319325
tokenize([$:,T1,T2|Rest], Line, Scope, Tokens) when
320-
?comp_op2(T1, T2) ->
326+
?comp_op2(T1, T2); ?and_op(T1, T2); ?or_op(T1, T2) ->
321327
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2]) }|Tokens]);
322328

323329
tokenize([$:,T1,T2|Rest], Line, Scope, Tokens) when ?op2(T1, T2) ->
@@ -381,6 +387,12 @@ tokenize([T|Rest], Line, Scope, Tokens) when T == $(;
381387
tokenize([T1,T2|Rest], Line, Scope, Tokens) when ?comp_op2(T1, T2) ->
382388
handle_op(Rest, Line, comp_op, list_to_atom([T1, T2]), Scope, Tokens);
383389

390+
tokenize([T1,T2|Rest], Line, Scope, Tokens) when ?and_op(T1, T2) ->
391+
handle_op(Rest, Line, and_op, list_to_atom([T1, T2]), Scope, Tokens);
392+
393+
tokenize([T1,T2|Rest], Line, Scope, Tokens) when ?or_op(T1, T2) ->
394+
handle_op(Rest, Line, or_op, list_to_atom([T1, T2]), Scope, Tokens);
395+
384396
tokenize([T1,T2|Rest], Line, Scope, Tokens) when ?op2(T1, T2) ->
385397
handle_op(Rest, Line, list_to_atom([T1, T2]), Scope, Tokens);
386398

@@ -846,16 +858,17 @@ check_keyword(Line, Identifier, Atom, Tokens) when
846858
Identifier == identifier; Identifier == do_identifier;
847859
Identifier == bracket_identifier; Identifier == paren_identifier ->
848860
case keyword(Atom) of
849-
true -> { ok, [{ Atom, Line }|Tokens] };
850-
op -> { ok, add_token_with_nl({ Atom, Line }, Tokens) };
851-
block -> { ok, [{ block_identifier, Line, Atom }|Tokens] };
852-
do ->
861+
do ->
853862
case do_keyword_valid(Tokens) of
854863
true -> { ok, [{ Atom, Line }|Tokens] };
855864
false -> { error, "do" }
856865
end;
857-
false -> nomatch;
858-
unary_op -> { ok, [{ unary_op, Line, Atom }|Tokens] }
866+
false -> nomatch;
867+
true -> { ok, [{ Atom, Line }|Tokens] };
868+
block -> { ok, [{ block_identifier, Line, Atom }|Tokens] };
869+
unary_op -> { ok, [{ unary_op, Line, Atom }|Tokens] };
870+
op -> { ok, add_token_with_nl({ Atom, Line }, Tokens) };
871+
Kind -> { ok, add_token_with_nl({ Kind, Line, Atom }, Tokens) }
859872
end;
860873

861874
check_keyword(_, _, _, _) -> nomatch.
@@ -876,9 +889,9 @@ keyword('do') -> do;
876889

877890
% Operators keywords
878891
keyword('not') -> unary_op;
879-
keyword('and') -> op;
880-
keyword('or') -> op;
881-
keyword('xor') -> op;
892+
keyword('and') -> and_op;
893+
keyword('or') -> or_op;
894+
keyword('xor') -> or_op;
882895
keyword('when') -> op;
883896
keyword('in') -> op;
884897
keyword('inlist') -> op;

lib/elixir/test/erlang/control_test.erl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ andand_test() ->
234234
{3, _} = eval("Bar.foo && 1 + 2"),
235235
{false, _} = eval("Bar.bar && error(:bad)"),
236236
{2, _} = eval("1 && 2"),
237-
{nil, _} = eval("nil && 2"),
238-
{false, _} = eval("false && false or true")
237+
{nil, _} = eval("nil && 2")
239238
end,
240239
test_helper:run_and_remove(F, ['Elixir.Bar']).
241240

0 commit comments

Comments
 (0)