Add backtick string literals and formatter support
This commit is contained in:
1 parent
e925195495
commit
fada848907
15 files changed
+273
-32
No files matched your search
@@ -15,15 +15,16 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
|
||||
|
||||
## 2. Lexical Syntax
|
||||
- Comments: `// line`, `/* block */`.
|
||||
- Strings: `"..."` (supports escapes). Multiline string content is normalized by indentation logic.
|
||||
- Supported escapes: `\n`, `\r`, `\t`, `\"`, `\\`, `\uXXXX` (4 hex digits).
|
||||
- Strings: `"..."` or `` `...` `` (supports escapes). Multiline string content is normalized by indentation logic.
|
||||
- Shared escapes: `\n`, `\r`, `\t`, `\\`, `\uXXXX` (4 hex digits).
|
||||
- Delimiter escapes: `\"` inside `"..."`, ``\` `` inside `` `...` ``.
|
||||
- Unicode escapes use exactly 4 hex digits (for example: `"\u0416"` -> `Ж`).
|
||||
- Unknown `\x` escapes in strings are preserved literally as two characters (`\` and `x`).
|
||||
- String interpolation is supported:
|
||||
- identifier form: `"$name"`
|
||||
- expression form: `"${expr}"`
|
||||
- escaped dollar: `"\$"` and `"$$"` both produce literal `$`.
|
||||
- `\\$x` means backslash + interpolated `x`.
|
||||
- identifier form: `"$name"` or `` `$name` ``
|
||||
- expression form: `"${expr}"` or `` `${expr}` ``
|
||||
- escaped dollar: `"\$"`, `"$$"`, `` `\$` ``, and `` `$$` `` all produce literal `$`.
|
||||
- `\\$x` means backslash + interpolated `x` in either delimiter form.
|
||||
- Per-file opt-out is supported via leading comment directive:
|
||||
- `// feature: interpolation: off`
|
||||
- with this directive, `$...` stays literal text.
|
||||
|
||||
+18
-1
@@ -1654,15 +1654,27 @@ The type for the character objects is `Char`.
|
||||
|
||||
### String literal escapes
|
||||
|
||||
Lyng string literals can use either double quotes or backticks:
|
||||
|
||||
val a = "hello"
|
||||
val b = `hello`
|
||||
assert(a == b)
|
||||
|
||||
| escape | ASCII value |
|
||||
|--------|-----------------------|
|
||||
| \n | 0x10, newline |
|
||||
| \r | 0x13, carriage return |
|
||||
| \t | 0x07, tabulation |
|
||||
| \\ | \ slash character |
|
||||
| \" | " double quote |
|
||||
| \uXXXX | unicode code point |
|
||||
|
||||
Delimiter-specific escapes:
|
||||
|
||||
| form | escape | value |
|
||||
|--------|--------|------------------|
|
||||
| `"..."` | \" | " double quote |
|
||||
| `` `...` `` | \` | ` backtick |
|
||||
|
||||
Unicode escape form is exactly 4 hex digits, e.g. `"\u263A"` -> `☺`.
|
||||
|
||||
Other `\c` combinations, where c is any char except mentioned above, are left intact, e.g.:
|
||||
@@ -1695,10 +1707,15 @@ Example:
|
||||
|
||||
val name = "Lyng"
|
||||
assertEquals("hello, Lyng!", "hello, $name!")
|
||||
assertEquals("hello, Lyng!", `hello, $name!`)
|
||||
assertEquals("sum=3", "sum=${1+2}")
|
||||
assertEquals("sum=3", `sum=${1+2}`)
|
||||
assertEquals("\$name", "\$name")
|
||||
assertEquals("\$name", "$$name")
|
||||
assertEquals("\$name", `\$name`)
|
||||
assertEquals("\$name", `$$name`)
|
||||
assertEquals("\\Lyng", "\\$name")
|
||||
assertEquals("\\Lyng", `\\$name`)
|
||||
>>> void
|
||||
|
||||
Interpolation and `printf`-style formatting can be combined when needed:
|
||||
|
||||
Reference in new issue
Block a user