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)

stratego-to-abox =
; if <is-double-quoted> i then
!|[ ~lit:i ]|
!|[ H hs=0 [ “"” ~lit:i “"”] ]|

which can cause problems when embedding object syntax as this trivial example shows:
  SayHi(e) -> |[ alert("Hi " + ~e)  ]|
the object language grammar keeps quotes with the string
  String("\"Hi \"")
meta explode creates
    "String", ...  Str("\"Hi \"") ...
which is all fine until this is pretty printed and the embedded double quotes are lost
  String("Hi ")
the object language pretty printer 'knows' that strings come with their own double quotes so we get
  alert(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?


Submitted by Bob Davison on 23 January 2013 at 15:00

On 24 January 2013 at 09:42 Gabriël Konat tagged pretty-printing

On 24 January 2013 at 17:16 Gabriël Konat tagged stratego

On 28 January 2013 at 14:40 André Vieira commented:

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.
-> {ast(“String(<2>)”)}

lexical syntax
“"” -> QMLex
StringChar* -> StringLex
~["\n] -> StringChar
“\"” -> StringChar

Log in to post comments