From 24f067b69ef2cba8bf2b8134f1ef6c0c8186d6cd Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Mon, 23 Dec 2019 20:37:48 +0100 Subject: [PATCH] Added nat and bytes to ReasonLIGO and CameLIGO (expressions). Use the correct ParserAPI for ReasonLIGO now. --- src/passes/1-parser/cameligo/ParErr.ml | 206 ++++---- src/passes/1-parser/cameligo/Parser.mly | 1 + src/passes/1-parser/cameligo/ParserAPI.ml | 2 - src/passes/1-parser/reasonligo/ParErr.ml | 510 +++++++++++++++++++ src/passes/1-parser/reasonligo/Parser.mly | 9 +- src/passes/1-parser/reasonligo/ParserAPI.ml | 53 +- src/passes/1-parser/reasonligo/ParserAPI.mli | 20 +- 7 files changed, 687 insertions(+), 114 deletions(-) create mode 100644 src/passes/1-parser/reasonligo/ParErr.ml diff --git a/src/passes/1-parser/cameligo/ParErr.ml b/src/passes/1-parser/cameligo/ParErr.ml index 98dd3439d..7debe48ef 100644 --- a/src/passes/1-parser/cameligo/ParErr.ml +++ b/src/passes/1-parser/cameligo/ParErr.ml @@ -46,7 +46,7 @@ let message = "\n" | 33 -> "\n" - | 459 -> + | 460 -> "\n" | 27 -> "\n" @@ -68,9 +68,9 @@ let message = "\n" | 133 -> "\n" - | 372 -> + | 373 -> "\n" - | 374 -> + | 375 -> "\n" | 134 -> "\n" @@ -80,7 +80,7 @@ let message = "\n" | 153 -> "\n" - | 373 -> + | 374 -> "\n" | 63 -> "\n" @@ -144,85 +144,83 @@ let message = "\n" | 156 -> "\n" - | 462 -> + | 463 -> "\n" - | 464 -> + | 465 -> "\n" - | 216 -> - "\n" - | 241 -> - "\n" - | 218 -> - "\n" - | 220 -> - "\n" - | 214 -> - "\n" - | 225 -> - "\n" - | 254 -> - "\n" - | 255 -> + | 217 -> "\n" | 242 -> "\n" - | 263 -> + | 219 -> "\n" - | 227 -> + | 221 -> + "\n" + | 215 -> + "\n" + | 226 -> + "\n" + | 255 -> "\n" | 256 -> "\n" + | 243 -> + "\n" + | 264 -> + "\n" + | 228 -> + "\n" | 257 -> "\n" - | 265 -> - "\n" - | 267 -> - "\n" - | 269 -> - "\n" - | 271 -> - "\n" - | 273 -> - "\n" - | 192 -> - "\n" | 258 -> "\n" - | 284 -> + | 266 -> "\n" - | 287 -> + | 268 -> "\n" - | 244 -> + | 270 -> "\n" - | 292 -> + | 272 -> "\n" - | 261 -> + | 274 -> + "\n" + | 192 -> + "\n" + | 259 -> + "\n" + | 285 -> + "\n" + | 288 -> + "\n" + | 245 -> + "\n" + | 293 -> + "\n" + | 262 -> "\n" | 160 -> "\n" | 164 -> "\n" - | 428 -> + | 429 -> "\n" - | 331 -> + | 332 -> "\n" - | 312 -> + | 313 -> "\n" - | 430 -> - "\n" - | 314 -> + | 431 -> "\n" | 315 -> "\n" | 316 -> "\n" - | 431 -> + | 317 -> "\n" - | 444 -> + | 432 -> "\n" | 445 -> "\n" - | 432 -> + | 446 -> "\n" | 433 -> "\n" @@ -234,19 +232,19 @@ let message = "\n" | 437 -> "\n" - | 439 -> + | 438 -> "\n" - | 327 -> - "\n" - | 329 -> - "\n" - | 333 -> - "\n" - | 330 -> + | 440 -> "\n" | 328 -> "\n" - | 339 -> + | 330 -> + "\n" + | 334 -> + "\n" + | 331 -> + "\n" + | 329 -> "\n" | 340 -> "\n" @@ -258,23 +256,25 @@ let message = "\n" | 344 -> "\n" - | 366 -> - "\n" | 345 -> "\n" - | 347 -> + | 367 -> "\n" - | 440 -> + | 346 -> "\n" - | 442 -> + | 348 -> "\n" - | 446 -> + | 441 -> "\n" - | 429 -> + | 443 -> "\n" - | 311 -> + | 447 -> "\n" - | 427 -> + | 430 -> + "\n" + | 312 -> + "\n" + | 428 -> "\n" | 165 -> "\n" @@ -286,23 +286,23 @@ let message = "\n" | 163 -> "\n" - | 447 -> - "\n" - | 449 -> + | 448 -> "\n" | 450 -> "\n" - | 166 -> + | 451 -> "\n" - | 234 -> + | 166 -> "\n" | 235 -> "\n" - | 238 -> + | 236 -> "\n" | 239 -> "\n" - | 424 -> + | 240 -> + "\n" + | 425 -> "\n" | 170 -> "\n" @@ -310,31 +310,29 @@ let message = "\n" | 172 -> "\n" - | 417 -> - "\n" | 418 -> "\n" - | 421 -> + | 419 -> "\n" | 422 -> "\n" - | 174 -> + | 423 -> "\n" - | 303 -> + | 174 -> "\n" | 304 -> "\n" - | 404 -> - "\n" - | 411 -> - "\n" - | 403 -> - "\n" | 305 -> "\n" - | 307 -> + | 405 -> "\n" - | 319 -> + | 412 -> + "\n" + | 404 -> + "\n" + | 306 -> + "\n" + | 308 -> "\n" | 320 -> "\n" @@ -350,19 +348,19 @@ let message = "\n" | 326 -> "\n" - | 377 -> + | 327 -> "\n" | 378 -> "\n" - | 380 -> + | 379 -> "\n" - | 334 -> + | 381 -> "\n" - | 309 -> + | 335 -> "\n" - | 306 -> + | 310 -> "\n" - | 394 -> + | 307 -> "\n" | 395 -> "\n" @@ -374,11 +372,13 @@ let message = "\n" | 399 -> "\n" - | 407 -> - "\n" | 400 -> "\n" - | 402 -> + | 408 -> + "\n" + | 401 -> + "\n" + | 403 -> "\n" | 175 -> "\n" @@ -390,9 +390,9 @@ let message = "\n" | 183 -> "\n" - | 301 -> + | 302 -> "\n" - | 299 -> + | 300 -> "\n" | 185 -> "\n" @@ -404,13 +404,13 @@ let message = "\n" | 190 -> "\n" + | 195 -> + "\n" + | 214 -> + "\n" | 194 -> "\n" - | 213 -> - "\n" - | 193 -> - "\n" - | 209 -> + | 210 -> "\n" | _ -> raise Not_found diff --git a/src/passes/1-parser/cameligo/Parser.mly b/src/passes/1-parser/cameligo/Parser.mly index 8863da3f1..11f858752 100644 --- a/src/passes/1-parser/cameligo/Parser.mly +++ b/src/passes/1-parser/cameligo/Parser.mly @@ -566,6 +566,7 @@ core_expr: "" { EArith (Int $1) } | "" { EArith (Mutez $1) } | "" { EArith (Nat $1) } +| "" { EBytes $1 } | "" | module_field { EVar $1 } | projection { EProj $1 } | "" { EString (String $1) } diff --git a/src/passes/1-parser/cameligo/ParserAPI.ml b/src/passes/1-parser/cameligo/ParserAPI.ml index 412ae199f..0c78cdeec 100644 --- a/src/passes/1-parser/cameligo/ParserAPI.ml +++ b/src/passes/1-parser/cameligo/ParserAPI.ml @@ -75,8 +75,6 @@ module Make (Lexer: Lexer.S) (* The parser has suspended itself because of a syntax error. Stop. *) - (* let fail _checkpoint = raise Parser.Error *) - let failure get_win checkpoint = let message = ParErr.message (state checkpoint) in match get_win () with diff --git a/src/passes/1-parser/reasonligo/ParErr.ml b/src/passes/1-parser/reasonligo/ParErr.ml new file mode 100644 index 000000000..594f9ecd4 --- /dev/null +++ b/src/passes/1-parser/reasonligo/ParErr.ml @@ -0,0 +1,510 @@ + +(* This file was auto-generated based on "Parser.msg". *) + +(* Please note that the function [message] can raise [Not_found]. *) + +let message = + fun s -> + match s with + | 0 -> + "\n" + | 1 -> + "\n" + | 2 -> + "\n" + | 3 -> + "\n" + | 4 -> + "\n" + | 5 -> + "\n" + | 55 -> + "\n" + | 6 -> + "\n" + | 52 -> + "\n" + | 54 -> + "\n" + | 7 -> + "\n" + | 38 -> + "\n" + | 8 -> + "\n" + | 9 -> + "\n" + | 32 -> + "\n" + | 36 -> + "\n" + | 35 -> + "\n" + | 10 -> + "\n" + | 31 -> + "\n" + | 11 -> + "\n" + | 509 -> + "\n" + | 503 -> + "\n" + | 48 -> + "\n" + | 12 -> + "\n" + | 19 -> + "\n" + | 20 -> + "\n" + | 43 -> + "\n" + | 46 -> + "\n" + | 49 -> + "\n" + | 13 -> + "\n" + | 14 -> + "\n" + | 60 -> + "\n" + | 65 -> + "\n" + | 505 -> + "\n" + | 145 -> + "\n" + | 146 -> + "\n" + | 144 -> + "\n" + | 329 -> + "\n" + | 331 -> + "\n" + | 330 -> + "\n" + | 61 -> + "\n" + | 64 -> + "\n" + | 59 -> + "\n" + | 143 -> + "\n" + | 338 -> + "\n" + | 340 -> + "\n" + | 339 -> + "\n" + | 151 -> + "\n" + | 152 -> + "\n" + | 78 -> + "\n" + | 325 -> + "\n" + | 327 -> + "\n" + | 326 -> + "\n" + | 92 -> + "\n" + | 155 -> + "\n" + | 118 -> + "\n" + | 125 -> + "\n" + | 87 -> + "\n" + | 105 -> + "\n" + | 107 -> + "\n" + | 108 -> + "\n" + | 106 -> + "\n" + | 88 -> + "\n" + | 93 -> + "\n" + | 80 -> + "\n" + | 81 -> + "\n" + | 82 -> + "\n" + | 132 -> + "\n" + | 334 -> + "\n" + | 336 -> + "\n" + | 335 -> + "\n" + | 133 -> + "\n" + | 136 -> + "\n" + | 137 -> + "\n" + | 157 -> + "\n" + | 159 -> + "\n" + | 158 -> + "\n" + | 512 -> + "\n" + | 218 -> + "\n" + | 514 -> + "\n" + | 216 -> + "\n" + | 250 -> + "\n" + | 248 -> + "\n" + | 249 -> + "\n" + | 230 -> + "\n" + | 235 -> + "\n" + | 252 -> + "\n" + | 254 -> + "\n" + | 255 -> + "\n" + | 258 -> + "\n" + | 219 -> + "\n" + | 226 -> + "\n" + | 227 -> + "\n" + | 260 -> + "\n" + | 262 -> + "\n" + | 264 -> + "\n" + | 266 -> + "\n" + | 194 -> + "\n" + | 195 -> + "\n" + | 206 -> + "\n" + | 215 -> + "\n" + | 199 -> + "\n" + | 207 -> + "\n" + | 208 -> + "\n" + | 196 -> + "\n" + | 197 -> + "\n" + | 198 -> + "\n" + | 256 -> + "\n" + | 257 -> + "\n" + | 277 -> + "\n" + | 233 -> + "\n" + | 279 -> + "\n" + | 67 -> + "\n" + | 463 -> + "\n" + | 464 -> + "\n" + | 387 -> + "\n" + | 121 -> + "\n" + | 122 -> + "\n" + | 120 -> + "\n" + | 466 -> + "\n" + | 467 -> + "\n" + | 483 -> + "\n" + | 492 -> + "\n" + | 469 -> + "\n" + | 470 -> + "\n" + | 468 -> + "\n" + | 471 -> + "\n" + | 472 -> + "\n" + | 473 -> + "\n" + | 475 -> + "\n" + | 476 -> + "\n" + | 477 -> + "\n" + | 478 -> + "\n" + | 487 -> + "\n" + | 488 -> + "\n" + | 474 -> + "\n" + | 499 -> + "\n" + | 497 -> + "\n" + | 465 -> + "\n" + | 321 -> + "\n" + | 315 -> + "\n" + | 316 -> + "\n" + | 318 -> + "\n" + | 317 -> + "\n" + | 314 -> + "\n" + | 71 -> + "\n" + | 410 -> + "\n" + | 298 -> + "\n" + | 304 -> + "\n" + | 305 -> + "\n" + | 308 -> + "\n" + | 309 -> + "\n" + | 300 -> + "\n" + | 178 -> + "\n" + | 73 -> + "\n" + | 75 -> + "\n" + | 419 -> + "\n" + | 420 -> + "\n" + | 77 -> + "\n" + | 160 -> + "\n" + | 412 -> + "\n" + | 413 -> + "\n" + | 415 -> + "\n" + | 416 -> + "\n" + | 193 -> + "\n" + | 229 -> + "\n" + | 74 -> + "\n" + | 447 -> + "\n" + | 448 -> + "\n" + | 456 -> + "\n" + | 457 -> + "\n" + | 459 -> + "\n" + | 460 -> + "\n" + | 449 -> + "\n" + | 450 -> + "\n" + | 76 -> + "\n" + | 440 -> + "\n" + | 441 -> + "\n" + | 425 -> + "\n" + | 422 -> + "\n" + | 428 -> + "\n" + | 429 -> + "\n" + | 434 -> + "\n" + | 438 -> + "\n" + | 437 -> + "\n" + | 433 -> + "\n" + | 423 -> + "\n" + | 427 -> + "\n" + | 162 -> + "\n" + | 163 -> + "\n" + | 290 -> + "\n" + | 295 -> + "\n" + | 296 -> + "\n" + | 357 -> + "\n" + | 400 -> + "\n" + | 401 -> + "\n" + | 402 -> + "\n" + | 403 -> + "\n" + | 404 -> + "\n" + | 405 -> + "\n" + | 399 -> + "\n" + | 297 -> + "\n" + | 311 -> + "\n" + | 312 -> + "\n" + | 322 -> + "\n" + | 323 -> + "\n" + | 377 -> + "\n" + | 384 -> + "\n" + | 342 -> + "\n" + | 343 -> + "\n" + | 324 -> + "\n" + | 344 -> + "\n" + | 345 -> + "\n" + | 346 -> + "\n" + | 370 -> + "\n" + | 371 -> + "\n" + | 372 -> + "\n" + | 373 -> + "\n" + | 379 -> + "\n" + | 380 -> + "\n" + | 369 -> + "\n" + | 393 -> + "\n" + | 391 -> + "\n" + | 313 -> + "\n" + | 348 -> + "\n" + | 349 -> + "\n" + | 347 -> + "\n" + | 350 -> + "\n" + | 351 -> + "\n" + | 352 -> + "\n" + | 359 -> + "\n" + | 360 -> + "\n" + | 361 -> + "\n" + | 362 -> + "\n" + | 364 -> + "\n" + | 363 -> + "\n" + | 358 -> + "\n" + | 292 -> + "\n" + | 293 -> + "\n" + | 164 -> + "\n" + | 165 -> + "\n" + | 166 -> + "\n" + | 167 -> + "\n" + | 168 -> + "\n" + | 169 -> + "\n" + | 174 -> + "\n" + | 175 -> + "\n" + | 176 -> + "\n" + | 188 -> + "\n" + | 237 -> + "\n" + | _ -> + raise Not_found diff --git a/src/passes/1-parser/reasonligo/Parser.mly b/src/passes/1-parser/reasonligo/Parser.mly index 23deaf776..2026605ac 100644 --- a/src/passes/1-parser/reasonligo/Parser.mly +++ b/src/passes/1-parser/reasonligo/Parser.mly @@ -320,6 +320,8 @@ core_pattern: | "_" { PWild $1 } | unit { PUnit $1 } | "" { PInt $1 } +| "" { PNat $1 } +| "" { PBytes $1 } | "true" { PTrue $1 } | "false" { PFalse $1 } | "" { PString $1 } @@ -424,7 +426,8 @@ fun_expr: {p.value with inside = arg_to_pattern p.value.inside} in PPar {p with value} | EUnit u -> PUnit u - | e -> raise (SyntaxError.Error (WrongFunctionArguments e)) + | e -> let open! SyntaxError + in raise (Error (WrongFunctionArguments e)) in let fun_args_to_pattern = function EAnnot { @@ -453,7 +456,8 @@ fun_expr: in arg_to_pattern (fst fun_args), bindings | EUnit e -> arg_to_pattern (EUnit e), [] - | e -> raise (SyntaxError.Error (WrongFunctionArguments e)) + | e -> let open! SyntaxError + in raise (Error (WrongFunctionArguments e)) in let binders = fun_args_to_pattern $1 in let f = {kwd_fun; @@ -682,6 +686,7 @@ common_expr: "" { EArith (Int $1) } | "" { EArith (Mutez $1) } | "" { EArith (Nat $1) } +| "" { EBytes $1 } | "" | module_field { EVar $1 } | projection { EProj $1 } | "" { EString (String $1) } diff --git a/src/passes/1-parser/reasonligo/ParserAPI.ml b/src/passes/1-parser/reasonligo/ParserAPI.ml index 7ae5c5ad4..0c78cdeec 100644 --- a/src/passes/1-parser/reasonligo/ParserAPI.ml +++ b/src/passes/1-parser/reasonligo/ParserAPI.ml @@ -28,13 +28,46 @@ module type PARSER = end end +(* Errors *) + +module type PAR_ERR = + sig + val message : int -> string (* From error states to messages *) + end + +let format_error ?(offsets=true) mode Region.{region; value} ~file = + let reg = region#to_string ~file ~offsets mode in + Printf.sprintf "\027[31mParse error %s:\n%s\027[0m%!" reg value + (* Main functor *) module Make (Lexer: Lexer.S) - (Parser: PARSER with type token = Lexer.Token.token) = + (Parser: PARSER with type token = Lexer.Token.token) + (ParErr: PAR_ERR) = struct + type message = string + type valid = Lexer.token + type invalid = Lexer.token + + exception Point of message * valid option * invalid module I = Parser.MenhirInterpreter + module S = MenhirLib.General (* Streams *) + + (* The call [stack checkpoint] extracts the parser's stack out of + a checkpoint. *) + + let stack = function + I.HandlingError env -> I.stack env + | _ -> assert false + + (* The call [state checkpoint] extracts the number of the current + state out of a parser checkpoint. *) + + let state checkpoint : int = + match Lazy.force (stack checkpoint) with + S.Nil -> 0 (* WARNING: Hack. The first state should be 0. *) + | S.Cons (I.Element (s,_,_,_),_) -> I.number s (* The parser has successfully produced a semantic value. *) @@ -42,14 +75,22 @@ module Make (Lexer: Lexer.S) (* The parser has suspended itself because of a syntax error. Stop. *) - let fail _checkpoint = raise Parser.Error + let failure get_win checkpoint = + let message = ParErr.message (state checkpoint) in + match get_win () with + Lexer.Nil -> assert false + | Lexer.One invalid -> + raise (Point (message, None, invalid)) + | Lexer.Two (invalid, valid) -> + raise (Point (message, Some valid, invalid)) (* The generic parsing function *) - let incr_contract Lexer.{read; buffer; close; _} : AST.t = - let supplier = I.lexer_lexbuf_to_supplier read buffer in - let parser = Parser.Incremental.contract buffer.Lexing.lex_curr_p in - let ast = I.loop_handle success fail supplier parser + let incr_contract Lexer.{read; buffer; get_win; close; _} : AST.t = + let supplier = I.lexer_lexbuf_to_supplier read buffer + and failure = failure get_win in + let parser = Parser.Incremental.contract buffer.Lexing.lex_curr_p in + let ast = I.loop_handle success failure supplier parser in close (); ast let mono_contract = Parser.contract diff --git a/src/passes/1-parser/reasonligo/ParserAPI.mli b/src/passes/1-parser/reasonligo/ParserAPI.mli index ff3fe4854..f3eeaaba8 100644 --- a/src/passes/1-parser/reasonligo/ParserAPI.mli +++ b/src/passes/1-parser/reasonligo/ParserAPI.mli @@ -29,11 +29,29 @@ module type PARSER = end +(* Errors *) + +module type PAR_ERR = + sig + val message : int -> string (* From error states to messages *) + end + +val format_error : + ?offsets:bool -> [`Byte | `Point] -> + string Region.reg -> file:bool -> string + (* Main functor *) module Make (Lexer: Lexer.S) - (Parser: PARSER with type token = Lexer.Token.token) : + (Parser: PARSER with type token = Lexer.Token.token) + (ParErr: PAR_ERR) : sig + type message = string + type valid = Lexer.token + type invalid = Lexer.token + + exception Point of message * valid option * invalid + val mono_contract : (Lexing.lexbuf -> Lexer.token) -> Lexing.lexbuf -> AST.t val incr_contract : Lexer.instance -> AST.t end