-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enh(elixir) Improves and fixes many issues with Elixir grammar #3212
Changes from all commits
76a58ed
1385181
4d17178
740119b
5f78052
67c5e97
e929414
969b834
c90b2f4
729dec8
3605ef9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<span class="hljs-keyword">case</span> x <span class="hljs-keyword">do</span> | ||
<span class="hljs-number">1</span> -> <span class="hljs-symbol">:one</span> | ||
<span class="hljs-number">2</span> -> <span class="hljs-symbol">:two</span> | ||
_ -> <span class="hljs-symbol">:error</span> | ||
<span class="hljs-keyword">end</span> | ||
|
||
<span class="hljs-keyword">cond</span> <span class="hljs-keyword">do</span> | ||
x > <span class="hljs-number">30</span> -> <span class="hljs-symbol">:ok</span> | ||
y <= <span class="hljs-number">7</span> -> <span class="hljs-symbol">:maybe</span> | ||
z == <span class="hljs-symbol">:skip</span> -> <span class="hljs-symbol">:ok</span> | ||
<span class="hljs-literal">true</span> -> <span class="hljs-symbol">:error</span> | ||
<span class="hljs-keyword">end</span> | ||
|
||
<span class="hljs-keyword">if</span> x > <span class="hljs-number">4</span> <span class="hljs-keyword">do</span> | ||
<span class="hljs-symbol">:ok</span> | ||
<span class="hljs-keyword">end</span> | ||
|
||
<span class="hljs-keyword">if</span> x > <span class="hljs-number">4</span>, <span class="hljs-symbol">do:</span> <span class="hljs-symbol">:ok</span> | ||
|
||
<span class="hljs-keyword">if</span> x > <span class="hljs-number">4</span> <span class="hljs-keyword">do</span> | ||
<span class="hljs-symbol">:ok</span> | ||
<span class="hljs-keyword">else</span> | ||
<span class="hljs-symbol">:error</span> | ||
<span class="hljs-keyword">end</span> | ||
|
||
<span class="hljs-keyword">if</span> x > <span class="hljs-number">4</span>, <span class="hljs-symbol">do:</span> <span class="hljs-symbol">:ok</span>, <span class="hljs-symbol">else:</span> <span class="hljs-symbol">:error</span> | ||
|
||
<span class="hljs-keyword">unless</span> y < <span class="hljs-number">50</span> <span class="hljs-keyword">do</span> | ||
<span class="hljs-symbol">:error</span> | ||
<span class="hljs-keyword">end</span> | ||
|
||
<span class="hljs-keyword">unless</span> y < <span class="hljs-number">50</span>, <span class="hljs-symbol">do:</span> <span class="hljs-symbol">:error</span> | ||
|
||
<span class="hljs-keyword">unless</span> y < <span class="hljs-number">50</span> <span class="hljs-keyword">do</span> | ||
<span class="hljs-symbol">:error</span> | ||
<span class="hljs-keyword">else</span> | ||
<span class="hljs-symbol">:ok</span> | ||
<span class="hljs-keyword">end</span> | ||
|
||
<span class="hljs-keyword">unless</span> y < <span class="hljs-number">50</span>, <span class="hljs-symbol">do:</span> <span class="hljs-symbol">:error</span>, <span class="hljs-symbol">else:</span> <span class="hljs-symbol">:ok</span> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
case x do | ||
1 -> :one | ||
2 -> :two | ||
_ -> :error | ||
end | ||
|
||
cond do | ||
x > 30 -> :ok | ||
y <= 7 -> :maybe | ||
z == :skip -> :ok | ||
true -> :error | ||
end | ||
|
||
if x > 4 do | ||
:ok | ||
end | ||
|
||
if x > 4, do: :ok | ||
|
||
if x > 4 do | ||
:ok | ||
else | ||
:error | ||
end | ||
|
||
if x > 4, do: :ok, else: :error | ||
|
||
unless y < 50 do | ||
:error | ||
end | ||
|
||
unless y < 50, do: :error | ||
|
||
unless y < 50 do | ||
:error | ||
else | ||
:ok | ||
end | ||
|
||
unless y < 50, do: :error, else: :ok |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,10 @@ end | |
|
||
def f!, do: IO.puts "hello world" | ||
|
||
defp f?, do: true | ||
|
||
defmacro foo, do: :ok | ||
|
||
defmacrop do_foo, do: :ok | ||
|
||
x = 5 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">User</span></span> <span class="hljs-keyword">do</span> | ||
<span class="hljs-keyword">defstruct</span> [<span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:email</span>, <span class="hljs-symbol">age:</span> <span class="hljs-number">18</span>] | ||
<span class="hljs-keyword">end</span> | ||
|
||
<span class="hljs-class"><span class="hljs-keyword">defprotocol</span> <span class="hljs-title">Size</span></span> <span class="hljs-keyword">do</span> | ||
<span class="hljs-variable">@doc</span> <span class="hljs-string">"Calculates the size (and not the length!) of a data structure"</span> | ||
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">size</span></span>(data) | ||
<span class="hljs-keyword">end</span> | ||
|
||
<span class="hljs-class"><span class="hljs-keyword">defimpl</span> <span class="hljs-title">Size</span></span>, <span class="hljs-symbol">for:</span> Map <span class="hljs-keyword">do</span> | ||
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">size</span></span>(map), <span class="hljs-symbol">do:</span> map_size(map) | ||
<span class="hljs-keyword">end</span> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule User do | ||
defstruct [:name, :email, age: 18] | ||
end | ||
|
||
defprotocol Size do | ||
@doc "Calculates the size (and not the length!) of a data structure" | ||
def size(data) | ||
end | ||
|
||
defimpl Size, for: Map do | ||
def size(map), do: map_size(map) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
integer = <span class="hljs-number">1234</span> | ||
integer_with_leading_zero = <span class="hljs-number">01234</span> | ||
integer_zero = <span class="hljs-number">0</span> | ||
big_integer = <span class="hljs-number">1_234_000</span> | ||
neg_integer = <span class="hljs-number">-20_000</span> | ||
float = <span class="hljs-number">2.34</span> | ||
float_with_leading_zero = <span class="hljs-number">0.34</span> | ||
float_zero = <span class="hljs-number">0.0</span> | ||
sci_float = <span class="hljs-number">2.4e23</span> | ||
plus_sci_float = <span class="hljs-number">2.4e+23</span> | ||
small_sci_float = <span class="hljs-number">2.4e-23</span> | ||
cap_sci_float = <span class="hljs-number">2.4E23</span> | ||
binary = <span class="hljs-number">0b1010</span> | ||
binary_with_leading_zero = <span class="hljs-number">0b0010</span> | ||
strange_binary = <span class="hljs-number">0b1010_1010_1010</span> | ||
octal = <span class="hljs-number">0o777</span> | ||
octal_with_leading_zero = <span class="hljs-number">0o077</span> | ||
strange_octal = <span class="hljs-number">0o777_666_555</span> | ||
hex = <span class="hljs-number">0x1ABEF</span> | ||
hex_with_leading_zero = <span class="hljs-number">0x0ABEF</span> | ||
strange_hex = <span class="hljs-number">0x1234_FACE_987D</span> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
integer = 1234 | ||
integer_with_leading_zero = 01234 | ||
integer_zero = 0 | ||
big_integer = 1_234_000 | ||
neg_integer = -20_000 | ||
float = 2.34 | ||
float_with_leading_zero = 0.34 | ||
float_zero = 0.0 | ||
sci_float = 2.4e23 | ||
plus_sci_float = 2.4e+23 | ||
small_sci_float = 2.4e-23 | ||
cap_sci_float = 2.4E23 | ||
binary = 0b1010 | ||
binary_with_leading_zero = 0b0010 | ||
strange_binary = 0b1010_1010_1010 | ||
octal = 0o777 | ||
octal_with_leading_zero = 0o077 | ||
strange_octal = 0o777_666_555 | ||
hex = 0x1ABEF | ||
hex_with_leading_zero = 0x0ABEF | ||
strange_hex = 0x1234_FACE_987D |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
a = <span class="hljs-string">"""test"""</span> | ||
b = <span class="hljs-string">'''test'''</span> | ||
a = <span class="hljs-string">""" | ||
test | ||
"""</span> | ||
b = <span class="hljs-string">''' | ||
test | ||
'''</span> | ||
c = <span class="hljs-string">"test"</span> | ||
d = <span class="hljs-string">'test'</span> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
a = """test""" | ||
b = '''test''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see how these are better examples (that you added), but if the previous were valid we should also leave them to serve as additional regression tests... one can still use a multi-line string for a single line - that is valid grammar, yes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, those were syntax errors in Elixir. The only allowed characters after the opening My assumption is that it's not a big deal if the syntax highlighting colors something that doesn't compile so I didn't feel the need to change the regexes for multiline strings, but the test examples should be valid Elixir code, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Whoa. I stand corrected. ;-)
Right, it's not our job to detect invalid code.
For the most part, yes. In this particular case this indeed makes sense. I just see people "swap" test cases often when they should be adding new cases instead, [not removing old ones]. Thought this was perhaps yet another case of that. :) |
||
a = """ | ||
test | ||
""" | ||
b = ''' | ||
test | ||
''' | ||
c = "test" | ||
d = 'test' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming the id should be the PR id, not the issue id?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep