major change to api, {incomplete, Next, Force} replaced by {incomplete, Next/1} where Next/1 accepts 'end_stream' to replicate what Force/1 used to do
This commit is contained in:
parent
1188f02d9f
commit
42a18cfcd9
5 changed files with 354 additions and 193 deletions
|
@ -83,39 +83,29 @@
|
|||
).
|
||||
|
||||
|
||||
%% two macros to simplify incomplete handling
|
||||
-define(incomplete(Valid, Incomplete, Finish),
|
||||
case Valid of
|
||||
true -> {error, badjson}
|
||||
; false -> {incomplete, Incomplete, Finish}
|
||||
end
|
||||
).
|
||||
|
||||
-define(ferror, fun() -> {error, badjson} end).
|
||||
|
||||
|
||||
%% compilation macros for unified decoder
|
||||
-ifdef(utf8).
|
||||
-define(encoding, utf8).
|
||||
-define(partial_codepoint(Bin), byte_size(Bin) >= 1).
|
||||
-define(partial_codepoint(Bin), byte_size(Bin) < 1).
|
||||
-endif.
|
||||
|
||||
-ifdef(utf16).
|
||||
-define(encoding, utf16).
|
||||
-define(partial_codepoint(Bin), byte_size(Bin) >= 2).
|
||||
-define(partial_codepoint(Bin), byte_size(Bin) < 2).
|
||||
-endif.
|
||||
|
||||
-ifdef(utf16le).
|
||||
-define(encoding, utf16-little).
|
||||
-define(partial_codepoint(Bin), byte_size(Bin) >= 2).
|
||||
-define(partial_codepoint(Bin), byte_size(Bin) < 2).
|
||||
-endif.
|
||||
|
||||
-ifdef(utf32).
|
||||
-define(encoding, utf32).
|
||||
-define(partial_codepoint(Bin), byte_size(Bin) >= 4).
|
||||
-define(partial_codepoint(Bin), byte_size(Bin) < 4).
|
||||
-endif.
|
||||
|
||||
-ifdef(utf32le).
|
||||
-define(encoding, utf32-little).
|
||||
-define(partial_codepoint(Bin), byte_size(Bin) >= 4).
|
||||
-define(partial_codepoint(Bin), byte_size(Bin) < 4).
|
||||
-endif.
|
|
@ -58,5 +58,6 @@
|
|||
-type jsx_parser() :: fun((json()) -> jsx_parser_result()).
|
||||
|
||||
-type jsx_parser_result() :: {event, jsx_event(), fun(() -> jsx_parser_result())}
|
||||
| {incomplete, jsx_parser(), fun(() -> jsx_parser_result())}
|
||||
| {error, badjson}.
|
||||
| {incomplete, jsx_parser()}
|
||||
| {error, badjson}
|
||||
| ok.
|
|
@ -71,10 +71,15 @@ start(<<S/?encoding, Rest/binary>>, Stack, Opts) when ?is_nonzero(S) ->
|
|||
start(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts)) ->
|
||||
maybe_comment(Rest, fun(Resume) -> start(Resume, Stack, Opts) end);
|
||||
start(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> start(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> start(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
maybe_done(<<S/?encoding, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
||||
maybe_done(Rest, Stack, Opts);
|
||||
|
@ -91,28 +96,32 @@ maybe_done(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts))
|
|||
maybe_done(Rest, [], ?multi_term(Opts)) ->
|
||||
{event, end_json, fun() -> start(Rest, [], Opts) end};
|
||||
maybe_done(Rest, [], Opts) ->
|
||||
{event, end_json, fun() ->
|
||||
{incomplete,
|
||||
fun(Stream) -> done(<<Rest/binary, Stream/binary>>, Opts) end,
|
||||
fun() -> done(Rest, Opts) end
|
||||
}
|
||||
end
|
||||
};
|
||||
{event, end_json, fun() -> done(Rest, Opts) end};
|
||||
maybe_done(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> maybe_done(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> case Bin == <<>> of true -> ok; false -> {error, badjson} end
|
||||
; (Stream) -> maybe_done(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
done(<<S/?encoding, Rest/binary>>, Opts) when ?is_whitespace(S) ->
|
||||
done(Rest, Opts);
|
||||
done(<<?solidus/?encoding, Rest/binary>>, ?comments_enabled(Opts)) ->
|
||||
maybe_comment(Rest, fun(Resume) -> done(Resume, Opts) end);
|
||||
done(Bin, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> done(<<Bin/binary, Stream/binary>>, Opts) end,
|
||||
fun() -> done(Bin, Opts) end
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> case Bin == <<>> of true -> ok; false -> {error, badjson} end
|
||||
; (Stream) -> done(<<Bin/binary, Stream/binary>>, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
object(<<S/?encoding, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
||||
|
@ -124,10 +133,15 @@ object(<<?end_object/?encoding, Rest/binary>>, [key|Stack], Opts) ->
|
|||
object(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts)) ->
|
||||
maybe_comment(Rest, fun(Resume) -> object(Resume, Stack, Opts) end);
|
||||
object(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> object(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> object(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
array(<<S/?encoding, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
||||
|
@ -155,10 +169,15 @@ array(<<?end_array/?encoding, Rest/binary>>, [array|Stack], Opts) ->
|
|||
array(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts)) ->
|
||||
maybe_comment(Rest, fun(Resume) -> array(Resume, Stack, Opts) end);
|
||||
array(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> array(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> array(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
value(<<S/?encoding, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
||||
|
@ -184,10 +203,15 @@ value(<<?start_array/?encoding, Rest/binary>>, Stack, Opts) ->
|
|||
value(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts)) ->
|
||||
maybe_comment(Rest, fun(Resume) -> value(Resume, Stack, Opts) end);
|
||||
value(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> value(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> value(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
colon(<<S/?encoding, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
||||
|
@ -197,10 +221,15 @@ colon(<<?colon/?encoding, Rest/binary>>, [key|Stack], Opts) ->
|
|||
colon(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts)) ->
|
||||
maybe_comment(Rest, fun(Resume) -> colon(Resume, Stack, Opts) end);
|
||||
colon(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> colon(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> colon(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
key(<<S/?encoding, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
||||
|
@ -210,10 +239,15 @@ key(<<?quote/?encoding, Rest/binary>>, Stack, Opts) ->
|
|||
key(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts)) ->
|
||||
maybe_comment(Rest, fun(Resume) -> key(Resume, Stack, Opts) end);
|
||||
key(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> key(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> key(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
%% string has an additional parameter, an accumulator (Acc) used to hold the intermediate
|
||||
|
@ -234,8 +268,13 @@ string(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when ?is_noncontrol(S) ->
|
|||
string(Rest, Stack, Opts, [S] ++ Acc);
|
||||
string(Bin, Stack, Opts, Acc) ->
|
||||
case partial_utf(Bin) of
|
||||
false -> {error, badjson}
|
||||
; _ -> {incomplete, fun(Stream) -> string(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc) end, ?ferror}
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> string(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
|
@ -307,10 +346,15 @@ escape(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc)
|
|||
when S =:= ?quote; S =:= ?solidus; S =:= ?rsolidus ->
|
||||
string(Rest, Stack, Opts, [S] ++ Acc);
|
||||
escape(Bin, Stack, Opts, Acc) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> escape(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> escape(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
%% this code is ugly and unfortunate, but so is json's handling of escaped unicode
|
||||
|
@ -357,10 +401,15 @@ escaped_unicode(<<D/?encoding, Rest/binary>>, Stack, Opts, String, [C, B, A]) wh
|
|||
escaped_unicode(<<S/?encoding, Rest/binary>>, Stack, Opts, String, Acc) when ?is_hex(S) ->
|
||||
escaped_unicode(Rest, Stack, Opts, String, [S] ++ Acc);
|
||||
escaped_unicode(Bin, Stack, Opts, String, Acc) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> escaped_unicode(<<Bin/binary, Stream/binary>>, Stack, Opts, String, Acc) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> escaped_unicode(<<Bin/binary, Stream/binary>>, Stack, Opts, String, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
%% upon encountering a low pair json/hex encoded value, check to see if there's a high
|
||||
%% value already in the accumulator
|
||||
|
@ -390,10 +439,15 @@ negative(<<$0/?encoding, Rest/binary>>, Stack, Opts, Acc) ->
|
|||
negative(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when ?is_nonzero(S) ->
|
||||
integer(Rest, Stack, Opts, [S] ++ Acc);
|
||||
negative(Bin, Stack, Opts, Acc) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> negative(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> negative(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
zero(<<?end_object/?encoding, Rest/binary>>, [object|Stack], Opts, Acc) ->
|
||||
|
@ -416,14 +470,21 @@ zero(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts), Acc) -
|
|||
maybe_comment(Rest, fun(Resume) -> zero(Resume, Stack, Opts, Acc) end);
|
||||
zero(<<>>, [], Opts, Acc) ->
|
||||
{incomplete,
|
||||
fun(Stream) -> zero(Stream, [], Opts, Acc) end,
|
||||
fun() -> {event, {integer, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} end
|
||||
fun
|
||||
(end_stream) -> {event, {integer, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end}
|
||||
; (Stream) -> zero(Stream, [], Opts, Acc)
|
||||
end
|
||||
};
|
||||
zero(Bin, Stack, Opts, Acc) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> zero(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> zero(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
integer(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when ?is_nonzero(S) ->
|
||||
|
@ -454,14 +515,21 @@ integer(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts), Acc
|
|||
maybe_comment(Rest, fun(Resume) -> integer(Resume, Stack, Opts, Acc) end);
|
||||
integer(<<>>, [], Opts, Acc) ->
|
||||
{incomplete,
|
||||
fun(Stream) -> integer(Stream, [], Opts, Acc) end,
|
||||
fun() -> {event, {integer, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} end
|
||||
fun
|
||||
(end_stream) -> {event, {integer, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end}
|
||||
; (Stream) -> integer(Stream, [], Opts, Acc)
|
||||
end
|
||||
};
|
||||
integer(Bin, Stack, Opts, Acc) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> integer(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> integer(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
initial_decimal(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when ?is_nonzero(S) ->
|
||||
|
@ -469,10 +537,15 @@ initial_decimal(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when ?is_nonzero
|
|||
initial_decimal(<<?zero/?encoding, Rest/binary>>, Stack, Opts, Acc) ->
|
||||
decimal(Rest, Stack, Opts, [?zero] ++ Acc);
|
||||
initial_decimal(Bin, Stack, Opts, Acc) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> initial_decimal(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> initial_decimal(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
decimal(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when ?is_nonzero(S) ->
|
||||
|
@ -501,14 +574,21 @@ decimal(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts), Acc
|
|||
maybe_comment(Rest, fun(Resume) -> decimal(Resume, Stack, Opts, Acc) end);
|
||||
decimal(<<>>, [], Opts, Acc) ->
|
||||
{incomplete,
|
||||
fun(Stream) -> decimal(Stream, [], Opts, Acc) end,
|
||||
fun() -> {event, {float, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} end
|
||||
fun
|
||||
(end_stream) -> {event, {float, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end}
|
||||
; (Stream) -> decimal(Stream, [], Opts, Acc)
|
||||
end
|
||||
};
|
||||
decimal(Bin, Stack, Opts, Acc) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> decimal(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> decimal(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
e(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when S =:= ?zero; ?is_nonzero(S) ->
|
||||
|
@ -516,19 +596,29 @@ e(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when S =:= ?zero; ?is_nonzero(
|
|||
e(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when S =:= ?positive; S =:= ?negative ->
|
||||
ex(Rest, Stack, Opts, [S] ++ Acc);
|
||||
e(Bin, Stack, Opts, Acc) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> e(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> e(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
ex(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when S =:= ?zero; ?is_nonzero(S) ->
|
||||
exp(Rest, Stack, Opts, [S] ++ Acc);
|
||||
ex(Bin, Stack, Opts, Acc) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> ex(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> ex(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
exp(<<S/?encoding, Rest/binary>>, Stack, Opts, Acc) when ?is_nonzero(S) ->
|
||||
|
@ -553,104 +643,161 @@ exp(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts), Acc) ->
|
|||
maybe_comment(Rest, fun(Resume) -> exp(Resume, Stack, Opts, Acc) end);
|
||||
exp(<<>>, [], Opts, Acc) ->
|
||||
{incomplete,
|
||||
fun(Stream) -> exp(Stream, [], Opts, Acc) end,
|
||||
fun() -> {event, {float, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end} end
|
||||
fun
|
||||
(end_stream) -> {event, {float, lists:reverse(Acc)}, fun() -> maybe_done(<<>>, [], Opts) end}
|
||||
; (Stream) -> exp(Stream, [], Opts, Acc)
|
||||
end
|
||||
};
|
||||
exp(Bin, Stack, Opts, Acc) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> exp(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> exp(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
tr(<<$r/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
tru(Rest, Stack, Opts);
|
||||
tr(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> tr(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> tr(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
tru(<<$u/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
true(Rest, Stack, Opts);
|
||||
tru(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> tru(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> tru(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
true(<<$e/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
{event, {literal, true}, fun() -> maybe_done(Rest, Stack, Opts) end};
|
||||
true(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> true(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> true(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
fa(<<$a/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
fal(Rest, Stack, Opts);
|
||||
fa(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> fa(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> fa(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
fal(<<$l/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
fals(Rest, Stack, Opts);
|
||||
fal(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> fal(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> fal(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
fals(<<$s/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
false(Rest, Stack, Opts);
|
||||
fals(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> fals(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> fals(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
false(<<$e/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
{event, {literal, false}, fun() -> maybe_done(Rest, Stack, Opts) end};
|
||||
false(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> false(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> false(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
nu(<<$u/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
nul(Rest, Stack, Opts);
|
||||
nu(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> nu(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> nu(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
nul(<<$l/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
null(Rest, Stack, Opts);
|
||||
nul(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> nul(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> nul(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
null(<<$l/?encoding, Rest/binary>>, Stack, Opts) ->
|
||||
{event, {literal, null}, fun() -> maybe_done(Rest, Stack, Opts) end};
|
||||
null(Bin, Stack, Opts) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> null(<<Bin/binary, Stream/binary>>, Stack, Opts) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> null(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
%% comments are c style, ex: /* blah blah */
|
||||
|
@ -662,10 +809,15 @@ null(Bin, Stack, Opts) ->
|
|||
maybe_comment(<<?star/?encoding, Rest/binary>>, Resume) ->
|
||||
comment(Rest, Resume);
|
||||
maybe_comment(Bin, Resume) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> maybe_comment(<<Bin/binary, Stream/binary>>, Resume) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> maybe_comment(<<Bin/binary, Stream/binary>>, Resume)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
comment(<<?star/?encoding, Rest/binary>>, Resume) ->
|
||||
|
@ -673,10 +825,15 @@ comment(<<?star/?encoding, Rest/binary>>, Resume) ->
|
|||
comment(<<_/?encoding, Rest/binary>>, Resume) ->
|
||||
comment(Rest, Resume);
|
||||
comment(Bin, Resume) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> comment(<<Bin/binary, Stream/binary>>, Resume) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> comment(<<Bin/binary, Stream/binary>>, Resume)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
||||
|
||||
|
||||
maybe_comment_done(<<?solidus/?encoding, Rest/binary>>, Resume) ->
|
||||
|
@ -684,7 +841,12 @@ maybe_comment_done(<<?solidus/?encoding, Rest/binary>>, Resume) ->
|
|||
maybe_comment_done(<<_/?encoding, Rest/binary>>, Resume) ->
|
||||
comment(Rest, Resume);
|
||||
maybe_comment_done(Bin, Resume) ->
|
||||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> maybe_comment_done(<<Bin/binary, Stream/binary>>, Resume) end,
|
||||
?ferror
|
||||
).
|
||||
case ?partial_codepoint(Bin) of
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> maybe_comment_done(<<Bin/binary, Stream/binary>>, Resume)
|
||||
end
|
||||
}
|
||||
; false -> {error, badjson}
|
||||
end.
|
|
@ -80,10 +80,14 @@ decode(JSON, Flags) ->
|
|||
P = jsx:parser(Flags),
|
||||
decode_loop(P(JSON), []).
|
||||
|
||||
decode_loop({incomplete, _Next, Force}, Acc) ->
|
||||
decode_loop(Force(), Acc);
|
||||
decode_loop({event, end_json, _}, Acc) ->
|
||||
lists:reverse(Acc);
|
||||
decode_loop({incomplete, Next}, Acc) ->
|
||||
case Next(end_stream) of
|
||||
{error, badjson} -> {error, badjson}
|
||||
; ok -> lists:reverse(Acc)
|
||||
; X -> decode_loop(X, Acc)
|
||||
end;
|
||||
decode_loop({event, end_json, Next}, Acc) ->
|
||||
decode_loop(Next(), Acc);
|
||||
decode_loop({event, E, Next}, Acc) ->
|
||||
decode_loop(Next(), [E] ++ Acc).
|
||||
|
||||
|
@ -92,16 +96,16 @@ incremental_decode(<<C:1/binary, Rest/binary>>, Flags) ->
|
|||
P = jsx:parser(Flags),
|
||||
incremental_decode_loop(P(C), Rest, []).
|
||||
|
||||
incremental_decode_loop({incomplete, Next, _}, <<C:1/binary, Rest/binary>>, Acc) ->
|
||||
incremental_decode_loop({incomplete, Next}, <<C:1/binary, Rest/binary>>, Acc) ->
|
||||
incremental_decode_loop(Next(C), Rest, Acc);
|
||||
incremental_decode_loop({incomplete, _Next, Force}, <<>>, Acc) ->
|
||||
case Force() of
|
||||
incremental_decode_loop({incomplete, Next}, <<>>, Acc) ->
|
||||
case Next(end_stream) of
|
||||
{error, badjson} -> {error, badjson}
|
||||
; {incomplete, _, _} -> Acc
|
||||
; _ -> incremental_decode_loop(Force(), <<>>, Acc)
|
||||
; ok -> lists:reverse(Acc)
|
||||
; X -> incremental_decode_loop(X, <<>>, Acc)
|
||||
end;
|
||||
incremental_decode_loop({event, end_json, Next}, Rest, Acc) ->
|
||||
incremental_decode_loop(Next(), Rest, lists:reverse(Acc));
|
||||
incremental_decode_loop(Next(), Rest, Acc);
|
||||
incremental_decode_loop({event, Event, Next}, Rest, Acc) ->
|
||||
incremental_decode_loop(Next(), Rest, [Event] ++ Acc).
|
||||
|
||||
|
@ -110,7 +114,7 @@ multi_decode(JSON, Flags) ->
|
|||
P = jsx:parser(Flags ++ [{multi_term, true}]),
|
||||
multi_decode_loop(P(JSON), [[]]).
|
||||
|
||||
multi_decode_loop({incomplete, _Next, _Force}, [[]|Acc]) ->
|
||||
multi_decode_loop({incomplete, _Next}, [[]|Acc]) ->
|
||||
lists:reverse(Acc);
|
||||
multi_decode_loop({event, end_json, Next}, [S|Acc]) ->
|
||||
multi_decode_loop(Next(), [[]|[lists:reverse(S)] ++ Acc]);
|
||||
|
|
32
src/jsx.erl
32
src/jsx.erl
|
@ -119,38 +119,42 @@ detect_encoding(<<X, Y, _Rest/binary>> = JSON, Opts) when X =/= 0, Y =/= 0 ->
|
|||
%% the problem
|
||||
detect_encoding(<<X>>, Opts) when X =/= 0 ->
|
||||
{incomplete,
|
||||
fun(Stream) -> detect_encoding(<<X, Stream/binary>>, Opts) end,
|
||||
fun() -> try
|
||||
{incomplete, _, Force} = jsx_utf8:parse(<<X>>, Opts),
|
||||
Force()
|
||||
fun(end_stream) ->
|
||||
try
|
||||
{incomplete, Next} = jsx_utf8:parse(<<X>>, Opts),
|
||||
Next(end_stream)
|
||||
catch error:function_clause -> {error, badjson}
|
||||
end
|
||||
; (Stream) -> detect_encoding(<<X, Stream/binary>>, Opts)
|
||||
end
|
||||
};
|
||||
detect_encoding(<<0, X>>, Opts) when X =/= 0 ->
|
||||
{incomplete,
|
||||
fun(Stream) -> detect_encoding(<<0, X, Stream/binary>>, Opts) end,
|
||||
fun() -> try
|
||||
{incomplete, _, Force} = jsx_utf16:parse(<<0, X>>, Opts),
|
||||
Force()
|
||||
fun(end_stream) ->
|
||||
try
|
||||
{incomplete, Next} = jsx_utf16:parse(<<0, X>>, Opts),
|
||||
Next(end_stream)
|
||||
catch error:function_clause -> {error, badjson}
|
||||
end
|
||||
; (Stream) -> detect_encoding(<<0, X, Stream/binary>>, Opts)
|
||||
end
|
||||
};
|
||||
detect_encoding(<<X, 0>>, Opts) when X =/= 0 ->
|
||||
{incomplete,
|
||||
fun(Stream) -> detect_encoding(<<X, 0, Stream/binary>>, Opts) end,
|
||||
fun() -> try
|
||||
{incomplete, _, Force} = jsx_utf16le:parse(<<X, 0>>, Opts),
|
||||
Force()
|
||||
fun(end_stream) ->
|
||||
try
|
||||
{incomplete, Next} = jsx_utf16le:parse(<<X, 0>>, Opts),
|
||||
Next(end_stream)
|
||||
catch error:function_clause -> {error, badjson}
|
||||
end
|
||||
; (Stream) -> detect_encoding(<<X, 0, Stream/binary>>, Opts)
|
||||
end
|
||||
};
|
||||
|
||||
%% not enough input, request more
|
||||
detect_encoding(Bin, Opts) ->
|
||||
{incomplete,
|
||||
fun(Stream) -> detect_encoding(<<Bin/binary, Stream/binary>>, Opts) end,
|
||||
fun() -> {error, badjson} end
|
||||
fun(end_stream) -> {error, badjson}
|
||||
; (Stream) -> detect_encoding(<<Bin/binary, Stream/binary>>, Opts)
|
||||
end
|
||||
}.
|
Loading…
Add table
Add a link
Reference in a new issue