Hello,
please do not ask why I want to do that, but I’m interested in generating some Elm code from another language. I need to use literal strings in the produced code, and unfortunately I couldn’t find a precise specification of what an Elm string is supposed to look like.
I was thinking of using json strings everywhere a litteral string is needed. Would that produce valid Elm code?
Thanks for your help.
I think you should be fine
1 Like
lydell
October 9, 2022, 6:07pm
3
Are you wondering if a valid JSON string literal is also a valid Elm string literal, syntax wise?
If so: They are not.
For example, "\u1234"
is a valid JSON string literal, but a syntax error in Elm (the equivalent syntax is "\u{1234}"
).
You can find the syntax definition for a JSON string literal here: JSON
For Elm, there is no specification, only the source code of the compiler: compiler/String.hs at 047d5026fe6547c842db65f7196fed3f0b4743ee · elm/compiler · GitHub
If your program that generates Elm code is written in JavaScript, you could switch from JSON.stringify(myString)
to makeElmString(myString)
defined here: node-test-runner/Generate.js at 026581eb144a32defd6dae3c3de5c46e5e987527 · rtfeldman/node-test-runner · GitHub (it should be at least somewhat complete).
Reading through the source code I linked to, I believe the rules are:
A starting double quote "
.
Zero or more of:
Any character but "
, \n
and \
. (Possibly other newline characters?)
Or a valid escape
An ending double quote "
.
A valid escape is:
A backslash followed by one of n
, r
, t
, "
, '
, \
Or a backslash followed by:
u
{
4-6 hex characters (0-9
, a-z
, A-Z
), which when treated as a number is at most 0x10FFFF
}
In summary, I think the differences to JSON are:
Escape syntax / valid escapes
Possibly newline characters
Possibly handling of characters 0x00-0xFF
7 Likes
system
Closed
October 19, 2022, 6:07pm
4
This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.