Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 71 lines (54 sloc) 2.124 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
Definitions.

ST = [^"]
L = [A-Za-z]
WS = ([\000-\s]|%.*)
D = [0-9]
H = [0-9a-fA-F]

Rules.

{ : {token, {'{', TokenLine}}.
} : {token, {'}', TokenLine}}.

\[ : {token, {'[', TokenLine}}.
\] : {token, {']', TokenLine}}.

\-?{D}+\.{D}+((E|e)(\+|\-)?{D}+)? : {token,{float,TokenLine,list_to_float(TokenChars)}}.
\-?{D}+(E|e)(\+|\-)?{D}+ : {token,{float,TokenLine,whole_float(TokenChars)}}.
\-?{D}+ : {token,{integer,TokenLine,list_to_integer(TokenChars)}}.

"[^"\\]*(\\.[^"\\]*)*" : {token,{string,TokenLine,parse_string(strip(TokenChars,TokenLen))}}.

true : {token,{'true', TokenLine}}.
false : {token,{'false', TokenLine}}.
null : {token,{'null', TokenLine}}.

: : {token, {':', TokenLine}}.
, : {token, {',', TokenLine}}.

{WS}+ : skip_token.

Erlang code.
% "

-define(LOG(Name, Value),
        io:format("{~p:~p}: ~p -> ~s~n", [?MODULE, ?LINE, Name, Value])).
-define(PLOG(Name, Value),
        io:format("{~p:~p}: ~p -> ~p~n", [?MODULE, ?LINE, Name, Value])).

strip(TokenChars,TokenLen) -> lists:sublist(TokenChars, 2, TokenLen - 2).

whole_float(Token_Chars) ->
       list_to_float(insert_point_zero(Token_Chars)).

insert_point_zero([$e|Cs]) -> ".0e" ++ Cs;
insert_point_zero([$E|Cs]) -> ".0e" ++ Cs;
insert_point_zero([C |Cs]) -> [C | insert_point_zero(Cs)].

unescape([$\\,$\"|Cs]) -> [$\"|unescape(Cs)];
unescape([$\\,$\\|Cs]) -> [$\\|unescape(Cs)];
unescape([$\\,$/|Cs]) -> [$/|unescape(Cs)];
unescape([$\\,$b|Cs]) -> [$\b|unescape(Cs)];
unescape([$\\,$f|Cs]) -> [$\f|unescape(Cs)];
unescape([$\\,$n|Cs]) -> [$\n|unescape(Cs)];
unescape([$\\,$r|Cs]) -> [$\r|unescape(Cs)];
unescape([$\\,$t|Cs]) -> [$\t|unescape(Cs)];
unescape([$\\,$u,C0,C1,C2,C3|Cs]) ->
    C = dehex(C3) bor
(dehex(C2) bsl 4) bor
(dehex(C1) bsl 8) bor
(dehex(C0) bsl 12),
    [C|unescape(Cs)];
unescape([C|Cs]) -> [C|unescape(Cs)];
unescape([]) -> [].

dehex(C) when C >= $0, C =< $9 -> C - $0;
dehex(C) when C >= $a, C =< $f -> C - $a + 10;
dehex(C) when C >= $A, C =< $F -> C - $A + 10.

parse_string(StringChars) ->
    unescape(StringChars).
Something went wrong with that request. Please try again.