diff --git a/src/bin/owi.ml b/src/bin/owi.ml index 68e357a2d..acdf51db4 100644 --- a/src/bin/owi.ml +++ b/src/bin/owi.ml @@ -376,6 +376,9 @@ let exit_code = | `Contract_unknown_func _id -> 71 | `Empty_annotation_id -> 72 | `Empty_identifier -> 73 + | `Unclosed_annotation -> 74 + | `Unclosed_comment -> 75 + | `Unclosed_string -> 76 end end | Error e -> ( diff --git a/src/parser/parse.ml b/src/parser/parse.ml index 860c29860..ffcf92da6 100644 --- a/src/parser/parse.ml +++ b/src/parser/parse.ml @@ -340,6 +340,9 @@ struct | Text_lexer.Empty_identifier -> Error `Empty_identifier | Text_lexer.Illegal_escape msg -> Error (`Illegal_escape msg) | Text_lexer.Illegal_character msg -> Error (`Lexer_illegal_character msg) + | Text_lexer.Unclosed_annotation -> Error `Unclosed_annotation + | Text_lexer.Unclosed_comment -> Error `Unclosed_comment + | Text_lexer.Unclosed_string -> Error `Unclosed_string | Text_lexer.Unknown_operator msg -> Error (`Lexer_unknown_operator msg) | Text_parser.Error -> let tok = Text_lexer.token buf |> token_to_string in diff --git a/src/parser/text_lexer.ml b/src/parser/text_lexer.ml index 5c7ec4caa..8d5d280f5 100644 --- a/src/parser/text_lexer.ml +++ b/src/parser/text_lexer.ml @@ -13,6 +13,12 @@ exception Illegal_character of string exception Illegal_escape of string +exception Unclosed_annotation + +exception Unclosed_comment + +exception Unclosed_string + exception Unknown_operator of string let illegal_character buf = @@ -111,10 +117,12 @@ let id_char = | '!' | '#' | '$' | '%' | '&' | '\'' | '*' | '+' | '-' | '.' | '/' | ':' | '<' | '=' | '>' | '?' | '@' | '\\' | '^' | '_' | '`' | '|' | '~' )] -let name = [%sedlex.regexp? "\"", Star (Sub (any, "\"") | "\\\""), "\""] +let string_elem = [%sedlex.regexp? Sub (any, "\"") | "\\\""] + +let name = [%sedlex.regexp? "\"", Star string_elem, "\""] let operator = - [%sedlex.regexp? Plus ('0' .. '9' | 'a' .. 'z' | '.' | '_' | ':'), Star name] + [%sedlex.regexp? 'a' .. 'z', Plus ('0' .. '9' | 'a' .. 'z' | '.' | '_' | ':')] let id = [%sedlex.regexp? "$", Plus id_char] @@ -126,7 +134,7 @@ let bad_num = [%sedlex.regexp? num, Plus id] let annot_atom = [%sedlex.regexp? - num | Plus (id_char | name) | ',' | ';' | '[' | ']' | '{' | '}'] + Plus id_char | num | name | ',' | ';' | '[' | ']' | '{' | '}'] let keywords = let tbl = Hashtbl.create 512 in @@ -481,6 +489,7 @@ let rec token buf = let name = String.sub name 1 (String.length name - 2) in let name = mk_string buf name in NAME name + | "\"", Star string_elem -> raise Unclosed_string | eof -> EOF (* | "" -> EOF *) | any -> unknown_operator buf @@ -492,14 +501,14 @@ and comment buf = | "(;" -> comment buf; comment buf - | eof -> Log.err "eof in comment" + | eof -> raise Unclosed_comment | any -> comment buf | _ -> assert false and single_comment buf = match%sedlex buf with | newline -> () - | eof -> Log.err "eof in single line comment" + | eof -> raise Unclosed_comment | any -> single_comment buf | _ -> assert false @@ -519,7 +528,8 @@ and annot buf = | annot_atom -> let annot_atom = Utf8.lexeme buf in Sexp.Atom annot_atom :: annot buf - | eof -> Log.err "eof in annotation" + | "\"", Star string_elem -> raise Unclosed_string + | eof -> raise Unclosed_annotation | any -> illegal_character buf | _ -> illegal_character buf diff --git a/src/parser/text_lexer.mli b/src/parser/text_lexer.mli index 9a6f6aef0..ead822180 100644 --- a/src/parser/text_lexer.mli +++ b/src/parser/text_lexer.mli @@ -9,11 +9,17 @@ exception Empty_annotation_id exception Empty_identifier +exception Illegal_character of string + exception Illegal_escape of string -exception Unknown_operator of string +exception Unclosed_annotation -exception Illegal_character of string +exception Unclosed_comment + +exception Unclosed_string + +exception Unknown_operator of string (** tokenizer *) val token : Sedlexing.lexbuf -> Text_parser.token diff --git a/src/utils/result.ml b/src/utils/result.ml index a81d7c832..ab2d57024 100644 --- a/src/utils/result.ml +++ b/src/utils/result.ml @@ -78,6 +78,9 @@ type err = | `Contract_unknown_func of Types.text Types.indice | `Empty_annotation_id | `Empty_identifier + | `Unclosed_annotation + | `Unclosed_comment + | `Unclosed_string ] type 'a t = ('a, err) Prelude.Result.t @@ -169,3 +172,6 @@ let rec err_to_string = function Fmt.str "contract: unknown function %a" Types.pp_indice id | `Empty_annotation_id -> Fmt.str "empty annotation id" | `Empty_identifier -> Fmt.str "empty identifier" + | `Unclosed_annotation -> Fmt.str "unclosed annotation" + | `Unclosed_comment -> Fmt.str "unclosed comment" + | `Unclosed_string -> Fmt.str "unclosed string" diff --git a/src/utils/result.mli b/src/utils/result.mli index b25aaeb5d..90f63cf8c 100644 --- a/src/utils/result.mli +++ b/src/utils/result.mli @@ -78,6 +78,9 @@ type err = | `Contract_unknown_func of Types.text Types.indice | `Empty_annotation_id | `Empty_identifier + | `Unclosed_annotation + | `Unclosed_comment + | `Unclosed_string ] type 'a t = ('a, err) Prelude.Result.t