opts now uses a record definition, rather than a handwritten tuple

This commit is contained in:
alisdair sullivan 2010-06-26 07:27:38 -07:00
parent 528bdc4791
commit 11e0adc05e
2 changed files with 22 additions and 12 deletions

View file

@ -25,11 +25,21 @@
-author("alisdairsullivan@yahoo.ca"). -author("alisdairsullivan@yahoo.ca").
%% the core parser api %% the core parser api
-export([parser/0, parser/1]). -export([parser/0, parser/1, opts/0]).
%% types for function specifications %% types for function specifications
-include("jsx_types.hrl"). -include("jsx_types.hrl").
%% opts record
-record(opts, {
comments = false,
escaped_unicode = codepoint,
multi_term = false,
encoding = auto
}).
opts() ->
io:format("~p~n", [#opts{}]).
-spec parser() -> jsx_parser(). -spec parser() -> jsx_parser().
-spec parser(Opts::jsx_opts()) -> jsx_parser(). -spec parser(Opts::jsx_opts()) -> jsx_parser().
@ -57,19 +67,19 @@ start(F, OptsList) ->
%% converts a proplist into a tuple %% converts a proplist into a tuple
parse_opts(Opts) -> parse_opts(Opts) ->
parse_opts(Opts, {false, codepoint, false}). parse_opts(Opts, #opts{}).
parse_opts([], Opts) -> parse_opts([], Opts) ->
Opts; Opts;
parse_opts([{comments, Value}|Rest], {_Comments, EscapedUnicode, Multi}) -> parse_opts([{comments, Value}|Rest], Opts) ->
true = lists:member(Value, [true, false]), true = lists:member(Value, [true, false]),
parse_opts(Rest, {Value, EscapedUnicode, Multi}); parse_opts(Rest, Opts#opts{comments = Value});
parse_opts([{escaped_unicode, Value}|Rest], {Comments, _EscapedUnicode, Multi}) -> parse_opts([{escaped_unicode, Value}|Rest], Opts) ->
true = lists:member(Value, [ascii, codepoint, none]), true = lists:member(Value, [ascii, codepoint, none]),
parse_opts(Rest, {Comments, Value, Multi}); parse_opts(Rest, Opts#opts{escaped_unicode = Value});
parse_opts([{multi_term, Value}|Rest], {Comments, EscapedUnicode, _Multi}) -> parse_opts([{multi_term, Value}|Rest], Opts) ->
true = lists:member(Value, [true, false]), true = lists:member(Value, [true, false]),
parse_opts(Rest, {Comments, EscapedUnicode, Value}); parse_opts(Rest, Opts#opts{multi_term = Value});
parse_opts([{encoding, _}|Rest], Opts) -> parse_opts([{encoding, _}|Rest], Opts) ->
parse_opts(Rest, Opts). parse_opts(Rest, Opts).

View file

@ -22,10 +22,10 @@
%% option flags %% option flags
-define(comments_enabled(X), {true, _, _} = X). -define(comments_enabled(X), {_, true, _, _, _} = X).
-define(escaped_unicode_to_ascii(X), {_, ascii, _} = X). -define(escaped_unicode_to_ascii(X), {_, _, ascii, _, _} = X).
-define(escaped_unicode_to_codepoint(X), {_, codepoint, _} = X). -define(escaped_unicode_to_codepoint(X), {_, _, codepoint, _, _} = X).
-define(multi_term(X), {_, _, true} = X). -define(multi_term(X), {_, _, _, true, _} = X).
%% whitespace %% whitespace
-define(space, 16#20). -define(space, 16#20).