Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Added support for `\x`, `\u` and `\U` escape codes

## v2.1.0 - 2026-04-30

- Added parsing of TOML documents to `Dynamic`.
Expand Down
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,3 @@ pub fn main() {
```

Further documentation can be found at <https://hexdocs.pm/tom>.

## Status

The following string escape sequences are not supported yet:

- `\xHH`
- `\uHHHH`
- `\UHHHHHHHH`
47 changes: 46 additions & 1 deletion src/tom.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,10 @@ fn merge(
}
}

fn expect_end_of_line(input: Tokens, next: fn(Tokens) -> Parsed(a)) -> Parsed(a) {
fn expect_end_of_line(
input: Tokens,
next: fn(Tokens) -> Parsed(a),
) -> Parsed(a) {
case input {
["\n", ..input] -> next(input)
["\r\n", ..input] -> next(input)
Expand Down Expand Up @@ -1113,6 +1116,21 @@ fn parse_string(input: Tokens, string: String) -> Parsed(Toml) {
["\\", "f", ..input] -> parse_string(input, string <> "\f")
["\\", "\"", ..input] -> parse_string(input, string <> "\"")
["\\", "\\", ..input] -> parse_string(input, string <> "\\")
["\\", "x", ..input] -> {
let #(hex_codepoints, input) = list.split(input, 2)
use str <- result.try(hex_codepoint_to_string(hex_codepoints))
parse_string(input, string <> str)
}
["\\", "u", ..input] -> {
let #(hex_codepoints, input) = list.split(input, 4)
use str <- result.try(hex_codepoint_to_string(hex_codepoints))
parse_string(input, string <> str)
}
["\\", "U", ..input] -> {
let #(hex_codepoints, input) = list.split(input, 8)
use str <- result.try(hex_codepoint_to_string(hex_codepoints))
parse_string(input, string <> str)
}
[] -> Error(Unexpected("EOF", "\""))
["\n", ..] -> Error(Unexpected("\n", "\""))
["\r\n", ..] -> Error(Unexpected("\r\n", "\""))
Expand All @@ -1135,6 +1153,21 @@ fn parse_multi_line_string(input: Tokens, string: String) -> Parsed(Toml) {
["\\", "r", ..input] -> parse_multi_line_string(input, string <> "\r")
["\\", "\"", ..input] -> parse_multi_line_string(input, string <> "\"")
["\\", "\\", ..input] -> parse_multi_line_string(input, string <> "\\")
["\\", "x", ..input] -> {
let #(hex_codepoints, input) = input |> skip_whitespace |> list.split(2)
use str <- result.try(hex_codepoint_to_string(hex_codepoints))
parse_multi_line_string(input, string <> str)
}
["\\", "u", ..input] -> {
let #(hex_codepoints, input) = input |> skip_whitespace |> list.split(4)
use str <- result.try(hex_codepoint_to_string(hex_codepoints))
parse_multi_line_string(input, string <> str)
}
["\\", "U", ..input] -> {
let #(hex_codepoints, input) = input |> skip_whitespace |> list.split(8)
use str <- result.try(hex_codepoint_to_string(hex_codepoints))
parse_multi_line_string(input, string <> str)
}
[] -> Error(Unexpected("EOF", "\""))
[g, ..input] -> parse_multi_line_string(input, string <> g)
}
Expand Down Expand Up @@ -1166,6 +1199,18 @@ fn parse_literal_string(input: Tokens, string: String) -> Parsed(Toml) {
}
}

fn hex_codepoint_to_string(input: Tokens) -> Result(String, ParseError) {
let hex_codepoints = string.concat(input)

use codepoint <- result.map(
int.base_parse(hex_codepoints, 16)
|> result.try(string.utf_codepoint)
|> result.replace_error(Unexpected(hex_codepoints, "0 to F")),
)

string.from_utf_codepoints([codepoint])
}

fn reverse_arrays_of_tables(toml: Toml) -> Toml {
case toml {
ArrayOfTables(tables) ->
Expand Down
15 changes: 15 additions & 0 deletions test/tom_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ pub fn parse_string_linefeed_test() {
assert tom.parse("hello = \"\\r\"\n") == Ok(expected)
}

pub fn parse_string_unicode_x_escape_test() {
let expected = dict.from_list([#("hello", tom.String("\u{00E9}"))])
assert tom.parse("hello = \"\\xE9\"\n") == Ok(expected)
}

pub fn parse_string_unicode_escape_test() {
let expected = dict.from_list([#("hello", tom.String("\u{1111}"))])
assert tom.parse("hello = \"\\u1111\"\n") == Ok(expected)
}

pub fn parse_string_unicode_wide_escape_test() {
let expected = dict.from_list([#("hello", tom.String("🌟"))])
assert tom.parse("hello = \"\\U0001F31F\"\n") == Ok(expected)
}

pub fn parse_escaped_slash_test() {
let expected = dict.from_list([#("hello", tom.String("\\"))])
assert tom.parse("hello = \"\\\\\"\n") == Ok(expected)
Expand Down