Skip to content

Commit 1cc3804

Browse files
committed
[interpreter] Minor code clean-ups
1 parent 4f69eee commit 1cc3804

File tree

5 files changed

+57
-68
lines changed

5 files changed

+57
-68
lines changed

interpreter/script/js.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,7 @@ let rec of_definition def =
526526
| Textual m -> of_bytes (Encode.encode m)
527527
| Encoded (_, bs) -> of_bytes bs
528528
| Quoted (_, s) ->
529-
try
530-
let _v, m = Parse.Module.from_string s in
531-
of_definition m
529+
try of_definition (snd (Parse.Module.from_string s))
532530
with Script.Syntax _ ->
533531
of_bytes "<malformed quote>"
534532

interpreter/script/run.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ let input_from get_script run =
105105
true
106106
with
107107
| Decode.Code (at, msg) -> error at "decoding error" msg
108-
| Syntax (at, msg) -> error at "syntax error" msg
108+
| Parse.Syntax (at, msg) -> error at "syntax error" msg
109109
| Valid.Invalid (at, msg) -> error at "invalid module" msg
110110
| Import.Unknown (at, msg) -> error at "link failure" msg
111111
| Eval.Link (at, msg) -> error at "link failure" msg
@@ -121,14 +121,12 @@ let input_from get_script run =
121121
let input_script name lexbuf run =
122122
input_from (fun () ->
123123
Lexing.set_filename lexbuf name;
124-
Parse.Script.from_lexbuf lexbuf)
125-
run
124+
Parse.Script.from_lexbuf lexbuf) run
126125

127126
let input_script1 name lexbuf run =
128127
input_from (fun () ->
129128
Lexing.set_filename lexbuf name;
130-
Parse.Script1.from_lexbuf lexbuf)
131-
run
129+
Parse.Script1.from_lexbuf lexbuf) run
132130

133131
let input_sexpr name lexbuf run =
134132
input_from (fun () ->

interpreter/text/arrange.ml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -679,25 +679,21 @@ let definition mode x_opt def =
679679
match def.it with
680680
| Textual m -> m
681681
| Encoded (_, bs) -> Decode.decode "" bs
682-
| Quoted (_, s) ->
683-
let _v, m = Parse.Module.from_string s in
684-
unquote m
682+
| Quoted (_, s) -> unquote (snd (Parse.Module.from_string s))
685683
in module_with_var_opt x_opt (unquote def)
686684
| `Binary ->
687685
let rec unquote def =
688686
match def.it with
689687
| Textual m -> Encode.encode m
690688
| Encoded (_, bs) -> Encode.encode (Decode.decode "" bs)
691-
| Quoted (_, s) ->
692-
let _v, m = Parse.Module.from_string s in
693-
unquote m
689+
| Quoted (_, s) -> unquote (snd (Parse.Module.from_string s))
694690
in binary_module_with_var_opt x_opt (unquote def)
695691
| `Original ->
696692
match def.it with
697693
| Textual m -> module_with_var_opt x_opt m
698694
| Encoded (_, bs) -> binary_module_with_var_opt x_opt bs
699695
| Quoted (_, s) -> quoted_module_with_var_opt x_opt s
700-
with Script.Syntax _ ->
696+
with Parse.Syntax _ ->
701697
quoted_module_with_var_opt x_opt "<invalid module>"
702698

703699
let access x_opt n =

interpreter/text/parse.ml

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,53 @@
1-
module Make (M : sig
1+
exception Syntax = Script.Syntax
2+
3+
module type S =
4+
sig
25
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
10+
end
311

12+
module type Rule =
13+
sig
14+
type t
415
val rule : (Lexing.lexbuf -> Parser.token) -> Lexing.lexbuf -> t
16+
end
517

6-
end) = struct
7-
8-
type nonrec t = M.t
9-
10-
let from_lexbuf =
11-
let parser = MenhirLib.Convert.Simplified.traditional2revised M.rule in
12-
fun buf ->
13-
let provider () =
14-
let tok = Lexer.token buf in
15-
let start = Lexing.lexeme_start_p buf in
16-
let stop = Lexing.lexeme_end_p buf in
17-
tok, start, stop
18-
in
19-
try parser provider with
20-
| Parser.Error ->
21-
let left = Lexer.convert_pos buf.Lexing.lex_start_p in
22-
let right = Lexer.convert_pos buf.Lexing.lex_curr_p in
23-
let region = { Source.left; right } in
24-
raise (Script.Syntax (region, "unexpected token"))
25-
| Script.Syntax (region, s) as exn ->
26-
if region <> Source.no_region then raise exn
27-
else
28-
let region' = {
29-
Source.left = Lexer.convert_pos buf.Lexing.lex_start_p;
30-
Source.right = Lexer.convert_pos buf.Lexing.lex_curr_p }
31-
in
32-
raise (Script.Syntax (region', s))
33-
34-
let from_file filename =
35-
let chan = open_in filename in
36-
Fun.protect ~finally:(fun () -> close_in chan)
37-
(fun () ->
38-
let lb = Lexing.from_channel ~with_positions:true chan in
39-
Lexing.set_filename lb filename;
40-
from_lexbuf lb)
18+
module Make (M : Rule) : S with type t = M.t =
19+
struct
20+
type t = M.t
4121

42-
let from_string s = from_lexbuf (Lexing.from_string ~with_positions:true s)
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
27+
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+
}
4332

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))
41+
42+
let from_string s = from_lexbuf (Lexing.from_string ~with_positions:true s)
4443
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
48+
Lexing.set_filename buf name;
49+
from_lexbuf buf
50+
)
4551
end
4652

4753
module Module = Make (struct

interpreter/text/parse.mli

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
module Module : sig
2-
type t = Script.var option * Script.definition
3-
val from_lexbuf : Lexing.lexbuf -> t
4-
val from_file : string -> t
5-
val from_string : string -> t
6-
val from_channel : in_channel -> t
7-
end
1+
exception Syntax of Source.region * string
82

9-
module Script1 : sig
10-
type t = Script.script
3+
module type S =
4+
sig
5+
type t
116
val from_lexbuf : Lexing.lexbuf -> t
127
val from_file : string -> t
138
val from_string : string -> t
149
val from_channel : in_channel -> t
1510
end
1611

17-
module Script : sig
18-
type t = Script.script
19-
val from_lexbuf : Lexing.lexbuf -> t
20-
val from_file : string -> t
21-
val from_string : string -> t
22-
val from_channel : in_channel -> t
23-
end
12+
module Module : S with type t = Script.var option * Script.definition
13+
module Script1 : S with type t = Script.script
14+
module Script : S with type t = Script.script

0 commit comments

Comments
 (0)