From: Matt S Trout Date: Tue, 19 Jun 2012 17:35:17 +0000 (+0000) Subject: number parsing and nested array X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FTenDotTcl.git;a=commitdiff_plain;h=fd341d42d5bab2ca9bc282fba65a7139d0476e6d number parsing and nested array --- diff --git a/json.tcl b/json.tcl index 9d382e6..4b8c13a 100644 --- a/json.tcl +++ b/json.tcl @@ -106,30 +106,51 @@ namespace eval ten::json { 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." } @@ -161,7 +182,7 @@ namespace eval ten::json { proc parse {} { variable json - set json [string trimleft $json] + eat_spaces if {$json eq ""} { return } @@ -180,7 +201,8 @@ namespace eval ten::json { 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