recup_char should work?

This commit is contained in:
Mylloon 2023-10-23 19:09:56 +02:00
parent 144b0bdc75
commit ca5b578afa
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -19,29 +19,26 @@
error msg (lex_join lexbuf.lex_start_p lexbuf.lex_curr_p)
(*Fonction qui va convertir une string de caractère ascii en vrai caractère.
On en a besoin pour certains test *)
(*)
let recup_char charac lexbuf =
let taille = String.length charac in
match charac with
| "\\n" -> '\n'
| "\\r" -> '\r'
| "\\t" -> '\t'
| "\\\'" -> '\''
| "\\\\" -> '\\'
| _ ->
(let s2 = String.get charac 1 in
if s2 = '0' || s2 = '1' || s2 = '2' then
(let s=String.sub charac 1 (taille - 1) in
let i = int_of_string s in
Char.chr i)
else error lexbuf )
*)
(* Fonction qui convertie une chaîne de caractère ascii en vrai caractère.
* Notamment les escapes : "\n" ou "\000" *)
let recup_char charac lexbuf =
let taille = String.length charac in
match charac with
| "\\n" -> Some '\n'
| "\\r" -> Some '\r'
| "\\t" -> Some '\t'
| "\\\'" -> Some '\''
| "\\\\" -> Some '\\'
| _ -> (
let s2 = String.get charac 1 in
if s2 = '0' || s2 = '1' || s2 = '2' then (
let s = String.sub charac 1 (taille - 1) in
let i = int_of_string s in
Some (Char.chr i))
else None )
}
let newline = ('\010' | '\013' | "\013\010")
let blank = [' ' '\009' '\012']
@ -141,9 +138,9 @@ rule token = parse
| ">?" { SUP_OP }
(** Identificateurs *)
| ident as s { ID s }
| type_variable as s { TID s }
| constr_id as s { CID s }
| ident as s { ID s }
| type_variable as s { TID s }
| constr_id as s { CID s }
(* Integers *)
| int as i { INT (Mint.of_string i) }
@ -152,17 +149,16 @@ rule token = parse
| '"' { read_string (Buffer.create 16) lexbuf }
(* Characters *)
| "'" (letter as c) "'" { CHAR c }
(*| "'" (atom as c) "'" {CHAR (recup_char c lexbuf )} (* On retire le \ du début
* TODO: fix *)
(*)
let code = int_of_string (String.sub c 1 ((String.length c) - 2))
in CHAR (Char.chr (code)) } *) *)
| "'" (letter as c) "'" { CHAR c }
| "'" (atom as a) "'" { match recup_char a lexbuf with
| Some c -> CHAR c
| None -> error lexbuf None "" }
(** Lexing error *)
(* erreur qui advient pour le test 22-char-literal, le code renvoie bizarrement que "Error (during lexing) "*)
(* erreur qui advient pour le test 22-char-literal,
* le code renvoie bizarrement que "Error (during lexing)" *)
| "'" ascii_trop_grand "'" { error lexbuf None "" }
| _ as _c { error lexbuf None (* (Some _c) *) "unexpected character." }
| _ as _c { error lexbuf None (* (Some _c) *) "unexpected character." }
(* TODO: Gérer les imbrications de commentaires *)
and commentary = parse