Update Parser with is_parsed.

Update parse_string
This commit is contained in:
jvelilla
2008-06-09 01:32:28 +00:00
parent 1052c10b8d
commit aaa10d178a
3 changed files with 255 additions and 187 deletions

View File

@@ -13,47 +13,59 @@ class
create create
make_parser make_parser
feature -- Access feature -- Access
is_parsed:BOOLEAN
current_errors:STRING
make_parser(a_json:STRING) is make_parser(a_json:STRING) is
-- --
do do
make(a_json) make(a_json)
is_parsed:=true
end end
parse_json:JSON_VALUE is
--
do
Result:=parse
if extra_elements then
is_parsed:=false
end
end
parse:JSON_VALUE is parse:JSON_VALUE is
-- --
local local
c:CHARACTER c:CHARACTER
do do
skip_withe_spaces if is_parsed then
c:=actual skip_withe_spaces
if c.is_equal (j_object_open) then c:=actual
Result:=parse_object if c.is_equal (j_object_open) then
elseif c.is_equal (j_string) then Result:=parse_object
Result:=parse_string elseif c.is_equal (j_string) then
elseif c.is_equal (j_array_open) then Result:=parse_string
Result:=parse_array elseif c.is_equal (j_array_open) then
elseif c.is_digit or c.is_equal (j_minus) or Result:=parse_array
c.is_equal (j_plus) or c.is_equal (j_dot) then elseif c.is_digit or c.is_equal (j_minus) or
Result:=parse_number c.is_equal (j_plus) or c.is_equal (j_dot) then
elseif is_null then Result:=parse_number
-- elseif is_null then
Result:=create {JSON_NULL} --
next;next;next; Result:=create {JSON_NULL}
elseif is_true then next;next;next;
Result:=create {JSON_BOOLEAN}.make_boolean (true) elseif is_true then
next;next;next; Result:=create {JSON_BOOLEAN}.make_boolean (true)
elseif is_false then next;next;next;
Result:=create {JSON_BOOLEAN}.make_boolean (false) elseif is_false then
next;next;next;next; Result:=create {JSON_BOOLEAN}.make_boolean (false)
else next;next;next;next;
else
Result:=void is_parsed:=false
Result:=void
end
end end
rescue end
handle_syntax_exception
retry
end
parse_object:JSON_OBJECT is parse_object:JSON_OBJECT is
-- object -- object
@@ -85,21 +97,28 @@ feature -- Access
next next
skip_withe_spaces skip_withe_spaces
else else
excpetions.raise("%N Input string is a not well formed JSON, expected: %":%", found:" +actual.out +"%N") is_parsed:=false
--excpetions.raise("%N Input string is a not well formed JSON, expected: %":%", found:" +actual.out +"%N")
has_more:=false has_more:=false
end end
l_value:=parse l_value:=parse
Result.put (l_json_string, l_value) if is_parsed then
next Result.put (l_value,l_json_string)
skip_withe_spaces next
if actual.is_equal (j_object_close) then skip_withe_spaces
has_more:=false if actual.is_equal (j_object_close) then
elseif not actual.is_equal (',') then has_more:=false
has_more:=false elseif not actual.is_equal (',') then
excpetions.raise("JSON Object sintactically malformed expected :%"'%" ") has_more:=false
end is_parsed:=false
end --excpetions.raise("JSON Object sintactically malformed expected :%"'%" ")
end
else
has_more:=false
-- explain the error
end
end
end end
end end
@@ -121,7 +140,21 @@ feature -- Access
has_more:=false has_more:=false
elseif close_tokens.has (actual) then elseif close_tokens.has (actual) then
has_more:=false has_more:=false
excpetions.raise("Input String is not well formed JSON, expected %"") is_parsed:=false
--excpetions.raise("Input String is not well formed JSON, expected %"")
elseif actual.is_equal ('%N') then
next
if not special_characters.has (actual) then
has_more:=false
is_parsed:=false
-- explain exception
else
l_json_string.append (actual.out)
end
elseif special_characters.has (actual) then
has_more:=false
is_parsed:=false
-- explain exception
else else
l_json_string.append (actual.out) l_json_string.append (actual.out)
end end
@@ -156,15 +189,20 @@ feature -- Access
next next
skip_withe_spaces skip_withe_spaces
l_value := parse l_value := parse
Result.add (l_value) if is_parsed then
next Result.add (l_value)
skip_withe_spaces next
skip_withe_spaces
if not actual.is_equal (j_array_close) and not actual.is_equal (',')then if not actual.is_equal (j_array_close) and not actual.is_equal (',')then
flag:=false
is_parsed:=false
--excpetions.raise("%NInput string is not well formed JSON, expected: ',' or ']', and found: " +actual.out+"%N")
elseif actual.is_equal (j_array_close) then
flag:= false
end
else
flag:=false flag:=false
excpetions.raise("%NInput string is not well formed JSON, expected: ',' or ']', and found: " +actual.out+"%N") --explain the error
elseif actual.is_equal (j_array_close) then
flag:= false
end end
end end
end end
@@ -200,7 +238,8 @@ feature -- Access
elseif sb.is_integer then elseif sb.is_integer then
create Result.make_integer (sb.to_integer) create Result.make_integer (sb.to_integer)
else else
excpetions.raise ("Input string is an not well formed JSON") is_parsed:=false
-- excpetions.raise ("Input string is an not well formed JSON")
-- print ("Input string is an not well formed JSON") -- print ("Input string is an not well formed JSON")
end end
end end
@@ -245,14 +284,29 @@ feature -- Access
end end
end end
feature {NONE} feature {NONE}
handle_syntax_exception is
--
do
next
end
excpetions:EXCEPTIONS once extra_elements:BOOLEAN is
create Result --has more elements?
end local
l_string:STRING
do
if has_next then
next
end
from
until not actual.is_space or not actual.is_equal ('%R') or
not actual.is_equal ('%U') or not actual.is_equal ('%T')
or not actual.is_equal ('%N') or not has_next
loop
next
end
if has_next then
Result:=True
end
end
end end

View File

@@ -73,7 +73,7 @@ feature -- Access
-- Remove withe spaces -- Remove withe spaces
do do
from from
until not actual.is_space and has_next until not actual.is_space or not has_next
loop loop
next next
end end

View File

@@ -30,4 +30,18 @@ feature -- Access
Result:=<<J_OBJECT_CLOSE,J_ARRAY_CLOSE,J_STRING >> Result:=<<J_OBJECT_CLOSE,J_ARRAY_CLOSE,J_STRING >>
end end
special_characters:ARRAY[CHARACTER] is
-- Control Characters
-- %F Form feed
-- %H backslasH
-- %N Newline
-- %R carriage Return
-- %T horizontal Tab
-- %B Backspace
-- / Solidus
-- " Quotation
do
Result:=<<'%F','%H','%N','%R','%T','%B','/','"'>>
end
end end