Problems with pretty printing Stratego strings
The Stratego pretty printer chooses not to add double quotes around strings that are already double quoted
(from strc-core/lib/stratego/strc/pp/stratego2abox.str)
which can cause problems when embedding object syntax as this trivial example shows:
stratego-to-abox =
?Str(i)
; if <is-double-quoted> i then
!|[ ~lit:i ]|
else
!|[ H hs=0 [ “"” ~lit:i “"”] ]|
endSayHi(e) -> |[ alert("Hi " + ~e) ]|the object language grammar keeps quotes with the stringString("\"Hi \"")meta explode creates"String", ... Str("\"Hi \"") ...which is all fine until this is pretty printed and the embedded double quotes are lostString("Hi ")the object language pretty printer 'knows' that strings come with their own double quotes so we getalert(Hi + ...)which is invalid syntax.(Although this example requires a grammar that keeps double quotes around strings a similar problem would occur with grammars that do not keep the quotes around strings when a string is surrounded by embedded quotes)
Can this part of the Stratego pretty printer be changed or does anything depend upon this behaviour?
If this behaviour has to stay should MetaExplode protect double quoted strings by wrapping them in additional quotes?
Thanks,
Submitted by Bob Davison on 23 January 2013 at 15:00
Bob.
Issue Log
This seems to be similar to an issue we had in WebDSL. There is a difference between the native Stratego string and SDF defined Strings. Commonly we parse Strings parsing the quotation marks as lexicals, where in reality they are mere delimiters of a string and should not be parsed into the value inside a constructor. This leads to a Str(i) to have two different possible interpretations, like we see in the strategy stratego-to-abox. Which makes it quite easy to mix up both interpretations within the compiler, mixers and pp-tables and yield failures like the one you report here.
For WebDSL we fixed it by changing the language’s string definition to parse Strings such that the quotation marks are not included in the ATerm String(..), this is the SDF that makes it possible (note that the kernel syntax and
ast
keyword were required to ensure parsing of spaces before and after a string.
syntax
-> {ast(“String(<2>)”)}lexical syntax
“"” -> QMLex
StringChar* -> StringLex
~["\n] -> StringChar
“\"” -> StringChar
Log in to post comments