From: Justin Collins Date: 2011-09-02T10:37:42+09:00 Subject: Re: ripper on array access operation On 08/31/2011 08:41 PM, Michael Edgar wrote: > On Aug 31, 2011, at 7:00 PM, justincollins@ucla.edu wrote: > >> Quoting "Surya G.": >> >>> unknown wrote in post #1019486: >>>> Quoting "Surya G.": >>>> >>>> I am pretty sure it's the :aref part. Ripper is making [] a special >>>> case. >>>> >>>> -Justin >>> Thats what it looks like but why not treat aref as any-other method >>> call. >>> >>> -- >>> Posted via http://www.ruby-forum.com/. > Aref is parsed differently from other method calls. Ripper delivers a parse tree, and by providing aref as a different AST node, a program consuming the parse tree can distinguish between a.[](1, 2) and a[1,2]. There are plenty of reasons (such as pretty-printing) that these two should not be conflated. > >> I don't know. Ripper's output looks like a mess to me when compared to RubyParser: >> >> s(:call, s(:call, nil, :a, s(:arglist)), :[], s(:arglist, s(:lit, 1))) >> >> -Justin >> > That's because RubyParser doesn't provide an abstract syntax tree, it provides a tree interpretation of Ruby programs. While Ripper comes close to a concrete syntax tree at times, at least it sticks to syntax. t discuss my choice of Ripper over RubyParser in my undergraduate thesis [0]; while it is perhaps suitable for an interpreter, its output discards so much of the syntax. It even inserts nodes which have nothing to do with syntax and everything to do with semantics. In this example, if I want my static analyzer to find examples of a.[](1), I can't using RubyParser (at least with the default settings). > > It adds :scope nodes which don't have anything to do with the syntax, it evaluates constant literals, from integers to regular expressions, which is not the job of a general-purpose AST. It even inserts a local assignment node when it parses "rescue Foo => x" (x = $!). > > RubyParser is a good library, and its value as a pure-ruby parser cannot be understated. It's not suitable for many uses because of the shortcuts it takes, the over-interpretation it does (this aref example is a clear one), and the overall focus on semantics and not on just *parsing*. > > [0] PDF: http://www.cs.dartmouth.edu/cms_file/SYS_techReport/532/TR2011-686.pdf I suppose it depends on what you want out of it. For what I use it for, I don't care about the exact syntax used when they mean the same thing, and the fewer node types I need to check for, the better. And I appreciate that RubyParser takes care of much of the semantics for me. In your case, you may have needed different information. So, different strokes for different folks. -Justin