|
1 |
| -exception Syntax = Script.Syntax |
| 1 | +exception Syntax = Parse_error.Syntax |
2 | 2 |
|
3 | 3 | module type S =
|
4 | 4 | sig
|
5 | 5 | 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 |
10 | 10 | end
|
11 | 11 |
|
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 |
27 | 17 |
|
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 | + } |
32 | 22 |
|
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 |
41 | 26 |
|
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 = |
48 | 28 | 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) |
0 commit comments