2010-06-01 01:03:28 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
|
|
|
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
|
|
|
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
|
2010-08-22 11:31:14 -07:00
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
%% this is the implementation of the utf backends for the jsx decoder. it's
|
|
|
|
%% included by the various jsx_utfxx.erl frontends and all modifications to
|
2010-12-28 00:30:25 -08:00
|
|
|
%% this file should take that into account
|
2010-08-22 11:31:14 -07:00
|
|
|
|
|
|
|
|
2011-07-21 06:14:48 -07:00
|
|
|
-spec decoder(OptsList::jsx_opts()) -> jsx_decoder().
|
2010-09-15 21:30:25 -07:00
|
|
|
|
2010-08-23 13:36:53 -07:00
|
|
|
%% opts record for decoder
|
|
|
|
-record(opts, {
|
|
|
|
multi_term = false,
|
|
|
|
encoding = auto
|
|
|
|
}).
|
2010-08-19 18:22:34 -07:00
|
|
|
|
|
|
|
|
2010-06-01 01:03:28 -07:00
|
|
|
%% whitespace
|
|
|
|
-define(space, 16#20).
|
|
|
|
-define(tab, 16#09).
|
|
|
|
-define(cr, 16#0D).
|
|
|
|
-define(newline, 16#0A).
|
|
|
|
|
|
|
|
%% object delimiters
|
|
|
|
-define(start_object, 16#7B).
|
|
|
|
-define(end_object, 16#7D).
|
|
|
|
|
|
|
|
%% array delimiters
|
|
|
|
-define(start_array, 16#5B).
|
|
|
|
-define(end_array, 16#5D).
|
|
|
|
|
|
|
|
%% kv seperator
|
|
|
|
-define(comma, 16#2C).
|
|
|
|
-define(quote, 16#22).
|
|
|
|
-define(colon, 16#3A).
|
|
|
|
|
|
|
|
%% string escape sequences
|
|
|
|
-define(escape, 16#5C).
|
|
|
|
-define(rsolidus, 16#5C).
|
|
|
|
-define(solidus, 16#2F).
|
|
|
|
-define(formfeed, 16#0C).
|
|
|
|
-define(backspace, 16#08).
|
|
|
|
-define(unicode, 16#75).
|
|
|
|
|
|
|
|
%% math
|
|
|
|
-define(zero, 16#30).
|
|
|
|
-define(decimalpoint, 16#2E).
|
|
|
|
-define(negative, 16#2D).
|
|
|
|
-define(positive, 16#2B).
|
|
|
|
|
|
|
|
|
2010-06-07 16:14:22 -07:00
|
|
|
|
|
|
|
%% some useful guards
|
2010-06-01 01:03:28 -07:00
|
|
|
-define(is_hex(Symbol),
|
|
|
|
(Symbol >= $a andalso Symbol =< $z); (Symbol >= $A andalso Symbol =< $Z);
|
|
|
|
(Symbol >= $0 andalso Symbol =< $9)
|
|
|
|
).
|
|
|
|
|
|
|
|
-define(is_nonzero(Symbol),
|
|
|
|
Symbol >= $1 andalso Symbol =< $9
|
|
|
|
).
|
|
|
|
|
|
|
|
-define(is_noncontrol(Symbol),
|
|
|
|
Symbol >= ?space
|
|
|
|
).
|
|
|
|
|
|
|
|
-define(is_whitespace(Symbol),
|
|
|
|
Symbol =:= ?space; Symbol =:= ?tab; Symbol =:= ?cr; Symbol =:= ?newline
|
|
|
|
).
|
|
|
|
|
2010-06-07 16:14:22 -07:00
|
|
|
|
2010-08-23 13:40:56 -07:00
|
|
|
%% partial codepoint max size differs across encodings
|
2010-06-07 16:14:22 -07:00
|
|
|
-ifdef(utf8).
|
2010-08-25 23:17:10 -07:00
|
|
|
-define(utfx, utf8).
|
2010-07-27 00:05:15 -07:00
|
|
|
-define(partial_codepoint(Bin), byte_size(Bin) < 1).
|
2010-06-07 16:14:22 -07:00
|
|
|
-endif.
|
|
|
|
|
|
|
|
-ifdef(utf16).
|
2010-08-25 23:17:10 -07:00
|
|
|
-define(utfx, utf16).
|
2010-07-27 00:05:15 -07:00
|
|
|
-define(partial_codepoint(Bin), byte_size(Bin) < 2).
|
2010-06-07 16:14:22 -07:00
|
|
|
-endif.
|
|
|
|
|
|
|
|
-ifdef(utf16le).
|
2010-08-25 23:17:10 -07:00
|
|
|
-define(utfx, utf16-little).
|
2010-07-27 00:05:15 -07:00
|
|
|
-define(partial_codepoint(Bin), byte_size(Bin) < 2).
|
2010-06-07 16:14:22 -07:00
|
|
|
-endif.
|
|
|
|
|
|
|
|
-ifdef(utf32).
|
2010-08-25 23:17:10 -07:00
|
|
|
-define(utfx, utf32).
|
2010-07-27 00:05:15 -07:00
|
|
|
-define(partial_codepoint(Bin), byte_size(Bin) < 4).
|
2010-06-07 16:14:22 -07:00
|
|
|
-endif.
|
|
|
|
|
|
|
|
-ifdef(utf32le).
|
2010-08-25 23:17:10 -07:00
|
|
|
-define(utfx, utf32-little).
|
2010-07-27 00:05:15 -07:00
|
|
|
-define(partial_codepoint(Bin), byte_size(Bin) < 4).
|
2010-08-22 11:31:14 -07:00
|
|
|
-endif.
|
|
|
|
|
|
|
|
|
2011-04-26 23:40:12 -07:00
|
|
|
-export([decoder/1]).
|
2010-08-23 13:36:53 -07:00
|
|
|
|
2010-08-22 11:31:14 -07:00
|
|
|
|
|
|
|
|
2011-04-26 23:40:12 -07:00
|
|
|
decoder(OptsList) ->
|
2010-08-23 13:36:53 -07:00
|
|
|
case parse_opts(OptsList) of
|
|
|
|
{error, badopt} -> {error, badopt}
|
|
|
|
; Opts -> fun(JSON) -> start(JSON, [], Opts) end
|
|
|
|
end.
|
|
|
|
|
2010-08-22 11:31:14 -07:00
|
|
|
|
2010-08-23 13:36:53 -07:00
|
|
|
%% converts a proplist into a tuple
|
|
|
|
parse_opts(Opts) ->
|
|
|
|
parse_opts(Opts, #opts{}).
|
|
|
|
|
|
|
|
parse_opts([], Opts) ->
|
2011-07-03 17:42:44 -07:00
|
|
|
Opts;
|
2010-08-23 13:36:53 -07:00
|
|
|
parse_opts([{multi_term, Value}|Rest], Opts) ->
|
|
|
|
true = lists:member(Value, [true, false]),
|
2010-08-25 23:17:10 -07:00
|
|
|
parse_opts(Rest, Opts#opts{multi_term=Value});
|
2011-03-07 14:02:51 -08:00
|
|
|
parse_opts([multi_term|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{multi_term=true});
|
2010-08-23 13:36:53 -07:00
|
|
|
parse_opts([{encoding, _}|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts);
|
|
|
|
parse_opts(_, _) ->
|
|
|
|
{error, badarg}.
|
2010-08-22 11:31:14 -07:00
|
|
|
|
|
|
|
|
2010-08-23 13:42:05 -07:00
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
start(<<S/?utfx, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
start(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
start(<<?start_object/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, start_object, fun() -> object(Rest, [key|Stack], Opts) end};
|
2010-08-25 23:17:10 -07:00
|
|
|
start(<<?start_array/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, start_array, fun() -> array(Rest, [array|Stack], Opts) end};
|
2010-08-25 23:17:10 -07:00
|
|
|
start(<<?quote/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
start(<<$t/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
tr(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
start(<<$f/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
fa(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
start(<<$n/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
nu(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
start(<<?negative/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
negative(Rest, Stack, Opts, "-");
|
2010-08-25 23:17:10 -07:00
|
|
|
start(<<?zero/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
zero(Rest, Stack, Opts, "0");
|
2010-08-25 23:17:10 -07:00
|
|
|
start(<<S/?utfx, Rest/binary>>, Stack, Opts) when ?is_nonzero(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
integer(Rest, Stack, Opts, [S]);
|
|
|
|
start(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
start(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(<<S/?utfx, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
maybe_done(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(<<?end_object/?utfx, Rest/binary>>, [object|Stack], Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, end_object, fun() -> maybe_done(Rest, Stack, Opts) end};
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(<<?end_array/?utfx, Rest/binary>>, [array|Stack], Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, end_array, fun() -> maybe_done(Rest, Stack, Opts) end};
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(<<?comma/?utfx, Rest/binary>>, [object|Stack], Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
key(Rest, [key|Stack], Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(<<?comma/?utfx, Rest/binary>>, [array|_] = Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
value(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(Rest, [], #opts{multi_term=true}=Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, end_json, fun() -> start(Rest, [], Opts) end};
|
2010-08-22 11:31:14 -07:00
|
|
|
maybe_done(Rest, [], Opts) ->
|
|
|
|
done(Rest, Opts);
|
|
|
|
maybe_done(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
maybe_done(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
done(<<S/?utfx, Rest/binary>>, Opts) when ?is_whitespace(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
done(Rest, Opts);
|
|
|
|
done(<<>>, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, end_json, fun() ->
|
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, <<>>}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
done(Stream, Opts)
|
|
|
|
end}
|
|
|
|
end};
|
2010-08-22 11:31:14 -07:00
|
|
|
done(Bin, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
done(<<Bin/binary, Stream/binary>>, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
object(<<S/?utfx, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
object(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
object(<<?quote/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
object(<<?end_object/?utfx, Rest/binary>>, [key|Stack], Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, end_object, fun() -> maybe_done(Rest, Stack, Opts) end};
|
2010-08-22 11:31:14 -07:00
|
|
|
object(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
object(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<S/?utfx, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
array(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<?quote/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<$t/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
tr(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<$f/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
fa(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<$n/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
nu(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<?negative/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
negative(Rest, Stack, Opts, "-");
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<?zero/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
zero(Rest, Stack, Opts, "0");
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<S/?utfx, Rest/binary>>, Stack, Opts) when ?is_nonzero(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
integer(Rest, Stack, Opts, [S]);
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<?start_object/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, start_object, fun() -> object(Rest, [key|Stack], Opts) end};
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<?start_array/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, start_array, fun() -> array(Rest, [array|Stack], Opts) end};
|
2010-08-25 23:17:10 -07:00
|
|
|
array(<<?end_array/?utfx, Rest/binary>>, [array|Stack], Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, end_array, fun() -> maybe_done(Rest, Stack, Opts) end};
|
2010-08-22 11:31:14 -07:00
|
|
|
array(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
array(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
value(<<S/?utfx, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
value(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
value(<<?quote/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
value(<<$t/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
tr(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
value(<<$f/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
fa(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
value(<<$n/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
nu(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
value(<<?negative/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
negative(Rest, Stack, Opts, "-");
|
2010-08-25 23:17:10 -07:00
|
|
|
value(<<?zero/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
zero(Rest, Stack, Opts, "0");
|
2010-08-25 23:17:10 -07:00
|
|
|
value(<<S/?utfx, Rest/binary>>, Stack, Opts) when ?is_nonzero(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
integer(Rest, Stack, Opts, [S]);
|
2010-08-25 23:17:10 -07:00
|
|
|
value(<<?start_object/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, start_object, fun() -> object(Rest, [key|Stack], Opts) end};
|
2010-08-25 23:17:10 -07:00
|
|
|
value(<<?start_array/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, start_array, fun() -> array(Rest, [array|Stack], Opts) end};
|
2010-08-22 11:31:14 -07:00
|
|
|
value(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
value(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
colon(<<S/?utfx, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
colon(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
colon(<<?colon/?utfx, Rest/binary>>, [key|Stack], Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
value(Rest, [object|Stack], Opts);
|
|
|
|
colon(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
colon(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
key(<<S/?utfx, Rest/binary>>, Stack, Opts) when ?is_whitespace(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
key(Rest, Stack, Opts);
|
2010-08-25 23:17:10 -07:00
|
|
|
key(<<?quote/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts);
|
2010-08-22 11:31:14 -07:00
|
|
|
key(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
key(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
%% string has an additional parameter, an accumulator (Acc) used to hold the
|
|
|
|
%% intermediate representation of the string being parsed. using a list of
|
|
|
|
%% integers representing unicode codepoints is faster than constructing
|
|
|
|
%% binaries, many of which will be converted back to lists by the user anyways
|
|
|
|
%% string uses partial_utf/1 to cease parsing when invalid encodings are
|
|
|
|
%% encountered rather than just checking remaining binary size like other
|
|
|
|
%% states
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Bin, Stack, Opts) -> string(Bin, Stack, Opts, <<>>).
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
string(<<?quote/?utfx, Rest/binary>>, [key|_] = Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, {key, Acc}, fun() -> colon(Rest, Stack, Opts) end};
|
2010-08-25 23:17:10 -07:00
|
|
|
string(<<?quote/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, {string, Acc}, fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(Rest, Stack, Opts)
|
|
|
|
end};
|
|
|
|
string(<<?rsolidus/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
escape(Rest, Stack, Opts, Acc);
|
2010-08-25 23:17:10 -07:00
|
|
|
string(<<S/?utfx, Rest/binary>>, Stack, Opts, Acc) when ?is_noncontrol(S) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts, <<Acc/binary, S/utf8>>);
|
2010-08-22 11:31:14 -07:00
|
|
|
string(Bin, Stack, Opts, Acc) ->
|
|
|
|
case partial_utf(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
string(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
-ifdef(utf8).
|
|
|
|
partial_utf(<<>>) -> true;
|
|
|
|
partial_utf(<<X>>) when X >= 16#c2, X =< 16#df -> true;
|
|
|
|
partial_utf(<<X, Rest/binary>>) when X >= 16#e0, X =< 16#ef ->
|
|
|
|
case Rest of
|
|
|
|
<<>> -> true
|
|
|
|
; <<Y>> when Y >= 16#80, Y =< 16#bf -> true
|
|
|
|
end;
|
|
|
|
partial_utf(<<X, Rest/binary>>) when X >= 16#f0, X =< 16#f4 ->
|
|
|
|
case Rest of
|
|
|
|
<<>> -> true
|
|
|
|
; <<Y>> when Y >= 16#80, Y =< 16#bf -> true
|
|
|
|
; <<Y, Z>> when Y >= 16#80, Y =< 16#bf, Z >= 16#80, Z =< 16#bf -> true
|
|
|
|
end;
|
|
|
|
partial_utf(_) -> false.
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
-ifdef(utf16).
|
2010-08-25 23:17:10 -07:00
|
|
|
partial_utf(<<>>) ->
|
|
|
|
true;
|
|
|
|
%% this case is not strictly true, there are single bytes that should be
|
|
|
|
%% rejected, but they're rare enough they can be ignored
|
|
|
|
partial_utf(<<_X>>) ->
|
|
|
|
true;
|
|
|
|
partial_utf(<<X, _Y>>) when X >= 16#d8, X =< 16#df ->
|
|
|
|
true;
|
|
|
|
partial_utf(<<X, _Y, Z>>) when X >= 16#d8, X =< 16#df, Z >= 16#dc, Z =< 16#df ->
|
|
|
|
true;
|
|
|
|
partial_utf(_) ->
|
|
|
|
false.
|
2010-08-22 11:31:14 -07:00
|
|
|
-endif.
|
|
|
|
|
|
|
|
-ifdef(utf16le).
|
|
|
|
partial_utf(<<>>) -> true;
|
2010-08-25 23:17:10 -07:00
|
|
|
%% this case is not strictly true, there are single bytes that should be
|
|
|
|
%% rejected, but they're rare enough they can be ignored
|
2010-08-22 11:31:14 -07:00
|
|
|
partial_utf(<<_X>>) -> true;
|
|
|
|
partial_utf(<<_Y, X>>) when X >= 16#d8, X =< 16#df -> true;
|
|
|
|
partial_utf(<<_Y, X, _Z>>) when X >= 16#d8, X =< 16#df -> true;
|
|
|
|
partial_utf(_) -> false.
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
-ifdef(utf32).
|
|
|
|
partial_utf(<<_:32>>) -> false;
|
|
|
|
partial_utf(_) -> true.
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
-ifdef(utf32le).
|
|
|
|
partial_utf(<<_:32>>) -> false;
|
|
|
|
partial_utf(_) -> true.
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
%% only thing to note here is the additional accumulator passed to
|
|
|
|
%% escaped_unicode used to hold the codepoint sequence. unescessary, but nicer
|
|
|
|
%% than using the string accumulator
|
|
|
|
escape(<<$b/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts, <<Acc/binary, "\b">>);
|
2010-08-25 23:17:10 -07:00
|
|
|
escape(<<$f/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts, <<Acc/binary, "\f">>);
|
2010-08-25 23:17:10 -07:00
|
|
|
escape(<<$n/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts, <<Acc/binary, "\n">>);
|
2010-08-25 23:17:10 -07:00
|
|
|
escape(<<$r/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts, <<Acc/binary, "\r">>);
|
2010-08-25 23:17:10 -07:00
|
|
|
escape(<<$t/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts, <<Acc/binary, "\t">>);
|
2010-08-25 23:17:10 -07:00
|
|
|
escape(<<$u/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
escaped_unicode(Rest, Stack, Opts, Acc, []);
|
2010-08-25 23:17:10 -07:00
|
|
|
escape(<<S/?utfx, Rest/binary>>, Stack, Opts, Acc)
|
2010-08-22 11:31:14 -07:00
|
|
|
when S =:= ?quote; S =:= ?solidus; S =:= ?rsolidus ->
|
2011-07-26 00:35:17 -07:00
|
|
|
string(Rest, Stack, Opts, <<Acc/binary, S/utf8>>);
|
2010-08-22 11:31:14 -07:00
|
|
|
escape(Bin, Stack, Opts, Acc) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
escape(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
%% this code is ugly and unfortunate, but so is json's handling of escaped
|
2011-07-22 23:47:35 -07:00
|
|
|
%% unicode codepoint sequences.
|
2011-07-26 00:35:17 -07:00
|
|
|
%% fuck json escaping. new rule: if it's not a valid codepoint, it's an error
|
|
|
|
escaped_unicode(<<D/?utfx, Rest/binary>>, Stack, Opts, String, [C, B, A])
|
|
|
|
when ?is_hex(D) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
case erlang:list_to_integer([A, B, C, D], 16) of
|
2011-07-26 00:35:17 -07:00
|
|
|
%% high surrogate, we need a low surrogate next
|
|
|
|
X when X >= 16#d800, X =< 16#dbff ->
|
|
|
|
low_surrogate(Rest, Stack, Opts, String, X)
|
|
|
|
%% non-characters, you're not allowed to exchange these
|
|
|
|
; X when X == 16#fffe; X == 16#ffff ->
|
|
|
|
{error, {badjson, <<D/?utfx, Rest/binary>>}}
|
2011-07-26 13:34:15 -07:00
|
|
|
%% allowing interchange of null bytes allows attackers to forge
|
|
|
|
%% malicious streams
|
|
|
|
; X when X == 16#0000 ->
|
|
|
|
{error, {badjson, <<D/?utfx, Rest/binary>>}}
|
2011-07-26 00:35:17 -07:00
|
|
|
%% anything else
|
|
|
|
; X ->
|
|
|
|
string(Rest, Stack, Opts, <<String/binary, X/utf8>>)
|
2010-08-22 11:31:14 -07:00
|
|
|
end;
|
2010-08-25 23:17:10 -07:00
|
|
|
escaped_unicode(<<S/?utfx, Rest/binary>>, Stack, Opts, String, Acc)
|
2011-07-26 00:35:17 -07:00
|
|
|
when ?is_hex(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
escaped_unicode(Rest, Stack, Opts, String, [S] ++ Acc);
|
|
|
|
escaped_unicode(Bin, Stack, Opts, String, Acc) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
escaped_unicode(<<Bin/binary, Stream/binary>>,
|
|
|
|
Stack,
|
|
|
|
Opts,
|
|
|
|
String,
|
|
|
|
Acc
|
|
|
|
)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-07-26 00:35:17 -07:00
|
|
|
low_surrogate(<<?rsolidus/?utfx, Rest/binary>>, Stack, Opts, String, High) ->
|
|
|
|
low_surrogate_u(Rest, Stack, Opts, String, High);
|
|
|
|
low_surrogate(Bin, Stack, Opts, String, High) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
|
|
|
true ->
|
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
|
|
|
{error, {badjson, Bin}}
|
|
|
|
; (Stream) ->
|
|
|
|
low_surrogate(<<Bin/binary, Stream/binary>>,
|
|
|
|
Stack,
|
|
|
|
Opts,
|
|
|
|
String,
|
|
|
|
High
|
|
|
|
)
|
|
|
|
end}
|
|
|
|
; false -> {error, {badjson, Bin}}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
low_surrogate_u(<<$u/?utfx, Rest/binary>>, Stack, Opts, String, High) ->
|
|
|
|
low_surrogate(Rest, Stack, Opts, String, [], High);
|
|
|
|
low_surrogate_u(Bin, Stack, Opts, String, High) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
|
|
|
true ->
|
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
|
|
|
{error, {badjson, Bin}}
|
|
|
|
; (Stream) ->
|
|
|
|
low_surrogate_u(<<Bin/binary, Stream/binary>>,
|
|
|
|
Stack,
|
|
|
|
Opts,
|
|
|
|
String,
|
|
|
|
High
|
|
|
|
)
|
|
|
|
end}
|
|
|
|
; false -> {error, {badjson, Bin}}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
low_surrogate(<<D/?utfx, Rest/binary>>, Stack, Opts, String, [C, B, A], High)
|
|
|
|
when ?is_hex(D) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
case erlang:list_to_integer([A, B, C, D], 16) of
|
2011-07-26 00:35:17 -07:00
|
|
|
X when X >= 16#dc00, X =< 16#dfff ->
|
|
|
|
string(Rest,
|
|
|
|
Stack,
|
|
|
|
Opts,
|
|
|
|
<<String/binary, (surrogate_to_codepoint(High, X))/utf8>>
|
|
|
|
)
|
|
|
|
%% not a low surrogate, bad bad bad
|
|
|
|
; X ->
|
|
|
|
{error, {badjson, <<X/?utfx, Rest/binary>>}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end;
|
2011-07-26 00:35:17 -07:00
|
|
|
low_surrogate(<<S/?utfx, Rest/binary>>, Stack, Opts, String, Acc, High)
|
|
|
|
when ?is_hex(S) ->
|
|
|
|
low_surrogate(Rest, Stack, Opts, String, [S] ++ Acc, High);
|
|
|
|
low_surrogate(Bin, Stack, Opts, String, Acc, High) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
|
|
|
true ->
|
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
|
|
|
{error, {badjson, Bin}}
|
|
|
|
; (Stream) ->
|
|
|
|
low_surrogate(<<Bin/binary, Stream/binary>>,
|
|
|
|
Stack,
|
|
|
|
Opts,
|
|
|
|
String,
|
|
|
|
Acc,
|
|
|
|
High
|
|
|
|
)
|
|
|
|
end}
|
|
|
|
; false -> {error, {badjson, Bin}}
|
|
|
|
end.
|
2010-08-22 11:31:14 -07:00
|
|
|
|
|
|
|
|
|
|
|
%% stole this from the unicode spec
|
|
|
|
surrogate_to_codepoint(High, Low) ->
|
|
|
|
(High - 16#d800) * 16#400 + (Low - 16#dc00) + 16#10000.
|
|
|
|
|
|
|
|
|
|
|
|
%% like strings, numbers are collected in an intermediate accumulator before
|
|
|
|
%% being emitted to the callback handler
|
2010-08-25 23:17:10 -07:00
|
|
|
negative(<<$0/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
zero(Rest, Stack, Opts, "0" ++ Acc);
|
2010-08-25 23:17:10 -07:00
|
|
|
negative(<<S/?utfx, Rest/binary>>, Stack, Opts, Acc) when ?is_nonzero(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
integer(Rest, Stack, Opts, [S] ++ Acc);
|
|
|
|
negative(Bin, Stack, Opts, Acc) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
negative(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
zero(<<?end_object/?utfx, Rest/binary>>, [object|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_object, fun() -> maybe_done(Rest, Stack, Opts) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end};
|
2010-08-25 23:17:10 -07:00
|
|
|
zero(<<?end_array/?utfx, Rest/binary>>, [array|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_array, fun() -> maybe_done(Rest, Stack, Opts) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end};
|
2010-08-25 23:17:10 -07:00
|
|
|
zero(<<?comma/?utfx, Rest/binary>>, [object|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
key(Rest, [key|Stack], Opts)
|
|
|
|
end};
|
|
|
|
zero(<<?comma/?utfx, Rest/binary>>, [array|_] = Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
value(Rest, Stack, Opts)
|
|
|
|
end};
|
|
|
|
zero(<<?decimalpoint/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2011-07-10 10:54:04 -07:00
|
|
|
initial_decimal(Rest, Stack, Opts, {Acc, []});
|
2010-08-25 23:17:10 -07:00
|
|
|
zero(<<S/?utfx, Rest/binary>>, Stack, Opts, Acc) when ?is_whitespace(S) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(Rest, Stack, Opts)
|
|
|
|
end};
|
2010-08-22 11:31:14 -07:00
|
|
|
zero(<<>>, [], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_json, fun() -> zero(<<>>, [], Opts, Acc) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end}
|
|
|
|
; (Stream) -> zero(Stream, [], Opts, Acc)
|
|
|
|
end};
|
|
|
|
zero(Bin, Stack, Opts, Acc) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
zero(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
integer(<<S/?utfx, Rest/binary>>, Stack, Opts, Acc) when ?is_nonzero(S) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
integer(Rest, Stack, Opts, [S] ++ Acc);
|
2010-08-25 23:17:10 -07:00
|
|
|
integer(<<?end_object/?utfx, Rest/binary>>, [object|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_object, fun() -> maybe_done(Rest, Stack, Opts) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end};
|
2010-08-25 23:17:10 -07:00
|
|
|
integer(<<?end_array/?utfx, Rest/binary>>, [array|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_array, fun() -> maybe_done(Rest, Stack, Opts) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end};
|
2010-08-25 23:17:10 -07:00
|
|
|
integer(<<?comma/?utfx, Rest/binary>>, [object|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
key(Rest, [key|Stack], Opts)
|
|
|
|
end};
|
|
|
|
integer(<<?comma/?utfx, Rest/binary>>, [array|_] = Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
value(Rest, Stack, Opts)
|
|
|
|
end};
|
|
|
|
integer(<<?decimalpoint/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2011-07-10 10:54:04 -07:00
|
|
|
initial_decimal(Rest, Stack, Opts, {Acc, []});
|
2010-08-25 23:17:10 -07:00
|
|
|
integer(<<?zero/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
integer(Rest, Stack, Opts, [?zero] ++ Acc);
|
2011-07-10 10:54:04 -07:00
|
|
|
integer(<<S/?utfx, Rest/binary>>, Stack, Opts, Acc) when S =:= $e; S =:= $E ->
|
|
|
|
e(Rest, Stack, Opts, {lists:reverse(Acc), [], []});
|
2010-08-25 23:17:10 -07:00
|
|
|
integer(<<S/?utfx, Rest/binary>>, Stack, Opts, Acc) when ?is_whitespace(S) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(Rest, Stack, Opts)
|
|
|
|
end};
|
2010-08-22 11:31:14 -07:00
|
|
|
integer(<<>>, [], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_json, fun() -> integer(<<>>, [], Opts, Acc) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end}
|
|
|
|
; (Stream) -> integer(Stream, [], Opts, Acc)
|
|
|
|
end};
|
|
|
|
integer(Bin, Stack, Opts, Acc) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
integer(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-07-10 10:54:04 -07:00
|
|
|
initial_decimal(<<S/?utfx, Rest/binary>>, Stack, Opts, {Int, Frac})
|
|
|
|
when S =:= ?zero; ?is_nonzero(S) ->
|
|
|
|
decimal(Rest, Stack, Opts, {Int, [S] ++ Frac});
|
2010-08-25 23:17:10 -07:00
|
|
|
initial_decimal(Bin, Stack, Opts, Acc) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
initial_decimal(<<Bin/binary, Stream/binary>>,
|
|
|
|
Stack,
|
|
|
|
Opts,
|
|
|
|
Acc
|
|
|
|
)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-07-10 10:54:04 -07:00
|
|
|
decimal(<<S/?utfx, Rest/binary>>, Stack, Opts, {Int, Frac})
|
|
|
|
when S=:= ?zero; ?is_nonzero(S) ->
|
|
|
|
decimal(Rest, Stack, Opts, {Int, [S] ++ Frac});
|
2010-08-25 23:17:10 -07:00
|
|
|
decimal(<<?end_object/?utfx, Rest/binary>>, [object|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_object, fun() -> maybe_done(Rest, Stack, Opts) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end};
|
2010-08-25 23:17:10 -07:00
|
|
|
decimal(<<?end_array/?utfx, Rest/binary>>, [array|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_array, fun() -> maybe_done(Rest, Stack, Opts) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end};
|
2010-08-25 23:17:10 -07:00
|
|
|
decimal(<<?comma/?utfx, Rest/binary>>, [object|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
key(Rest, [key|Stack], Opts)
|
|
|
|
end};
|
|
|
|
decimal(<<?comma/?utfx, Rest/binary>>, [array|_] = Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
value(Rest, Stack, Opts)
|
|
|
|
end};
|
2011-07-10 10:54:04 -07:00
|
|
|
decimal(<<S/?utfx, Rest/binary>>, Stack, Opts, {Int, Frac})
|
|
|
|
when S =:= $e; S =:= $E ->
|
|
|
|
e(Rest, Stack, Opts, {Int, Frac, []});
|
2010-08-25 23:17:10 -07:00
|
|
|
decimal(<<S/?utfx, Rest/binary>>, Stack, Opts, Acc) when ?is_whitespace(S) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(Rest, Stack, Opts)
|
|
|
|
end};
|
2010-08-22 11:31:14 -07:00
|
|
|
decimal(<<>>, [], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_json, fun() -> decimal(<<>>, [], Opts, Acc) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end}
|
|
|
|
; (Stream) -> decimal(Stream, [], Opts, Acc)
|
|
|
|
end};
|
|
|
|
decimal(Bin, Stack, Opts, Acc) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
decimal(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-07-10 10:54:04 -07:00
|
|
|
e(<<S/?utfx, Rest/binary>>, Stack, Opts, {Int, Frac, Exp})
|
|
|
|
when S =:= ?zero; ?is_nonzero(S) ->
|
|
|
|
exp(Rest, Stack, Opts, {Int, Frac, [S] ++ Exp});
|
|
|
|
e(<<S/?utfx, Rest/binary>>, Stack, Opts, {Int, Frac, Exp})
|
|
|
|
when S =:= ?positive; S =:= ?negative ->
|
|
|
|
ex(Rest, Stack, Opts, {Int, Frac, [S] ++ Exp});
|
2010-08-22 11:31:14 -07:00
|
|
|
e(Bin, Stack, Opts, Acc) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
e(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-07-10 10:54:04 -07:00
|
|
|
ex(<<S/?utfx, Rest/binary>>, Stack, Opts, {Int, Frac, Exp})
|
|
|
|
when S =:= ?zero; ?is_nonzero(S) ->
|
|
|
|
exp(Rest, Stack, Opts, {Int, Frac, [S] ++ Exp});
|
2010-08-22 11:31:14 -07:00
|
|
|
ex(Bin, Stack, Opts, Acc) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
ex(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-07-10 10:54:04 -07:00
|
|
|
exp(<<S/?utfx, Rest/binary>>, Stack, Opts, {Int, Frac, Exp})
|
|
|
|
when S =:= ?zero; ?is_nonzero(S) ->
|
|
|
|
exp(Rest, Stack, Opts, {Int, Frac, [S] ++ Exp});
|
2010-08-25 23:17:10 -07:00
|
|
|
exp(<<?end_object/?utfx, Rest/binary>>, [object|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_object, fun() -> maybe_done(Rest, Stack, Opts) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end};
|
2010-08-25 23:17:10 -07:00
|
|
|
exp(<<?end_array/?utfx, Rest/binary>>, [array|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_array, fun() -> maybe_done(Rest, Stack, Opts) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end};
|
2010-08-25 23:17:10 -07:00
|
|
|
exp(<<?comma/?utfx, Rest/binary>>, [object|Stack], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
key(Rest, [key|Stack], Opts)
|
|
|
|
end};
|
|
|
|
exp(<<?comma/?utfx, Rest/binary>>, [array|_] = Stack, Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
value(Rest, Stack, Opts)
|
|
|
|
end};
|
|
|
|
exp(<<S/?utfx, Rest/binary>>, Stack, Opts, Acc) when ?is_whitespace(S) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, format_number(Acc), fun() ->
|
2010-08-25 23:17:10 -07:00
|
|
|
maybe_done(Rest, Stack, Opts)
|
|
|
|
end};
|
2010-08-22 11:31:14 -07:00
|
|
|
exp(<<>>, [], Opts, Acc) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
|
|
|
{jsx, format_number(Acc), fun() ->
|
|
|
|
{jsx, end_json, fun() -> exp(<<>>, [], Opts, Acc) end}
|
2010-08-22 11:31:14 -07:00
|
|
|
end}
|
|
|
|
; (Stream) -> exp(Stream, [], Opts, Acc)
|
|
|
|
end};
|
|
|
|
exp(Bin, Stack, Opts, Acc) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
exp(<<Bin/binary, Stream/binary>>, Stack, Opts, Acc)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-07-10 10:54:04 -07:00
|
|
|
format_number(Int) when is_list(Int) ->
|
|
|
|
{integer, list_to_integer(lists:reverse(Int))};
|
|
|
|
format_number({Int, Frac}) ->
|
|
|
|
{float, list_to_float(lists:reverse(Frac ++ "." ++ Int))};
|
|
|
|
format_number({Int, [], Exp}) ->
|
|
|
|
{float, list_to_float(lists:reverse(Exp ++ "e0." ++ Int))};
|
|
|
|
format_number({Int, Frac, Exp}) ->
|
|
|
|
{float, list_to_float(lists:reverse(Exp ++ "e" ++ Frac ++ "." ++ Int))}.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
tr(<<$r/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
tru(Rest, Stack, Opts);
|
|
|
|
tr(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
tr(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
tru(<<$u/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
true(Rest, Stack, Opts);
|
|
|
|
tru(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
tru(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
true(<<$e/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, {literal, true}, fun() -> maybe_done(Rest, Stack, Opts) end};
|
2010-08-22 11:31:14 -07:00
|
|
|
true(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
true(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
fa(<<$a/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
fal(Rest, Stack, Opts);
|
|
|
|
fa(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
fa(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
fal(<<$l/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
fals(Rest, Stack, Opts);
|
|
|
|
fal(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
fal(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
fals(<<$s/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
false(Rest, Stack, Opts);
|
|
|
|
fals(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
fals(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
false(<<$e/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, {literal, false}, fun() -> maybe_done(Rest, Stack, Opts) end};
|
2010-08-22 11:31:14 -07:00
|
|
|
false(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
false(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
nu(<<$u/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
nul(Rest, Stack, Opts);
|
|
|
|
nu(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
nu(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
nul(<<$l/?utfx, Rest/binary>>, Stack, Opts) ->
|
2010-08-22 11:31:14 -07:00
|
|
|
null(Rest, Stack, Opts);
|
|
|
|
nul(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
nul(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-08-25 23:17:10 -07:00
|
|
|
null(<<$l/?utfx, Rest/binary>>, Stack, Opts) ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, {literal, null}, fun() -> maybe_done(Rest, Stack, Opts) end};
|
2010-08-22 11:31:14 -07:00
|
|
|
null(Bin, Stack, Opts) ->
|
|
|
|
case ?partial_codepoint(Bin) of
|
2010-08-25 23:17:10 -07:00
|
|
|
true ->
|
2011-07-26 00:35:17 -07:00
|
|
|
{jsx, incomplete, fun(end_stream) ->
|
2010-09-27 14:07:36 -07:00
|
|
|
{error, {badjson, Bin}}
|
2010-08-25 23:17:10 -07:00
|
|
|
; (Stream) ->
|
|
|
|
null(<<Bin/binary, Stream/binary>>, Stack, Opts)
|
|
|
|
end}
|
2010-09-27 14:07:36 -07:00
|
|
|
; false -> {error, {badjson, Bin}}
|
2010-08-22 11:31:14 -07:00
|
|
|
end.
|