Skip to content

Commit

Permalink
Added the missing \b escape sequence in chars and strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Ary Borenszweig committed Oct 20, 2014
1 parent c1df6ce commit 4f462ba
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 2 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 @@ -186,6 +186,7 @@ describe "Lexer" do
it_lexes_u64 ["9223372036854775808", "-9223372036854775809"]

it_lexes_char "'a'", 'a'
it_lexes_char "'\\b'", 8.chr
it_lexes_char "'\\n'", '\n'
it_lexes_char "'\\t'", '\t'
it_lexes_char "'\\v'", '\v'
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 @@ -45,6 +45,7 @@ describe "Char" do
end

it "escapes" do
# TODO '\b'.ord.should eq(8)
'\t'.ord.should eq(9)
'\n'.ord.should eq(10)
'\v'.ord.should eq(11)
Expand Down
2 changes: 1 addition & 1 deletion spec/std/json/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe "Json serialization" do
end

it "does for String with control codes" do
"\b".to_json.should eq("\"\\b\"")
# TODO "\b".to_json.should eq("\"\\b\"")
"\f".to_json.should eq("\"\\f\"")
"\n".to_json.should eq("\"\\n\"")
"\r".to_json.should eq("\"\\r\"")
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 @@ -409,6 +409,7 @@ describe "String" do
"a".dump.should eq("\"a\"")
"\\".dump.should eq("\"\\\\\"")
"\"".dump.should eq("\"\\\"\"")
# TODO "\b".dump.should eq("\"\\b\"")
"\e".dump.should eq("\"\\e\"")
"\f".dump.should eq("\"\\f\"")
"\n".dump.should eq("\"\\n\"")
Expand All @@ -424,6 +425,7 @@ describe "String" do
"a".inspect.should eq("\"a\"")
"\\".inspect.should eq("\"\\\\\"")
"\"".inspect.should eq("\"\\\"\"")
# TODO "\b".inspect.should eq("\"\\b\"")
"\e".inspect.should eq("\"\\e\"")
"\f".inspect.should eq("\"\\f\"")
"\n".inspect.should eq("\"\\n\"")
Expand Down Expand Up @@ -486,6 +488,7 @@ describe "String" do
end

it "escapes chars" do
# TODO "\b"[0].should eq('\b')
"\t"[0].should eq('\t')
"\n"[0].should eq('\n')
"\v"[0].should eq('\v')
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ module Crystal
case char1 = next_char
when '\\'
case char2 = next_char
when 'b'
@token.value = 8.chr # TODO use \b
when 'e'
@token.value = '\e'
when 'f'
Expand Down Expand Up @@ -1375,6 +1377,8 @@ module Crystal
@token.value = "\\#{char}"
else
case char = next_char
when 'b'
string_token_escape_value "\u{8}"
when 'n'
string_token_escape_value "\n"
when 'r'
Expand Down
2 changes: 1 addition & 1 deletion src/json/to_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class String
io << "\\\\"
when '"'
io << "\\\""
when '\b'
when 8.chr # TODO use '\b'
io << "\\b"
when '\f'
io << "\\f"
Expand Down
1 change: 1 addition & 0 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ class String
case current_char
when '"' then io << "\\\""
when '\\' then io << "\\\\"
when (8.chr) then io << "\\b" # TODO use \b in when
when '\e' then io << "\\e"
when '\f' then io << "\\f"
when '\n' then io << "\\n"
Expand Down

0 comments on commit 4f462ba

Please sign in to comment.