-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[interpreter] Switch to menhir (#1705)
- Loading branch information
1 parent
3be4c2f
commit 4f69eee
Showing
10 changed files
with
165 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,60 @@ | ||
type 'a start = | ||
| Module : (Script.var option * Script.definition) start | ||
| Script : Script.script start | ||
| Script1 : Script.script start | ||
|
||
exception Syntax = Script.Syntax | ||
|
||
let parse' name lexbuf start = | ||
lexbuf.Lexing.lex_curr_p <- | ||
{lexbuf.Lexing.lex_curr_p with Lexing.pos_fname = name}; | ||
try start Lexer.token lexbuf | ||
with Syntax (region, s) -> | ||
let region' = if region <> Source.no_region then region else | ||
{Source.left = Lexer.convert_pos lexbuf.Lexing.lex_start_p; | ||
Source.right = Lexer.convert_pos lexbuf.Lexing.lex_curr_p} in | ||
raise (Syntax (region', s)) | ||
|
||
let parse (type a) name lexbuf : a start -> a = function | ||
| Module -> parse' name lexbuf Parser.module1 | ||
| Script -> parse' name lexbuf Parser.script | ||
| Script1 -> parse' name lexbuf Parser.script1 | ||
|
||
let string_to start s = | ||
let lexbuf = Lexing.from_string s in | ||
parse "string" lexbuf start | ||
|
||
let string_to_script s = string_to Script s | ||
let string_to_module s = snd (string_to Module s) | ||
module Make (M : sig | ||
type t | ||
|
||
val rule : (Lexing.lexbuf -> Parser.token) -> Lexing.lexbuf -> t | ||
|
||
end) = struct | ||
|
||
type nonrec t = M.t | ||
|
||
let from_lexbuf = | ||
let parser = MenhirLib.Convert.Simplified.traditional2revised M.rule in | ||
fun buf -> | ||
let provider () = | ||
let tok = Lexer.token buf in | ||
let start = Lexing.lexeme_start_p buf in | ||
let stop = Lexing.lexeme_end_p buf in | ||
tok, start, stop | ||
in | ||
try parser provider with | ||
| Parser.Error -> | ||
let left = Lexer.convert_pos buf.Lexing.lex_start_p in | ||
let right = Lexer.convert_pos buf.Lexing.lex_curr_p in | ||
let region = { Source.left; right } in | ||
raise (Script.Syntax (region, "unexpected token")) | ||
| Script.Syntax (region, s) as exn -> | ||
if region <> Source.no_region then raise exn | ||
else | ||
let region' = { | ||
Source.left = Lexer.convert_pos buf.Lexing.lex_start_p; | ||
Source.right = Lexer.convert_pos buf.Lexing.lex_curr_p } | ||
in | ||
raise (Script.Syntax (region', s)) | ||
|
||
let from_file filename = | ||
let chan = open_in filename in | ||
Fun.protect ~finally:(fun () -> close_in chan) | ||
(fun () -> | ||
let lb = Lexing.from_channel ~with_positions:true chan in | ||
Lexing.set_filename lb filename; | ||
from_lexbuf lb) | ||
|
||
let from_string s = from_lexbuf (Lexing.from_string ~with_positions:true s) | ||
|
||
let from_channel c = from_lexbuf (Lexing.from_channel ~with_positions:true c) | ||
end | ||
|
||
module Module = Make (struct | ||
type t = Script.var option * Script.definition | ||
let rule = Parser.module1 | ||
end) | ||
|
||
module Script1 = Make (struct | ||
type t = Script.script | ||
let rule = Parser.script1 | ||
end) | ||
|
||
module Script = Make (struct | ||
type t = Script.script | ||
let rule = Parser.script | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
type 'a start = | ||
| Module : (Script.var option * Script.definition) start | ||
| Script : Script.script start | ||
| Script1 : Script.script start | ||
module Module : sig | ||
type t = Script.var option * Script.definition | ||
val from_lexbuf : Lexing.lexbuf -> t | ||
val from_file : string -> t | ||
val from_string : string -> t | ||
val from_channel : in_channel -> t | ||
end | ||
|
||
exception Syntax of Source.region * string | ||
module Script1 : sig | ||
type t = Script.script | ||
val from_lexbuf : Lexing.lexbuf -> t | ||
val from_file : string -> t | ||
val from_string : string -> t | ||
val from_channel : in_channel -> t | ||
end | ||
|
||
val parse : string -> Lexing.lexbuf -> 'a start -> 'a (* raises Syntax *) | ||
|
||
val string_to_script : string -> Script.script (* raises Syntax *) | ||
val string_to_module : string -> Script.definition (* raises Syntax *) | ||
module Script : sig | ||
type t = Script.script | ||
val from_lexbuf : Lexing.lexbuf -> t | ||
val from_file : string -> t | ||
val from_string : string -> t | ||
val from_channel : in_channel -> t | ||
end |
Oops, something went wrong.