incomplete work on multi term streams
This commit is contained in:
parent
37f9d2a07a
commit
46df6e5a38
4 changed files with 31 additions and 11 deletions
17
src/jsx.erl
17
src/jsx.erl
|
@ -160,14 +160,19 @@ parse_opts(Opts) ->
|
|||
|
||||
parse_opts([], Opts) ->
|
||||
Opts;
|
||||
parse_opts([{comments, Value}|Rest], {_Comments, EscapedUnicode, Stream}) ->
|
||||
parse_opts([{comments, Value}|Rest], {_Comments, EscapedUnicode, Multi}) ->
|
||||
true = lists:member(Value, [true, false]),
|
||||
parse_opts(Rest, {Value, EscapedUnicode, Stream});
|
||||
parse_opts([{escaped_unicode, Value}|Rest], {Comments, _EscapedUnicode, Stream}) ->
|
||||
parse_opts(Rest, {Value, EscapedUnicode, Multi});
|
||||
parse_opts([{escaped_unicode, Value}|Rest], {Comments, _EscapedUnicode, Multi}) ->
|
||||
true = lists:member(Value, [ascii, codepoint, none]),
|
||||
parse_opts(Rest, {Comments, Value, Stream});
|
||||
parse_opts([{stream_mode, Value}|Rest], {Comments, EscapedUnicode, _Stream}) ->
|
||||
true = lists:member(Value, [true, false]),
|
||||
parse_opts(Rest, {Comments, Value, Multi});
|
||||
parse_opts([{multi_term, Value}|Rest], {Comments, EscapedUnicode, _Multi}) ->
|
||||
ok = case Value of
|
||||
S when is_binary(S) -> ok
|
||||
; whitespace -> ok
|
||||
; true -> ok
|
||||
; false -> ok
|
||||
end,
|
||||
parse_opts(Rest, {Comments, EscapedUnicode, Value});
|
||||
parse_opts([{encoding, _}|Rest], Opts) ->
|
||||
parse_opts(Rest, Opts).
|
||||
|
|
|
@ -88,6 +88,10 @@ maybe_done(<<?comma/?encoding, Rest/binary>>, [array|_] = Stack, Opts) ->
|
|||
value(Rest, Stack, Opts);
|
||||
maybe_done(<<?solidus/?encoding, Rest/binary>>, Stack, ?comments_enabled(Opts)) ->
|
||||
maybe_comment(Rest, fun(Resume) -> maybe_done(Resume, Stack, Opts) end);
|
||||
maybe_done(Bin, [], ?multi_term(Opts)) ->
|
||||
{event, end_json, fun(Stream) ->
|
||||
Rest = strip(<<Bin/binary, Stream/binary>>, Opts),
|
||||
start(<<Bin/binary, Stream/binary>>, [], Opts) end};
|
||||
maybe_done(<<>>, [], Opts) ->
|
||||
{event, end_json, fun(Stream) -> maybe_done(Stream, [], Opts) end};
|
||||
maybe_done(Bin, Stack, Opts) ->
|
||||
|
@ -635,8 +639,8 @@ null(Bin, Stack, Opts) ->
|
|||
).
|
||||
|
||||
|
||||
%% comments are c style, /* blah blah */ and are STRONGLY discouraged. any unicode
|
||||
%% character is valid in a comment, except, obviously the */ sequence which ends
|
||||
%% comments are c style, ex: /* blah blah */
|
||||
%% any unicode character is valid in a comment except the */ sequence which ends
|
||||
%% the comment. they're implemented as a closure called when the comment ends that
|
||||
%% returns execution to the point where the comment began. comments are not
|
||||
%% recorded in any way, simply parsed.
|
||||
|
@ -669,4 +673,15 @@ maybe_comment_done(Bin, Resume) ->
|
|||
?incomplete(?partial_codepoint(Bin),
|
||||
fun(Stream) -> maybe_comment_done(<<Bin/binary, Stream/binary>>, Resume) end,
|
||||
?ferror
|
||||
).
|
||||
).
|
||||
|
||||
|
||||
%% strip whitespace and comments (if enabled) from a stream, returning the
|
||||
%% stream when the first non-whitespace/comment character is encountered
|
||||
|
||||
strip(<<S/?encoding, Rest/binary>>, Opts) when ?is_whitespace(S) ->
|
||||
strip(Rest, Opts);
|
||||
strip(<<?solidus/?encoding, Rest/binary>>, ?comments_enabled(Opts)) ->
|
||||
maybe_comment(Rest, fun(Resume) -> strip(Resume, Opts) end);
|
||||
strip(Bin, _Opts) ->
|
||||
Bin.
|
|
@ -25,7 +25,7 @@
|
|||
-define(comments_enabled(X), {true, _, _} = X).
|
||||
-define(escaped_unicode_to_ascii(X), {_, ascii, _} = X).
|
||||
-define(escaped_unicode_to_codepoint(X), {_, codepoint, _} = X).
|
||||
-define(stream_mode(X), {_, _, true} = X).
|
||||
-define(multi_term(X), {_, _, true} = X).
|
||||
|
||||
%% whitespace
|
||||
-define(space, 16#20).
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
-type jsx_opts() :: [jsx_opt()].
|
||||
-type jsx_opt() :: {comments, true | false}
|
||||
| {escaped_unicode, ascii | codepoint | none}
|
||||
| {stream_mode, true | false}
|
||||
| {multi_term, true | false | whitespace | binary()}
|
||||
| {encoding, auto | utf8 | utf16 | utf16le | utf32 | utf32le }.
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue