-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
123 additions
and
116 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
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,32 +1,52 @@ | ||
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_definition s = snd (string_to Module s) | ||
let string_to_module s = | ||
match (string_to_definition s).Source.it with | ||
| Script.Textual m -> m | ||
| _ -> assert false | ||
exception Syntax = Parse_error.Syntax | ||
|
||
module type S = | ||
sig | ||
type t | ||
val parse : string -> Lexing.lexbuf -> t | ||
val parse_file : string -> t | ||
val parse_string : string -> t | ||
val parse_channel : in_channel -> t | ||
end | ||
|
||
let provider buf () = | ||
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 | ||
|
||
let convert_pos buf = | ||
{ Source.left = Lexer.convert_pos buf.Lexing.lex_start_p; | ||
Source.right = Lexer.convert_pos buf.Lexing.lex_curr_p | ||
} | ||
|
||
let make (type a) (start : _ -> _ -> a) : (module S with type t = a) = | ||
(module struct | ||
type t = a | ||
|
||
let parse name buf = | ||
Lexing.set_filename buf name; | ||
try | ||
MenhirLib.Convert.Simplified.traditional2revised start (provider buf) | ||
with | ||
| Parser.Error -> | ||
raise (Syntax (convert_pos buf, "unexpected token")) | ||
| Syntax (region, s) when region <> Source.no_region -> | ||
raise (Syntax (convert_pos buf, s)) | ||
|
||
let parse_string s = | ||
parse "string" (Lexing.from_string ~with_positions:true s) | ||
|
||
let parse_channel oc = | ||
parse "channel" (Lexing.from_channel ~with_positions:true oc) | ||
|
||
let parse_file name = | ||
let oc = open_in name in | ||
Fun.protect ~finally:(fun () -> close_in oc) (fun () -> | ||
parse name (Lexing.from_channel ~with_positions:true oc) | ||
) | ||
end) | ||
|
||
module Module = (val make Parser.module1) | ||
module Script = (val make Parser.script) | ||
module Script1 = (val make Parser.script1) |
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,12 +1,14 @@ | ||
type 'a start = | ||
| Module : (Script.var option * Script.definition) start | ||
| Script : Script.script start | ||
| Script1 : Script.script start | ||
|
||
exception Syntax of Source.region * string | ||
|
||
val parse : string -> Lexing.lexbuf -> 'a start -> 'a (* raises Syntax *) | ||
module type S = | ||
sig | ||
type t | ||
val parse : string -> Lexing.lexbuf -> t | ||
val parse_file : string -> t | ||
val parse_string : string -> t | ||
val parse_channel : in_channel -> t | ||
end | ||
|
||
val string_to_script : string -> Script.script (* raises Syntax *) | ||
val string_to_definition : string -> Script.definition (* raises Syntax *) | ||
val string_to_module : string -> Ast.module_ (* raises Syntax *) | ||
module Module : S with type t = Script.var option * Script.definition | ||
module Script1 : S with type t = Script.script | ||
module Script : S with type t = Script.script |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(* This is here since both Lexer, Parser, and Parse need it, | ||
* but menhir cannot create a Parser that exports it. *) | ||
exception Syntax of Source.region * string |
Oops, something went wrong.