@@ -21,7 +21,9 @@ structure JSONStreamParser : sig
21
21
error : 'ctx * string -> 'ctx
22
22
}
23
23
24
- val parser : 'ctx callbacks -> (TextIO.instream * 'ctx) -> unit
24
+ val parse : 'ctx callbacks -> (TextIO.instream * 'ctx) -> 'ctx
25
+
26
+ val parseFile : 'ctx callbacks -> (string * 'ctx) -> 'ctx
25
27
26
28
end = struct
27
29
@@ -47,7 +49,7 @@ structure JSONStreamParser : sig
47
49
#error cb (ctx, msg);
48
50
raise Fail " error" )
49
51
50
- fun parser (cb : 'a callbacks) (inStrm, ctx) = let
52
+ fun parser (cb : 'a callbacks) (srcMap, inStrm, ctx) = let
51
53
val lexer = Lex.lex (AntlrStreamPos.mkSourcemap ())
52
54
fun parseValue (strm : Lex.strm, ctx) = let
53
55
val (tok, pos, strm) = lexer strm
@@ -112,7 +114,29 @@ structure JSONStreamParser : sig
112
114
(strm, #endObject cb ctx)
113
115
end
114
116
in
115
- ignore (parseValue (Lex.streamifyInstream inStrm, ctx))
117
+ #2 (parseValue (Lex.streamifyInstream inStrm, ctx))
118
+ end
119
+
120
+ fun parse cb = let
121
+ val parser = parser cb
122
+ fun parse ' (inStrm, ctx) =
123
+ parser(AntlrStreamPos.mkSourcemap (), inStrm, ctx)
124
+ in
125
+ parse'
126
+ end
127
+
128
+ fun parseFile cb = let
129
+ val parser = parser cb
130
+ fun parse (fileName, ctx) = let
131
+ val inStrm = TextIO.openIn fileName
132
+ val ctx = parser (AntlrStreamPos.mkSourcemap' fileName, inStrm, ctx)
133
+ handle ex => (TextIO.closeIn inStrm; raise ex)
134
+ in
135
+ TextIO.closeIn inStrm;
136
+ ctx
137
+ end
138
+ in
139
+ parse
116
140
end
117
141
118
142
end
0 commit comments