From 6df363c78d1ca5ee6822ce2bc0cbe5ed3a371216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luc=C3=A0s?= <86352901+LucasVbr@users.noreply.github.com> Date: Fri, 5 Jul 2024 01:35:03 +0200 Subject: [PATCH] fix: Handle escaped char --- lib/ast/lexer.mll | 46 +++++++++++++++++++++++++++++------------ lib/ast/print.ml | 2 +- test/ressources/test.🥐 | 4 +--- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/lib/ast/lexer.mll b/lib/ast/lexer.mll index e63cbb8..f030599 100644 --- a/lib/ast/lexer.mll +++ b/lib/ast/lexer.mll @@ -2,10 +2,9 @@ { open Parser exception Error of char -} -let line_comment = "//" [^'.']*'\n' - let block_comment = "/*" [^'.']* "*/" + let buffer = Buffer.create 256 +} let letter = ['a'-'z' 'A'-'Z'] let digit = ['0'-'9'] @@ -15,16 +14,12 @@ let non_digit = '_' let identifier = letter (alphanum | non_digit)* let integer = digit+ let float = digit* '.' digit+ -let char = "'" [^'.'] "'" -let string = '"' [^'.']* '"' rule token = parse | ' ' | '\t' { token lexbuf } - | '\n' | line_comment { Lexing.new_line lexbuf; token lexbuf } - | block_comment as comment { - String.iter (fun c -> if c = '\n' then Lexing.new_line lexbuf) comment; - token lexbuf - } + | "/*" { block_comment lexbuf } + | "//" { line_comment lexbuf } + | '\n' { Lexing.new_line lexbuf; token lexbuf } | '+' { PLUS } | '-' { MINUS } @@ -62,9 +57,34 @@ rule token = parse | integer as lxm { INTEGER(int_of_string lxm) } | float as lxm { FLOAT(float_of_string lxm) } - | char as lxm { CHARACTER(lxm.[1]) } - | string as lxm { STRING(String.sub lxm 1 (String.length lxm - 2)) } + + | "'\\''" { CHARACTER '\'' } + | "'\\n'" { CHARACTER '\n' } + | "'\\t'" { CHARACTER '\t' } + | "'\\\\'" { CHARACTER '\\' } + | "'\\r'" { CHARACTER '\r' } + | "'\\b'" { CHARACTER '\b' } + | "'" [^'\\'] "'" { CHARACTER (String.get (Lexing.lexeme lexbuf) 1) } + + | "\"" { Buffer.clear buffer; string lexbuf } | identifier as lxm { IDENTIFIER(lxm) } | eof { EOF } - | _ as c { raise (Error c) } \ No newline at end of file + | _ as c { raise (Error c) } + +and string = parse + | "\"" { STRING(Buffer.contents buffer)} + | "\\\"" { Buffer.add_char buffer '"'; string lexbuf } + | '\\' { Buffer.add_char buffer '\\'; string lexbuf } + | _ as c { Buffer.add_char buffer c; string lexbuf } + +and line_comment = parse + | '\n' { Lexing.new_line lexbuf; token lexbuf } + | eof { EOF } + | _ { line_comment lexbuf } + +and block_comment = parse + | "*/" { token lexbuf } + | '\n' { Lexing.new_line lexbuf; block_comment lexbuf } + | eof { EOF } + | _ { block_comment lexbuf } \ No newline at end of file diff --git a/lib/ast/print.ml b/lib/ast/print.ml index 578ca96..31c885b 100644 --- a/lib/ast/print.ml +++ b/lib/ast/print.ml @@ -16,7 +16,7 @@ let string_of_literal = function | Integer i -> "Integer(" ^ string_of_int i ^ ")" | Float f -> "Float(" ^ string_of_float f ^ ")" | Character c -> "Character('" ^ Char.escaped c ^ "')" - | String s -> "String(\"" ^ s ^ "\")" + | String s -> "String(\"" ^ String.escaped s ^ "\")" | Boolean b -> "Boolean(" ^ string_of_bool b ^ ")" | Null -> "Null" diff --git a/test/ressources/test.🥐 b/test/ressources/test.🥐 index 8b2c66d..2e75362 100644 --- a/test/ressources/test.🥐 +++ b/test/ressources/test.🥐 @@ -1,3 +1 @@ -/*Hello, World!*/ - -var a : chaine = "Hello World"; \ No newline at end of file +var a : caractere = ''; \ No newline at end of file