Skip to content

Commit d55f8e6

Browse files
committed
Some tests for brackets syntax.
1 parent 83976fe commit d55f8e6

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

lib/list.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ object List
1919
Erlang.lists.foldr(function, acc, self)
2020
end
2121

22+
def [](number)
23+
Erlang.lists.nth(number + 1, self)
24+
end
25+
2226
% Calls the function once for each element in the list.
2327
%
2428
% Returns a new list containing the values returned by the function.

src/elixir_parser.yrl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ np_method_call_expr -> without_args_method_call_expr : '$1'.
175175
without_args_method_call_expr -> np_call_exprs dot_eol method_name : build_method_call('$1', '$3', []).
176176

177177
% Brackets call
178-
brackets_expr -> call_exprs open_bracket comma_expr close_bracket : build_method_call('$1', '[]', '$3').
178+
brackets_expr -> call_exprs open_bracket comma_expr close_bracket : build_method_call('$1', { identifier, ?line('$2'), '[]' }, '$3').
179179
brackets_expr -> call_exprs : '$1'.
180180

181181
% Calls with parens
@@ -226,7 +226,7 @@ _np_method_call_expr -> _without_args_method_call_expr : '$1'.
226226
_without_args_method_call_expr -> _np_call_exprs dot_eol method_name : build_method_call('$1', '$3', []).
227227

228228
% Brackets call
229-
_brackets_expr -> _call_exprs open_bracket comma_expr close_bracket : build_method_call('$1', '[]', '$3').
229+
_brackets_expr -> _call_exprs open_bracket comma_expr close_bracket : build_method_call('$1', { identifier, ?line('$2'), '[]' }, '$3').
230230
_brackets_expr -> _call_exprs : '$1'.
231231

232232
% Calls with parens
@@ -530,10 +530,11 @@ method_name -> method_ops_identifier : { identifier, ?line('$1'), ?op('$1') }.
530530
implicit_method_ops_identifier -> div : '$1'.
531531
implicit_method_ops_identifier -> rem : '$1'.
532532

533-
method_ops_identifier -> '+' : '$1'.
534-
method_ops_identifier -> '-' : '$1'.
535-
method_ops_identifier -> '*' : '$1'.
536-
method_ops_identifier -> '/' : '$1'.
533+
method_ops_identifier -> '+' : '$1'.
534+
method_ops_identifier -> '-' : '$1'.
535+
method_ops_identifier -> '*' : '$1'.
536+
method_ops_identifier -> '/' : '$1'.
537+
method_ops_identifier -> '[' ']' : { '[]', ?line('$1') }.
537538

538539
Erlang code.
539540

test/elixir/list_test.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,16 @@ object ListTest
6666
[8,6,4,9] = x.delete(6)
6767
end
6868
69+
def brackets_syntax_test
70+
2 = [1,2,3][1]
71+
2 = a_list()[1]
72+
2 = self.a_list()[1]
73+
2 = ([1] + [2,3])[1]
74+
end
75+
76+
protected
77+
78+
def a_list
79+
[1,2,3]
80+
end
6981
end

0 commit comments

Comments
 (0)