incomplete work on multi term streams

This commit is contained in:
alisdair sullivan 2010-06-23 16:43:09 -07:00
parent 37f9d2a07a
commit 46df6e5a38
4 changed files with 31 additions and 11 deletions

View file

@ -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).

View file

@ -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.
@ -670,3 +674,14 @@ maybe_comment_done(Bin, Resume) ->
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.

View file

@ -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).

View file

@ -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 }.