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