Skip to content

Commit 025e84c

Browse files
committed
[interpreter] Simplify functor
1 parent 1cc3804 commit 025e84c

File tree

9 files changed

+61
-78
lines changed

9 files changed

+61
-78
lines changed

interpreter/script/js.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,8 @@ let rec of_definition def =
526526
| Textual m -> of_bytes (Encode.encode m)
527527
| Encoded (_, bs) -> of_bytes bs
528528
| Quoted (_, s) ->
529-
try of_definition (snd (Parse.Module.from_string s))
530-
with Script.Syntax _ ->
529+
try of_definition (snd (Parse.Module.parse_string s))
530+
with Parse.Syntax _ ->
531531
of_bytes "<malformed quote>"
532532

533533
let of_wrapper mods x_opt name wrap_action wrap_assertion at =
@@ -595,7 +595,7 @@ let of_command mods cmd =
595595
match def.it with
596596
| Textual m -> m
597597
| Encoded (_, bs) -> Decode.decode "binary" bs
598-
| Quoted (_, s) -> unquote (snd (Parse.Module.from_string s))
598+
| Quoted (_, s) -> unquote (snd (Parse.Module.parse_string s))
599599
in bind mods x_opt (unquote def);
600600
"let " ^ current_var mods ^ " = instance(" ^ of_definition def ^ ");\n" ^
601601
(if x_opt = None then "" else

interpreter/script/run.ml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,14 @@ let input_from get_script run =
119119
| Abort _ -> false
120120

121121
let input_script name lexbuf run =
122-
input_from (fun () ->
123-
Lexing.set_filename lexbuf name;
124-
Parse.Script.from_lexbuf lexbuf) run
122+
input_from (fun () -> Parse.Script.parse name lexbuf) run
125123

126124
let input_script1 name lexbuf run =
127-
input_from (fun () ->
128-
Lexing.set_filename lexbuf name;
129-
Parse.Script1.from_lexbuf lexbuf) run
125+
input_from (fun () -> Parse.Script1.parse name lexbuf) run
130126

131127
let input_sexpr name lexbuf run =
132128
input_from (fun () ->
133-
let var_opt, def = Parse.Module.from_lexbuf lexbuf in
129+
let var_opt, def = Parse.Module.parse name lexbuf in
134130
[Module (var_opt, def) @@ no_region]) run
135131

136132
let input_binary name buf run =
@@ -344,7 +340,7 @@ let rec run_definition def : Ast.module_ =
344340
Decode.decode name bs
345341
| Quoted (_, s) ->
346342
trace "Parsing quote...";
347-
let _, def' = Parse.Module.from_string s in
343+
let _, def' = Parse.Module.parse_string s in
348344
run_definition def'
349345

350346
let run_action act : Values.value list =
@@ -450,7 +446,7 @@ let run_assertion ass =
450446
trace "Asserting malformed...";
451447
(match ignore (run_definition def) with
452448
| exception Decode.Code (_, msg) -> assert_message ass.at "decoding" msg re
453-
| exception Syntax (_, msg) -> assert_message ass.at "parsing" msg re
449+
| exception Parse.Syntax (_, msg) -> assert_message ass.at "parsing" msg re
454450
| _ -> Assert.error ass.at "expected decoding/parsing error"
455451
)
456452

interpreter/script/script.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ and meta' =
6363

6464
and script = command list
6565

66-
exception Syntax of Source.region * string
67-
6866

6967
let () =
7068
let type_of_ref' = !Values.type_of_ref' in

interpreter/text/arrange.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,14 +679,14 @@ let definition mode x_opt def =
679679
match def.it with
680680
| Textual m -> m
681681
| Encoded (_, bs) -> Decode.decode "" bs
682-
| Quoted (_, s) -> unquote (snd (Parse.Module.from_string s))
682+
| Quoted (_, s) -> unquote (snd (Parse.Module.parse_string s))
683683
in module_with_var_opt x_opt (unquote def)
684684
| `Binary ->
685685
let rec unquote def =
686686
match def.it with
687687
| Textual m -> Encode.encode m
688688
| Encoded (_, bs) -> Encode.encode (Decode.decode "" bs)
689-
| Quoted (_, s) -> unquote (snd (Parse.Module.from_string s))
689+
| Quoted (_, s) -> unquote (snd (Parse.Module.parse_string s))
690690
in binary_module_with_var_opt x_opt (unquote def)
691691
| `Original ->
692692
match def.it with

interpreter/text/lexer.mll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let region lexbuf =
1414
let right = convert_pos (Lexing.lexeme_end_p lexbuf) in
1515
{left = left; right = right}
1616

17-
let error lexbuf msg = raise (Script.Syntax (region lexbuf, msg))
17+
let error lexbuf msg = raise (Parse_error.Syntax (region lexbuf, msg))
1818
let error_nest start lexbuf msg =
1919
lexbuf.Lexing.lex_start_p <- start;
2020
error lexbuf msg

interpreter/text/parse.ml

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,52 @@
1-
exception Syntax = Script.Syntax
1+
exception Syntax = Parse_error.Syntax
22

33
module type S =
44
sig
55
type t
6-
val from_lexbuf : Lexing.lexbuf -> t
7-
val from_file : string -> t
8-
val from_string : string -> t
9-
val from_channel : in_channel -> t
6+
val parse : string -> Lexing.lexbuf -> t
7+
val parse_file : string -> t
8+
val parse_string : string -> t
9+
val parse_channel : in_channel -> t
1010
end
1111

12-
module type Rule =
13-
sig
14-
type t
15-
val rule : (Lexing.lexbuf -> Parser.token) -> Lexing.lexbuf -> t
16-
end
17-
18-
module Make (M : Rule) : S with type t = M.t =
19-
struct
20-
type t = M.t
21-
22-
let provider buf () =
23-
let tok = Lexer.token buf in
24-
let start = Lexing.lexeme_start_p buf in
25-
let stop = Lexing.lexeme_end_p buf in
26-
tok, start, stop
12+
let provider buf () =
13+
let tok = Lexer.token buf in
14+
let start = Lexing.lexeme_start_p buf in
15+
let stop = Lexing.lexeme_end_p buf in
16+
tok, start, stop
2717

28-
let convert_pos buf =
29-
{ Source.left = Lexer.convert_pos buf.Lexing.lex_start_p;
30-
Source.right = Lexer.convert_pos buf.Lexing.lex_curr_p
31-
}
18+
let convert_pos buf =
19+
{ Source.left = Lexer.convert_pos buf.Lexing.lex_start_p;
20+
Source.right = Lexer.convert_pos buf.Lexing.lex_curr_p
21+
}
3222

33-
let from_lexbuf buf =
34-
try
35-
MenhirLib.Convert.Simplified.traditional2revised M.rule (provider buf)
36-
with
37-
| Parser.Error ->
38-
raise (Syntax (convert_pos buf, "unexpected token"))
39-
| Syntax (region, s) when region <> Source.no_region ->
40-
raise (Syntax (convert_pos buf, s))
23+
let make (type a) (start : _ -> _ -> a) : (module S with type t = a) =
24+
(module struct
25+
type t = a
4126

42-
let from_string s = from_lexbuf (Lexing.from_string ~with_positions:true s)
43-
let from_channel c = from_lexbuf (Lexing.from_channel ~with_positions:true c)
44-
let from_file name =
45-
let chan = open_in name in
46-
Fun.protect ~finally:(fun () -> close_in chan) (fun () ->
47-
let buf = Lexing.from_channel ~with_positions:true chan in
27+
let parse name buf =
4828
Lexing.set_filename buf name;
49-
from_lexbuf buf
50-
)
51-
end
52-
53-
module Module = Make (struct
54-
type t = Script.var option * Script.definition
55-
let rule = Parser.module1
56-
end)
57-
58-
module Script1 = Make (struct
59-
type t = Script.script
60-
let rule = Parser.script1
61-
end)
62-
63-
module Script = Make (struct
64-
type t = Script.script
65-
let rule = Parser.script
66-
end)
29+
try
30+
MenhirLib.Convert.Simplified.traditional2revised start (provider buf)
31+
with
32+
| Parser.Error ->
33+
raise (Syntax (convert_pos buf, "unexpected token"))
34+
| Syntax (region, s) when region <> Source.no_region ->
35+
raise (Syntax (convert_pos buf, s))
36+
37+
let parse_string s =
38+
parse "string" (Lexing.from_string ~with_positions:true s)
39+
40+
let parse_channel oc =
41+
parse "channel" (Lexing.from_channel ~with_positions:true oc)
42+
43+
let parse_file name =
44+
let oc = open_in name in
45+
Fun.protect ~finally:(fun () -> close_in oc) (fun () ->
46+
parse name (Lexing.from_channel ~with_positions:true oc)
47+
)
48+
end)
49+
50+
module Module = (val make Parser.module1)
51+
module Script = (val make Parser.script)
52+
module Script1 = (val make Parser.script1)

interpreter/text/parse.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ exception Syntax of Source.region * string
33
module type S =
44
sig
55
type t
6-
val from_lexbuf : Lexing.lexbuf -> t
7-
val from_file : string -> t
8-
val from_string : string -> t
9-
val from_channel : in_channel -> t
6+
val parse : string -> Lexing.lexbuf -> t
7+
val parse_file : string -> t
8+
val parse_string : string -> t
9+
val parse_channel : in_channel -> t
1010
end
1111

1212
module Module : S with type t = Script.var option * Script.definition

interpreter/text/parse_error.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(* This is here since both Lexer, Parser, and Parse need it,
2+
* but menhir cannot create a Parser that exports it. *)
3+
exception Syntax of Source.region * string

interpreter/text/parser.mly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ open Script
88

99
(* Error handling *)
1010

11-
let error at msg = raise (Script.Syntax (at, msg))
11+
let error at msg = raise (Parse_error.Syntax (at, msg))
1212

1313

1414
(* Position handling *)

0 commit comments

Comments
 (0)