Skip to content

Commit

Permalink
add 'unclosed annotation', 'unclosed comment' and 'unclosed string'
Browse files Browse the repository at this point in the history
  • Loading branch information
Laplace-Demon committed Aug 21, 2024
1 parent 87c666f commit 62eb6e8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/bin/owi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> (
Expand Down
3 changes: 3 additions & 0 deletions src/parser/parse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions src/parser/text_lexer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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]

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down
10 changes: 8 additions & 2 deletions src/parser/text_lexer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/utils/result.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
3 changes: 3 additions & 0 deletions src/utils/result.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 62eb6e8

Please sign in to comment.