[Rubygrammar-grammarians] Ruby Grammar Project
Martin Traverso
mtraverso at gmail.com
Sat Nov 26 17:04:37 EST 2005
Here's another tricky one: parsing double quoted strings, especially when
they use the %Q syntax.
First, strings can contain nested ruby expressions (full programs,
actually):
"1 + 2 = #{1 + 2}" -> 1 + 2 = 3
Double-quoted strings can also be constructed with % or %Q and an delimiter
character, according to these rules (from the Pickaxe book).
"Following the type character is a delimiter, which can be any nonalphabetic
or nonmultibyte
character. If the delimiter is one of the characters (, [, {, or <, the
literal
consists of the characters up to the matching closing delimiter, taking
account of nested
delimiter pairs. For all other delimiters, the literal comprises the
characters up to the
next occurrence of the delimiter character."
Here are a few of examples of valid strings:
%Q/1 + 2 = #{ 1 + 2 }/ # -> 1 + 2 = 3
%/1 + 2 = #{ 1 + 2 }/
%{1 + 2 = #{ 1 + 2 }}
%(1 + #{ %(2 = #{ 1 + 2 })})
%((1 + 2) * 3 = 9) # -> (1 + 2) * 3 = 9
Martin
On 11/26/05, Terence Parr <parrt at cs.usfca.edu> wrote:
>
>
> On Nov 25, 2005, at 4:44 PM, MenTaLguY wrote:
>
> Howdy....thanks for the examples...
>
> > Hmm, I'm hoping some of the grammarian-newcomers can help out with
> > that,
> > but to start off here are some bits with method calls:
> >
> > First:
> >
> > foo bar baz
>
> Which object is foo a method of? Is that the "main" thing I keep
> reading about? foo returns an object that bar is a method of?
>
> > (where foo and bar are method names) parses the same as:
> >
> > foo( bar( baz ) )
>
> Ah. So it calls bar(baz) first and then uses result as an arg to
> foo? That is very different from how'd I read it w/o the parens.
> Holy smokes.
>
> > Note that omitting the parenthesis for more than one function in the
> > "stack" is deprecated, however.
>
> Thank gawd.
>
> > Next:
> >
> > def foo
> > zort = 3 # assign to variable
> > blah = zort # assign to variable from variable
> > ...
> > end
> >
> > versus:
> >
> > def foo
> > blah = zort # assign to variable from method result
> > zort = 3 # assign to variable
> > ...
> > end
> >
> > I am not sure that 'blah = zort' is parsed differently in these cases,
> > however; that subtree may look the same in the AST and simply get
> > interpreted differently.
>
> Unless you have lazy evaluation, shouldn't the second version result
> in blah being nil?
>
> Regardless, the AST should just be syntax so you should see two
> assignment trees. One of the operands juts happens to be an INT.
> I'd generate
>
> (ASSIGN ID ID)
>
> and
>
> (ASSIGN ID INT)
>
> or some such.
>
> > Next:
> >
> > foo 1, 2, 3 # ok
>
> ack.
>
> > foo(1, 2, 3) # ok
> > bar(1, 2, 3) { ... } # ok
> > bar 1, 2, 3 { ... } # parse error
> > bar 1, 2, baz { ... } # parses as bar(1, 2, baz { ... })
>
> Yes, that all seems ok. It's just
>
> methodCall : ID arglist ;
> arglist : "(" exprList ")" | exprList ;
>
> no sweat. Uh, 'cept for that wacky newline issue where you can have
> \n in the middle of an expression. I'll figure that part out later...
>
> Ter
>
> _______________________________________________
> Rubygrammar-grammarians mailing list
> Rubygrammar-grammarians at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rubygrammar-grammarians
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rubygrammar-grammarians/attachments/20051126/29dec60f/attachment-0001.htm
More information about the Rubygrammar-grammarians
mailing list