Skip to content

Commit

Permalink
Add \a escape sequence (#5864)
Browse files Browse the repository at this point in the history
  • Loading branch information
wooster0 authored and sdogruyol committed May 11, 2018
1 parent 0e64622 commit 1ecc65b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions spec/compiler/lexer/lexer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ describe "Lexer" do
it_lexes_number :i8, ["0i8", "0"]

it_lexes_char "'a'", 'a'
it_lexes_char "'\\a'", 7.chr
it_lexes_char "'\\b'", 8.chr
it_lexes_char "'\\n'", '\n'
it_lexes_char "'\\t'", '\t'
Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/lexer/lexer_string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe "Lexer string" do

tester.string_should_start_correctly
tester.next_token_should_be(:NEWLINE)
tester.next_string_token_should_be("abc")
tester.next_string_token_should_be("\abc")
tester.string_should_have_an_interpolation_of("foo")
tester.string_should_end_correctly
end
Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe "Parser" do
it_parses %(:"\\\\foo"), "\\foo".symbol
it_parses %(:"\\\"foo"), "\"foo".symbol
it_parses %(:"\\\"foo\\\""), "\"foo\"".symbol
it_parses %(:"\\b\\n\\r\\t\\v\\f\\e"), "\b\n\r\t\v\f\e".symbol
it_parses %(:"\\a\\b\\n\\r\\t\\v\\f\\e"), "\a\b\n\r\t\v\f\e".symbol
it_parses %(:"\\u{61}"), "a".symbol

it_parses "[1, 2]", ([1.int32, 2.int32] of ASTNode).array
Expand Down
1 change: 1 addition & 0 deletions spec/std/char_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ describe "Char" do
end

it "escapes" do
7.chr.ord.should eq(7) # TODO: use \a
'\b'.ord.should eq(8)
'\t'.ord.should eq(9)
'\n'.ord.should eq(10)
Expand Down
3 changes: 3 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,7 @@ describe "String" do
"a".dump.should eq %("a")
"\\".dump.should eq %("\\\\")
"\"".dump.should eq %("\\\"")
# TODO: "\a".dump.should eq %("\\a")
"\b".dump.should eq %("\\b")
"\e".dump.should eq %("\\e")
"\f".dump.should eq %("\\f")
Expand Down Expand Up @@ -1475,6 +1476,7 @@ describe "String" do
"a".inspect.should eq %("a")
"\\".inspect.should eq %("\\\\")
"\"".inspect.should eq %("\\\"")
# TODO: "\a".inspect.should eq %("\\a")
"\b".inspect.should eq %("\\b")
"\e".inspect.should eq %("\\e")
"\f".inspect.should eq %("\\f")
Expand Down Expand Up @@ -1653,6 +1655,7 @@ describe "String" do
end

it "escapes chars" do
# TODO: "\a"[0].should eq('\a')
"\b"[0].should eq('\b')
"\t"[0].should eq('\t')
"\n"[0].should eq('\n')
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ module Crystal
case char
when '\\'
case char = next_char
when 'a'
io << "\u{7}"
when 'b'
io << "\u{8}"
when 'n'
Expand Down Expand Up @@ -608,6 +610,8 @@ module Crystal
@token.value = '\\'
when '\''
@token.value = '\''
when 'a'
@token.value = 7.chr # TODO: use \a
when 'b'
@token.value = '\b'
when 'e'
Expand Down Expand Up @@ -1790,6 +1794,8 @@ module Crystal
end
else
case char = next_char
when 'a'
string_token_escape_value "\u{7}"
when 'b'
string_token_escape_value "\u{8}"
when 'n'
Expand Down
19 changes: 10 additions & 9 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3937,15 +3937,16 @@ class String
while reader.has_next?
current_char = reader.current_char
case current_char
when '"' then io << "\\\""
when '\\' then io << "\\\\"
when '\b' then io << "\\b"
when '\e' then io << "\\e"
when '\f' then io << "\\f"
when '\n' then io << "\\n"
when '\r' then io << "\\r"
when '\t' then io << "\\t"
when '\v' then io << "\\v"
when '"' then io << "\\\""
when '\\' then io << "\\\\"
when (7.chr) then io << "\\a" # TODO: use \a
when '\b' then io << "\\b"
when '\e' then io << "\\e"
when '\f' then io << "\\f"
when '\n' then io << "\\n"
when '\r' then io << "\\r"
when '\t' then io << "\\t"
when '\v' then io << "\\v"
when '#'
current_char = reader.next_char
if current_char == '{'
Expand Down

0 comments on commit 1ecc65b

Please sign in to comment.