From: Matt S Trout Date: Tue, 19 Jun 2012 17:49:47 +0000 (+0000) Subject: basic parsing X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=015669f7ce033c77e6471afa90c98b549ac61c1d;p=scpubgit%2FTenDotTcl.git basic parsing --- diff --git a/json.tcl b/json.tcl index 4b8c13a..bb7a155 100644 --- a/json.tcl +++ b/json.tcl @@ -60,8 +60,8 @@ namespace eval ten::json { proc deparse {data} { switch -regexp [lindex $data 0] { - ^true|false|null$ { lindex $data 0 } - ^num|str|obj|list$ { eval $data } + ^(true|false|null)$ { lindex $data 0 } + ^(num|str|obj|list)$ { eval $data } default { error [ concat "Invalid JSON type " [lindex $data 0 0] ] } } } @@ -90,8 +90,8 @@ namespace eval ten::json { proc tclify {data} { switch -regexp [lindex $data 0] { - ^true|false|null$ { uplevel 1 return [lindex $data 0] } - ^num|str|obj|list$ {} + ^(true|false|null)$ { uplevel 1 return [lindex $data 0] } + ^(num|str|obj|list)$ {} default { error [ concat "Invalid JSON type " [lindex $data 0 0] ] } } eval $data @@ -129,6 +129,7 @@ namespace eval ten::json { eat_any set tcl {list} while {"$json" ne ""} { + eat_spaces if {[string index $json 0] eq "]"} { eat_any return $tcl @@ -141,14 +142,19 @@ namespace eval ten::json { proc parse_obj {} { variable json - eat_char + eat_any set tcl {obj} while {"$json" ne ""} { - if {[string index $json 0] eq "]"} { - eat_char + eat_spaces + if {[string index $json 0] eq "\}"} { + eat_any return $tcl } - lappend tcl [ + eat_spaces + lappend tcl [ parse_str ] + eat_spaces + eat_char : + eat_spaces lappend tcl [ parse ] eat_char , } @@ -160,7 +166,7 @@ namespace eval ten::json { # like Text::Balanced except ugly (borrowed from tcvJSON's code) set reStr {(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\"))} if {![regexp $reStr $json string]} { - error [ concat "Invalid string: " [string range $json 0 32] + error "Invalid string: $json" } set json [string range $json [string length $string] end] # chop off outer ""s and substitute backslashes @@ -180,6 +186,16 @@ namespace eval ten::json { list num $num } + proc parse_bare {} { + variable json + if [regexp {^(true|false|null)} $json matched] { + set json [ string range $json [ string length $matched ] end ] + return $matched + } else { + error "Out of ideas parsing: $json" + } + } + proc parse {} { variable json eat_spaces @@ -193,7 +209,7 @@ namespace eval ten::json { {[-0-9]} { parse_num } - default { error "argh" } + default { parse_bare } } } } @@ -228,6 +244,6 @@ dict for {k v} [ ten::json::tclify_json [ lindex [ ten::json::tclify_json $ex_json ] 2 ] ] { puts "$k: $v" } -#puts [ ten::json::parse_json $jtext ] +puts [ ten::json::parse_json $jtext ] puts [ ten::json::parse_json {["foo",2345,["bar"]]} ]