From d96e1839eb800bf26bcc38272072d98af69f5d83 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Tue, 26 Dec 2023 10:08:23 -0600 Subject: Support const keyword --- lib/lexer.mll | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib/lexer.mll') diff --git a/lib/lexer.mll b/lib/lexer.mll index e0c5453..6e9b975 100644 --- a/lib/lexer.mll +++ b/lib/lexer.mll @@ -23,6 +23,7 @@ rule read = | "_int" { INTSYMBOL } | "(" { LBRACE } | ")" { RBRACE } + | '"' { read_string (Buffer.create 20) lexbuf } | int { INT (Lexing.lexeme lexbuf |> int_of_string) } | white { read lexbuf } | ":" { COLON } @@ -32,3 +33,20 @@ rule read = | "." { DOT } | eof { EOF } | _ as c { failwith (Printf.sprintf "unexpected character: %C" c) } + +and read_string buf = + parse + | '"' { STRING (Buffer.contents buf) } + | '\\' '/' { Buffer.add_char buf '/'; read_string buf lexbuf } + | '\\' '\\' { Buffer.add_char buf '\\'; read_string buf lexbuf } + | '\\' 'b' { Buffer.add_char buf '\b'; read_string buf lexbuf } + | '\\' 'f' { Buffer.add_char buf '\012'; read_string buf lexbuf } + | '\\' 'n' { Buffer.add_char buf '\n'; read_string buf lexbuf } + | '\\' 'r' { Buffer.add_char buf '\r'; read_string buf lexbuf } + | '\\' 't' { Buffer.add_char buf '\t'; read_string buf lexbuf } + | [^ '"' '\\']+ + { Buffer.add_string buf (Lexing.lexeme lexbuf); + read_string buf lexbuf + } + | _ { raise (SyntaxError ("Illegal string character: " ^ Lexing.lexeme lexbuf)) } + | eof { raise (SyntaxError ("String is not terminated")) } -- cgit v1.2.3