Update Parser with is_parsed.
Update parse_string
This commit is contained in:
@@ -13,11 +13,24 @@ 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
|
||||||
|
|
||||||
|
parse_json:JSON_VALUE is
|
||||||
|
--
|
||||||
|
do
|
||||||
|
Result:=parse
|
||||||
|
if extra_elements then
|
||||||
|
is_parsed:=false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
parse:JSON_VALUE is
|
parse:JSON_VALUE is
|
||||||
@@ -25,6 +38,7 @@ feature -- Access
|
|||||||
local
|
local
|
||||||
c:CHARACTER
|
c:CHARACTER
|
||||||
do
|
do
|
||||||
|
if is_parsed then
|
||||||
skip_withe_spaces
|
skip_withe_spaces
|
||||||
c:=actual
|
c:=actual
|
||||||
if c.is_equal (j_object_open) then
|
if c.is_equal (j_object_open) then
|
||||||
@@ -47,12 +61,10 @@ feature -- Access
|
|||||||
Result:=create {JSON_BOOLEAN}.make_boolean (false)
|
Result:=create {JSON_BOOLEAN}.make_boolean (false)
|
||||||
next;next;next;next;
|
next;next;next;next;
|
||||||
else
|
else
|
||||||
|
is_parsed:=false
|
||||||
Result:=void
|
Result:=void
|
||||||
end
|
end
|
||||||
rescue
|
end
|
||||||
handle_syntax_exception
|
|
||||||
retry
|
|
||||||
end
|
end
|
||||||
|
|
||||||
parse_object:JSON_OBJECT is
|
parse_object:JSON_OBJECT is
|
||||||
@@ -85,19 +97,26 @@ 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
|
||||||
|
Result.put (l_value,l_json_string)
|
||||||
next
|
next
|
||||||
skip_withe_spaces
|
skip_withe_spaces
|
||||||
if actual.is_equal (j_object_close) then
|
if actual.is_equal (j_object_close) then
|
||||||
has_more:=false
|
has_more:=false
|
||||||
elseif not actual.is_equal (',') then
|
elseif not actual.is_equal (',') then
|
||||||
has_more:=false
|
has_more:=false
|
||||||
excpetions.raise("JSON Object sintactically malformed expected :%"'%" ")
|
is_parsed:=false
|
||||||
|
--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,16 +189,21 @@ feature -- Access
|
|||||||
next
|
next
|
||||||
skip_withe_spaces
|
skip_withe_spaces
|
||||||
l_value := parse
|
l_value := parse
|
||||||
|
if is_parsed then
|
||||||
Result.add (l_value)
|
Result.add (l_value)
|
||||||
next
|
next
|
||||||
skip_withe_spaces
|
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
|
flag:=false
|
||||||
excpetions.raise("%NInput string is not well formed JSON, expected: ',' or ']', and found: " +actual.out+"%N")
|
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
|
elseif actual.is_equal (j_array_close) then
|
||||||
flag:= false
|
flag:= false
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
flag:=false
|
||||||
|
--explain the error
|
||||||
|
end
|
||||||
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
|
|
||||||
--
|
extra_elements:BOOLEAN is
|
||||||
|
--has more elements?
|
||||||
|
local
|
||||||
|
l_string:STRING
|
||||||
do
|
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
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
excpetions:EXCEPTIONS once
|
if has_next then
|
||||||
create Result
|
Result:=True
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user