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

@@ -1,97 +1,97 @@
indexing indexing
description: "Objects that ..." description: "Objects that ..."
author: "" author: ""
date: "$Date$" date: "$Date$"
revision: "$Revision$" revision: "$Revision$"
class class
JSON_READER JSON_READER
create create
make make
feature -- Access feature -- Access
make(a_json:STRING) is make(a_json:STRING) is
-- --
do do
representation:=a_json representation:=a_json
index:=1 index:=1
end end
read:CHARACTER is read:CHARACTER is
-- --
do do
if not representation.is_empty then if not representation.is_empty then
Result:= representation.item (index) Result:= representation.item (index)
end end
end end
actual:CHARACTER is actual:CHARACTER is
-- --
do do
Result:=representation.item (index) Result:=representation.item (index)
end end
has_next:BOOLEAN is has_next:BOOLEAN is
-- --
do do
if index <= representation.count then if index <= representation.count then
Result:=True Result:=True
end end
end end
has_previous:BOOLEAN is has_previous:BOOLEAN is
-- --
do do
if index >=1 then if index >=1 then
Result:=True Result:=True
end end
end end
next is next is
-- --
require require
has_more_elements: has_next has_more_elements: has_next
do do
index:=index + 1 index:=index + 1
ensure ensure
incremented: old index + 1 = index incremented: old index + 1 = index
end end
previous is previous is
-- --
require require
not_is_first: has_previous not_is_first: has_previous
do do
index:=index - 1 index:=index - 1
ensure ensure
incremented: old index - 1 = index incremented: old index - 1 = index
end end
skip_withe_spaces is skip_withe_spaces is
-- 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
end end
json_substring (start_index, end_index: INTEGER_32):STRING is json_substring (start_index, end_index: INTEGER_32):STRING is
-- --
do do
Result:=representation.substring (start_index, end_index) Result:=representation.substring (start_index, end_index)
end end
feature -- Implementation feature -- Implementation
representation:STRING representation:STRING
--linear representation of the original json string --linear representation of the original json string
index:INTEGER index:INTEGER
--actual index --actual index
end end

View File

@@ -1,33 +1,47 @@
indexing indexing
description: "Objects that ..." description: "Objects that ..."
author: "" author: ""
date: "$Date$" date: "$Date$"
revision: "$Revision$" revision: "$Revision$"
class class
JSON_TOKENS JSON_TOKENS
feature -- Access feature -- Access
J_OBJECT_OPEN:CHARACTER is '{' J_OBJECT_OPEN:CHARACTER is '{'
J_ARRAY_OPEN:CHARACTER is '[' J_ARRAY_OPEN:CHARACTER is '['
J_OBJECT_CLOSE:CHARACTER is '}' J_OBJECT_CLOSE:CHARACTER is '}'
J_ARRAY_CLOSE:CHARACTER is ']' J_ARRAY_CLOSE:CHARACTER is ']'
J_STRING:CHARACTER is '"' J_STRING:CHARACTER is '"'
J_PLUS:CHARACTER is '+' J_PLUS:CHARACTER is '+'
J_MINUS:CHARACTER is '-' J_MINUS:CHARACTER is '-'
J_DOT:CHARACTER is '.' J_DOT:CHARACTER is '.'
open_tokens:ARRAY[CHARACTER] is open_tokens:ARRAY[CHARACTER] is
-- Characters wich open a type -- Characters wich open a type
once once
Result:=<<J_OBJECT_OPEN,J_ARRAY_OPEN,J_STRING,J_PLUS,J_MINUS,J_DOT>> Result:=<<J_OBJECT_OPEN,J_ARRAY_OPEN,J_STRING,J_PLUS,J_MINUS,J_DOT>>
end end
close_tokens:ARRAY[CHARACTER] is close_tokens:ARRAY[CHARACTER] is
-- Characters wich close a type -- Characters wich close a type
once once
Result:=<<J_OBJECT_CLOSE,J_ARRAY_CLOSE,J_STRING >> Result:=<<J_OBJECT_CLOSE,J_ARRAY_CLOSE,J_STRING >>
end 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