From: James Edward Gray II Date: 2007-02-07T01:27:39+09:00 Subject: Re: ruby CSS parser On Feb 6, 2007, at 10:05 AM, James Edward Gray II wrote: > On Feb 6, 2007, at 9:55 AM, Keith Fahlgren wrote: > >> I assume you've seen this stuff: >> http://www.lshift.net/blog/2005/08/22/json-for-mzscheme-and-a- >> portable-packrat-parsing-combinator-library > > Interesting. I hadn't seen that before now. Thanks for sharing. Looking into this a little further, I find this oddity in the parser: (define (skip-comment-char results) (comment-body (parse-results-next results))) I'm not familiar with scheme, so I don't know exactly what's going on here, but the JSON language does not have comments, so this is a little odd. For the curious, the current DSL goal in Ghost Wheel is: JSON_PARSER = GhostWheel::build_parser do rule( :keyword, alt("true", "false", "null") { |k| {"true" => true, "false" => false, "null" => nil}[k] } ) rule( :number, /-?(?:0|[1-9]\d*)(?:\.\d+(?:[eE][+-]?\d+)?)?/ ) { |n| n.include?(".") ? Float(n) : Integer(n) } rule( :string_content, alt( lit(%r{\\["\\/]}) { |e| e[-1, 1] }, lit(/\\[bfnrt]/) { |e| Hash[*%W[b \n f \f n \n r \r t \t]][e[-1, 1]] }, lit(/\\u[0-9a-fA-F]{4}/) { |e| [Integer("0x#{e [2..-1]}")].pack("U") }, /[^\\"]+/ ) ) rule( :string, seq( skip('"'), zplus(:string_content), skip('"') ) { |s| s.flatten.join } ) rule( :array, seq( skip(/\[\s*/), alt( seq( oplus(seq(:value, skip(/\s*,\s*/))), :value ) { |a| a[0].inject([]) { |vs, v| vs.push(*v) } + a [-1..-1] }, seq(opt(:value)) ), skip(/\s*\]/) ) { |a| a.first } ) rule(:object_pair, seq(:string, /\s*:\s*/, :value) { |kv| {kv[0] => kv[-1]} }) rule( :object, seq( skip(/\{\s*/), alt( seq( oplus(seq(:object_pair, skip(/\s*;\s*/))), :object_pair ) { |ps| ps[0][0] + ps[-1..-1] }, seq(opt(:object_pair)) ), skip(/\}\s*/) ) { |ps| ps[0].inject({}) { |h, p| h.merge(p) } } ) rule(:value_space, opt(skip(/\s+/))) rule( :value, seq( :value_space, alt(:object, :array, :string, :number, :keyword), :value_space ) { |v| v[0] } ) parser(:json, seq(:value, eof) { |j| j[0] }) end This is a good bit more compact than the Scheme equivalent at the above link. James Edward Gray II