[#289458] Parsing JSON (#155) — Ruby Quiz <james@...>

The three rules of Ruby Quiz:

86 messages 2008/02/01
[#289675] Re: [QUIZ] Parsing JSON (#155) — steve <oksteev@...> 2008/02/03

Hey guys

[#289709] Re: [QUIZ] Parsing JSON (#155) — "Eric Mahurin" <eric.mahurin@...> 2008/02/04

On Feb 3, 2008 8:14 AM, steve <oksteev@yahoo.com> wrote:

[#289712] Re: [QUIZ] Parsing JSON (#155) — Clifford Heath <no@...> 2008/02/04

Eric Mahurin wrote:

[#289715] Re: [QUIZ] Parsing JSON (#155) — "Eric Mahurin" <eric.mahurin@...> 2008/02/04

On Feb 3, 2008 6:25 PM, Clifford Heath <no@spam.please.net> wrote:

[#289718] Re: [QUIZ] Parsing JSON (#155) — Clifford Heath <no@...> 2008/02/04

Eric Mahurin wrote:

[#289722] Re: [QUIZ] Parsing JSON (#155) — "Eric Mahurin" <eric.mahurin@...> 2008/02/04

On Feb 3, 2008 6:39 PM, Clifford Heath <no@spam.please.net> wrote:

[#289557] Comparing Active Record VS Datamapper...? — Softmind Technology <softmindtechnology@...>

Hello,

15 messages 2008/02/02
[#289633] Re: Comparing Active Record VS Datamapper...? — Ilan Berci <coder68@...> 2008/02/02

Softmind Technology wrote:

[#289636] Re: Comparing Active Record VS Datamapper...? — "s.ross" <cwdinfo@...> 2008/02/03

On 2/2/08 3:56 PM, "Ilan Berci" <coder68@yahoo.com> wrote:

[#289572] Extracting vowels and consonants using regular expression — Dondi <Donovan.Dillon@...>

I am trying to parse a string and extract all vowels and consonants

10 messages 2008/02/02

[#289579] FastRI 0.3.1: faster, Leopard compatibility, etc. — Mauricio Fernandez <mfp@...>

FastRI is an alternative to the ri documentation browser for Ruby.

21 messages 2008/02/02
[#289683] Re: [ANN] FastRI 0.3.1: faster, Leopard compatibility, etc. — botp <botpena@...> 2008/02/03

On Feb 3, 2008 1:07 AM, Mauricio Fernandez <mfp@acm.org> wrote:

[#290164] Re: [ANN] FastRI 0.3.1: faster, Leopard compatibility, etc. — Eric Hodel <drbrain@...7.net> 2008/02/06

On Feb 3, 2008, at 07:40 AM, botp wrote:

[#291478] Re: [ANN] FastRI 0.3.1: faster, Leopard compatibility, etc. — Mauricio Fernandez <mfp@...> 2008/02/18

On Thu, Feb 07, 2008 at 07:52:20AM +0900, Eric Hodel wrote:

[#291493] Re: [ANN] FastRI 0.3.1: faster, Leopard compatibility, etc. — James Gray <james@...> 2008/02/18

On Feb 18, 2008, at 5:53 AM, Mauricio Fernandez wrote:

[#289674] Ruby for game programming — t3chn0n3rd <darrin_allen@...>

Is anyone using Ruby for game programming?

17 messages 2008/02/03

[#289859] how to say # in ruby — Dan Ford <wade@...>

I'm accustomed to usenet as opposed to whatever this is.

22 messages 2008/02/04

[#289900] Introducing Waves - Web App Framework — Dan Yoder <dan@...>

I am pleased to announce the first beta release of Waves, an open-

32 messages 2008/02/05
[#290017] Re: [ANN] Introducing Waves - Web App Framework — Daniel DeLorme <dan-ml@...42.com> 2008/02/06

Dan Yoder wrote:

[#290001] Computer Science Problems — markonlinux@...

Hi all,

33 messages 2008/02/05

[#290020] Ruby type-safe? Ruby strongly/weakly typed? Ruby pitfalls? — "rule.rule.rule@..." <rule.rule.rule@...>

Hi,

36 messages 2008/02/06

[#290022] simple ruby proxy server? — Dt Town <dtown22@...>

I am trying to right at application which will simply record the headers

13 messages 2008/02/06

[#290167] Array Practice — Adam Akhtar <adamtemporary@...>

As some of you may know from previous threads im trying to practice

27 messages 2008/02/06

[#290195] info on block arguments — Russell Me <russ@...>

I'm trying to pick up ruby and I'm impressed by all the cool stuff it

24 messages 2008/02/07

[#290208] idiom I've not seen before — Rob Saul <wyrd@...>

12 messages 2008/02/07

[#290244] Parsing JSON (#155) — Ruby Quiz <james@...>

We saw a large variety of solutions for this week's problem. Many of them used

13 messages 2008/02/07

[#290245] using net::ssh shell to sudo to another user and execute commands — "wbsurfver@..." <wbsurfver@...>

11 messages 2008/02/07

[#290272] is lots of files with Threads faster? — Chris Richards <evilgeenius@...>

Im required to open 50+ files and parse the data in them. WOuld using

11 messages 2008/02/07

[#290296] BigDecimal.new('15.25') == 15.25, false ?? — Henry Jones <mathieu.houle@...>

Hi,

14 messages 2008/02/07

[#290328] trouble with FileUtils.rm() -- Invalid arguement error — an an <dtown22@...>

I have been screwing with this for the last hour, and I still cant get

12 messages 2008/02/08

[#290374] Internal Rate of Return (#156) — Ruby Quiz <james@...>

The three rules of Ruby Quiz:

47 messages 2008/02/08

[#290483] Beginner help - txt dungeon — Jonathon Hartoon <ezrickknight@...>

Hi. I have tried to learn other programming languages before and ruby

20 messages 2008/02/09

[#290716] non case sensitive searching — Adam Akhtar <adamtemporary@...>

If i want to see if a list contains a particular word how would i go

15 messages 2008/02/11

[#290735] Rev/actor TCP monkey patching — fedzor <fedzor@...>

Short and sweet -

13 messages 2008/02/11

[#290825] How do C programmers do unit testing? — "M. Edward (Ed) Borasky" <znmeb@...>

This may seem like a silly question, but how do C programmers (assume

13 messages 2008/02/12

[#290931] Robert's Ruby Riddle: Local or Method — "Robert Dober" <robert.dober@...>

Hi list I was just thinking it might fun to present some of Ruby's

10 messages 2008/02/13

[#290982] ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...>

I recently upgraded to ruby 1.8.6 on mac os x 10.4.7. I had ruby tk

47 messages 2008/02/13
[#290994] Re: ruby tk -- how do you get it working? — Jeremy Henty <onepoint@...> 2008/02/13

On 2008-02-13, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

[#291001] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/14

Jeremy Henty wrote:

[#291006] Re: ruby tk -- how do you get it working? — Hidetoshi NAGAI <nagai@...> 2008/02/14

From: 7stud -- <bbxx789_05ss@yahoo.com>

[#291014] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/14

Hidetoshi NAGAI wrote:

[#291447] Re: ruby tk -- how do you get it working? — Hidetoshi NAGAI <nagai@...> 2008/02/18

From: 7stud -- <bbxx789_05ss@yahoo.com>

[#291459] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/18

Hidetoshi NAGAI wrote:

[#291519] Re: ruby tk -- how do you get it working? — Morton Goldberg <m_goldberg@...> 2008/02/18

On Feb 18, 2008, at 2:52 AM, 7stud -- wrote:

[#291529] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/18

Morton Goldberg wrote:

[#291556] Re: ruby tk -- how do you get it working? — Morton Goldberg <m_goldberg@...> 2008/02/19

On Feb 18, 2008, at 3:05 PM, 7stud -- wrote:

[#291567] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/19

Morton Goldberg wrote:

[#291601] Re: ruby tk -- how do you get it working? — Morton Goldberg <m_goldberg@...> 2008/02/19

On Feb 18, 2008, at 10:28 PM, 7stud -- wrote:

[#291603] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/19

Morton Goldberg wrote:

[#291621] Re: ruby tk -- how do you get it working? — Morton Goldberg <m_goldberg@...> 2008/02/19

On Feb 19, 2008, at 7:05 AM, 7stud -- wrote:

[#291626] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/19

Morton Goldberg wrote:

[#291637] Re: ruby tk -- how do you get it working? — John Joyce <dangerwillrobinsondanger@...> 2008/02/19

[#291707] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/20

John Joyce wrote:

[#291742] Re: ruby tk -- how do you get it working? — Morton Goldberg <m_goldberg@...> 2008/02/20

On Feb 19, 2008, at 7:41 PM, 7stud -- wrote:

[#291747] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/20

Morton Goldberg wrote:

[#291819] Re: ruby tk -- how do you get it working? — "Leslie Viljoen" <leslieviljoen@...> 2008/02/20

On Feb 20, 2008 8:59 AM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

[#291846] Re: ruby tk -- how do you get it working? — John Joyce <dangerwillrobinsondanger@...> 2008/02/20

[#291967] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/21

John Joyce wrote:

[#292774] Re: ruby tk -- how do you get it working? — Tim Ferrell <s0nspark@...> 2008/02/27

7stud -- wrote:

[#292964] Re: ruby tk -- how do you get it working? — 7stud -- <bbxx789_05ss@...> 2008/02/29

Tim Ferrell wrote:

[#292971] Re: ruby tk -- how do you get it working? — Tim Ferrell <s0nspark@...> 2008/02/29

7stud -- wrote:

[#292992] Re: ruby tk -- how do you get it working? — Tim Ferrell <s0nspark@...> 2008/02/29

Tim Ferrell wrote:

[#290985] my noob "numbering lines" programe. How to improve? — Adam Akhtar <adamtemporary@...>

Give us hints on how to improve this program. If you want to show your

11 messages 2008/02/13

[#291013] Memcached: Dealing with unknown keys — Tony Garcia <tony23@...>

I was wondering if there is a way to handle reading keys when you don't

14 messages 2008/02/14

[#291039] Internal Rate of Return (#156) — Ruby Quiz <james@...>

Solving the IRR equation is essentially a matter of computational guesswork.

15 messages 2008/02/14

[#291084] object specific methods and id — UpsNDowns <tnospamhomas@...>

Hi,

24 messages 2008/02/14
[#291088] Re: object specific methods and id — 7stud -- <bbxx789_05ss@...> 2008/02/14

UpsNDowns wrote:

[#291089] Re: object specific methods and id — Gary Wright <gwtmp01@...> 2008/02/14

[#291184] Re: object specific methods and id — UpsNDowns <tnospamhomas@...> 2008/02/15

Thanks for the reply.

[#291261] Re: object specific methods and id — Gary Wright <gwtmp01@...> 2008/02/16

[#291302] Re: object specific methods and id — UpsNDowns <tnospamhomas@...> 2008/02/16

Gary Wright wrote:

[#291141] The Smallest Circle (#157) — Matthew D Moss <matthew.moss@...>

The three rules of Ruby Quiz 2:

61 messages 2008/02/15

[#291192] Proper way to RDoc markup? — Serg Koren <skoren@...>

Hi,

12 messages 2008/02/15

[#291269] Alter String base class to perform new (private methods) before returning itself when called by print statements — "Steven G. Harms" <steven.harms@...>

class String

8 messages 2008/02/16

[#291280] GC error ? — Piotr Sawicki <piotr.sawicki@...>

Run this program and observe memory usage.

26 messages 2008/02/16
[#291584] Re: GC error ? — "evanwebb@..." <evanwebb@...> 2008/02/19

On Feb 16, 4:12=A0am, Piotr Sawicki <piotr.sawi...@gmail.com> wrote:

[#291281] TRAC - Trac, Project Leads, Python, and Mr. Noah Kantrowitz (sanitizer) — Ilias Lazaridis <ilias@...>

Essence:

14 messages 2008/02/16

[#291354] Slide Show v0.1 - A Free Web Alternative to PowerPoint and KeyNote in Ruby Now Live — "Gerald Bauer" <geraldbauer2007@...>

Hello,

12 messages 2008/02/17

[#291381] displaying user inputed arrays — Isaac Toothyxdip <toothyxdip@...>

[code]

36 messages 2008/02/17
[#291405] Re: displaying user inputed arrays — Wally T Terrible <wally.terrible@...> 2008/02/17

I'm not entirely sure what you intend to do. If you wanted to get five

[#291424] Re: displaying user inputed arrays — Siep Korteling <s.korteling@...> 2008/02/17

Wally T Terrible wrote:

[#291858] Re: displaying user inputed arrays — "Todd Benson" <caduceass@...> 2008/02/20

On Feb 17, 2008 9:18 AM, Isaac Toothyxdip <toothyxdip@gmail.com> wrote:

[#291877] Re: displaying user inputed arrays — "Todd Benson" <caduceass@...> 2008/02/20

On Wed, Feb 20, 2008 at 1:30 PM, Todd Benson <caduceass@gmail.com> wrote:

[#291884] Re: displaying user inputed arrays — Isaac Toothyxdip <toothyxdip@...> 2008/02/20

Thanks that works!

[#291887] Re: displaying user inputed arrays — Isaac Toothyxdip <toothyxdip@...> 2008/02/20

Isaac Toothyxdip wrote:

[#291899] Re: displaying user inputed arrays — "Todd Benson" <caduceass@...> 2008/02/20

On Wed, Feb 20, 2008 at 3:56 PM, Isaac Toothyxdip <toothyxdip@gmail.com> wrote:

[#291515] Connecting to HyperTerminal — Active View <active.view@...>

Hi all...

16 messages 2008/02/18
[#291525] Re: Connecting to HyperTerminal — Ben Bleything <ben@...> 2008/02/18

On Tue, Feb 19, 2008, Active View wrote:

[#291630] Object#freeze as a basis for caching of method results? — "Shot (Piotr Szotkowski)" <shot@...>

Hello, ruby-talk.

12 messages 2008/02/19

[#291664] ||= [] idiom — "Leslie Viljoen" <leslieviljoen@...>

I often make use of this idiom to add something to an array in a hash of arrays:

24 messages 2008/02/19

[#291677] Is there any object-oriented File class in ruby ? — tom_33 <tomjbr.56770318@...>

I am very new to Ruby and my question is about trying to understand

22 messages 2008/02/19

[#291682] HELP: Need to continue the loop after the exception — mirth <mirthcyy@...>

hi guys,

12 messages 2008/02/19

[#291717] IP Address to Decimal - is an one-liner possible? — "Tiago Pinto" <thpinto@...>

Hi guys,

22 messages 2008/02/20

[#291774] What does *args do? — Stedwick <philip.brocoum@...>

I sometimes see funcs declared with def fun (blah, *args)

14 messages 2008/02/20

[#291853] 32bit vs 64bit vs UML performance — Lionel Bouton <lionel-subscription@...>

I've run the QUIZ benchmark on several systems to check their relative

17 messages 2008/02/20

[#291990] beginner's problem with sqlite3 — Tom Cloyd <tomcloyd@...>

It would be helpful if the sqlite3-ruby documentation offered one or two

15 messages 2008/02/21

[#292056] Where does instance_eval _magically_ set the variable to? — Andrew Chen <hangfei@...>

Hi,

13 messages 2008/02/21

[#292080] Newbie Mem Leak Issue — Keith Barr <keith.barr@...>

I am fairly new to Ruby and a program that I have created seems to have

14 messages 2008/02/21

[#292186] gsub("\\", "\\\\") seems unintuitive — John Woods <jqwoods@...>

The following confusing behavior is noted in the pickaxe book (2nd ed)

11 messages 2008/02/22

[#292250] ANN: Teach yourself Ruby - the hard way! — "Martin DeMello" <martindemello@...>

A frequent question from Ruby newcomers is "Okay, I've read the

12 messages 2008/02/23

[#292269] Monkeypatching is Destroying Ruby — "Avdi Grimm" <avdi@...>

Hi folks,

118 messages 2008/02/23
[#292274] Re: Monkeypatching is Destroying Ruby — "Eric Mahurin" <eric.mahurin@...> 2008/02/23

On Sat, Feb 23, 2008 at 3:07 PM, Avdi Grimm <avdi@avdi.org> wrote:

[#292385] Re: Monkeypatching is Destroying Ruby — James Gray <james@...> 2008/02/25

On Feb 23, 2008, at 4:23 PM, Eric Mahurin wrote:

[#292281] Re: Monkeypatching is Destroying Ruby — "M. Edward (Ed) Borasky" <znmeb@...> 2008/02/23

Avdi Grimm wrote:

[#292514] Re: Monkeypatching is Destroying Ruby — "Michal Suchanek" <hramrach@...> 2008/02/25

On 24/02/2008, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:

[#292584] Re: Monkeypatching is Destroying Ruby — "Jones, Brian - McClatchy Interactive" <bjones@...> 2008/02/26

I'd be at least a little interested in potentially offering developers

[#292594] Re: Monkeypatching is Destroying Ruby — Trans <transfire@...> 2008/02/26

[#292601] Re: Monkeypatching is Destroying Ruby — James Britt <james.britt@...> 2008/02/26

Trans wrote:

[#292603] Re: Monkeypatching is Destroying Ruby — "Avdi Grimm" <avdi@...> 2008/02/26

On Tue, Feb 26, 2008 at 1:25 PM, James Britt <james.britt@gmail.com> wrote:

[#292362] Re: Monkeypatching is Destroying Ruby — furtive.clown@... 2008/02/24

[#292366] Re: Monkeypatching is Destroying Ruby — Gary Wright <gwtmp01@...> 2008/02/24

[#292961] Re: Monkeypatching is Destroying Ruby — Trans <transfire@...> 2008/02/29

[#292978] Re: Monkeypatching is Destroying Ruby — "Eric Mahurin" <eric.mahurin@...> 2008/02/29

On Fri, Feb 29, 2008 at 6:20 AM, Trans <transfire@gmail.com> wrote:

[#292368] Suprising behaviour with "def property=" method — "Farrel Lifson" <farrel.lifson@...>

I had a bit of a surprise with the following

14 messages 2008/02/24

[#292394] NoMethodError: private method `to_date' — Sukeerthi Adiga <sukeerthiadiga@...>

Loading development environment.

12 messages 2008/02/25

[#292398] Thread#raise, Thread#kill, and timeout.rb are unsafe — Charles Oliver Nutter <charles.nutter@...>

I wrote up an article on Thread#raise, Thread#kill, and timeout.rb that

58 messages 2008/02/25
[#294446] Re: Thread#raise, Thread#kill, and timeout.rb are unsafe — Yukihiro Matsumoto <matz@...> 2008/03/13

Hi,

[#294479] Re: Thread#raise, Thread#kill, and timeout.rb are unsafe — Paul Brannan <pbrannan@...> 2008/03/13

On Fri, Mar 14, 2008 at 01:02:52AM +0900, Yukihiro Matsumoto wrote:

[#294491] Re: Thread#raise, Thread#kill, and timeout.rb are unsafe — Yukihiro Matsumoto <matz@...> 2008/03/13

Hi,

[#294551] Re: Thread#raise, Thread#kill, and timeout.rb are unsafe — Paul Brannan <pbrannan@...> 2008/03/14

On Fri, Mar 14, 2008 at 07:43:28AM +0900, Yukihiro Matsumoto wrote:

[#294559] Re: Thread#raise, Thread#kill, and timeout.rb are unsafe — Yukihiro Matsumoto <matz@...> 2008/03/14

Hi,

[#294591] Re: Thread#raise, Thread#kill, and timeout.rb are unsafe — Tanaka Akira <akr@...> 2008/03/14

In article <E1JaB7P-0000Zc-QP@x61.netlab.jp>,

[#294639] Re: Thread#raise, Thread#kill, and timeout.rb are unsafe — Yukihiro Matsumoto <matz@...> 2008/03/15

Hi,

[#294917] Re: Thread#raise, Thread#kill, and timeout.rb are unsafe — Tanaka Akira <akr@...> 2008/03/18

In article <E1JaM9H-0000jj-AB@x61.netlab.jp>,

[#295093] Re: Thread#raise, Thread#kill, and timeout.rb are unsafe — Paul Brannan <pbrannan@...> 2008/03/19

On Tue, Mar 18, 2008 at 02:04:40PM +0900, Tanaka Akira wrote:

[#292540] Math problem — grandmabeckie@...

I am try to help my daughter:

25 messages 2008/02/26

[#292704] Getting number of days in a month — Shandy Nantz <shandybleu@...>

This is probably an easy question but I am trying to get at the number

26 messages 2008/02/27

[#292781] MacRuby — "Laurent Sansonetti" <laurent.sansonetti@...>

Hi,

16 messages 2008/02/28

[#292795] Need a regex searching html code — Chirantan <chirantan.rajhans@...>

I have an html code into string. I want to retrieve the content (Can

18 messages 2008/02/28

[#292816] proc.new and return — S2 <email@...>

I am sure this is a really easy question for most of you, but i was not

15 messages 2008/02/28

[#292839] Proposed Solutions - Was [ Re: Monkeypatching is Destroying Ruby ] — hemant <gethemant@...>

On Tue, Feb 26, 2008 at 11:55 PM, James Britt <james.britt@gmail.com> wrote:

17 messages 2008/02/28

[#292860] Named sprintf parameters — Trans <transfire@...>

I have a question and perhaps a bit of challenge for those with mad

15 messages 2008/02/28

[#292865] constants defined in Kernel are also defined in Object? — Paul Brannan <pbrannan@...>

This seems peculiar to me:

15 messages 2008/02/28

[#293044] making a monthly calendar... — Mikkel Bruun <mikkel@...>

Hey

25 messages 2008/02/29
[#293088] Re: making a monthly calendar... — "Todd Benson" <caduceass@...> 2008/03/01

On Fri, Feb 29, 2008 at 4:30 PM, Mikkel Bruun <mikkel@helenius.dk> wrote:

[#293090] Re: making a monthly calendar... — Mikkel Bruun <mikkel@...> 2008/03/01

Todd Benson wrote:

[#293092] Re: making a monthly calendar... — "Todd Benson" <caduceass@...> 2008/03/01

On Sat, Mar 1, 2008 at 2:34 AM, Mikkel Bruun <mikkel@helenius.dk> wrote:

[#293223] Re: making a monthly calendar... — "Todd Benson" <caduceass@...> 2008/03/03

Oh my goodness! A thousand apologies. I gave you a rotating month. See below.

[#293264] Re: making a monthly calendar... — Mikkel Bruun <mikkel@...> 2008/03/03

So I ended up with:

[SUMMARY] Parsing JSON (#155)

From: Ruby Quiz <james@...>
Date: 2008-02-07 14:08:45 UTC
List: ruby-talk #290244
We saw a large variety of solutions for this week's problem.  Many of them used
a parser generator to construct their parser.  You do that by defining a grammar
that describes the syntax you need to read.  The parser generator then
translates your grammar into parsing code that will match the described syntax. 
Of the generators used, Treetop was definitely the most popular and is surely
worth a look if you want to do some grammar based parsing.

I'm not going to show a grammar based solution here, not because I don't like
them, but because I want to show a few of the ideas behind simple parsing.  To
do that, we will need to examine a hand-rolled solution.  Just to be clear
though, using grammar based parsers can often be a more robust choice if you can
find an official grammar for the content you need to parse.

Eric Mahurin sent in a hand-rolled solution that has quite a few advantages. 
First, it is trivial to adapt so that the entire content to be parsed doesn't
need to be read into memory.  It's also some very efficient code. 
Unfortunately, that makes it a touch less obvious if you aren't already familiar
with parsing techniques.

Given that, I'm going to show my own hand-rolled recursive descent parser.  It's
not as cool as Eric's but it's a little easier to take in.  We will say it's a
good introduction to Eric's code, which you should be able to follow after I
break this down.

Here's the beginning of my parser:

	#!/usr/bin/env ruby -wKU
	
	require "strscan"
	
	class JSONParser
	  AST = Struct.new(:value)
	  
	  def parse(input)
	    @input = StringScanner.new(input)
	    parse_value.value
	  ensure
	    @input.eos? or error("Unexpected data")
	  end
	  
	  private
	  
	  # ...

One of the first concerns when parsing is the need to manage where you currently
are in the input.  If you treat the input as an IO object, you can read input
piece by piece and the IO object itself naturally keeps track of where you are. 
For String input though, it's often easier to use Ruby's standard StringScanner
class.  It wraps the input and allows you to test regular expression matches
against the head of that input.  It will tell you when they match or don't and
when they do, your position automatically advances forward beyond that match. 
You can see that I set this up in the code above.

The only public method for my class is parse().  It prepares the StringScanner
as I just described, tries to parse a JSON value out of the input, then makes
sure we consumed all of the input.  Note that my use of ensure here isn't very
standard.  I'm just using it to run some code at the end of the method without
changing the return value of the method.

The AST definition also merits a bit of discussion.  It would have been nice to
just have each method return the Ruby objects for the JSON it parsed.  However,
false and nil (null in JSON) are legal JSON parses in some places.  If I return
those, I won't be able to tell if my parse succeeded or failed.  To get around
that, all parsed JSON values are wrapped in a trivial abstract syntax tree
object.  Returning this object is always true and, after I've verified that the
parse worked, it's just one more method call to retrieve the actual value it
parsed.

It's worth mentioning that this parser is based on the not quite correct
definition of JSON I put forth in the quiz tests.  Only objects and arrays are
allowed to be top-level JSON values.  An easy fix is to replace this line

	    # ...
	    
	    parse_value.value
	    
	    # ...

with:

	    # ...
	    
	    if top_level = parse_object || parse_array
	      top_level.value
	    else
	      error("Illegal top-level JSON object")
	    end
	    
	    # ...

Now let's look at the main parser:

	  # ...
	  
	  def parse_value
	    trim_space
	    parse_object  or
	    parse_array   or
	    parse_string  or
	    parse_number  or
	    parse_keyword or
	    error("Illegal JSON value")
	  ensure
	    trim_space
	  end
	  
	  # ...

This method really sums up the basic strategy of recursive descent parsing.  At
each point, try to read out one of the legal values that can occur there.  You
can do that just by drilling down into more specialized methods that know how to
read that one thing.  If at any time you can't read a legal value, you have an
error.

Let's dig into the specialized parsers a bit more to see how this works:

	  # ...
	  
	  def parse_object
	    if @input.scan(/\{\s*/)
	      object     = Hash.new
	      more_pairs = false
	      while key = parse_string
	        @input.scan(/\s*:\s*/) or error("Expecting object separator")
	        object[key.value] = parse_value.value
	        more_pairs = @input.scan(/\s*,\s*/) or break
	      end
	      error("Missing object pair") if more_pairs
	      @input.scan(/\s*\}/) or error("Unclosed object")
	      AST.new(object)
	    else
	      false
	    end
	  end
	  
	  # ...

This code reads JSON objects.  It's pretty linear, so let's digest it in order. 
First, we have to have an opening brace or we don't have an object at all.  We
can see here that I try a regular expression on the StringScanner to see if that
is indeed what's up next.  If it is scan() will return true and @input will
advance past that brace for our future matches.  If it's false, we can't read an
object and the whole attempt is a bust.

When we know we're inside an object, we create the Ruby equivalent (a Hash),
fill it with all of the string/value pairs we can read, then make sure we find a
closing brace.  Reading the pairs is the trickiest part because we have to match
a string, followed by a colon, and finally a value.  Then, if we find a comma,
we know another pair is expected.  If not, we've read the whole object.  Note
that I verify these assumptions at every step and toss error()s if any of them
fail.  For parsing strings and values, we just reuse the parse_string() method
we first saw called in parse_value() and parse_value() itself.

You can see that I'm constantly trimming space around the JSON syntax.  I could
have also done that with repeated calls to the trim_space() helper we saw used
in parse_value(), but that fattens up the code a bit and slows things down with
more tests.  For these simple cases, I opted for the shortcut.

Having deciphered parse_object(), parse_array() is trivial:

	  # ...
	  
	  def parse_array
	    if @input.scan(/\[\s*/)
	      array       = Array.new
	      more_values = false
	      while contents = parse_value rescue nil
	        array << contents.value
	        more_values = @input.scan(/\s*,\s*/) or break
	      end
	      error("Missing value") if more_values
	      @input.scan(/\s*\]/) or error("Unclosed array")
	      AST.new(array)
	    else
	      false
	    end
	  end
	  
	  # ...

This is identical to the process we just examined save that it's pulling
individual values in the middle instead of string/value pairs.  This simplifies
the code a bit, as you can see.  We also throw these objects into a Ruby Array
instead of a Hash.

Now we are ready for parse_string() and it has a couple of helpers:

	  # ...
	  
	  def parse_string
	    if @input.scan(/"/)
	      string = String.new
	      while contents = parse_string_content || parse_string_escape
	        string << contents.value
	      end
	      @input.scan(/"/) or error("Unclosed string")
	      AST.new(string)
	    else
	      false
	    end
	  end
	  
	  def parse_string_content
	    @input.scan(/[^\\"]+/) and AST.new(@input.matched)
	  end
	  
	  def parse_string_escape
	    if @input.scan(%r{\\["\\/]})
	      AST.new(@input.matched[-1])
	    elsif @input.scan(/\\[bfnrt]/)
	      AST.new(eval(%Q{"#{@input.matched}"}))
	    elsif @input.scan(/\\u[0-9a-fA-F]{4}/)
	      AST.new([Integer("0x#{@input.matched[2..-1]}")].pack("U"))
	    else
	      false
	    end
	  end
	  
	  # ...

Whenever a structure you need to read gets more complicated, you want to break
it down into smaller parsers that read more specialized pieces.  Some probably
would have broken down the the string/value pairs from object (into a
parse_object_pair()), but you don't gain much for that and it was just simple
enough that I opted for the easier approach.  Here though we need to handle
content and escapes differently and there may be any combination of them in any
order inside the string.  Now the gain is worth it.

Content is easy enough to handle, since we can pass it through unaltered.  It's
already content in a Ruby String object.  Escapes we have to work on a little
more.  Some we just unescape, but others need to be converted.  I used pack() to
handle Unicode, but you can see that I was lazy and used eval() on the special
string escapes.  All of these have the same meaning in Ruby and thanks to the
match I know it's safe to eval() the contents without worrying about embedded
Ruby nastiness.

With those defined, parse_string() is similar to parse_array().  Find the start
of a JSON string, pull content and escapes as long as we can, then find the end
of the string.

The last two parsers are the easiest of all:

	  # ...
	  
	  def parse_number
	    @input.scan(/-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?\b/) and
	    AST.new(eval(@input.matched))
	  end
	  
	  def parse_keyword
	    @input.scan(/\b(?:true|false|null)\b/) and
	    AST.new(eval(@input.matched.sub("null", "nil")))
	  end
	  
	  # ...

These are just match and eval() as you can plainly see.  Again the evals() are
safe because the match ensures we aren't facing any dangerous content.

Some feel that using regular expressions like this isn't true parsing.  We
certainly could have chopped the number rule down into a bunch of smaller rules.
However, the number definition is squarely in the domain of what regular
expressions do well and I'm more of a practical kind of guy.  I have access to
regular expressions with this setup, the needed expression isn't really all that
complex, and I like easy jobs.  Thus I use them.

All that is left are the two helpers I used, though the implementations won't be
any surprise:

	  # ...
	  
	  def trim_space
	    @input.scan(/\s+/)
	  end
	  
	  def error(message)
	    if @input.eos?
	      raise "Unexpected end of input."
	    else
	      raise "#{message}:  #{@input.peek(@input.string.length)}"
	    end
	  end
	end

First, trim_space() can just try a match to advance us pass any whitespace.  It
may fail, because there wasn't any whitespace to skip, but that doesn't affect
us any.  We know that we aren't about to read whitespace after it is called,
either way.

My error() wrapper just raise()s exceptions.  It adds the content left to parse
so you can see where you had trouble or replaces the message altogether to warn
you that all of the content was exhausted.

That's all it takes to build a simple JSON parser.  Take some time to go look
through the other hand-rolled solutions now and I bet you'll be surprised by how
similar they work.  Then you can look into grammars and how they simplify the
process of defining new grammars.

The final Ruby Quiz will take us back into the world of finance...

In This Thread

Prev Next