---
src/ktj_decode.erl | 9 ++---
src/ktj_encode.erl | 84 ++++++++++++++++++---------------------------
src/ktj_parse.erl | 80 +++++++++++++++++++++-----------------------
src/ktt_decode.erl | 65 +++++++++++++++++++++---------------
src/ktuo_parse_utils.erl | 41 ++++++++++++++---------
5 files changed, 139 insertions(+), 140 deletions(-)
diff --git a/src/ktj_decode.erl b/src/ktj_decode.erl
index c25ccf4..ec4bb99 100644
--- a/src/ktj_decode.erl
+++ b/src/ktj_decode.erl
@@ -1,7 +1,5 @@
-%% -*- mode: Erlang; fill-column: 132; comment-column: 118; -*-
+%% -*- mode: Erlang; fill-column: 80; comment-column: 76; -*-
%%%-------------------------------------------------------------------
-%%% Copyright (c) 2006,2007,2008 Erlware
-%%%
%%% 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
@@ -24,7 +22,7 @@
%%% OTHER DEALINGS IN THE SOFTWARE.
%%%---------------------------------------------------------------------------
%%% @author Eric Merritt
-%%% @copyright (C) 2006
+%%% @copyright (C) 2006-2010
%%% @doc This module has been discontinued, use {...@link ktj_parse} instead.
%%%
%%% ktj_parse API is not backwards compatible with former ktj_decode API.
@@ -39,7 +37,8 @@
-module(ktj_decode).
-deprecated(module).
--export([decode/1, decode/3]).
+-export([decode/1,
+ decode/3]).
decode(_,_,_) ->
decode(none).
diff --git a/src/ktj_encode.erl b/src/ktj_encode.erl
index 5cf5992..3e09d1e 100644
--- a/src/ktj_encode.erl
+++ b/src/ktj_encode.erl
@@ -1,7 +1,5 @@
-%% -*- mode: Erlang; fill-column: 132; comment-column: 118; -*-
+%% -*- mode: Erlang; fill-column: 80; comment-column: 76; -*-
%%%-------------------------------------------------------------------
-%%% Copyright (c) 2006,2007,2008 Erlware
-%%%
%%% 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
@@ -24,21 +22,9 @@
%%% OTHER DEALINGS IN THE SOFTWARE.
%%%---------------------------------------------------------------------------
%%% @author Eric Merritt
-%%% @copyright (C) 2006
+%%% @copyright (C) 2006-2010 Erlware
%%% @doc
%%% Parsing erlang into json
-%%%
-%%% @type in_string() = binary()
-%%% @type in_array() = [in_value()]
-%%% @type in_atom() = string()
-%%% @type in_object() = {obj, [{in_string(), in_value()}]}
-%%% @type in_number() = integer() | float()
-%%% @type in_bool() = true | false
-%%% @type in_null() = null
-%%% @type in_value() = in_string() | in_array() | in_atom() | in_object()
-%%% | in_number() | in_bool() | in_null()
-%%%
-%%%
%%% @end
%%% Created : 19 Dec 2006 by Eric Merritt
%%%-------------------------------------------------------------------
@@ -48,18 +34,40 @@
-export([encode/1]).
+-export_type([prop_list/0,
+ ktj_string/0,
+ ktj_array/0,
+ ktj_atom/0,
+ object/0,
+ ktj_number/0,
+ ktj_bool/0,
+ null/0,
+ value/0]).
+
+%%=============================================================================
+%% Types
+%%=============================================================================
+
+-type prop_list() :: [{ktj_string(), value()}].
+-type ktj_string() :: binary().
+-type ktj_array() :: [value()].
+-type ktj_atom() :: string().
+-type object() :: {obj, prop_list()}.
+-type ktj_number() :: integer() | float().
+-type ktj_bool() :: true | false.
+-type null() :: null.
+-type value() :: ktj_string() | ktj_array() | ktj_atom() | object()
+ | ktj_number() | ktj_bool() | null().
+
%%=============================================================================
%% API
%%=============================================================================
-%%--------------------------------------------------------------------
%% @doc
%% Parses a list of data objects into a list. The list is a deeply
%% nested list and should be flattened if you wish to use it as a
%% string. Otherwise, io functions will flatten the list for you.
-%%
-%% @spec encode(DataObjects::in_value()) -> Output::string()
%% @end
-%%--------------------------------------------------------------------
+-spec encode(value()) -> string().
encode(Data) when is_list(Data) ->
lists:reverse(encode_array(Data, []));
encode({obj, Value}) ->
@@ -83,8 +91,6 @@ encode(Data) when is_atom(Data)->
%%=============================================================================
%% Internal Functions
%%=============================================================================
-%%--------------------------------------------------------------------
-%%
%% @doc
%% Encodes the string according to the rules of json.
%% ``
@@ -109,11 +115,8 @@ encode(Data) when is_atom(Data)->
%% \u four-hex-digits
%%
%% ''
-%%
-%% @spec encode_string(Value::in_string()) -> EncodedList::string()
-%% @private
%% @end
-%%--------------------------------------------------------------------
+-spec encode_string(ktj_string()) -> string().
encode_string(Value) when is_binary(Value) ->
[$\", escape_string(binary_to_list(Value), []), $\"];
encode_string(Value) when is_atom(Value) ->
@@ -121,14 +124,10 @@ encode_string(Value) when is_atom(Value) ->
encode_string(Value) when is_list(Value) ->
[$\", escape_string(Value, []), $\"].
-%%--------------------------------------------------------------------
%% @doc
%% escapes a string as required for the json
-%%
-%% @spec escape_string(Value::list(), Acc::list()) -> EscapedString::string()
-%% @private
%% @end
-%%--------------------------------------------------------------------
+-spec escape_string(string(), list()) -> string().
escape_string([$\" | Rest], Acc) ->
escape_string(Rest, [$\", $\\ | Acc]);
escape_string([$\\ | Rest], Acc) ->
@@ -150,7 +149,6 @@ escape_string([N | Rest], Acc) ->
escape_string([], Acc) ->
lists:reverse(Acc).
-%%--------------------------------------------------------------------
%% @doc
%% Encode the integer according to the precepts of json.
%%
@@ -165,15 +163,11 @@ escape_string([], Acc) ->
%% digit
%% digit digits
%% ''
-%%
-%% @spec encode_integer(Value::integer()) -> List::string()
-%% @private
%% @end
-%%--------------------------------------------------------------------
+-spec encode_integer(integer()) -> string().
encode_integer(Value) when is_integer(Value) ->
integer_to_list(Value).
-%%--------------------------------------------------------------------
%% @doc
%% Encode the float according to json rules.
%% ``
@@ -202,16 +196,11 @@ encode_integer(Value) when is_integer(Value) ->
%% E+
%% E-
%% ''
-%%
-%% @spec encode_float(Value::float()) -> List::string()
-%% @private
%% @end
-%%--------------------------------------------------------------------
+-spec encode_float(float()) -> string().
encode_float(Value) when is_float(Value) ->
float_to_list(Value).
-
-%%--------------------------------------------------------------------
%% @doc
%% Encode erlang array according to jsonic precepts
%% <pre>
@@ -230,10 +219,8 @@ encode_float(Value) when is_float(Value) ->
%% false
%% null
%% </pre>
-%% @spec encode_array(Array::list(), Acc::string()) -> List::string()
-%% @private
%% @end
-%%--------------------------------------------------------------------
+-spec encode_array(list(), string()) -> string().
encode_array([H | T], []) ->
encode_array(T, [encode(H), $[]);
encode_array([H | T], TAcc) ->
@@ -244,8 +231,6 @@ encode_array([], TAcc) ->
[$] | TAcc].
-%%--------------------------------------------------------------------
-%%
%% @doc
%% Encode a property list into a json array.
%% ``
@@ -273,9 +258,8 @@ encode_array([], TAcc) ->
%% null
%%
%% ''
-%% @spec encode_object(PropList, Acc::string()) -> List::string()
%% @end
-%%--------------------------------------------------------------------
+-spec encode_object(prop_list(), string()) -> string().
encode_object([{Key, Value} | T], []) ->
encode_object(T, [encode_string(Key), $:,
encode(Value), $}]);
diff --git a/src/ktj_parse.erl b/src/ktj_parse.erl
index 7c7342f..472fa99 100644
--- a/src/ktj_parse.erl
+++ b/src/ktj_parse.erl
@@ -1,7 +1,5 @@
-%% -*- mode: Erlang; fill-column: 132; comment-column: 118; -*-
+%% -*- mode: Erlang; fill-column: 80; comment-column: 76; -*-
%%%-------------------------------------------------------------------
-%%% Copyright (c) 2006,2007,2008 Erlware
-%%%
%%% 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
@@ -24,18 +22,10 @@
%%% OTHER DEALINGS IN THE SOFTWARE.
%%%---------------------------------------------------------------------------
%%% @author Eric Merritt
-%%% @copyright (C) 2006
+%%% @copyright (C) 2006-2010
%%% @doc
%%% Used for decoding json from a string or binary
%%% Parsing strings into erlang.
-%%% @type key() = json_string()
-%%% @type value() = object() | json_number() | array() | json_string() |
json_bool() | null()
-%%% @type object() = {obj, [{key(), value()}]}
-%%% @type array() = [value()]
-%%% @type json_number() = integer() | float()
-%%% @type json_string() = binary()
-%%% @type json_bool() = true | false
-%%% @type null() = null
%%% @end
%%% Created : 19 Dec 2006 by Eric Merritt
%%%-------------------------------------------------------------------
@@ -45,9 +35,34 @@
-export([parse/1, parse/3]).
+-export_type([key/0,
+ value/0,
+ object/0,
+ atj_array/0,
+ json_number/0,
+ json_string/0,
+ json_bool/0,
+ null/0,
+ mid_parse_stream/0,
+ stream/0]).
+
+
%%=============================================================================
-%% API
+%% Types
%%=============================================================================
+
+-type key() :: json_string().
+-type value() :: object() | json_number() | atj_array() | json_string() |
json_bool() | null().
+-type object() :: {obj, [{key(), value()}]}.
+-type atj_array() :: [value()].
+-type json_number() :: integer() | float().
+-type json_string() :: binary().
+-type json_bool() :: true | false.
+-type null() :: null.
+
+-opaque mid_parse_stream() :: {value(), string(), {integer(), integer()}}.
+-type stream() :: binary() | string() | mid_parse_stream().
+
%%--------------------------------------------------------------------
%% @doc
%% Parses the incoming stream into valid json objects.
@@ -63,10 +78,9 @@
%% of the json stream decode will return an 'end_of_stream'.
%%
%%
-%% @spec (Stream) -> {JSONValue::value(), UnparsedRemainder, LineInfo}
-%% | end_of_stream
%% @end
%%--------------------------------------------------------------------
+-spec parse(stream()) -> mid_parse_stream() | end_of_stream.
parse({_, [], _}) ->
end_of_stream;
parse({_, UnparsedRemainder, {NewLines, Chars}}) ->
@@ -80,11 +94,9 @@ parse(Stream) when is_binary(Stream) ->
%%--------------------------------------------------------------------
%% @doc
%% Decodes the value with the fixed set of newlines and chars.
-%%
-%% @spec (Stream, NewLines, Chars) ->
-%% {DecodedValue::value(), UnparsedRemainder, CharRemainder}
%% @end
%%--------------------------------------------------------------------
+-spec parse(string(), integer(), integer()) -> mid_parse_stream().
parse(Stream, NewLines, Chars) ->
value(Stream, NewLines, Chars).
@@ -101,6 +113,7 @@ parse(Stream, NewLines, Chars) ->
%% @private
%% @end
%%--------------------------------------------------------------------
+-spec value(string(), integer(), integer()) -> mid_parse_stream().
value([$\" | T], NewLines, Chars) ->
ktuo_parse_utils:stringish_body($\", T, [], NewLines, Chars + 1);
value([$- | T], NewLines, Chars) ->
@@ -146,7 +159,7 @@ value([$\n | T], NewLines, _Chars) ->
value(Stream, NewLines, Chars) ->
ident(Stream, [], NewLines, Chars).
-
+-spec array_body(string(), [value()], integer(), integer()) ->
mid_parse_stream().
array_body([$] | T], Acc, NewLines, Chars) ->
{lists:reverse(Acc), T, {NewLines, Chars + 1}};
array_body([$, | T], Acc, NewLines, Chars) ->
@@ -163,6 +176,7 @@ array_body(Stream, Acc, NewLines, Chars) ->
{Value, Rest, {NLines, NChars}} = value(Stream, NewLines, Chars),
array_body(Rest, [Value | Acc], NLines, NChars).
+-spec object_body(string(), [{string(), value()}], integer(), integer()) ->
mid_parse_stream().
object_body([$} | T], Acc, NewLines, Chars) ->
{{obj, Acc}, T, {NewLines, Chars + 1}};
object_body([$, | T], Acc, NewLines, Chars) ->
@@ -179,13 +193,10 @@ object_body(Else, Acc, NewLines, Chars) ->
do_key(Else, Acc, NewLines, Chars).
-%%--------------------------------------------------------------------
%% @doc
%% Parse out the key. When that is complete parse out the value.
-%%
-%% @spec (Stream, Acc, NewLines, Chars) -> Error | {Prop, Rest, N, C}
%% @end
-%%--------------------------------------------------------------------
+-spec do_key(string(), [{string(), value()}], integer(), integer()) ->
mid_parse_stream().
do_key(Stream, Acc, NewLines, Chars) ->
case key(Stream, NewLines, Chars) of
{Key, Rest1, {NLines, NChars}} ->
@@ -194,14 +205,10 @@ do_key(Stream, Acc, NewLines, Chars) ->
Else
end.
-%%--------------------------------------------------------------------
%% @doc
%% Parse out the value. Then continue with the object.
-%%
-%% @spec (Key, Stream, Acc, NewLines, Chars) -> Error |
-%% {PropList, Rest, {NewLines, Chars}}
%% @end
-%%--------------------------------------------------------------------
+-spec do_value(string(), string(), [{string(), value()}], integer(),
integer()) -> mid_parse_stream().
do_value(Key, Stream, Acc, NewLines, Chars) ->
case find($:, Stream, NewLines, Chars) of
{Rest, NLines, NChars} ->
@@ -216,16 +223,12 @@ do_value(Key, Stream, Acc, NewLines, Chars) ->
Else1
end.
-%%--------------------------------------------------------------------
%% @doc
%% Make an effort to run to the next instance of the delimeter.
%% Only whitespace and newlines are expected to be between the start
%% and the delim.
-%%
-%%d @spec (Delim, Stream, NewLines, Chars) -> Error | {Rest, NewLines,
-%% Chars}
%% @end
-%%--------------------------------------------------------------------
+-spec find(string(), string(), integer(), integer()) -> mid_parse_stream().
find(Delim, [Delim | T], NewLines, Chars) ->
{T, NewLines, Chars + 1};
find(Delim, [$\s | T], NewLines, Chars) ->
@@ -240,13 +243,10 @@ find(Delim, _Rest, NewLines, Chars) ->
{error, {"Expected object seperator", Delim, NewLines, Chars}}.
-%%--------------------------------------------------------------------
%% @doc
%% Parse the key as part of the object.
-%%
-%% @spec (Stream, NewLines, Chars) -> {Key, NewLine, Chars} | Error
%% @end
-%%--------------------------------------------------------------------
+-spec key(string(), integer(), integer()) -> mid_parse_stream().
key([$\" | T], NewLines, Chars) ->
ktuo_parse_utils:stringish_body($\", T, [], NewLines, Chars + 1);
key([$\s | T], NewLines, Chars) ->
@@ -261,15 +261,11 @@ key(Stream, NewLines, Chars) ->
ident(Stream, [], NewLines, Chars).
-%%--------------------------------------------------------------------
%% @doc
%% Parse a single word ident from the stream. Idents may be
%% of the form [a-z][a-zA-Z0-9_]*
-%%
-%% @spec (Stream, Acc, NewLines, Chars) -> {Ident, Rest, N, L} |
-%% Error
%% @end
-%%--------------------------------------------------------------------
+-spec ident(string(), string(), integer(), integer()) -> mid_parse_stream().
ident([H | T], Acc, NewLines, Chars) when H >= $a, H =< $z ->
ident(T, [H | Acc], NewLines, Chars + 1);
ident([H | T], Acc, NewLines, Chars) when H >= $A, H =< $Z ->
diff --git a/src/ktt_decode.erl b/src/ktt_decode.erl
index 5d82452..efd9dc1 100644
--- a/src/ktt_decode.erl
+++ b/src/ktt_decode.erl
@@ -1,7 +1,5 @@
-%% -*- mode: Erlang; fill-column: 132; comment-column: 118; -*-
+%% -*- mode: Erlang; fill-column: 80; comment-column: 76; -*-
%%%-------------------------------------------------------------------
-%%% Copyright (c) 2006,2007,2008 Erlware
-%%%
%%% 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
@@ -24,7 +22,7 @@
%%% OTHER DEALINGS IN THE SOFTWARE.
%%%---------------------------------------------------------------------------
%%% @author Eric Merritt
-%%% @copyright (C) 2006
+%%% @copyright (C) 2006-2010 Erlware
%%% @doc
%%% Does a safe parsing of erlang tuple syntax. If it finds an atom it
%%% uses list_to_existing atom to translate it. This will only work
@@ -37,28 +35,50 @@
-include_lib("eunit/include/eunit.hrl").
--compile(export_all).
+-export([decode/1,
+ decode/3]).
--export([decode/1, decode/3]).
+-export_type([ktt_list/0,
+ ktt_string/0,
+ ktt_number/0,
+ ktt_atom/0,
+ value/0,
+ stream/0,
+ position_indicator/0,
+ result/0]).
+%%=============================================================================
+%% Types
+%%=============================================================================
+-type ktt_list() :: list().
+-type ktt_string() :: binary().
+-type ktt_number() :: number().
+-type ktt_atom() :: atom().
+-type value() :: ktt_list() | ktt_string() | ktt_number() | ktt_atom().
+-type stream() :: binary() | string().
+-type position_indicator() :: {integer(), integer()}.
+-type result() :: {[value()], string(), position_indicator()}.
-%%--------------------------------------------------------------------
-%% @spec decode(Stream) -> {ParsedTuples, UnparsedRemainder}
-%%
+%%=============================================================================
+%% API
+%%=============================================================================
%% @doc
%% Parses the incoming stream into valid erlang objects.
%% ``
%% Tuple == Erlang
%% List List
-%% String List
+%% String binary
%% Number Number
%% Atom Atom
%% ''
%% This decode function parses a subset of erlang data notation.
%% @end
-%%--------------------------------------------------------------------
+-spec decode(stream()) -> result().
+decode(Stream) when is_binary(Stream) ->
+ decode(binary_to_list(Stream), 0, 0);
decode(Stream) ->
decode(Stream, 0, 0).
+-spec decode(stream(), integer(), integer()) -> result().
decode(Stream, NewLines, Chars) ->
value(Stream, NewLines, Chars).
@@ -66,13 +86,10 @@ decode(Stream, NewLines, Chars) ->
%%=============================================================================
%% Internal Functions
%%=============================================================================
-%%--------------------------------------------------------------------
%% @doc
%% Parse a tuple value.
-%%
-%% @spec value(Stream, NewLines, Chars) -> {Value, Rest, {N, L}}
%% @end
-%%--------------------------------------------------------------------
+-spec value(string(), integer(), integer()) -> result().
value([$\" | T], NewLines, Chars) ->
ktuo_parse_utils:stringish_body($\", T, [], NewLines, Chars + 1);
value([$\' | T], NewLines, Chars) ->
@@ -120,13 +137,11 @@ value(Stream, NewLines, Chars) ->
bare_atom(Stream, [], NewLines, Chars).
-%%--------------------------------------------------------------------
%% @doc
%% Parse a list body. A list is [ elements, ..].
-%%
-%% @spec list_body(Stream, Acc, NewLines, Chars) -> {List, Rest, {N, L}} |
Error
%% @end
-%%--------------------------------------------------------------------
+-spec list_body(string(), [value()], integer(), integer()) ->
+ result() | {error, {string(), integer(), integer()}}.
list_body([$] | T], Acc, NewLines, Chars) ->
{lists:reverse(Acc), T, {NewLines, Chars + 1}};
list_body([$, | T], Acc, NewLines, Chars) ->
@@ -147,13 +162,11 @@ list_body(Stream, Acc, NewLines, Chars) ->
Else
end.
-%%--------------------------------------------------------------------
%% @doc
%% Parse the tuple body. Tuple bodies are of the form { element, ..}.
-%%
-%% @spec tuple_body(Stream, Acc, NewLines, Chars) -> {Tuple, Rest, {N, C}} |
Error
%% @end
-%%--------------------------------------------------------------------
+-spec tuple_body(string(), [value()], integer(), integer()) ->
+ result() | {error, {string(), integer(), integer()}}.
tuple_body([$} | T], Acc, NewLines, Chars) ->
{list_to_tuple(lists:reverse(Acc)), T, {NewLines, Chars + 1}};
tuple_body([$, | T], Acc, NewLines, Chars) ->
@@ -175,13 +188,11 @@ tuple_body(Stream, Acc, NewLines, Chars) ->
end.
-%%--------------------------------------------------------------------
%% @doc
%% Parse an atom that doesn't have single quote delimeters.
-%%
-%% @spec bare_atom(Stream, Acc, NewLines, Chars) -> {Atom, Rest, {N, C}} |
Error
%% @end
-%%--------------------------------------------------------------------
+-spec bare_atom(string(), string(), integer(), integer()) ->
+ result() | {error, {string(), integer(), integer()}}.
bare_atom([H | T], Acc, NewLines, Chars) when H >= $a, H =< $z ->
bare_atom(T, [H | Acc], NewLines, Chars + 1);
bare_atom([H | T], Acc, NewLines, Chars) when H >= $A, H =< $Z ->
diff --git a/src/ktuo_parse_utils.erl b/src/ktuo_parse_utils.erl
index 14de144..9b55143 100644
--- a/src/ktuo_parse_utils.erl
+++ b/src/ktuo_parse_utils.erl
@@ -1,6 +1,5 @@
-%% -*- mode: Erlang; fill-column: 132; comment-column: 118; -*-
+%% -*- mode: Erlang; fill-column: 80; comment-column: 76; -*-
%%%-------------------------------------------------------------------
-%%% Copyright (c) 2006,2007,2008 Erlware
%%%
%%% Permission is hereby granted, free of charge, to any
%%% person obtaining a copy of this software and associated
@@ -24,7 +23,7 @@
%%% OTHER DEALINGS IN THE SOFTWARE.
%%%---------------------------------------------------------------------------
%%% @author Eric Merritt
-%%% @copyright (C) 2006
+%%% @copyright (C) 2006-2010 Erlware
%%% @doc
%%% Help parsing the general parts of the system.
%%% @end
@@ -34,7 +33,11 @@
-include_lib("eunit/include/eunit.hrl").
--export([stringish_body/5, digit/5]).
+-export([stringish_body/5,
+ digit/5]).
+
+-export_type([string_result/0,
+ number_result/0]).
-define(LOC_1, 1).
-define(LOC_2, 16).
@@ -42,19 +45,23 @@
-define(LOC_4, 4096).
%%=============================================================================
+%% Types
+%%=============================================================================
+-type string_result() :: {binary(), string(), {integer(), integer()}}.
+-type number_result() :: {number(), string(), {integer(), integer()}}.
+
+%%=============================================================================
%% API
%%=============================================================================
-%%--------------------------------------------------------------------
-%%
%% @doc
%% Parses a string body into a string.
%% It expects the fact that something is a string to already be
%% detected. So strings should be of the form
%%
%% this is a string body"
-%% @spec stringish_body(Delim, Stream, Acc, NewLines, Chars) -> {String, Rest}
%% @end
-%%--------------------------------------------------------------------
+-spec stringish_body(char(), string(), string(), integer(), integer()) ->
+ string_result() | {error, {string(), integer(), integer()}}.
stringish_body(Delim, [$\\, $\" | T], Acc, NewLines, Chars) ->
stringish_body(Delim, T, [$\" | Acc], NewLines, Chars + 2);
stringish_body(Delim, [$\\, $/ | T], Acc, NewLines, Chars) ->
@@ -84,13 +91,10 @@ stringish_body(Delim, [H | T], Acc, NewLines, Chars) ->
stringish_body(_Delim, [], _Acc, NewLines, Chars) ->
{error, {"Found end of file while parsing string", NewLines, Chars}}.
-%%--------------------------------------------------------------------
%% @doc
%% Parse out the specified digit set.
-%%
-%% @spec digit(Stream, Acc, Next, NewLines, Chars) -> {Res, Rest}
%% @end
-%%--------------------------------------------------------------------
+-spec digit(string(), string(), string(), integer(), integer()) -> {number(),
string()}.
digit([$0 | T], Acc, Next, NewLines, Chars) ->
digit(T, [$0 | Acc], Next, NewLines, Chars + 1);
digit([$1 | T], Acc, Next, NewLines, Chars) ->
@@ -117,6 +121,7 @@ digit(Stream, Acc, Next, NewLines, Chars) ->
%%=============================================================================
%% Internal functions
%%=============================================================================
+-spec parse_hex_digit(string(), string(), string(), char(), integer(),
integer()) -> number_result().
parse_hex_digit([$0 | T], Acc, HexAcc, Delim, NewLines, Chars)
when length(HexAcc) < 4 ->
parse_hex_digit(T, Acc, [$0 | HexAcc], Delim, NewLines, Chars + 1);
@@ -189,12 +194,13 @@ parse_hex_digit(Stream, Acc, HexAcc, Delim, NewLines,
Chars)
Char = hexlist_to_integer([D4, D3, D2, D1]),
stringish_body(Delim, Stream, [Char | Acc], NewLines, Chars).
-
+-spec decimal(string(), string(), integer(), integer()) -> number_result().
decimal([$.| T], Acc, NewLines, Chars) when length(T) > 0 ->
digit(T, [$. | Acc], decimal, NewLines, Chars + 1);
decimal(Stream, Acc, NewLines, Chars) ->
integer_end(Stream, Acc, NewLines, Chars).
+-spec exponent(string(), string(), integer(), integer()) -> number_result().
exponent([$e, $+ | T], Acc, NewLines, Chars) ->
digit(T, [$+, $e | Acc], exponent, NewLines, Chars + 2);
exponent([$E, $+ | T], Acc, NewLines, Chars) ->
@@ -210,14 +216,16 @@ exponent([$e | T], Acc, NewLines, Chars) ->
exponent(Stream, Acc, NewLines, Chars) ->
float_end(Stream, Acc, NewLines, Chars).
+-spec integer_end(string(), string(), integer(), integer()) -> number_result().
integer_end(Stream, Acc, NewLines, Chars) ->
{list_to_integer(lists:reverse(Acc)), Stream, {NewLines, Chars}}.
-
+-spec float_end(string(), string(), integer(), integer()) -> number_result().
float_end(Stream, Acc, NewLines, Chars) ->
{list_to_float(lists:reverse(Acc)), Stream, {NewLines, Chars}}.
-
+-spec digit_next(string(), string(), front | decimal | exponent,
+ integer(), integer()) -> number_result().
digit_next(Stream, Acc, front, NewLines, Chars) ->
decimal(Stream, Acc, NewLines, Chars);
digit_next(Stream, Acc, decimal, NewLines, Chars) ->
@@ -225,6 +233,7 @@ digit_next(Stream, Acc, decimal, NewLines, Chars) ->
digit_next(Stream, Acc, exponent, NewLines, Chars) ->
float_end(Stream, Acc, NewLines, Chars).
+-spec hexlist_to_integer([number()]) -> integer().
hexlist_to_integer([Size]) when Size >= 48 , Size =< 57 ->
Size - 48;
%% A-F
@@ -235,11 +244,11 @@ hexlist_to_integer([Size]) when Size >= 97 , Size =< 102
->
Size - 87;
hexlist_to_integer([_Size]) ->
not_a_num;
-
hexlist_to_integer(Size) ->
Len = string:span(Size, "1234567890abcdefABCDEF"),
hexlist_to_integer2(Size, 16 bsl (4 *(Len-2)),0).
+-spec hexlist_to_integer2([number()], integer(), integer()) -> integer().
hexlist_to_integer2([],_Pos,Sum)->
Sum;
hexlist_to_integer2([HexVal | HexString], Pos, Sum)
--
1.7.1.1
--
You received this message because you are subscribed to the Google Groups
"erlware-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/erlware-dev?hl=en.