Skip to content

Commit

Permalink
Support decimal codes in string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
tjammer committed Aug 24, 2024
1 parent 76f1273 commit 7e1eb98
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ rule read =
{ let v = decimal_code c d u in
if v > 255 then
raise (SyntaxError
(Printf.sprintf "illegal escape sequence \\%c%c%c" c d u))
(Printf.sprintf "Illegal escape sequence \\%c%c%c" c d u))
else
U8 (Char.chr v) }
| "'" '\\' 'x'
Expand Down Expand Up @@ -216,6 +216,14 @@ and read_string buf =
| '\\' 'r' { Buffer.add_char buf '\r'; read_string buf lexbuf }
| '\\' 't' { Buffer.add_char buf '\t'; read_string buf lexbuf }
| '\\' '"' { Buffer.add_char buf '"'; read_string buf lexbuf }
| '\\' (['0'-'9'] as c) (['0'-'9'] as d) (['0'-'9'] as u)
{ let v = decimal_code c d u in
if v > 255 then
raise (SyntaxError (Printf.sprintf
"Illegal backslash escape in string: '\\%c%c%c'" c d u))
else
Buffer.add_char buf (Char.chr v);
read_string buf lexbuf }
| [^ '"' '\\' '\n' '\r']+
{ Buffer.add_string buf (Lexing.lexeme lexbuf);
read_string buf lexbuf
Expand Down
5 changes: 5 additions & 0 deletions test/typing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ let test_const_i32 () = test "i32" "let a = 123i32\na"
let test_const_neg_i32 () = test "i32" "let a = -123i32\na"
let test_const_f32 () = test "f32" "let a = 1.0f32\na"
let test_const_neg_f32 () = test "f32" "let a = -1.0f32\na"

let test_const_string_ansi () =
test "string/t" "let s = \"\\027[2K\ram idling: \"\ns"

let test_hint_int () = test "int" "let a : int = 1\na"
let test_func_id () = test "('a) -> 'a" "fun (a): copy(a)"
let test_func_id_hint () = test "(int) -> int" "fun (a : int): a"
Expand Down Expand Up @@ -1426,6 +1430,7 @@ let () =
case "-i32" test_const_neg_i32;
case "f32" test_const_f32;
case "-f32" test_const_neg_f32;
case "string ansi" test_const_string_ansi;
] );
("hints", [ case "int" test_hint_int ]);
( "funcs",
Expand Down

0 comments on commit 7e1eb98

Please sign in to comment.