Skip to content

Commit e941d78

Browse files
author
José Valim
committed
Move operator table clean up
1 parent 8d34758 commit e941d78

File tree

2 files changed

+83
-81
lines changed

2 files changed

+83
-81
lines changed

lib/elixir/src/elixir_parser.yrl

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ 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 bin_concat_op
6-
match_op default_op when_op pipe_op in_op inc_op range_op
7-
colon_colon_op three_op
5+
two_op bin_concat_op
6+
match_op default_op tail_op
7+
colon_colon_op
88
comp_op_eol at_op_eol unary_op_eol dual_op_eol and_op_eol or_op_eol
9-
arrow_op_eol
9+
arrow_op_eol pin_op_eol mult_op_eol range_op_eol
10+
when_op_eol in_op_eol inc_op_eol
1011
open_paren close_paren empty_paren
1112
open_bracket close_bracket
1213
open_curly close_curly
@@ -29,36 +30,34 @@ Terminals
2930
fn 'end' aliases
3031
number signed_number atom bin_string list_string sigil
3132
dot_call_op op_identifier
32-
comp_op at_op unary_op dual_op and_op or_op arrow_op
33-
'when' 'in' 'inlist' 'inbits' 'do'
34-
'true' 'false' 'nil'
35-
'=' '*' '/' '++' '--' '**' '//'
36-
'(' ')' '[' ']' '{' '}' '<<' '>>' '::'
37-
eol ',' '&' '|' '.' '<>' '->'
38-
'...' '..'
39-
'^^^'
33+
comp_op at_op unary_op dual_op and_op or_op arrow_op pin_op
34+
mult_op range_op in_op inc_op when_op
35+
'=' '++' '--' '**' '//' '::' '|' '<>' '->'
36+
'true' 'false' 'nil' 'do' eol ',' '.' '&'
37+
'(' ')' '[' ']' '{' '}' '<<' '>>'
38+
'...'
4039
.
4140

4241
Rootsymbol grammar.
4342

4443
Left 5 do.
4544
Right 10 stab_op.
46-
Left 20 ','. % Solve nested call_args conflicts
45+
Left 20 ','.
4746
Right 30 colon_colon_op.
48-
Right 40 when_op.
47+
Right 40 when_op_eol. %% when
4948
Right 50 default_op.
50-
Left 60 pipe_op.
51-
Left 70 inc_op.
49+
Left 60 tail_op.
50+
Left 70 inc_op_eol. %% inlist, inbits
5251
Right 80 match_op.
5352
Left 130 or_op_eol. %% ||, |||, or, xor
5453
Left 140 and_op_eol. %% &&, &&&, and
55-
Left 150 comp_op_eol. %% <, >, <=, >=, ==, !=, =~, ===, !===
56-
Right 160 arrow_op_eol. %% < (op), (op) >
57-
Left 170 in_op.
58-
Left 200 range_op.
59-
Left 210 three_op.
60-
Left 220 dual_op_eol. %% +, -
61-
Left 230 mult_op.
54+
Left 150 comp_op_eol. %% <, >, <=, >=, ==, !=, =~, ===, !==
55+
Right 160 arrow_op_eol. %% < (op), (op) > (e.g <-, |>, <<<, >>>)
56+
Left 170 in_op_eol. %% in
57+
Left 200 range_op_eol. %% ..
58+
Left 210 dual_op_eol. %% +, -
59+
Left 220 mult_op_eol. %% *, /
60+
Left 230 pin_op_eol. %% ^ (op) (e.g ^^^)
6261
Right 240 bin_concat_op.
6362
Right 250 two_op.
6463
Nonassoc 300 unary_op_eol. %% +, -, !, ^, not, ~~~
@@ -101,35 +100,35 @@ unmatched_expr -> block_expr : '$1'.
101100

