[grammarians] [Rubygrammar-grammarians] embedded expressions and dynamic quotes

Mauricio Fernández mfp at acm.org
Sun Nov 27 18:07:52 EST 2005


On Sun, Nov 27, 2005 at 11:00:34AM -0800, Terence Parr wrote:
> 
> On Nov 27, 2005, at 1:23 AM, Mauricio Fernández wrote:
> >> Holy crap!  That is VERY tough for a static lexer to deal with.  I
> >> wonder if a semantic predicate will help us out here...hmm...
> >
> > What about this then?
> >
> > puts "foo #{%	#{<<E1}	.chomp + " bar #{<<E2.chomp}"} baz"
> > 1 2 3
> > 4 5 6
> > E1
> > blergh
> > E2
> >
> > Output:
> >
> > foo 1 2 3
> > 4 5 6 bar blergh baz
> 
> Yikes!  Can you parse that for me?

The AST would be a NODE_EVSTR, NODE_HEREDOC, etc. mess. It's too late to
expand it manually and anyway ParseTree knows better.

Here are some hints instead:

puts "foo #{%	#{<<E1}	.chomp + " bar #{<<E2.chomp}"} baz"
          ^   ^ ^   ^  ^         ^     ^  ^
interpolation | |   |  |         |     |  |
              | |   |  |         |     |  | 
\t used as string delimiter      |     |  |
                |   |            |     |  |
    nested interpolation         |     |  |
		    |            |     |  |
             1st heredoc         |     |  |
	                nested string inside interpolation
				       |  |
		            nested interpolation
                                          |
                                      second heredoc

Some simpler examples:

puts <<E, 1
foo
E

Output:
foo
1

puts <<E1, <<E2
1
E1
2
E2

Output:
1
2


--
Mauricio Fernandez


More information about the Rubygrammar-grammarians mailing list