From 4f5e33ef7b96d6daee29ff1088ea381b9302f846 Mon Sep 17 00:00:00 2001 From: Chimrod <> Date: Thu, 26 Oct 2023 17:16:36 +0200 Subject: Used the same state for the expression levels --- lib/qparser/lexer.ml | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'lib/qparser/lexer.ml') diff --git a/lib/qparser/lexer.ml b/lib/qparser/lexer.ml index 75072c6..abe47ac 100644 --- a/lib/qparser/lexer.ml +++ b/lib/qparser/lexer.ml @@ -11,6 +11,14 @@ exception EOF (* Extract the location name from the pattern *) let location_name = Str.regexp {|.* \(.*\)|} +(** Remove all the expression state when we are leaving the expression itself. *) +let rec leave_expression buffer = + match Lexbuf.state buffer with + | Some Lexbuf.Expression -> + Lexbuf.leave_state buffer; + leave_expression buffer + | _ -> () + (** Try to read the identifier and check if this is a function, a keyword, or just a variable. @@ -20,7 +28,9 @@ let build_ident buffer = try let value = Hashtbl.find Idents.keyword_table id in let _ = - match value with IF | ELIF -> Lexbuf.incr_level buffer | _ -> () + match value with + | IF | ELIF -> Lexbuf.enter_state buffer Lexbuf.Expression + | _ -> () in value with Not_found -> @@ -172,7 +182,7 @@ let rec token : Lexbuf.t -> token = LOCATION_START ident | '-', Plus '-', Star (Sub (any, ('\r' | '\n'))) -> - Lexbuf.reset_level buffer; + leave_expression buffer; LOCATION_END | Plus digit -> INTEGER (Lexbuf.content buffer) | '+' -> PLUS @@ -184,31 +194,38 @@ let rec token : Lexbuf.t -> token = | '*' -> STAR | ':' -> (* We are leaving the block, the comment will be handled again *) - Lexbuf.decr_level buffer; - + Lexbuf.leave_state buffer; COLUMN | '[' -> L_BRACKET | ']' -> R_BRACKET | '(' -> - Lexbuf.incr_level buffer; + Lexbuf.enter_state buffer Lexbuf.Expression; L_PAREN | ')' -> - Lexbuf.decr_level buffer; + Lexbuf.leave_state buffer; R_PAREN | '<' -> LT | '>' -> GT + | ">>" -> + Lexbuf.leave_state buffer; + token buffer | coma -> COMA | '=' -> - Lexbuf.incr_level buffer; + Lexbuf.enter_state buffer Lexbuf.Expression; + EQUAL | ident -> build_ident buffer | eol -> - Lexbuf.reset_level buffer; + leave_expression buffer; + EOL | '&' -> - Lexbuf.reset_level buffer; + leave_expression buffer; AMPERSAND - | '!' -> if Lexbuf.level buffer > 0 then EXCLAMATION else skip_comment buffer + | '!' -> ( + match Lexbuf.state buffer with + | Some Lexbuf.Expression -> EXCLAMATION + | _ -> skip_comment buffer) | spaces -> token buffer | '\'' -> Lexbuf.enter_state buffer Lexbuf.String; @@ -224,7 +241,6 @@ let rec token : Lexbuf.t -> token = | _ -> let tok = Lexbuf.content buffer in let msg = Format.asprintf "Unexpected character %S" tok in - raise @@ LexError msg let main buffer = @@ -249,7 +265,7 @@ let rec discard buffer = discard buffer | eof -> raise EOF | '-', Plus '-', Star (Sub (any, ('\r' | '\n'))) -> - Lexbuf.reset_level buffer; + leave_expression buffer; () | '!' -> ignore @@ skip_comment buffer; -- cgit v1.2.3