Hi,

>      - 191529 2 1  445 Thomas Link (RE lexer, ruby eval)
> 359665 279539 3 0  405 Thomas & Paolo (RE + eval)

May I ask which test are missing here, since it passes all my test.

> 2079190     - 0 0  653 Eric Mahurin (Grammar, unreleased, ruby2cext)

This made me quite curious. Unfortunately it seems ruby2cext doesn't
support ruby19 yet. Anyway, will this coming-up version of grammar be
a drop-in replacement/update for grammar 0.5, i.e. is it safe to use
grammar 0.5 now?

BTW I took Paolo's hacked version of my humble submission and
converted it for use with StringScanner which has better performance
it seems. Since Paolo uses numbered groups, StringScanner is fine. I
slightly modified the regexp to catch something like '"a" "b"'.

Regards,
Thomas.


require 'strscan'

class JSONParser

    def initialize
        @rxe = /
            \[|\]|
            \{|\}|
            (:)|
            (,[ \t\r\n]*[}\]])|
            ,|
            ("(?>[^"\\]+|\\(?:u[0-9a-fA-F]{4}|[bfnrt"\/\\]))*"(?![ \t\r
\n]"))|
            -?(?=\d)(?>0|[1-9]\d*)(?>\.\d+)?(?>[Ee][+-]?\d+)?(?!\d)|
            true|
            false|
            (null)|
            (?>[ \t\r\n]+)|
            ((?>.+))
            /xmu
    end

    def parse(json)
        scanner = StringScanner.new(json)
        out = []
        until (scanner.skip(/[[:space:][:cntrl:]]*/); scanner.eos?)
            scan = scanner.scan(@rxe)
            if scanner[5] or scanner[2]
                invalid(scanner[2] || scanner[5])
            elsif scanner[1]
                out << '=>'
            elsif scanner[3]
                out << scanner[3].gsub(/#/, '\\\\#')
            elsif scanner[4]
                out << 'nil'
            else
                out << scan
            end
        end
        begin
            return eval(out.join(' '))
        rescue Exception => e
            invalid(json)
        end
    end

    def invalid(string)
        raise RuntimeError, 'Invalid JSON: %s' % string
    end

end