Skip to content

Commit

Permalink
Compiler: Add symbol literals support equal sign = as last char (#5969
Browse files Browse the repository at this point in the history
)

* Compiler: Add symbol literals support equal sign `=` as last char

* fixup! Compiler: Add symbol literals support equal sign `=` as last char
straight-shoota authored and RX14 committed Apr 22, 2018
1 parent 2f2c04f commit d6de698
Showing 3 changed files with 39 additions and 2 deletions.
38 changes: 37 additions & 1 deletion spec/compiler/lexer/lexer_spec.cr
Original file line number Diff line number Diff line change
@@ -255,7 +255,7 @@ describe "Lexer" do
it_lexes_instance_var "@foo"
it_lexes_class_var "@@foo"
it_lexes_globals ["$foo", "$FOO", "$_foo", "$foo123"]
it_lexes_symbols [":foo", ":foo!", ":foo?", ":\"foo\"", ":かたな", ":+", ":-", ":*", ":/",
it_lexes_symbols [":foo", ":foo!", ":foo?", ":foo=", ":\"foo\"", ":かたな", ":+", ":-", ":*", ":/",
":==", ":<", ":<=", ":>", ":>=", ":!", ":!=", ":=~", ":!~", ":&", ":|",
":^", ":~", ":**", ":>>", ":<<", ":%", ":[]", ":[]?", ":[]=", ":<=>", ":===",
]
@@ -452,6 +452,42 @@ describe "Lexer" do
token.value.should eq("\\")
end

it "lexes symbol followed by !=" do
lexer = Lexer.new ":a!=:a"
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
token = lexer.next_token
token.type.should eq(:"!=")
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
end

it "lexes symbol followed by ==" do
lexer = Lexer.new ":a==:a"
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
token = lexer.next_token
token.type.should eq(:"==")
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
end

it "lexes symbol followed by ===" do
lexer = Lexer.new ":a===:a"
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
token = lexer.next_token
token.type.should eq(:"===")
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
end

it "lexes /=" do
lexer = Lexer.new("/=")
lexer.slash_is_regex = false
1 change: 1 addition & 0 deletions spec/std/object_spec.cr
Original file line number Diff line number Diff line change
@@ -99,6 +99,7 @@ private class TestObject
end

private class DelegatedTestObject
# TODO: Replace with `:property1=` when v > 0.24.2
delegate "property1=", to: @test_object

def initialize(@test_object : TestObject)
2 changes: 1 addition & 1 deletion src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
@@ -540,7 +540,7 @@ module Crystal
while ident_part?(next_char)
# Nothing to do
end
if current_char == '!' || current_char == '?'
if current_char == '?' || ((current_char == '!' || current_char == '=') && peek_next_char != '=')
next_char
end
@token.type = :SYMBOL

0 comments on commit d6de698

Please sign in to comment.