102101
op_expr -> match_op expr : { '$1', '$2' }.
103102
op_expr -> dual_op_eol expr : { '$1', '$2' }.
104-
op_expr -> mult_op expr : { '$1', '$2' }.
103+
op_expr -> mult_op_eol expr : { '$1', '$2' }.
105104
op_expr -> two_op expr : { '$1', '$2' }.
106105
op_expr -> and_op_eol expr : { '$1', '$2' }.
107106
op_expr -> or_op_eol expr : { '$1', '$2' }.
108-
op_expr -> three_op expr : { '$1', '$2' }.
109-
op_expr -> pipe_op expr : { '$1', '$2' }.
107+
op_expr -> pin_op_eol expr : { '$1', '$2' }.
108+
op_expr -> tail_op expr : { '$1', '$2' }.
110109
op_expr -> bin_concat_op expr : { '$1', '$2' }.
111-
op_expr -> in_op expr : { '$1', '$2' }.
112-
op_expr -> inc_op expr : { '$1', '$2' }.
113-
op_expr -> when_op expr : { '$1', '$2' }.
114-
op_expr -> range_op expr : { '$1', '$2' }.
110+
op_expr -> in_op_eol expr : { '$1', '$2' }.
111+
op_expr -> inc_op_eol expr : { '$1', '$2' }.
112+
op_expr -> when_op_eol expr : { '$1', '$2' }.
113+
op_expr -> range_op_eol expr : { '$1', '$2' }.
115114
op_expr -> default_op expr : { '$1', '$2' }.
116115
op_expr -> colon_colon_op expr : { '$1', '$2' }.
117116
op_expr -> comp_op_eol expr : { '$1', '$2' }.
118117
op_expr -> arrow_op_eol expr : { '$1', '$2' }.
119118

120119
matched_op_expr -> match_op matched_expr : { '$1', '$2' }.
121120
matched_op_expr -> dual_op_eol matched_expr : { '$1', '$2' }.
122-
matched_op_expr -> mult_op matched_expr : { '$1', '$2' }.
121+
matched_op_expr -> mult_op_eol matched_expr : { '$1', '$2' }.
123122
matched_op_expr -> two_op matched_expr : { '$1', '$2' }.
124123
matched_op_expr -> and_op_eol matched_expr : { '$1', '$2' }.
125124
matched_op_expr -> or_op_eol matched_expr : { '$1', '$2' }.
126-
matched_op_expr -> three_op matched_expr : { '$1', '$2' }.
127-
matched_op_expr -> pipe_op matched_expr : { '$1', '$2' }.
125+
matched_op_expr -> pin_op_eol matched_expr : { '$1', '$2' }.
126+
matched_op_expr -> tail_op matched_expr : { '$1', '$2' }.
128127
matched_op_expr -> bin_concat_op matched_expr : { '$1', '$2' }.
129-
matched_op_expr -> in_op matched_expr : { '$1', '$2' }.
130-
matched_op_expr -> inc_op matched_expr : { '$1', '$2' }.
131-
matched_op_expr -> when_op matched_expr : { '$1', '$2' }.
132-
matched_op_expr -> range_op matched_expr : { '$1', '$2' }.
128+
matched_op_expr -> in_op_eol matched_expr : { '$1', '$2' }.
129+
matched_op_expr -> inc_op_eol matched_expr : { '$1', '$2' }.
130+
matched_op_expr -> when_op_eol matched_expr : { '$1', '$2' }.
131+
matched_op_expr -> range_op_eol matched_expr : { '$1', '$2' }.
133132
matched_op_expr -> default_op matched_expr : { '$1', '$2' }.
134133
matched_op_expr -> colon_colon_op matched_expr : { '$1', '$2' }.
135134
matched_op_expr -> comp_op_eol matched_expr : { '$1', '$2' }.
@@ -259,10 +258,8 @@ close_curly -> eol '}' : '$2'.
259258
dual_op_eol -> dual_op : '$1'.
260259
dual_op_eol -> dual_op eol : '$1'.
261260

262-
mult_op -> '*' : '$1'.
263-
mult_op -> '/' : '$1'.
264-
mult_op -> '*' eol : '$1'.
265-
mult_op -> '/' eol : '$1'.
261+
mult_op_eol -> mult_op : '$1'.
262+
mult_op_eol -> mult_op eol : '$1'.
266263

267264
two_op -> '++' : '$1'.
268265
two_op -> '--' : '$1'.
@@ -271,8 +268,8 @@ two_op -> '--' eol : '$1'.
271268
two_op -> '**' : '$1'.
272269
two_op -> '**' eol : '$1'.
273270

274-
three_op -> '^^^' : '$1'.
275-
three_op -> '^^^' eol : '$1'.
271+
pin_op_eol -> pin_op : '$1'.
272+
pin_op_eol -> pin_op eol : '$1'.
276273

277274
default_op -> '//' : '$1'.
278275
default_op -> '//' eol : '$1'.
@@ -294,28 +291,26 @@ and_op_eol -> and_op eol : '$1'.
294291
or_op_eol -> or_op : '$1'.
295292
or_op_eol -> or_op eol : '$1'.
296293

297-
pipe_op -> '|' : '$1'.
298-
pipe_op -> '|' eol : '$1'.
294+
tail_op -> '|' : '$1'.
295+
tail_op -> '|' eol : '$1'.
299296

300297
bin_concat_op -> '<>' : '$1'.
301298
bin_concat_op -> '<>' eol : '$1'.
302299

303-
in_op -> 'in' : '$1'.
304-
in_op -> 'in' eol : '$1'.
300+
in_op_eol -> in_op : '$1'.
301+
in_op_eol -> in_op eol : '$1'.
305302

306-
inc_op -> 'inlist' : '$1'.
307-
inc_op -> 'inlist' eol : '$1'.
308-
inc_op -> 'inbits' : '$1'.
309-
inc_op -> 'inbits' eol : '$1'.
303+
inc_op_eol -> inc_op : '$1'.
304+
inc_op_eol -> inc_op eol : '$1'.
310305

311-
when_op -> 'when' : '$1'.
312-
when_op -> 'when' eol : '$1'.
306+
when_op_eol -> when_op : '$1'.
307+
when_op_eol -> when_op eol : '$1'.
313308

314309
stab_op -> '->' : '$1'.
315310
stab_op -> '->' eol : '$1'.
316311

317-
range_op -> '..' : '$1'.
318-
range_op -> '..' eol : '$1'.
312+
range_op_eol -> range_op : '$1'.
313+
range_op_eol -> range_op eol : '$1'.
319314

320315
at_op_eol -> at_op : '$1'.
321316
at_op_eol -> at_op eol : '$1'.

lib/elixir/src/elixir_tokenizer.erl

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
existing_atoms_only=false
1111
}).
1212

13-
-define(container2(T1, T2),
13+
-define(container(T1, T2),
1414
T1 == ${, T2 == $};
1515
T1 == $[, T2 == $]
1616
).
1717

18-
-define(op3(T1, T2, T3),
19-
T1 == $^, T2 == $^, T3 == $^
20-
).
21-
2218
-define(op2(T1, T2),
2319
T1 == $<, T2 == $>;
2420
T1 == $+, T2 == $+;
@@ -50,6 +46,13 @@
5046
-define(unary_op3(T1, T2, T3),
5147
T1 == $~, T2 == $~, T3 == $~).
5248

49+
-define(pin_op3(T1, T2, T3),
50+
T1 == $^, T2 == $^, T3 == $^
51+
).
52+
53+
-define(mult_op(T),
54+
T == $* orelse T == $/).
55+
5356
-define(dual_op(T),
5457
T == $+ orelse T == $-).
5558

@@ -88,6 +91,9 @@
8891
T1 == $<, T2 == $-;
8992
T1 == $|, T2 == $>).
9093

94+
-define(range_op(T1, T2),
95+
T1 == $., T2 == $.).
96+
9197
tokenize(String, Line, Opts) ->
9298
File = case lists:keyfind(file, 1, Opts) of
9399
{ file, V1 } -> V1;
@@ -230,29 +236,27 @@ tokenize([$.,T|Tail], Line, Scope, Tokens) when ?is_space(T) ->
230236
tokenize(".<<>>" ++ Rest, Line, Scope, Tokens) ->
231237
handle_call_identifier(Rest, Line, '<<>>', Scope, Tokens);
232238

233-
tokenize([$.,T1,T2|Rest], Line, Scope, Tokens) when ?container2(T1, T2) ->
239+
tokenize([$.,T1,T2|Rest], Line, Scope, Tokens) when ?container(T1, T2) ->
234240
handle_call_identifier(Rest, Line, list_to_atom([T1, T2]), Scope, Tokens);
235241

236242
% ## Three Token Operators
237243
tokenize([$.,T1,T2,T3|Rest], Line, Scope, Tokens) when
238244
?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) ->
240-
handle_call_identifier(Rest, Line, list_to_atom([T1, T2, T3]), Scope, Tokens);
241-
242-
tokenize([$.,T1,T2,T3|Rest], Line, Scope, Tokens) when ?op3(T1, T2, T3) ->
245+
?arrow_op3(T1, T2, T3); ?pin_op3(T1, T2, T3) ->
243246
handle_call_identifier(Rest, Line, list_to_atom([T1, T2, T3]), Scope, Tokens);
244247

