Skip to content

Commit 601fbfa

Browse files
committed
Improvements to the JSON parsers
1 parent 10a4b25 commit 601fbfa

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

json-parser.sml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ structure JSONParser : sig
9191
fun parseFile fileName = let
9292
val inStrm = TextIO.openIn fileName
9393
val v = parse' (AntlrStreamPos.mkSourcemap' fileName, inStrm)
94+
handle ex => (TextIO.closeIn inStrm; raise ex)
9495
in
9596
TextIO.closeIn inStrm;
9697
v

json-stream-parser.sml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ structure JSONStreamParser : sig
2121
error : 'ctx * string -> 'ctx
2222
}
2323

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
2527

2628
end = struct
2729

@@ -47,7 +49,7 @@ structure JSONStreamParser : sig
4749
#error cb (ctx, msg);
4850
raise Fail "error")
4951

50-
fun parser (cb : 'a callbacks) (inStrm, ctx) = let
52+
fun parser (cb : 'a callbacks) (srcMap, inStrm, ctx) = let
5153
val lexer = Lex.lex (AntlrStreamPos.mkSourcemap ())
5254
fun parseValue (strm : Lex.strm, ctx) = let
5355
val (tok, pos, strm) = lexer strm
@@ -112,7 +114,29 @@ structure JSONStreamParser : sig
112114
(strm, #endObject cb ctx)
113115
end
114116
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
116140
end
117141

118142
end

0 commit comments

Comments
 (0)