------art_3836_18216911.1202085030823
Content-Type: multipart/alternative; 
	boundary---art_3837_26110848.1202085030824"

------art_3837_26110848.1202085030824
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Feb 1, 2008 7:55 AM, Ruby Quiz <james / grayproductions.net> wrote:

> In honor of that, this week's Ruby Quiz is to write a parser for JSON.


My first solution uses an parser generator API similar to my rubyforge
"grammar" package.  The parser generator is only 89 lines (excluding
comment/blank lines).  The API is fairly complete to build a variety of
parsers.  The same API can be use to build a parser, lexer, preprocessor,
etc (and you could multi-thread them).  The reason this is so simple is that
I'm using Ruby as a DSL to specify the language grammar.  The file
specifying the JSONParser class is only 58 lines.

Here is the simple Grammar0 parsing DSL class and the JSONParser class using
it:

http://pastie.caboo.se/147074

http://pastie.caboo.se/147075

------art_3837_26110848.1202085030824
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Feb 1, 2008 7:55 AM, Ruby Quiz &lt;<a hrefailto:james / grayproductions.net">james / grayproductions.net</a>&gt; wrote:<br><div classmail_quote"><blockquote classmail_quote" styleorder-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
In honor of that, this week&#39;s Ruby Quiz is to write a parser for JSON.</blockquote><div><br>My first solution uses an parser generator API similar to my rubyforge &quot;grammar&quot; package.&nbsp; The parser generator is only 89 lines (excluding comment/blank lines).&nbsp; The API is fairly complete to build a variety of parsers.&nbsp; The same API can be use to build a parser, lexer, preprocessor, etc (and you could multi-thread them).&nbsp; The reason this is so simple is that I&#39;m using Ruby as a DSL to specify the language grammar.&nbsp; The file specifying the JSONParser class is only 58 lines.<br>
<br>Here is the simple Grammar0 parsing DSL class and the JSONParser class using it:<br><br><a hrefttp://pastie.caboo.se/147074">http://pastie.caboo.se/147074</a><br><br><a hrefttp://pastie.caboo.se/147075">http://pastie.caboo.se/147075</a><br>
<br><br></div></div>

------art_3837_26110848.1202085030824--

------art_3836_18216911.1202085030823
Content-Type: application/octet-stream; name=grammar0.rb
Content-Transfer-Encoding: base64
X-Attachment-Id: f_fc8adfdp0
Content-Disposition: attachment; filename=grammar0.rb

IyBtaW5pbWFsbHkgZmVhdHVyZWQgR3JhbW1hciBjbGFzcyAoY29tcGxldGVseSBvbi10aGUtZmx5
LCBubyBjb2RlIGdlbikKCmNsYXNzIEdyYW1tYXIwCgogICAgIyBwcm94eSBmb3IgYW5vdGhlciBn
cmFtbWFyIG9yIGEgYmxvY2sgYWN0aW5nIGFzIGEgZ3JhbW1hcgogICAgZGVmIGluaXRpYWxpemUo
Z3JhbW1hcj1uaWwsJmJsb2NrKSAjIDp5aWVsZDogaW5wdXQsIG91dHB1dCwgbG9va2FoZWFkLCBm
YWlsCiAgICAgICAgQGdyYW1tYXIgPSBncmFtbWFyfHxibG9jawogICAgZW5kCiAgICAjIHBhcnNl
IGlucHV0IHRvIG91dHB1dAogICAgZGVmIHNjYW4oaW5wdXQsb3V0cHV0LGxvb2thaGVhZD1baW5w
dXQuZ2V0Y10sZmFpbD10cnVlKQogICAgICAgIEBncmFtbWFyW2lucHV0LG91dHB1dCxsb29rYWhl
YWQsZmFpbF0KICAgIGVuZAogICAgYWxpYXNfbWV0aG9kKDpbXSwgOnNjYW4pCiAgICAjIGFsdGVy
bmF0aW9uIG9mICtzZWxmKyBvciArb3RoZXIrIAogICAgZGVmIHwob3RoZXIpCiAgICAgICAgR3Jh
bW1hcjAgeyB8aW5wdXQsb3V0cHV0LGxvb2thaGVhZCxmYWlsfAogICAgICAgICAgICBzZWxmW2lu
cHV0LG91dHB1dCxsb29rYWhlYWQsZmFsc2VdIHx8CiAgICAgICAgICAgIG90aGVyW2lucHV0LG91
dHB1dCxsb29rYWhlYWQsZmFpbF0KICAgICAgICB9CiAgICBlbmQKICAgICMgc2VxdWVuY2Ugb2Yg
K3NlbGYrIGZvbGxvd2VkIGJ5ICtvdGhlcisKICAgIGRlZiArKG90aGVyKQogICAgICAgI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ZW5kCiAgICAKICAgICMgTWF0Y2ggbm90aGluZwogICAgTlVMTCA9IEdyYW1tYXIwIHsgdHJ1ZSB9
CiAgICAjIE1hdGNoIHRoZSBlbmQgb2Ygb3VyIGlucHV0IChlbmQtb2YtZmlsZSkKICAgIEVPRiA9
IEdyYW1tYXIwIHsgfF8sXyxsb29rYWhlYWQsZmFpbHwKICAgICAgICAhbG9va2FoZWFkWzBdIHx8
IGZhaWwgJiYgZXJyb3IoIkVPRiIsIGxvb2thaGVhZCkKICAgIH0KICAgICMgTWF0Y2ggYW55IHNp
bmdsZSBlbGVtZW50CiAgICBBTlkgPSBHcmFtbWFyMCB7IHxpbnB1dCxvdXRwdXQsbG9va2FoZWFk
LGZhaWx8CiAgICAgICAgaWYgbG9va2FoZWFkWzBdCiAgICAgICAgICAgIG91dHB1dCA8PCBsb29r
YWhlYWRbMF0KICAgICAgICAgICAgbG9va2FoZWFkWzBdID0gaW5wdXQuZ2V0YwogICAgICAgICAg
ICB0cnVlCiAgICAgICAgZWxzaWYgZmFpbAogICAgICAgICAgICBlcnJvcigiQU5ZIiwgbmlsLCAi
RU9GIikKICAgICAgICBlbmQKICAgIH0KCiAgICAjIEFuIGVtcHR5IGNvbnRhaW5lciB0aGF0IGRp
c2NhcmRzIGFueXRoaW5nIHlvdSBwdXQgaW4gaXQuICBOb3QgYSBncmFtbWFyLgogICAgVk9JRCA9
IE9iamVjdC5uZXcKICAgIGNsYXNzIDw8IFZPSUQKICAgICAgICBkZWYgY29uY2F0KHNlcSk7IHNl
bGY7IGVuZAogICAgICAgIGRlZiA8PChzZXEpOyBzZWxmOyBlbmQKICAgIGVuZAoKZW5kCgoK
------art_3836_18216911.1202085030823
Content-Type: application/octet-stream; name=jsonparser.grammar0.rb
Content-Transfer-Encoding: base64
X-Attachment-Id: f_fc8adxra1
Content-Disposition: attachment; filename=jsonparser.grammar0.rb