245248
% ## Two Token Operators
246249
tokenize([$.,T1,T2|Rest], Line, Scope, Tokens) when
247-
?comp_op2(T1, T2); ?and_op(T1, T2); ?or_op(T1, T2); ?arrow_op(T1, T2) ->
250+
?comp_op2(T1, T2); ?and_op(T1, T2); ?or_op(T1, T2); ?arrow_op(T1, T2);
251+
?range_op(T1, T2) ->
248252
handle_call_identifier(Rest, Line, list_to_atom([T1, T2]), Scope, Tokens);
249253

250254
tokenize([$.,T1,T2|Rest], Line, Scope, Tokens) when ?op2(T1, T2) ->
251255
handle_call_identifier(Rest, Line, list_to_atom([T1, T2]), Scope, Tokens);
252256

253257
% ## Single Token Operators
254258
tokenize([$.,T|Rest], Line, Scope, Tokens) when
255-
?at_op(T); ?unary_op(T); ?dual_op(T); ?comp_op(T) ->
259+
?at_op(T); ?unary_op(T); ?dual_op(T); ?mult_op(T); ?comp_op(T) ->
256260
handle_call_identifier(Rest, Line, list_to_atom([T]), Scope, Tokens);
257261

258262
tokenize([$.,T|Rest], Line, Scope, Tokens) when ?op1(T); T == $& ->
@@ -316,29 +320,27 @@ tokenize(":..." ++ Rest, Line, Scope, Tokens) ->
316320
tokenize(":<<>>" ++ Rest, Line, Scope, Tokens) ->
317321
tokenize(Rest, Line, Scope, [{ atom, Line, '<<>>' }|Tokens]);
318322

319-
tokenize([$:,T1,T2|Rest], Line, Scope, Tokens) when ?container2(T1, T2) ->
323+
tokenize([$:,T1,T2|Rest], Line, Scope, Tokens) when ?container(T1, T2) ->
320324
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2]) }|Tokens]);
321325

322326
% ## Three Token Operators
323327
tokenize([$:,T1,T2,T3|Rest], Line, Scope, Tokens) when
324328
?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) ->
326-
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2,T3]) }|Tokens]);
327-
328-
tokenize([$:,T1,T2,T3|Rest], Line, Scope, Tokens) when ?op3(T1, T2, T3) ->
329+
?arrow_op3(T1, T2, T3); ?pin_op3(T1, T2, T3) ->
329330
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2,T3]) }|Tokens]);
330331

331332
% ## Two Token Operators
332333
tokenize([$:,T1,T2|Rest], Line, Scope, Tokens) when
333-
?comp_op2(T1, T2); ?and_op(T1, T2); ?or_op(T1, T2); ?arrow_op(T1, T2) ->
334+
?comp_op2(T1, T2); ?and_op(T1, T2); ?or_op(T1, T2); ?arrow_op(T1, T2);
335+
?range_op(T1, T2) ->
334336
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2]) }|Tokens]);
335337

336338
tokenize([$:,T1,T2|Rest], Line, Scope, Tokens) when ?op2(T1, T2) ->
337339
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T1,T2]) }|Tokens]);
338340

