What's the best practice for dealing with complex literals in Rascal?
Two examples from JavaScript (my DSL has similar cases):
- Strings with
\
escapes - have to be unescaped into actual value. - Regular expression literals - need their own sub-AST.
implode
refuses to map lexicals to abstract trees, they are obviously handed differently from syntax productions, despite having complete parse trees available. For example, the following parser fails with IllegalArgument("Missing lexical constructor")
:
module lexicals
import Prelude;
lexical Char = "\\" ![] | ![\\]; // potentially escaped character
lexical String = "\"" Char* "\""; // if I make this "syntax", implode works as expected
start syntax Expr = string: String;
data EXPR = string(list[str] chars);
void main(list[str] args) {
str text = "\"Hello\\nworld\"";
print(implode(#EXPR, parse(#Expr, text)));
}
The only idea I have so far is to capture all lexicals as raw strings and later re-parse them (implode and all) using separately defined syntaxes without layout whitespace. Hopefully, there's a better way.