Reia PEG Issue 6 : Interpolation

1 view
Skip to first unread message

graeme defty

unread,
Aug 25, 2010, 3:06:32 AM8/25/10
to Reia
This one is more of a question . . .

In Ruby a distinction is drawn between the two types of string
depending on the delimiter used:

- apostrophe : limited escaping is honoured, no embedded expressions

- quotes : more extensive set of escapes and embedded expressions are
allowed

In Reia, it seems, the two are not distinguished (if I have read it
right ;-) ).

Is this deliberate?

I suspect that embedded expressions is one of those areas where a PEG
makes it much easier than lex/yacc syntax.

g
PS
When I use ire to parse() a string such as "ab{1+2}c" the result is
just a string with characters "ab{}c", which also seems a bit odd.

Tony Arcieri

unread,
Aug 25, 2010, 4:05:03 PM8/25/10
to re...@googlegroups.com
On Wed, Aug 25, 2010 at 1:06 AM, graeme defty <graeme...@gmail.com> wrote:
This one is more of a question . . .

In Ruby a distinction is drawn between the two types of string
depending on the delimiter used:

- apostrophe : limited escaping is honoured, no embedded expressions

- quotes : more extensive set of escapes and embedded expressions are
allowed

In Reia, it seems, the two are not distinguished (if I have read it
right ;-) ).

Is this deliberate?

Correct, and yes, it's deliberate. I really don't see the need for this distinction, and people often avoid single quoting out of fear that if they ever want to interpolate they'll have to switch the quotes. The horror! I personally like single quoted strings and use them all the time, but I find it annoying you can't interpolate within them.
 
I suspect that embedded expressions is one of those areas where a PEG
makes it much easier than lex/yacc syntax.

Oh hell yes. This is quite a hack right now, and interpolation doesn't work as well as it does in languages like Ruby. For one thing, you may have noticed you can't nest strings within interpolated strings, e.g.:

  "foobar#{func("baz")}"

...works fine in Ruby, but won't parse in Reia.

 
When I use ire to parse() a string such as "ab{1+2}c" the result is
just a string with characters "ab{}c", which also seems a bit odd.

How very strange. Clearly a bug.
 
--
Tony Arcieri
Medioh! A Kudelski Brand

graeme defty

unread,
Aug 25, 2010, 9:13:16 PM8/25/10
to Reia
Ha ha! Talk about "your mileage may vary". I think this distinction is
one of the neat things in Ruby and use it all teh time! lol

(actually, on reflection I suspect that I have very few fixed strings,
and those I have would in fact be candidates for use of atoms)

Anyway, I will see what can be done with a PEG. The question is what
you would like the parser to return. A string is now in reality a list
of expressions to which the to_s method is applied before they are all
concatenated to give the final string.

How about a change to the string record to replace the 'characters'
field with a field called 'values' which will contain a list of
expressions?

g
__________________________________
On Aug 26, 3:05 am, Tony Arcieri <tony.arci...@medioh.com> wrote:

graeme defty

unread,
Aug 29, 2010, 5:52:51 AM8/29/10
to Reia
Tony,

I made a start on the basis suggested (i.e. change #string) and
quickly realised that I need TWO records for string. The first one is
just a simple sequence of characters but I ALSO need another
representing an interpolated string. I was destroying the first to
create the second. SO... I went to create #istring
(interpolated_string) and noticed you already have #dstring. Looking
at the interpolation code, it seems that #dstring is exactly what my
#istring was going to be. I didn't bottom out the analysis of the code
(as you said, a little tangly) but perhaps you can confirm that this
is the case? If so, I will use #dstring, of course.

Cheers,

Graeme
BTW, using a PEG seems to make parsing these constructs almost
trivial, as we had hoped it would. My only issue is how the parse tree
should look.

g
______________________________

Tony Arcieri

unread,
Aug 29, 2010, 12:26:02 PM8/29/10
to re...@googlegroups.com
Hi, yes, I'd been meaning to ask you about that since there were two nodes depending on whether the string contains interpolation or not, string and dstring. But yes, that is the case... the latter is for interpolated strings, and the former for string literals with no interpolation.

--
You received this message because you are subscribed to the Google Groups "Reia" group.
To post to this group, send email to re...@googlegroups.com.
To unsubscribe from this group, send email to reia+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/reia?hl=en.

Graeme Defty

unread,
Aug 29, 2010, 9:42:41 PM8/29/10
to re...@googlegroups.com
Great - Thanks

Tony Arcieri

unread,
Aug 30, 2010, 11:31:16 PM8/30/10
to re...@googlegroups.com
On Wed, Aug 25, 2010 at 7:13 PM, graeme defty <graeme...@gmail.com> wrote:
Ha ha! Talk about "your mileage may vary". I think this distinction is
one of the neat things in Ruby and use it all teh time! lol

Here's the use case for interpolating on both single or double quotes.  Say you want to print out this string:

  The bird says "Tweet tweet"

Let's say we have variables: 

animal = "bird"
noise = "Tweet tweet"

In Ruby, how do we interpolate this?

  "The #{animal} says \"#{noise}\""

Ugly escaping! But what if single quotes interpolated?

 'The #{animal} says "#{noise}"'

No ugly escaping needed!

I don't see what the use case is for the opposite approach. Personally I've found the limitations of single quoted strings kind of irritating, even though I use them because I like their aesthetics, especially for short strings.

Graeme Defty

unread,
Aug 30, 2010, 11:38:41 PM8/30/10
to re...@googlegroups.com
Convinced!

When I think back to my early days with Ruby, interpolation in strings was WAY cooler than the distinction between string types.

Cheers,

g
__________________________________________

--
Reply all
Reply to author
Forward
0 new messages