mirror of
https://github.com/LucasVbr/croissant.git
synced 2026-05-13 17:12:10 +00:00
fix: Handle escaped char
This commit is contained in:
+32
-12
@@ -2,10 +2,9 @@
|
|||||||
{
|
{
|
||||||
open Parser
|
open Parser
|
||||||
exception Error of char
|
exception Error of char
|
||||||
}
|
|
||||||
|
|
||||||
let line_comment = "//" [^'.']*'\n'
|
let buffer = Buffer.create 256
|
||||||
let block_comment = "/*" [^'.']* "*/"
|
}
|
||||||
|
|
||||||
let letter = ['a'-'z' 'A'-'Z']
|
let letter = ['a'-'z' 'A'-'Z']
|
||||||
let digit = ['0'-'9']
|
let digit = ['0'-'9']
|
||||||
@@ -15,16 +14,12 @@ let non_digit = '_'
|
|||||||
let identifier = letter (alphanum | non_digit)*
|
let identifier = letter (alphanum | non_digit)*
|
||||||
let integer = digit+
|
let integer = digit+
|
||||||
let float = digit* '.' digit+
|
let float = digit* '.' digit+
|
||||||
let char = "'" [^'.'] "'"
|
|
||||||
let string = '"' [^'.']* '"'
|
|
||||||
|
|
||||||
rule token = parse
|
rule token = parse
|
||||||
| ' ' | '\t' { token lexbuf }
|
| ' ' | '\t' { token lexbuf }
|
||||||
| '\n' | line_comment { Lexing.new_line lexbuf; token lexbuf }
|
| "/*" { block_comment lexbuf }
|
||||||
| block_comment as comment {
|
| "//" { line_comment lexbuf }
|
||||||
String.iter (fun c -> if c = '\n' then Lexing.new_line lexbuf) comment;
|
| '\n' { Lexing.new_line lexbuf; token lexbuf }
|
||||||
token lexbuf
|
|
||||||
}
|
|
||||||
|
|
||||||
| '+' { PLUS }
|
| '+' { PLUS }
|
||||||
| '-' { MINUS }
|
| '-' { MINUS }
|
||||||
@@ -62,9 +57,34 @@ rule token = parse
|
|||||||
|
|
||||||
| integer as lxm { INTEGER(int_of_string lxm) }
|
| integer as lxm { INTEGER(int_of_string lxm) }
|
||||||
| float as lxm { FLOAT(float_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) }
|
| identifier as lxm { IDENTIFIER(lxm) }
|
||||||
|
|
||||||
| eof { EOF }
|
| eof { EOF }
|
||||||
| _ as c { raise (Error c) }
|
| _ 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 }
|
||||||
+1
-1
@@ -16,7 +16,7 @@ let string_of_literal = function
|
|||||||
| Integer i -> "Integer(" ^ string_of_int i ^ ")"
|
| Integer i -> "Integer(" ^ string_of_int i ^ ")"
|
||||||
| Float f -> "Float(" ^ string_of_float f ^ ")"
|
| Float f -> "Float(" ^ string_of_float f ^ ")"
|
||||||
| Character c -> "Character('" ^ Char.escaped c ^ "')"
|
| Character c -> "Character('" ^ Char.escaped c ^ "')"
|
||||||
| String s -> "String(\"" ^ s ^ "\")"
|
| String s -> "String(\"" ^ String.escaped s ^ "\")"
|
||||||
| Boolean b -> "Boolean(" ^ string_of_bool b ^ ")"
|
| Boolean b -> "Boolean(" ^ string_of_bool b ^ ")"
|
||||||
| Null -> "Null"
|
| Null -> "Null"
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1 @@
|
|||||||
/*Hello, World!*/
|
var a : caractere = '';
|
||||||
|
|
||||||
var a : chaine = "Hello World";
|
|
||||||
Reference in New Issue
Block a user