cmVxdWlyZSAnZ3JhbW1hcjAnCnJlcXVpcmUgJ3N0cmluZ2lvJwoKY2xh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gICAgICAgICAgICBFKD90KS5ncm91cCB7ID9cdCB9IHwKICAgICAgICAg
ICAgICAgIEUoP3UpLmZpbHRlciArIChoZXgraGV4K2hleCtoZXgpLmdyb3VwKCIiKSB7IHxzfCBz
LnRvX2koMTYpIH0KICAgICAgICAgICAgKSB8CiAgICAgICAgICAgIEFOWQogICAgICAgIHN0cmlu
ZyA9IEUoP1wiKS5maWx0ZXIgKyBjaGFyLnJlcGVhdDAoRSg/XCIpLmZpbHRlcikuZ3JvdXAoIiIp
CiAgICAgICAgCiAgICAgICAgYWxwaGEgPSBFKD9fKSB8IEUoP2EuLj96KSB8IEUoP0EuLj9aKQog
ICAgICAgIGJvb2xlYW4gPSBhbHBoYS5yZXBlYXQxLmdyb3VwKCIiKSB7IHxzfAogICAgICAgICAg
ICBjYXNlIHMKICAgICAgICAgICAgd2hlbiAidHJ1ZSIgOiB0cnVlCiAgICAgICAgICAgIHdoZW4g
ImZhbHNlIiA6IGZhbHNlCiAgICAgICAgICAgIHdoZW4gIm51bGwiIDogbmlsCiAgICAgICAgICAg
IGVsc2UgZXJyb3IoInRydWV8ZmFsc2V8bnVsbCIsIG5pbCwgcykKICAgICAgICAgICAgZW5kCiAg
ICAgICAgfQogICAgICAgIAogICAgICAgIHZhbHVlID0gUmVjdXJzZSB7IHx2YWx1ZXwKICAgICAg
ICAgICAgZWxlbWVudHMgPSBSaWdodCB7IHxlbGVtZW50c3wKICAgICAgICAgICAgICAgIHZhbHVl
ICsgd3MgKyAoRSg/LCkuZmlsdGVyICsgd3MgKyBlbGVtZW50cyB8IE5VTEwpCiAgICAgICAgICAg
IH0KICAgICAgICAgICAgYXJyYXkgPSBFKD9bKS5maWx0ZXIgKyB3cyArIChlbGVtZW50cyArIHdz
fE5VTEwpLmdyb3VwKFtdKSArIEUoP10pLmZpbHRlcgogICAgICAgICAgICAKICAgICAgICAgICAg
cGFpciA9IChzdHJpbmcgKyB3cyArIEUoPzopLmZpbHRlciArIHdzICsgdmFsdWUpLnByb2Nlc3Mo
W10pIHsgfGgsa3Z8CiAgICAgICAgICAgICAgICBoW2t2WzBdXSA9IGt2WzFdCiAgICAgICAgICAg
IH0KICAgICAgICAgICAgbWVtYmVycyA9IFJpZ2h0IHsgfG1lbWJlcnN8CiAgICAgICAgICAgICAg
ICBwYWlyICsgd3MgKyAoRSg/LCkuZmlsdGVyICsgd3MgKyBtZW1iZXJzIHwgTlVMTCkKICAgICAg
ICAgICAgfQogICAgICAgICAgICBvYmplY3QgPSBFKD97KS5maWx0ZXIgKyB3cyArIChtZW1iZXJz
fE5VTEwpLmdyb3VwKHt9KSArIEUoP30pLmZpbHRlciAKCiAgICAgICAgICAgIHN0cmluZyB8IG51
bWJlciB8IG9iamVjdCB8IGFycmF5IHwgYm9vbGVhbgogICAgICAgIH0KCiAgICAgICAgc3VwZXIo
d3MgKyB2YWx1ZSArIHdzICsgRU9GKQoKICAgIGVuZAoKICAgIGRlZiBwYXJzZShzdHIpCiAgICAg
ICAgc2VsZi5zY2FuKFN0cmluZ0lPLm5ldyhzdHIpLCBvdXRwdXQ9W10pCiAgICAgICAgcmFpc2Ug
aWYgb3V0cHV0Lmxlbmd0aCE9MQogICAgICAgIG91dHB1dFswXQogICAgZW5kCgplbmQKCgo------art_3836_18216911.1202085030823--