339341
% ## Single Token Operators
340342
tokenize([$:,T|Rest], Line, Scope, Tokens) when
341-
?at_op(T); ?unary_op(T); ?dual_op(T); ?comp_op(T) ->
343+
?at_op(T); ?unary_op(T); ?dual_op(T); ?mult_op(T); ?comp_op(T) ->
342344
tokenize(Rest, Line, Scope, [{ atom, Line, list_to_atom([T]) }|Tokens]);
343345

344346
tokenize([$:,T|Rest], Line, Scope, Tokens) when ?op1(T); T == $&; T == $. ->
@@ -386,8 +388,8 @@ tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?or_op3(T1, T2, T3) ->
386388
tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?arrow_op3(T1, T2, T3) ->
387389
handle_op(Rest, Line, arrow_op, list_to_atom([T1,T2,T3]), Scope, Tokens);
388390

389-
tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?op3(T1, T2, T3) ->
390-
handle_op(Rest, Line, list_to_atom([T1,T2,T3]), Scope, Tokens);
391+
tokenize([T1,T2,T3|Rest], Line, Scope, Tokens) when ?pin_op3(T1, T2, T3) ->
392+
handle_op(Rest, Line, pin_op, list_to_atom([T1,T2,T3]), Scope, Tokens);
391393

392394
% ## Containers + punctuation tokens
393395
tokenize([T,T|Rest], Line, Scope, Tokens) when T == $<; T == $> ->
@@ -400,6 +402,9 @@ tokenize([T|Rest], Line, Scope, Tokens) when T == $(;
400402
handle_terminator(Rest, Line, Scope, Token, Tokens);
401403

402404
% ## Two Token Operators
405+
tokenize([T1,T2|Rest], Line, Scope, Tokens) when ?range_op(T1, T2) ->
406+
handle_op(Rest, Line, range_op, list_to_atom([T1, T2]), Scope, Tokens);
407+
403408
tokenize([T1,T2|Rest], Line, Scope, Tokens) when ?arrow_op(T1, T2) ->
404409
handle_op(Rest, Line, arrow_op, list_to_atom([T1, T2]), Scope, Tokens);
405410

@@ -428,6 +433,9 @@ tokenize([T|Rest], Line, Scope, Tokens) when ?comp_op(T) ->
428433
tokenize([T|Rest], Line, Scope, Tokens) when ?dual_op(T) ->
429434
handle_unary_op(Rest, Line, dual_op, list_to_atom([T]), Scope, Tokens);
430435

436+
tokenize([T|Rest], Line, Scope, Tokens) when ?mult_op(T) ->
437+
handle_op(Rest, Line, mult_op, list_to_atom([T]), Scope, Tokens);
438+
431439
tokenize([T|Rest], Line, Scope, Tokens) when ?op1(T) ->
432440
handle_op(Rest, Line, list_to_atom([T]), Scope, Tokens);
433441

@@ -886,7 +894,6 @@ check_keyword(Line, Identifier, Atom, Tokens) when
886894
true -> { ok, [{ Atom, Line }|Tokens] };
887895
block -> { ok, [{ block_identifier, Line, Atom }|Tokens] };
888896
unary_op -> { ok, [{ unary_op, Line, Atom }|Tokens] };
889-
op -> { ok, add_token_with_nl({ Atom, Line }, Tokens) };
890897
Kind -> { ok, add_token_with_nl({ Kind, Line, Atom }, Tokens) }
891898
end;
892899

@@ -911,10 +918,10 @@ keyword('not') -> unary_op;
911918
keyword('and') -> and_op;
912919
keyword('or') -> or_op;
913920
keyword('xor') -> or_op;
914-
keyword('when') -> op;
915-
keyword('in') -> op;
916-
keyword('inlist') -> op;
917-
keyword('inbits') -> op;
921+
keyword('when') -> when_op;
922+
keyword('in') -> in_op;
923+
keyword('inlist') -> inc_op;
924+
keyword('inbits') -> inc_op;
918925

919926
% Block keywords
920927
keyword('after') -> block;

0 commit comments

Comments
 (0)