Skip to content

Commit c2ac967

Browse files
author
José Valim
committed
Move all unary operators to new schema
1 parent dd573c9 commit c2ac967

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

lib/elixir/src/elixir_parser.yrl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ Terminals
2929
number signed_number atom bin_string list_string sigil
3030
dot_call_op op_identifier
3131
comp_op at_op unary_op dual_op
32-
'not' 'and' 'or' 'xor' 'when' 'in' 'inlist' 'inbits' 'do'
32+
'and' 'or' 'xor' 'when' 'in' 'inlist' 'inbits' 'do'
3333
'true' 'false' 'nil'
3434
'=' '*' '/' '++' '--' '**' '//'
3535
'(' ')' '[' ']' '{' '}' '<<' '>>' '::'
3636
eol ',' '&' '|' '.' '<-' '<>' '->' '|>' '=~'
3737
'&&' '||' '...' '..'
38-
'<<<' '>>>' '&&&' '|||' '^^^' '~~~'
38+
'<<<' '>>>' '&&&' '|||' '^^^'
3939
.
4040

4141
Rootsymbol grammar.
@@ -64,7 +64,7 @@ Left 220 dual_op_eol. %% +, -
6464
Left 230 mult_op.
6565
Right 240 bin_concat_op.
6666
Right 250 two_op.
67-
Nonassoc 300 unary_op_eol. %% +, -, !, ^
67+
Nonassoc 300 unary_op_eol. %% +, -, !, ^, not, ~~~
6868
Left 310 dot_call_op.
6969
Left 310 dot_op.
7070
Nonassoc 320 at_op_eol. %% @<op>
@@ -309,10 +309,6 @@ unary_op_eol -> unary_op : '$1'.
309309
unary_op_eol -> unary_op eol : '$1'.
310310
unary_op_eol -> dual_op : '$1'.
311311
unary_op_eol -> dual_op eol : '$1'.
312-
unary_op_eol -> 'not' : '$1'.
313-
unary_op_eol -> 'not' eol : '$1'.
314-
unary_op_eol -> '~~~' : '$1'.
315-
unary_op_eol -> '~~~' eol : '$1'.
316312

317313
match_op -> '=' : '$1'.
318314
match_op -> '=' eol : '$1'.

lib/elixir/src/elixir_tokenizer.erl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,18 @@
7070
-define(at_op(T),
7171
T == $@).
7272

73-
-define(dual_op(T),
74-
T == $+ orelse T == $-).
75-
7673
-define(unary_op(T),
7774
% T == $&;
7875
T == $!;
7976
T == $^
8077
).
8178

79+
-define(unary_op3(T1, T2, T3),
80+
T1 == $~, T2 == $~, T3 == $~).
81+
82+
-define(dual_op(T),
83+
T == $+ orelse T == $-).
84+
8285
tokenize(String, Line, Opts) ->
8386
File = case lists:keyfind(file, 1, Opts) of
8487
{ file, V1 } -> V1;
@@ -227,6 +230,9 @@ tokenize([$.,T1,T2|Rest], Line, Scope, Tokens) when ?container2(T1, T2) ->
227230
handle_call_identifier(Rest, Line, list_to_atom([T1, T2]), Scope, Tokens);
228231

229232
% ## Three Token Operators
233+
tokenize([$.,T1,T2,T3|Rest], Line, Scope, Tokens) when ?unary_op3(T1, T2, T3) ->
234+
handle_call_identifier(Rest, Line, list_to_atom([T1, T2, T3]), Scope, Tokens);
235+
230236
tokenize([$.,T1,T2,T3|Rest], Line, Scope, Tokens) when ?comp3(T1, T2, T3); ?op3(T1, T2, T3) ->
231237
handle_call_identifier(Rest, Line, list_to_atom([T1, T2, T3]), Scope, Tokens);
232238

@@ -303,6 +309,9 @@ tokenize([$:,T1,T2|Rest], Line, Scope, Tokens) when ?container2(T1, T2) ->
303309
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2]) }|Tokens]);
304310

305311
% ## Three Token Operators
312+
tokenize([$:,T1,T2,T3|Rest], Line, Scope, Tokens) when ?unary_op3(T1, T2, T3) ->
313+
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2,T3]) }|Tokens]);
314+
306315
tokenize([$:,T1,T2,T3|Rest], Line, Scope, Tokens) when ?comp3(T1, T2, T3); ?op3(T1, T2, T3) ->
307316
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2,T3]) }|Tokens]);
308317

@@ -349,6 +358,9 @@ tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?comp3(T1, T2, T3) ->
349358
handle_comp_op(Rest, Line, list_to_atom([T1,T2,T3]), Scope, Tokens);
350359

351360
% ## Three token operators
361+
tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?unary_op3(T1, T2, T3) ->
362+
handle_unary_op(Rest, Line, unary_op, list_to_atom([T1,T2,T3]), Scope, Tokens);
363+
352364
tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?op3(T1, T2, T3) ->
353365
handle_op(Rest, Line, list_to_atom([T1,T2,T3]), Scope, Tokens);
354366

@@ -847,7 +859,8 @@ check_keyword(Line, Identifier, Atom, Tokens) when
847859
true -> { ok, [{ Atom, Line }|Tokens] };
848860
false -> { error, "do" }
849861
end;
850-
false -> nomatch
862+
false -> nomatch;
863+
unary_op -> { ok, [{ unary_op, Line, Atom }|Tokens] }
851864
end;
852865

853866
check_keyword(_, _, _, _) -> nomatch.
@@ -862,12 +875,12 @@ keyword('end') -> true;
862875
keyword('true') -> true;
863876
keyword('false') -> true;
864877
keyword('nil') -> true;
865-
keyword('not') -> true;
866878

867879
% Special handling for do
868880
keyword('do') -> do;
869881

870-
% Bin operator keywords
882+
% Operators keywords
883+
keyword('not') -> unary_op;
871884
keyword('and') -> op;
872885
keyword('or') -> op;
873886
keyword('xor') -> op;

0 commit comments

Comments
 (0)