variable json
- proc eat_comma {} {
+ proc eat_spaces {} {
variable json
set json [string trimleft $json]
- if {[string index $json 0] eq ","} {
- set json [string range $json 1 end]
+ }
+
+ proc eat_char {char} {
+ variable json
+ eat_spaces
+ if {[string index $json 0] eq "$char"} {
+ eat_any
}
}
- proc eat_char {} {
+ proc eat_any {} {
variable json
set json [ string range $json 1 end ]
}
proc parse_list {} {
variable json
- eat_char
+ eat_any
set tcl {list}
while {"$json" ne ""} {
if {[string index $json 0] eq "]"} {
+ eat_any
+ return $tcl
+ }
+ lappend tcl [ parse ]
+ eat_char ,
+ }
+ error "Ran out of JSON. Confused now."
+ }
+
+ proc parse_obj {} {
+ variable json
+ eat_char
+ set tcl {obj}
+ while {"$json" ne ""} {
+ if {[string index $json 0] eq "]"} {
eat_char
return $tcl
}
+ lappend tcl [
lappend tcl [ parse ]
- eat_comma
+ eat_char ,
}
error "Ran out of JSON. Confused now."
}
proc parse {} {
variable json
- set json [string trimleft $json]
+ eat_spaces
if {$json eq ""} {
return
}
proc parse_json {json} {
set parse::json [ string trim $json ]
set result [ parse::parse ]
- if {[string trimleft $parse::json] ne ""} {
+ parse::eat_spaces
+ if {$parse::json ne ""} {
error "Had JSON left over: $parse::json"
}
return $result