1. roc-parser: A manual for parsing text and data in Roc§

Parse text and data in Roc. roc-parser gives you small parsers that combine into larger ones, and ready-made parsers for CSV, YAML, XML, JSON, Markdown and HTTP/1.1 built from the same pieces.

Browse the API reference · Download this manual as a PDF.

1.1. Choose a path§

New to roc-parser

Read What roc-parser is, then Getting started, then Your first parser.

Parsing a format roc-parser already supports

Go to its guide: Read CSV data, Read YAML configuration and frontmatter, Read XML documents, Parse HTML, SVG and MathML, Read and write JSON, Markdown or HTTP messages.

Writing your own parser

Read Combinators by task, Parse a format of your own and Report parse errors.

Looking something up

Modules, Conformance, Performance, Compatibility and Glossary.

Not sure whether you need a parser

Read Choosing how to read your input.

Contributing to roc-parser

Start with Contributing to roc-parser.

Curious where the design comes from

Read Where the design comes from.

2. Start here§

2.1. What roc-parser is§

roc-parser turns text into typed Roc values. You give it a string, such as the contents of a CSV file, a YAML configuration, or an HTTP request, and it gives back either a Roc value you can use directly or an error that says what was wrong with the input.

It is written in pure Roc, so it works with any platform, and it comes in two layers:

Web markup has ready-made parsers too: Html, Svg and MathML retain namespaces and original source, with checked literals and typed interpolation plans for downstream renderers.

This manual is for Roc programmers. You do not need to know anything about parsing theory.

2.1.1. The problem it solves§

Most programs start by reading text that someone else wrote. Splitting a line on commas, or matching it with a regular expression, works until the input contains a quoted comma, a missing field, or a value of the wrong kind. Then the code either crashes, silently produces wrong data, or grows a pile of special cases.

A parser describes the shape of valid input once, as a Roc value, and checks every input against it. With roc-parser:

Compared with a regular expression, a combinator parser is longer to write for a one-off match, but it can describe nested structure (a regular expression cannot match balanced brackets), it produces a value rather than a list of captured substrings, and it reads as ordinary Roc code.

2.1.2. What a first success looks like§

This program decodes a CSV file with a header row into typed records and prints them. Getting started walks through it.

import cli.Stdout
import parser.CSV

Planet : { name : Str, moons : U64 }

input =
	\\name,moons
	\\Mercury,0
	\\Earth,1
	\\Mars,2

planets : Str -> Try(List(Planet), [InvalidCsv(CSV.Error)])
planets = |text| CSV.decode_str(text, {})

describe : Str -> Str
describe = |text| {
	match planets(text) {
		Ok(rows) => Str.join_with(rows.map(|p| "${p.name}: ${p.moons.to_str()} ${if (p.moons == 1) "moon" else "moons"}"), "\n")
		Err(InvalidCsv(error)) => match error.problem {
			MissingRequiredField(name) => "no ${name} column"
			_ => "${Str.inspect(error.location)}: ${Str.inspect(error.problem)}"
		}
	}
}

main! = |_args| {
	Stdout.line!(describe(input))
}

Output:

Mercury: 0 moons
Earth: 1 moon
Mars: 2 moons

2.1.3. Supported formats§

Each module follows a published specification. Where a module implements only part of one, its chapter says which part, and input outside that part is rejected with an error rather than misread.

ModuleReadsSpecification
CSVComma-separated rows decoded into your own record typeRFC 4180, with the usual relaxations (any line ending, rows of different lengths)
YamlConfiguration files and Markdown frontmatter, as a tree or your own record typeA documented subset of YAML 1.2: one document, no anchors, aliases or tags
XmlAn XML document as a tree, or an event at a time with limits for untrusted inputXML 1.0 (Fifth Edition) well-formedness, without document type declarations
Xml.NamespacedXML with namespaces resolved, read as a tree or an event at a time, and written backNamespaces in XML 1.0 (Third Edition)
JsonValueA JSON document of any shape as a lossless tree, and JSON written back outRFC 8259, with opt-in relaxations (comments, trailing commas, a byte order mark, NaN and Infinity) and opt-in checks (size limits, the top-level shape, I-JSON numbers)
MarkdownA Markdown document as a syntax treeCommonMark 0.31.2 with GitHub Flavored Markdown tables, task lists and strikethrough
HTTPOne HTTP/1.x request or responseRFC 9112 message syntax and RFC 9110 field syntax
ParserAnything you describe with combinatorsNot applicable

The format chapters describe each module’s exact behaviour. The API reference lists every public function.

2.1.4. How the pieces fit together§

A combinator is a function that takes parsers and returns a new parser. The Parser.Utf8 namespace provides parsers for the smallest pieces of text, such as one byte, a digit, or an exact word. The Parser module combines them: one after another, one of several alternatives, repeated, or with the result transformed. Each format module also exposes its parser as a Parser value, so your own parsers and the library’s can be mixed.

Small parsers combine into larger ones
Figure 1. Small parsers combine into larger ones

Your first parser teaches the combinators by building a small parser one step at a time.

2.1.5. When to choose something else§

roc-parser provides typed parsing with clear errors. CSV, QIF and OFX also support incremental input and output; see Read and write documents in chunks. Consider something else when:

2.1.6. Stability§

roc-parser tracks recent nightly builds of the Roc compiler. Because Roc is still changing, the package’s API may change between releases. Each release names the compiler build it was tested with, and the release notes describe any changes you need to make when upgrading.

2.2. Getting started§

In this chapter you add roc-parser to a Roc app and use it to read three lines of CSV into typed records. At the end the app prints one line per record. You need a terminal, an internet connection, and a little Roc: how to write a function, a record, and a match.

2.2.1. Before you start§

You need:

Check the compiler:

roc version

It prints the name of the nightly build, which should match the one in the release notes:

Roc compiler version nightly-<date>-<commit>

2.2.2. Add the package§

A Roc app names each package it uses by URL in its header. The compiler downloads the package the first time it builds the app and checks it against the hash in the URL.

  1. Open the roc-parser releases page and choose the newest release.

  2. Under Assets, find the file ending in .tar.zst and copy its link.

  3. Put that link in your app’s header under a short name. This manual uses parser.

Create a file named main.roc with this header, replacing the parser URL with the one you copied:

app [main!] {
	cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.28.1/7brgwR8YUqfeGrkdfF1V4GPPN7DnP7ftqub9KcrTEZtr.tar.zst",
	parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/<version>/<hash>.tar.zst",
}

The examples in this manual are tested against the package source in the repository, so their headers say parser: "../../package/main.roc" instead. Everything else in them is the same.

For complete programs to start from, download roc-parser-examples-<version>.zip from the same release. Its apps are already pinned to that release; unzip it and follow the README.md inside, which runs them with roc <name>.roc from its examples/ directory.

2.2.3. Read CSV into records§

Add the rest of the program below the header:

import cli.Stdout
import parser.CSV

Planet : { name : Str, moons : U64 }

input =
	\\name,moons
	\\Mercury,0
	\\Earth,1
	\\Mars,2

planets : Str -> Try(List(Planet), [InvalidCsv(CSV.Error)])
planets = |text| CSV.decode_str(text, {})

describe : Str -> Str
describe = |text| {
	match planets(text) {
		Ok(rows) => Str.join_with(rows.map(|p| "${p.name}: ${p.moons.to_str()} ${if (p.moons == 1) "moon" else "moons"}"), "\n")
		Err(InvalidCsv(error)) => match error.problem {
			MissingRequiredField(name) => "no ${name} column"
			_ => "${Str.inspect(error.location)}: ${Str.inspect(error.problem)}"
		}
	}
}

main! = |_args| {
	Stdout.line!(describe(input))
}

Run it:

roc main.roc

Output:

Mercury: 0 moons
Earth: 1 moon
Mars: 2 moons

The first run takes longer while the compiler downloads the platform and the package.

2.2.4. What the program does§

import parser.CSV makes the CSV module from the package named parser available.

Planet is the record type each row becomes. The first line of the input is a header row, and each field of Planet reads the column with the same name.

planets calls CSV.decode_str, which splits the text into rows and decodes each row into a record. CSV.decode_str does not take the row type as an argument: the compiler infers it from the type annotation on planets, and finds the decoding code through the static dispatch method parser_for, which every record type has. Change the annotation and the same call decodes a different record.

The result is Ok with a list of Planet records, or Err(InvalidCsv(error)). The diagnostic’s problem distinguishes syntax, cell conversion, and missing-column failures. Its location identifies the source position when one exists, and path names the logical record, field, or missing key. A missing column has the MissingRequiredField(name) problem.

describe matches the problem tags to present a missing column separately. Nothing is printed until every row has been read successfully.

To see the error path, change Mars,2 to Mars,two and run the program again. It reports line 4 and an ExpectedValue problem containing the expected U64 type and a bounded excerpt of the original cell bytes. Rename the moons column and it prints no moons column. Report parse errors shows how to report every kind of failure, and Read CSV data shows optional columns, empty cells and hand-built row parsers that match columns by position.

2.2.5. Next steps§

2.3. Choosing how to read your input§

Roc gives you four ways to turn text into values, and roc-parser provides two of them. This chapter helps you pick one before you write any code. In short:

  1. Decode into your own type when the input is a standard format and you already know the shape you want. The record type is the specification, and there is almost no code to write.

  2. Read a document tree with one of roc-parser’s format modules when you do not know the shape in advance, or you need what a record would throw away.

  3. Build a parser with combinators when the format is your own, or you need control over exactly what is accepted and what the errors say.

  4. Use no parser at all when a Str function does the job, or when the input is too large or too hot for any of these.

The examples come from choosing-approaches.roc; every expect in it passes.

2.3.1. Decode into your own type§

Roc has type-directed decoding built in. A format (such as the builtin Json) knows how to read strings, numbers, lists and records. Your type knows which of those it is made of. The compiler combines the two, so the annotation alone decides what is accepted:

# (a) Type-directed decoding: the record type is the whole specification.
Service : { name : Str, port : U64 }

decode_service : Str -> Try(Service, [InvalidJson(Str)])
decode_service = |json| Json.parse(json)

expect decode_service("{\"name\": \"web\", \"port\": 8080}") == Ok({ name: "web", port: 8080 })
expect decode_service("{\"name\": \"web\"}") == Err(InvalidJson("Missing field: port"))

The parsers chapter of the language reference describes the mechanism. Every type has a parser_for method, which the compiler derives for records, lists and tuples, and a format is a type module whose methods read one value at a time. Static dispatch joins the two. A field of type Try(_, [Missing]) is optional, a missing required field is reported by name, and when the parser is a top-level constant it is assembled at compile time.

Choose this when:

The builtin Json is such a format, for JSON whose shape you know in advance. For dynamic JSON, whose keys are not Roc identifiers or whose arrays mix shapes, or that must keep member order and every digit of its numbers, read a document tree with roc-parser’s JsonValue instead (see Read and write JSON).

roc-parser’s CSV and Yaml modules are formats for this mechanism. CSV.decode_str reads a file with a header row into a list of records, matching columns to fields by name, and Yaml.decode_str reads a document into a record. Neither takes the type as an argument: the annotation chooses it. A required field with no column or key produces the native MissingRequiredField(name) problem inside InvalidCsv or InvalidYaml, retaining its logical path:

# roc-parser's CSV and Yaml modules are formats for the same mechanism.
Planet : { name : Str, moons : U64, rings : Try(Bool, [Missing]) }

planets : Str -> Try(List(Planet), [InvalidCsv(CSV.Error)])
planets = |text| CSV.decode_str(text, {})

expect planets("name,moons\nMars,2\n") == Ok([{ name: "Mars", moons: 2, rings: Err(Missing) }])
expect planets("name\nMars\n").map_err(|InvalidCsv(error)| error.problem) == Err(MissingRequiredField("moons"))

service : Str -> Try(Service, [InvalidYaml(Yaml.Error)])
service = |text| Yaml.decode_str(text, {})

expect service("name: web\nport: 8080\n") == Ok({ name: "web", port: 8080 })
expect service("name: web\n").map_err(|InvalidYaml(error)| error.problem) == Err(MissingRequiredField("port"))

Its limits come from the same design. The format decides the representation, so you cannot ask it to accept yes as a boolean or to read a date in your own layout. Diagnostics retain a logical path and the physical location when known; missing values use an explicit unavailable location. The result type must be known when you compile.

2.3.2. Read a document tree§

roc-parser’s Yaml, Xml, JsonValue and Markdown modules read a whole document into a tree that mirrors the format, and HTTP reads a message into a record of its parts. You then walk the tree with pattern matching, or with the tree methods such as get_path and as_i64 on a Yaml or JsonValue value and attribute and children_named on an Xml.Node:

# (b) A ready-made format parser: read the whole tree, then decide what to
# do with each key, whatever keys the file happens to contain.
top_level_keys : Str -> Try(List(Str), [NotAMapping, InvalidYaml(Yaml.Error)])
top_level_keys = |text| {
	match Yaml.parse_str(text, {})? {
		Mapping(entries) => Ok(entries.map(|entry| entry.key))
		_ => Err(NotAMapping)
	}
}

expect top_level_keys("name: web\nport: 8080\nextra: [1, 2]") == Ok(["name", "port", "extra"])

# The tree methods follow a path without a match per level.
port_of : Str -> Try(I64, [Missing, WrongType, InvalidYaml(Yaml.Error)])
port_of = |text| Yaml.parse_str(text, {})?.get_path(["server", "port"])?.as_i64()

expect port_of("server:\n  port: 8080\n") == Ok(8080)
expect port_of("server: {}\n") == Err(Missing)

Choose this when:

The cost is code: you write the walk, and a missing or mistyped key is something you check, not something the type system checks for you. The format chapters (Read YAML configuration and frontmatter, Read XML documents, Read and write JSON, Markdown and HTTP messages) show helper functions that keep the walk short.

2.3.3. Build a parser with combinators§

When no format module reads your input, write a parser from the Parser and Parser.Utf8 combinators. A parser is a value made of smaller parsers, so the code has the shape of the grammar:

# (c) A parser of your own: a legacy "host:port" line, where the port must
# be digits and the grammar is yours to define.
Endpoint : { host : Str, port : U64 }

endpoint : Parser(Parser.Utf8.Bytes, Endpoint, _)
endpoint =
	Parser.const(|host| |port| { host, port })
		.keep(Parser.chomp_while(|b| b != ':').map(Str.from_utf8_lossy))
		.skip(Parser.Utf8.codeunit(':'))
		.keep(Parser.Utf8.digits({}))

expect (endpoint).parse_str("example.com:443") == Ok({ host: "example.com", port: 443 })
expect (endpoint).parse_str("example.com:https").is_err()

Choose this when:

Combinators are also how you extend a format module. CSV.record and CSV.field are combinators, format modules provide parser(props) constructors to embed, and you can mix Parser.Utf8 parsers into any parser of your own. A combinator parser’s diagnostic preserves the furthest failure; text runners retain byte offset, line and column. Your first parser teaches the method and Parse a format of your own applies it to a real format.

2.3.4. Process incremental input§

When input is too large to retain or arrives over a connection, construct a format decoder and call .read_iter(chunks) or .read_stream(source). CSV, QIF, OFX and JSON records produce values; XML and HTTP produce events. Consume the final Finished event to validate the end of input. Use .collect only when retaining all output is appropriate. See Read and write documents in chunks for recovery and resource limits.

2.3.5. Use no parser at all§

Parser combinators are not always the right tool, and roc-parser’s own format modules show where the line falls.

The input has no structure worth a grammar

One separator, no quoting and no nesting is a job for Str.split_on, Str.trim and friends. A parser would be longer and no clearer:

# (d) No parser at all: one separator and no nesting is a job for `Str`.
fields : Str -> List(Str)
fields = |line| line.split_on("\t")

expect fields("a\tb\tc") == ["a", "b", "c"]
The parser is on a hot path

Each combinator adds a function call and an intermediate Try. That is cheap for configuration files and messages, but Add a format module records that the CSV, XML, HTTP and Markdown modules moved their document-level parsing to byte scanners with an explicit stack, to stay linear and avoid deep recursion; the JSON module was written that way from the start. Do the same when profiling shows the parser matters. Performance compares the built-in parsers with Go, Rust and Python libraries.

The grammar is ambiguous or left-recursive

Combinators take the first alternative that succeeds and cannot handle a rule that starts with itself, such as expr = expr "-" term. A large grammar written for a parser generator, such as an existing yacc or ANTLR grammar, is often better served by a generator, which checks the grammar for conflicts before any input is read. Where the design comes from explains these limits.

2.3.6. Decision table§

You needDecode into a typeDocument treeCombinatorsNo parser
A standard format (CSV, YAML) into known records, with little codeBestPossiblePossibleNo
Any keys or structure, discovered at run timeNoBestPossibleNo
Mapping key order or XML mixed content keptNoYesYesNo
Line and column in errorsYes for CSV and YamlYesText location with a byte offsetNo
Your own or a legacy format, a protocol or a small languageNoNoBestIf trivial
Values validated while parsing, with your own problem tagsNoAfterwardsBestBy hand
Read one message and keep the restNoRequest prefix parser retains restYesBy hand
Very large or streaming inputCSV typed decoderFormat event decoderCustom decoderFor trivial delimiters
Ambiguous or left-recursive grammarNoNoNoUse a parser generator

2.3.7. Decision flow§

The same choice as a sequence of questions:

Choosing how to read your input
Figure 2. Choosing how to read your input

The questions are ordered from least code to most. You can also combine answers: read a Markdown file as a tree, read its frontmatter as a YAML tree, and parse one custom field inside it with a combinator.

2.4. Your first parser§

In this tutorial you write a parser for a small settings format, one line per setting:

name="demo"
port=8080
debug=true

By the end you have a parser that turns that text into a list of typed records, and you know how to read its errors. Along the way you meet the combinators that every parser in this package is built from.

You need the setup from Getting started. Each step is a complete program in the repository’s docs/examples directory; the step’s heading names the file. Run a step with roc <file> and its tests with roc test <file>.

2.4.1. Step 1: read a key§

combinator-primer-step1.roc

A key is one or more lowercase letters or underscores. Start with a parser for one such byte, repeat it, and turn the bytes into a Str:

is_key_byte : U8 -> Bool
is_key_byte = |b| (b >= 'a' and b <= 'z') or b == '_'

key : Parser(Parser.Utf8.Bytes, Str, _)
key =
	Parser.Utf8.codeunit_satisfies(is_key_byte)
		.one_or_more()
		.map(Str.from_utf8_lossy)

Three ideas appear here:

parser.parse_str runs a parser on a whole string. These tests pass:

expect (key).parse_str("name") == Ok("name")
expect (key).parse_str("Name").is_err()

The second fails because N is not a lowercase letter, so not even one key byte can be read.

2.4.2. Step 2: read a whole setting§

combinator-primer-step2.roc

A setting is a key, an =, and a value. For now, the value is everything up to the end of the line:

value : Parser(Parser.Utf8.Bytes, Str, _)
value =
	Parser.chomp_while(|b| b != '\n')
		.map(Str.from_utf8_lossy)

Entry : { key : Str, value : Str }

entry : Parser(Parser.Utf8.Bytes, Entry, _)
entry =
	Parser.const(|k| |v| { key: k, value: v })
		.keep(key)
		.skip(Parser.Utf8.codeunit('='))
		.keep(value)

This is the pattern for reading things in sequence:

  1. Parser.const(f) is a parser that reads nothing and produces f, the function that builds the result. f takes its arguments one at a time (|k| |v| ...), because they arrive one at a time.

  2. .keep(p) runs p next and passes its result to the function.

  3. .skip(p) runs p next and throws its result away. The = must be there, but you do not need it in the record.

Parser.chomp_while(pred) reads bytes while pred accepts them. It always succeeds, even when it reads nothing, which is why "empty=" gives an empty value:

expect (entry).parse_str("name=roc") == Ok({ key: "name", value: "roc" })
expect (entry).parse_str("empty=") == Ok({ key: "empty", value: "" })

Running the step prints:

Ok({ key: "name", value: "roc" })

2.4.3. Step 3: choose between kinds of value§

combinator-primer-step3.roc

A value is a number, true or false, or text in double quotes. Write one parser for each kind, then try them in order with Parser.one_of:

Value : [Number(U64), Flag(Bool), Text(Str)]

number : Parser(Parser.Utf8.Bytes, Value, _)
number = Parser.Utf8.digits({}).map(|n| Number(n))

flag : Parser(Parser.Utf8.Bytes, Value, _)
flag =
	Parser.one_of([
		Parser.const(Flag(Bool.True)).skip(Parser.Utf8.string("true")),
		Parser.const(Flag(Bool.False)).skip(Parser.Utf8.string("false")),
	])

text : Parser(Parser.Utf8.Bytes, Value, _)
text =
	Parser.chomp_while(|b| b != '"')
		.map(|bytes| Text(Str.from_utf8_lossy(bytes)))
		.between(Parser.Utf8.codeunit('"'), Parser.Utf8.codeunit('"'))

value : Parser(Parser.Utf8.Bytes, Value, _)
value = Parser.one_of([number, flag, text])

Parser.one_of tries each parser on the same input and returns the first success. If one fails part-way through, the next one starts again from the beginning, so text does not see input already examined by number or flag.

Parser.Utf8.digits({}) reads a whole number as a U64, and Parser.Utf8.string("true") reads exactly that text. .between(open, close) reads open, the parser, and close, and keeps only the middle result.

expect (value).parse_str("8080") == Ok(Number(8080))
expect (value).parse_str("true") == Ok(Flag(Bool.True))
expect (value).parse_str("\"hello world\"") == Ok(Text("hello world"))
expect (value).parse_str("maybe").is_err()

The entry parser from step 2 now uses this value. Running the step prints:

Ok({ key: "port", value: Number(8080) })

2.4.4. Step 4: read every line, and read the errors§

combinator-primer-step4.roc

A settings file is entries separated by line breaks. sep_by reads that shape and drops the separators:

config : Parser(Parser.Utf8.Bytes, List(Entry), _)
config = entry.sep_by(Parser.Utf8.codeunit('\n'))
expect
	(config).parse_str("port=8080\ndebug=true")
		== Ok([{ key: "port", value: Number(8080) }, { key: "debug", value: Flag(Bool.True) }])

parser.parse_str returns a value or a diagnostic. The following program reports four representative outcomes:

report : Str -> Str
report = |source| {
	match (config).parse_str(source) {
		Ok(entries) => "parsed ${entries.len().to_str()} entries"
		Err(ParseError({ problem, location, .. })) => "failed at ${Str.inspect(location)}: ${Str.inspect(problem)}"
	}
}

report_entry : Str -> Str
report_entry = |source| {
	match (entry).parse_str(source) {
		Ok(_) => "parsed one entry"
		Err(ParseError({ problem, location, .. })) => "failed at ${Str.inspect(location)}: ${Str.inspect(problem)}"
	}
}

main! = |_args| {
	Stdout.line!(report("name=\"demo\"\nport=8080\ndebug=true"))?
	Stdout.line!(report("name=\"demo\"\nport=eighty"))?
	Stdout.line!(report("name=\"demo\"\n"))?
	Stdout.line!(report_entry("port=eighty"))
}

Output:

parsed 3 entries
failed at Text({ column: 6, line: 2, offset: 17 }): ExpectedByte(34)
failed at Text({ column: 1, line: 2, offset: 12 }): UnexpectedEnd
failed at Text({ column: 6, line: 1, offset: 5 }): ExpectedByte(34)

Each line of output shows one outcome:

  1. The whole input matched: Ok.

  2. port=eighty is not a valid entry, so sep_by stopped after the first entry and did not reach the end of the input. parse_str reports the furthest failure instead: none of the value parsers accepted eighty, at byte 17.

  3. The trailing line break is a separator with no entry after it, so the parse fails at the end of the input, where a key was expected. Real files often end with a line break; Parse a format of your own shows one way to accept it.

  4. Parsing a single bad entry directly gives the same failure, at byte 5.

The second line is worth remembering. A repetition such as sep_by or many stops at the first element it cannot read, but that element’s failure is kept, so a mistake in the middle of a file is reported where it is. Report parse errors explains how to get better messages.

2.4.5. What you have learned§

Combinators by task covers the remaining combinators and the rules for alternatives and repetition in more depth.

3. Guides§

3.1. Read and write documents in chunks§

Use the CSV, QIF, OFX and JSON record APIs, or XML and HTTP event APIs, when input is too large to keep in memory. The package parses bytes without performing I/O. Roc’s builtin Iter supplies pure chunks; builtin Stream supplies effectful chunks from a file or stdin. The pinned basic-cli platform provides File.Reader.chunks, Stdin.chunks and File.Writer.write_stream! for these pipelines.

Use decoder.read_iter(chunks) for pure chunks or decoder.read_stream(source) for an effectful source. Choose a format-specific decoder and output unit:

FormatDecoder constructorsValues
CSVCSV.decoder, CSV.typed_decoder, CSV.positional_decoderRows; typed decoding adapters are also available
QIFQIF.decoder, QIF.transaction_decoderSection context and transaction records
OFXOFX.decoderStatement context and transaction records
JSONJsonValue.record_decoderNDJSON records or top-level array items
XMLXml.decoderStart, end and bounded text events
XML with namespacesXml.Namespaced.decoderEvents with resolved names
HTTP/1.1HTTP.Request.decoder, HTTP.Response.decoderHeads, body fragments, trailers and message boundaries

CSV, QIF, OFX and JSON also provide writer constructors (record_encoder for JSON). Pass an encoder to encoder.write_iter(items) or encoder.write_stream(items). Set chunk_bytes in the encoder constructor props (64 KiB by default). See JSON record streams, chunked XML events and chunked HTTP messages for format-specific setup.

3.1.1. Consume a document completely§

Chunk boundaries have no syntax significance. A chunk may end inside a UTF-8 character, CRLF, quoted field, tag or entity. Empty chunks are permitted and do not signal EOF. Only exhaustion of the source finalizes the decoder.

Readers yield Try items. Successful events are Value(value), Rejected(problem) when reporting recoverable errors, and one final Finished(report). Terminal failures yield the native format error once and stop, without an additional parser or collector wrapper. Source failures retain their original value under SourceErr; they never finalize a truncated document as though it had reached EOF.

HTTP’s MessageEnd completes one message in a pipeline; Finished completes the source. XML text fragments may split adjacent character data into several events; concatenate them when an application needs one text value.

Drain the reader through Finished to validate the whole document. Stopping early releases the remaining pipeline when its values are dropped, but leaves the unread suffix unvalidated. Keep only the latest iterator/stream state to allow consumed chunks to be released.

decoder.collect(chunks) requires successful completion and rejects any omitted or reported invalid records. decoder.values_stream(source) extracts values for a fail-fast writer pipeline and requires Finished; rejected or skipped records remain errors. Use decoder.collect_report(chunks) when the application intentionally wants both values and diagnostics. Both collectors retain the collected values; use an ordinary pull loop to process and discard records.

3.1.2. Choose an error policy§

Configure common recovery with the constructor props errors: { policy: ReportInvalidRecords, max_errors: 1000 }. FailFast is the default. SkipInvalidRecords discards records that were completely framed but failed semantic decoding. ReportInvalidRecords emits Rejected and continues. The completion report counts emitted items and rejected records; metadata/context events count as emitted items too.

JSON recovery applies to independently framed NDJSON lines. Errors within a JSON array, XML document or HTTP pipeline are terminal; these readers do not guess where valid input resumes after broken syntax.

These policies never recover from broken framing, invalid encoding, source failures, invalid account/statement context, or resource-limit failures. For example, an unclosed CSV quote cannot safely be skipped at a physical newline because quoted fields can contain newlines.

max_errors defaults to 1,000 and permits exactly that many recoverable rejections. The next rejection is terminal and retains its triggering error. An allowance of zero stops at the first invalid record. RecoveryLimitExceeded retains { max, observed: Exact(count), cause }; DecoderProtocol identifies invalid consumption or completion from a custom decoder. Both use the common diagnostic envelope.

3.1.3. Bound retained memory§

Choose format props for unfinished fields/records, token lengths, nesting and context storage. Memory depends on those bounds and the current source chunk, not just the number of records already processed. Retaining output values intentionally contributes to the application’s memory use.

Writers validate each logical record before emitting its first bytes, then split it into bounded chunks. They can still fail after earlier records have been written. Use an application-managed temporary file and rename after success if the destination must appear atomically.

QIF and OFX support UTF-8/ASCII and Windows-1252. Unsupported input encodings, invalid bytes and unrepresentable output characters produce errors. Writers never replace characters silently. Round trips preserve supported semantics; formatting, quoting and permitted numeric/date spellings may be normalized.

3.1.4. Reuse correctness oracle answers§

The conformance review caches deterministic reference answers in .roc-parser-tmp/oracle-cache, including rejections and independent writer output. Every comparison still runs the current Roc probe. Exact input bytes, oracle dependency versions, options, adapter code and Python version identify the reference answer; changing Roc alone does not invalidate it.

Pass --refresh-oracles to recompute reference answers, or --no-oracle-cache to bypass both cache reads and writes. Corrupt entries are recomputed, and transient execution failures are never stored. Cached answers remain usable without installed oracle packages when their version receipt matches the requested dependency pins.

These comparisons and their caches are local-only. Share the cache directory across local runs to reuse unchanged reference answers; each comparison still builds and executes the current Roc probe.

3.1.5. Implement another chunk decoder§

Parser.Step returns the updated state, the number of bytes consumed from this call, and Item, RecordError, NeedInput, Done or Failed. A decoder must retain unfinished syntax and its original byte positions. It must drain pending output before requesting more input. NeedInput consumes the supplied input, and is invalid after Final; Done requires Final and complete consumption. The shared adapters reject these protocol violations.

Construct a descriptor with Parser.decoder({ initial_state, step, errors: {} }), then call its read_iter or read_stream method. For manual backpressure, call decoder.step({ bytes, end: More }) or use Final at EOF and retain the returned descriptor. decoder.metadata() exposes the state’s declared read-only metadata capability without exposing parser bookkeeping.

Parser.encoder({ initial_state, encode, finish, chunk_bytes: 65536 }) configures a stateful writer. The encode function returns updated state and validated record bytes; finish returns the final suffix. Use encoder.write_iter or encoder.write_stream to bound output chunks. Source state belongs in the evolving iterator state so terminal states can release it.

3.2. Read CSV data§

This chapter shows how to turn comma-separated text into Roc values: as records chosen by the type you ask for, as tuples when the file has no header, as text or raw fields, and with hand-built record parsers when a column needs custom handling. It also shows how to tell the user which row was wrong. It is for Roc programmers who have added parser to their app’s dependencies.

Every example on this page comes from docs/examples/csv-guide.roc, which CI runs, and imports parser.CSV and parser.Parser.

3.2.1. Decode rows into records§

When the first row names the columns, CSV.decode_str decodes every other row into the record type you annotate. Each field reads the column whose header is exactly its name, so the column order does not matter:

Product : { name : Str, quantity : U64, price : Dec }

products : Str -> Try(List(Product), [InvalidCsv(CSV.Error)])
products = |text| CSV.decode_str(text, {})

print_products! : Str => Try({}, _)
print_products! = |text| {
	match products(text) {
		Ok(found) =>
			for p in found {
				Stdout.line!("${p.name}: ${p.quantity.to_str()} at ${p.price.to_str()}")?
			}

		Err(InvalidCsv(error)) => match error.problem {
			MissingRequiredField(name) => Stdout.line!("no column named ${name}")?
			_ => Stdout.line!("${Str.inspect(error.location)}: ${Str.inspect(error.problem)}")?
		}

	}
	Ok({})
}

Running it on a good file, on a file without a quantity column, and on a file whose second data row is short:

print_products!("name,price,quantity\nbolt,0.15,250\nnut,.05,+40\n")?
print_products!("name,price\nbolt,0.15\n")?
print_products!("name,price,quantity\nbolt,0.15,250\nnut,0.05\n")?
Output
bolt: 250 at 0.15
nut: 40 at 0.05
no column named quantity
Text({ column: 1, line: 3, offset: 34 }): WidthMismatch({ expected: 3, found: 2 })

Missing required columns produce InvalidCsv(error) with error.problem == MissingRequiredField(name) and Key(name) in the diagnostic path. Native decoding errors share this envelope. Columns that no field names are ignored, and when two columns share a name the last one wins. Select validation: Checked({}) with header: Exact or Normalized when parsing raw CSV to reject duplicate or empty header names before decoding.

A cell decodes according to its field’s type:

TypeAccepted cell text
StrAny text, kept exactly, including leading and trailing spaces.
Booltrue or false in any letter case.
U8 … U128, I8 … I128ASCII digits with an optional leading `` (or - for signed types) that fit in the type. Spaces, _ and 0x prefixes are rejected.
DecAn optional sign, digits and an optional fraction: 1.50, -2, .5.
F32, F64An optional sign, then inf, infinity or nan in any letter case, or a decimal number with an optional fraction and exponent: 12, 1.5, .5, 5., 1e-3, 2.5E+10. Values too large become infinity and values too small become zero.
Tags without payloadsThe tag name, such as Active.

These match Rust’s from_str for each number type. A field that is a record, a tuple or a tag with a payload fails with an InvalidCsv error when it is decoded; a field that is a list or a dictionary is a compile-time error, because a CSV cell cannot hold one.

3.2.1.1. Optional columns, empty cells and header spelling§

A field of type Try(a, [Missing]) is Err(Missing) when the file has no such column. A field of type Try(a, [Null]) is Err(Null) when its cell is empty. CSV.decode_str(text, { header: Normalized }) matches header names after trimming spaces, lowercasing ASCII letters and turning runs of spaces, - and _ into one _, so First Name, first-name and FIRST_NAME all fill first_name:

Contact : {
	name : Str,
	email : Try(Str, [Missing]),
	age : Try(U64, [Null]),
	status : [Active, Inactive],
}

contacts : Str -> Try(List(Contact), [InvalidCsv(CSV.Error)])
contacts = |text| CSV.decode_str(text, { header: Normalized })

describe_contact : Contact -> Str
describe_contact = |{ name, email, age, status }| {
	email_text =
		match email {
			Ok(address) => address
			Err(Missing) => "no email column"
		}
	age_text =
		match age {
			Ok(years) => years.to_str()
			Err(Null) => "age not given"
		}
	status_text =
		match status {
			Active => "active"
			Inactive => "inactive"
		}
	"${name} (${email_text}, ${age_text}, ${status_text})"
}

For "Name,Age,Status\nAda,36,Active\nAlan,,Inactive\n" this prints:

Output
Ada (no email column, 36, active)
Alan (no email column, age not given, inactive)

3.2.1.2. Row width and blank lines§

Blank lines are skipped. Every other row must have exactly as many fields as the header: a shorter row and a longer row are both an InvalidCsv error naming the row, so that a misplaced comma cannot shift values into the wrong columns. A header with no data rows, and empty input, decode to an empty list.

3.2.2. Files without a header§

CSV.decode_str(text, { header: Absent }) decodes every row into a tuple, one element per column, with the same cell rules. Every row must have as many fields as the tuple has elements:

points : Str -> Try(List((Str, F64, F64)), [InvalidCsv(CSV.Error)])
points = |text| CSV.decode_str(text, { header: Absent })

Output
Ok([("home", -33.87, 151.21), ("work", -33.86, 151.2)])

3.2.3. Read every field as text§

Use CSV.parse_string_records to collect a table as List(List(Str)) when every column should remain text. The first row stays in the result, fields remain untrimmed, and CSV quoting is decoded:

text_rows = CSV.parse_string_records("name,note\nAda,\"said \"\"hello\"\"\"\n", {})?
for row in text_rows {
	Stdout.line!(Str.join_with(row, " | "))?
}
Output
name | note
Ada | said "hello"

It accepts the same dialect and reports the same syntax errors as CSV.parse_str. Empty input gives no rows; a blank line gives one empty field. Use CSV.decode_str when a header should select typed record fields, or CSV.rows_parser({ row: parser }).parse_str(text) when a column needs a custom parser.

3.2.4. Read every field as raw bytes§

Use CSV.parse_str when the columns vary or you want to inspect rows yourself. It returns a CSV document whose .records contains one CSV.Record, a List(List(U8)), per row. CSV.parse_bytes(bytes, {}) accepts arbitrary field bytes without imposing UTF-8:

raw_records : Str -> Str
raw_records = |text| {
	match CSV.parse_str(text, {}).map_ok(|document| document.records) {
		Ok(records) =>
			Str.join_with(records.map(|fields| Str.join_with(fields.map(Str.from_utf8_lossy), " | ")), "\n")

		Err(InvalidCsv(error)) =>
			"not valid CSV at ${Str.inspect(error.location)}: ${Str.inspect(error.problem)}"
	}
}

Given a header row, a quoted field containing "" and ,, and a row whose last field is empty:

Stdout.line!(raw_records("name,note\r\nwidget,\"says \"\"hi\"\", twice\"\nempty,"))?
Output
name | note
widget | says "hi", twice
empty | 

document.split_header() separates the first record as column names:

column_names : Str -> Str
column_names = |text| {
	match CSV.parse_str(text, {}).map_ok(|document| document.records) {
		Ok(records) => {
			{ header, rows } = CSV.{ records }.split_header()
			"${Str.join_with(header, ", ")}; ${rows.len().to_str()} rows"
		}

		Err(_) => "not valid CSV"
	}
}

Output
sku, count; 2 rows

CSV.parser({}) constructs a Parser(Parser.Utf8.Bytes, CSV, [InvalidCsv(CSV.Error)]) for use inside larger parsers. It accepts any bytes. Its .parse and .run methods use the shared ParseError(diagnostic) envelope.

3.2.5. Hand-built record parsers§

When a column needs custom handling, such as a list stored in one cell, describe one row with CSV.record, giving it a function that takes one argument per column, then add one .keep(CSV.field(...)) per column in order. A field parser is any Parser(Parser.Utf8.Bytes, a, problem); CSV.string({}), CSV.u64({}) and CSV.f64({}) read cells with the rules above:

Movie : { title : Str, year : U64, cast : List(Str) }

cast : Parser(Parser.Utf8.Bytes, List(Str), _)
cast = CSV.string({}).map(|text| Str.split_on(text, ";"))

movie : Parser(CSV.Record, Movie, _)
movie =
	CSV.record(|title| |year| |actors| { title, year, cast: actors })
		.keep(CSV.field(CSV.string({})))
		.keep(CSV.field(CSV.u64({})))
		.keep(CSV.field(cast))

Run it on text with CSV.rows_parser({ row: parser }).parse_str(text), or on an existing document with document.decode_rows({ row: parser }). Columns are matched by position, and the record parser must read every field in the row: a row with more fields than .keep calls fails, and so does a row with fewer.

3.2.6. Read and write large files§

Construct CSV.decoder({}), then call .read_iter(chunks) for an Iter(List(U8)) or .read_stream(chunks) for an effectful Stream(Try(List(U8), source_error)). These methods yield Value(record), recoverable Rejected(error) events when configured, and Finished(report) after complete validation. Consume through Finished; stopping early validates only a prefix. Source failures keep the original typed failure inside SourceErr.

Chunks may end inside UTF-8 characters, quoted multiline fields, doubled quotes, or CRLF. Empty chunks do not end the document. For manual backpressure call decoder.step({ bytes, end: More }), or end: Final for the last input, and retain the returned state.

CSV.positional_decoder({ row: parser }) applies a custom record parser. CSV.typed_decoder({}) infers the row type; select header: Exact, Normalized, or Absent. Headers are retained once; completed rows leave the decoder. Default limits are 16 MiB per field, 64 MiB per unfinished record, and 65,536 fields. The raw decoder takes these limits directly. Typed and positional constructors place them in reader: { max_field_bytes, max_record_bytes, max_fields }.

All reader constructors accept the same outer errors props. The default errors: { policy: FailFast } stops on the first failure. SkipInvalidRecords counts and skips a row that fails typed decoding; ReportInvalidRecords also emits its Rejected diagnostic. Both allow at most max_errors rejected records (1,000 by default). The next rejection is terminal and retains its triggering diagnostic under RecoveryLimitExceeded. Malformed CSV syntax and resource limits are terminal in every mode because their record boundaries cannot safely be guessed.

decoder.collect(chunks) fails on a rejection, skipped records, or a missing completion event. Use .collect_report(chunks) to explicitly collect values, reported diagnostics, and completion counts. Collecting retains all output; ordinary iteration or folding keeps parser memory bounded by the current chunk, unfinished record, header, and consumer-retained values.

CSV.encoder({}).write_iter(records) and its .write_stream(records) counterpart validate each complete record before emitting its bytes. Set chunk_bytes in the constructor props; the default is 64 KiB and zero is rejected with InvalidChunkSize. CSV.WriteProps defaults to necessary quoting and CRLF. Set quoting: Always or line_ending: Lf/Cr for another dialect. A record must contain at least one field; a lone empty field is quoted so it survives a round trip. For an already collected document, document.encode({}) returns all encoded bytes.

The complete streaming example connects basic-cli’s file or stdin chunks to CSV parsing and a bounded file writer. It never reads the entire document at once:

roc examples/csv-streaming.roc normalized.csv input.csv

The whole-document raw scanner remains optimized for complete buffers and is compared against the incremental scanner by fuzzing arbitrary bytes and chunk boundaries. Typed complete-document and streaming APIs report syntax and decoding failures in the order their records are encountered.

3.2.7. Report which row failed§

Native CSV operations return InvalidCsv(error). Custom positional decoding returns InvalidRow(error), preserving its typed problem under ParserFailure. A generic CSV.rows_parser(...).parse_str(text) wraps these native tagged failures inside ParseError(diagnostic) like every shared parser.

Each diagnostic contains:

FieldMeaning
problemA descriptive tagged failure with structured limits, expected scalar type, widths or offending source data. ExpectedValue and UnknownTag retain an excerpt of at most 40 owned bytes together with the original total_bytes. Match tags to classify errors; render text at the application boundary.
locationText({ offset, line, column }) identifies the byte offset and one-based line and byte column in retained source. Decoding an existing document uses Unavailable(NotRetained) because the source was not retained. Missing schema fields have no physical source location. A nested field parser diagnostic identifies positions in the decoded field bytes separately.
pathLogical Record(number) and Field(number) segments use one-based indexes. Records include the header and blank lines. A missing schema field uses Key(name) instead of inventing a field index.
describe : Str -> Str
describe = |text| {
	match CSV.rows_parser({ row: movie }).parse_str(text) {
		Ok(movies) => "${movies.len().to_str()} movies"
		Err(ParseError({ problem: InvalidRow(error), .. })) => {
			problem = match error.problem {
				ParserFailure(FieldProblem(field)) => Str.inspect(field.problem)
				other => Str.inspect(other)
			}
			"${Str.inspect(error.location)}, ${Str.inspect(error.path)}: ${problem}"
		}
		Err(ParseError(error)) =>
			"${Str.inspect(error.location)}, ${Str.inspect(error.path)}: ${Str.inspect(error.problem)}"
	}
}

Stdout.line!(describe("Airplane!,1980,Robert Hays;Julie Hagerty"))?
Stdout.line!(describe("Airplane!,1980,Robert Hays\nCaddyshack,soon,Bill Murray"))?
Stdout.line!(describe("Airplane!,1980,Robert Hays,extra"))?
Output
1 movies
Text({ column: 12, line: 2, offset: 38 }), [Record(2), Field(2)]: ExpectedValue({ expected: U64, source: { bytes: [115, 111, 111, 110], total_bytes: 4 } })
Text({ column: 28, line: 1, offset: 27 }), [Record(1), Field(4)]: UnreadFields({ found: 4, read: 3 })

3.2.8. Which CSV files are accepted§

The dialect is RFC 4180 with the relaxations that Python’s csv module (in strict mode) and Rust’s csv crate also accept:

Stdout.line!(raw_records("size,6\" pipe\rcount,"))?
Stdout.line!(raw_records("bolt,\"250"))?
Stdout.line!("empty input: ${Str.inspect(CSV.parse_str("", {}).map_ok(|document| document.records))}")?
Output
size | 6" pipe
count | 
not valid CSV at Text({ column: 6, line: 1, offset: 5 }): Syntax(UnterminatedQuotedField)
empty input: Ok([])

3.2.9. Limitations§

For every function and type in the module, see the CSV API reference.

3.2.10. Performance§

Whole-document scanning uses SIMD delimiter searches and can share field slices with the input. CSV.parse_string_records collects text fields directly while scanning. Incremental scanning owns unfinished records so completed records do not pin old source chunks. Typed decoding adds each conversion’s cost, and whole-document collection retains the resulting values.

Run scripts/bench.py --formats csv for separate collection and string-record consumption comparisons. bench/roc/csv-file-stream.roc reads a real file or stdin without preloading it and reports record count, content checksum, first-item latency and elapsed time including reads. Use that driver for source-retention measurements; parse-only benchmarks intentionally keep their input in memory. See Benchmarks and Performance for build modes, receipts and comparison requirements.

The conformance review uses the shared correctness oracle cache.

3.3. Read and write QIF§

QIF reads ordered Quicken Interchange Format sections and records. Raw parsing preserves repeated and unknown fields. Typed decoding supports bank, cash and credit-card transactions, account names, repeated split fields, and exact amount text. Category and class records remain available as context. Other sections are available through raw parsing and rejected by typed decoding.

3.3.1. Read chunks§

Use QIF.decoder(props).read_iter(chunks) for raw events and QIF.transaction_decoder(props) for typed transactions. decoder.read_stream(chunks) accepts an effectful source of fallible byte chunks. The package remains platform-independent. The complete program in examples/qif-stream.roc connects basic-cli stdin chunks to the incremental parser and writer; it never collects the document.

Use QIF.Decoder or QIF.TransactionDecoder when annotating the configured decoder’s type. Both support the common reader operations; scanner state remains opaque.

Results use the shared Value, Rejected and Finished events. Default handling stops on the first error. Configure the decoder with errors: { policy: ReportInvalidRecords } in its constructor props to receive diagnostics for fully framed records, or use SkipInvalidRecords to omit them. Both modes count rejected records and stop when the configured error budget is exceeded. Missing terminators, invalid encoding, resource limits and malformed account context remain terminal. Consume through Finished to validate the entire document.

QIF.parse_str(source, props) collects raw events into a document; doc.transactions(props) decodes its transactions with fail-fast handling. Use decoder.collect_report(chunks) to collect events, diagnostics, and completion counts. QIF.parser(props) composes complete raw parsing with other byte parsers. Quotations and QIF.validate use the same grammar and options.

3.3.2. Decode dates and amounts§

The default date policy rejects ambiguous dates such as 01/02/2024. Choose DayFirst, MonthFirst or YearFirst explicitly when the export convention is known. Unambiguous dates are validated against the Gregorian calendar. Two-digit years 70–99 mean 1970–1999; 00–69 mean 2000–2069.

Amounts remain decimal strings, preserving precision and spelling. The supported grammar is an optional sign, decimal digits and at most one decimal point, with at least one digit. Locale grouping separators and exponents are rejected. QIF.amount_to_dec performs a fallible conversion to Roc’s bounded Dec type. Identifiers such as check numbers remain strings, including leading zeroes.

3.3.3. Write incrementally§

QIF.encoder(props).write_iter(events) and .write_stream(source) validate each raw event before emitting its bytes. Configure output fragments with chunk_bytes in the encoder props. Supply ordered headers and records, as produced by raw parsing. A zero chunk size is an error. Default output uses CRLF and UTF-8; line_ending also accepts Lf or Cr, and encoding accepts Windows1252. Unsupported Windows-1252 characters fail without replacement.

doc.encode(props) collect the same canonical output. Field order, unknown fields and repeated values survive semantic round trips; source locations and original line endings need not. A leading UTF-8 BOM is accepted and omitted from canonical output. Every record requires a terminating ^, including the last. NUL and embedded record/header line breaks are rejected.

3.3.4. Limits and verification§

Read props limit individual lines (1 MiB), records (16 MiB), fields per record (65,536), and section/account context (64 KiB). Limits count original bytes; errors locate original byte positions even for Windows-1252 input. The parser retains only context and the unfinished record. A caller collecting results naturally retains those results too.

The QIF fuzz targets exercise arbitrary bytes, chunk equivalence, independent models, malformed records, recovery, and allocation/retention ceilings. Run the pinned Quiffen conformance and cross-writer checks with python3 scripts/review_qif.py check. Its campaign and replay commands retain reproducers in .roc-parser-tmp/qif-review. Intentional differences include strict terminators, empty documents, and preserving numeric reference strings.

The conformance review uses the shared correctness oracle cache.

For complete numeric dates with four-digit years, the reference cache is independent of the clock. Partial and two-digit dates also include dateutil’s implicit local-date and timezone defaults in the cache identity.

Errors use { problem, location, path }. Source failures retain original byte coordinates; detached values explicitly identify unavailable locations. Limits carry a typed resource, max, and observed: Exact(count) or AtLeast(count). Writer failures use Unavailable(GeneratedValue). Logical paths identify records or fields where available. Large offending values use bounded excerpts with their original size.

3.4. Reading and writing OFX§

OFX reads bank and credit-card statement responses from OFX 1.x SGML and 2.x XML. Both dialects can be written: WriteProps.syntax defaults to Xml203; select Sgml102 for legacy consumers. Runtime parsing returns InvalidOfx with an original byte offset, line, column, and structured OFX.Problem tag.

3.4.1. Choose whole-document or incremental reading§

OFX.parse_str(text, props) reads already decoded UTF-8. OFX.parse_bytes(bytes, props) detects declarations and accepts UTF-8, ASCII, and Windows-1252. An explicit Props.encoding must agree with a declaration. Undefined Windows-1252 bytes, unsupported encodings, and contradictory headers fail. A leading UTF-8 BOM is accepted, including when split across chunks.

OFX.decoder(props).read_iter(chunks) consumes an Iter(List(U8)) lazily. OFX.decoder(props).read_stream(chunks) accepts a Stream(Try(List(U8), source_error)). The source owns file and network I/O; the package never opens a connection. Byte boundaries may split tags, CRLF, references, and encoded characters. Empty chunks are ordinary input. Source exhaustion alone finalizes the parser. Source failure is terminal and never treated as a successful end of file.

Items are StatementStart(context), Transaction(transaction), LedgerBalance(balance), AvailableBalance(balance), and StatementEnd. A successful item does not establish validity of the entire document: consume the shared Finished(report) event to establish successful EOF. The report counts emitted logical items and rejected transactions.

Decoder error handling defaults to FailFast. Configure it with errors: { policy: ReportInvalidRecords } in its constructor props. SkipInvalidRecords and ReportInvalidRecords recover only semantic failures in a fully delimited STMTTRN. Report mode also emits Rejected(error). Syntax, encoding, context, source, and resource-limit failures remain terminal. The error budget allows 1,000 rejected transactions by default; the next is terminal and includes its diagnostic. Zero stops on the first rejected transaction.

Whole-document parse_bytes uses fail-fast handling. Use decoder.collect_report(chunks) to collect events, diagnostics, and the completion report under a selected policy. The executable example is docs/examples/ofx-guide.roc.

3.4.2. Preserve financial values§

Transaction amounts, balances, dates, and identifiers are strings. Leading zeroes, decimal spelling, and OFX timestamp offsets survive a semantic round trip without binary floating-point conversion. Timestamps validate calendar dates, right-truncated time components, optional fractions, and offsets.

Context preserves bank/card identity, account fields, statement dates, wrapper TRNUID, successful status fields, file identifiers, and the supported signon server/language/institution fields. Non-success response statuses are explicitly unsupported. Missing wrapper identity/status, duplicate accounts or balances, and late context changes fail rather than changing an already emitted context.

Transaction extensions retain supported XML elements in relative order. Attributes and unknown SGML extension tags are unsupported. SGML has omitted leaf closes, so guessing whether an unknown tag is a leaf would be ambiguous. Padded CDATA in typed scalar fields is explicitly unsupported; ordinary scalar whitespace is normalized. This avoids discarding spaces that CDATA makes significant. DTD declarations, external entities, investment statements, and nonstatement message sets are outside this banking/card subset. Files containing unsupported constructs are rejected rather than silently interpreted as another format.

3.4.3. Write bounded output§

OFX.encoder(props) configures contextual writing through .write_iter(events) and .write_stream(source). Each item is validated before bytes are emitted; an invalid transaction cannot leave a partial record. Successful source completion closes the document, while an unfinished statement fails.

Set chunk_bytes in the encoder props to bound output fragments. A zero chunk size fails. The effectful writer preserves source errors and does not finish a failed source. Windows-1252 writing rejects unrepresentable characters. document.encode(props) collects the same encoding for callers who need a complete output buffer.

The reader retains one unfinished record and its stack. Defaults bound tokens at 65,536 bytes, captured records at 4 MiB, nesting at 64, and transaction fields at 1,024. A consumer retaining all transactions still needs memory proportional to that result. Streaming callers can discard each transaction as it arrives.

3.4.4. Verify changes§

Run scripts/review_ofx.py check with the pinned packages from scripts/ofx/requirements.txt. The primary oracle is ofxtools 1.1.1; ofxparse 0.21 checks its represented account and transaction fields independently. The adapter explicitly decodes declared Windows-1252 using strict Python codecs and normalizes transport for libraries that assume UTF-8 or ASCII. XML values are projected from Python’s standard XML parser after ofxtools validation, preserving CDATA/reference provenance. SGML raw-tree entity spelling is decoded once. Cross-writer checks compare original semantic values as well as readers of the written output. Reproduced ofxtools 1.1.1 text loss for CDATA/numeric references is reported separately with both models; unexpected writer data changes fail the review.

The review exercises bank/card, SGML/XML, and UTF-8/Windows-1252 combinations, our writers through external readers, and the independent ofxtools XML writer through our reader. campaign --smoke, campaign --seconds N --seed S, corpus DIR, and replay REPRO.json support reproducible exploration.

The ofx-raw, ofx-stream, ofx-roundtrip, and ofx-malformed roc-fuzz targets check source bytes, chunk schedules, independent generated models, incremental reader/writer pipelines, and safe recovery. ofx-ceilings creates distinct chunks inside Fuzz.measure! and discards events immediately. Its small/large examples hold chunk size, record size, and depth fixed while increasing record count, enforcing constant peak retained memory and linear allocator traffic.

The retention measuring examples keep a 32-byte first transaction identifier while draining later records. Their first source chunk includes a 64-KiB record and 256 KiB of unrelated markup. Both the source chunk and captured record must be released: only the retained string remains live after the drain. This checks retention by returned values separately from the discard-all parser-state test.

The conformance review uses the shared correctness oracle cache.

Errors use { problem, location, path }. Source failures retain original byte coordinates; detached values explicitly identify unavailable locations. Limits carry a typed resource, max, and observed: Exact(count) or AtLeast(count). Writer failures use Unavailable(GeneratedValue). Logical paths identify records or fields where available. Large offending values use bounded excerpts with their original size.

3.5. Read YAML configuration and frontmatter§

This chapter shows how to read a YAML configuration file or the frontmatter at the top of a Markdown post into a Roc value, pull settings out of it, and report mistakes with a line and column. It is for Roc programmers who have added parser to their app’s dependencies and know the YAML they want to read.

The parser handles the YAML that configuration files and frontmatter use in practice, and rejects the rest with an error instead of guessing. If your files use anchors, tags or several documents in one file, read what is rejected first.

The decoding examples come from docs/examples/yaml-guide.roc; the source-highlighting example comes from docs/examples/yaml-source-guide.roc. CI checks, tests and runs both against the package beside this manual. Both import parser.Yaml.

3.5.1. Decode a document into a record§

Yaml.decode_str(source, props) reads a document straight into your own Roc type. You do not pass the type: it is inferred from how the result is used, usually from a type annotation. This configuration, held in a Roc multi-line string (each \\ starts one line of the file):

config_text =
	\\# deployment settings
	\\name: web
	\\replicas: 3
	\\debug: false
	\\ports: [80, 443]
	\\owners:
	\\  - name: Ada
	\\    email: ada@example.com
	\\  - name: Grace

is read by:

Owner : { name : Str, email : Try(Str, [Missing]) }

Config : {
	name : Str,
	replicas : U8,
	debug : Bool,
	ports : List(U16),
	owners : List(Owner),
}

## The record type tells `Yaml.decode_str` what to expect.
load_config : Str -> Try(Config, [InvalidYaml(Yaml.Error)])
load_config = |text| Yaml.decode_str(text, {})

summarize : Str -> Try(Str, [InvalidYaml(Yaml.Error)])
summarize = |text| {
	config = load_config(text)?
	owner_names = config.owners.map(|owner| owner.name)

	Ok("${config.name}: ${config.replicas.to_str()} replicas, owners ${Str.join_with(owner_names, " and ")}")
}

summarize(config_text) returns:

Output
web: 3 replicas, owners Ada and Grace

The rules that map YAML onto Roc types:

A value of the wrong shape or out of range fails with InvalidYaml at that value’s line and column, the same error invalid YAML gives. A required field whose key is absent fails with InvalidYaml: its problem is MissingRequiredField(name), its path includes Key(name), and its physical location is Unavailable(NotRetained). No additional error tag is needed in the result annotation:

Stdout.line!(Str.inspect(load_config("name: web\nreplicas: 300\n")))?
Stdout.line!(Str.inspect(load_config("name: web\nreplicas: 3\ndebug: no\n")))?
Stdout.line!(Str.inspect(load_config("name: web\nreplicas: 3\ndebug: false\nports: []\n")))?
Output
Err(InvalidYaml({ location: Text({ column: 11, line: 2, offset: 20 }), path: [], problem: IntegerOutOfRange({ target: U8, value: "300" }) }))
Err(InvalidYaml({ location: Text({ column: 8, line: 3, offset: 29 }), path: [], problem: TypeMismatch({ actual: PlainScalar("no"), expected: BooleanValue }) }))
Err(InvalidYaml({ location: Unavailable(NotRetained), path: [Key("owners")], problem: MissingRequiredField("owners") }))

3.5.1.1. Other key spellings and unknown keys§

The props of Yaml.decode_str select other conventions. keys says how YAML spells Roc’s snake_case field names: SnakeCase (as written), KebabCase (user-id) or CamelCase (userId). unknown_keys: Reject turns a key the record does not have into an InvalidYaml error, which catches typos in hand-written configuration:

load : Str -> Try({ user_id : U64 }, [InvalidYaml(Yaml.Error)])
load = |text| Yaml.decode_str(text, { keys: KebabCase, unknown_keys: Reject })

For a reusable configured parser, construct Yaml.value_parser(props) and call its .parse_str(source) runner. It reports the shared ParseError diagnostic envelope. A generic function over decodable types requires a.Yaml.Parseable(errors) in its where clause.

Custom scalar types implement parser_for using Yaml.Format(errors) and return the same complete error row from their cursor callback. Include custom problem tags in a Try result annotation when combining them with a derived record; the format hooks preserve those payloads alongside native YAML errors.

3.5.2. Explore a document as a tree§

When the shape is not known in advance, Yaml.parse_str returns a Yaml tree:

Yaml := [
    Null,
    Bool(Bool),
    Int(I64),
    Float(F64),
    Text(Str),
    Sequence(List(Yaml)),
    Mapping(List({ key : Str, value : Yaml })),
]

A mapping keeps its entries in source order. get looks up a key, at an index, and get_path follows several of them, using decimal strings such as "0" for sequence indexes. Each returns Err(Missing) when there is nothing there. as_str, as_i64, as_bool and as_list unwrap one variant or return Err(WrongType):

## Explore a document without a record type.
first_owner : Str -> Try(Str, [InvalidYaml(Yaml.Error), Missing, WrongType])
first_owner = |text| {
	config = Yaml.parse_str(text, {})?
	name = config.get_path(["owners", "0", "name"])?
	name.as_str()
}

For config_text, and then for "owners: []":

Output
Ok("Ada")
Err(Missing)

Empty input, or a document holding only comments, parses as Null. To combine YAML with other parsers, Yaml.parser(props) is a Parser(Parser.Utf8.Bytes, Yaml, Yaml.Problem) that reads the rest of its input. Its runners report ParseError with the shared { problem, location, path } envelope.

3.5.3. Read Markdown frontmatter§

Frontmatter is a YAML document between two --- lines at the start of a Markdown file. Split the text at the closing --- and parse the first part. The parser accepts the opening --- (and a closing ...) as document markers, so you do not need to strip them:

## Split a Markdown file into its YAML frontmatter and body.
frontmatter : Str -> Try({ meta : Yaml, body : Str }, [NoFrontmatter, InvalidYaml(Yaml.Error)])
frontmatter = |markdown| {
	match Str.split_on(markdown, "\n---\n") {
		[header, .. as rest] if Str.starts_with(header, "---\n") =>
			Ok({ meta: Yaml.parse_str(header, {})?, body: Str.join_with(rest, "\n---\n") })

		_ => Err(NoFrontmatter)
	}
}

For "---\ntitle: Hello\ntags: [roc, yaml]\n---\n# Hello\n", printing Yaml.to_inspect(meta) and then the body gives:

Output
Mapping([{ key: "title", value: Text("Hello") }, { key: "tags", value: Sequence([Text("roc"), Text("yaml")]) }])
# Hello

Yaml.decode_str(source, props) works on the same text when you know the frontmatter’s fields.

3.5.4. How plain values are typed§

In a Yaml tree, an unquoted value is resolved with the YAML 1.2 core schema. Anything that matches none of these forms is Text:

Written asBecomes
~, null, Null, NULL, or nothingNull
true, True, TRUE, false, False, FALSEBool
Decimal digits with an optional sign; unsigned 0x hexadecimal or 0o octalInt (an I64; a value outside its range is an error)
Decimal with a fraction or exponent; .inf, -.inf, .nan in the three letter casesFloat
show!("[~, null, true, FALSE, 42, -7, 0x1F, 0o17, 1.5, 6.02e23, .inf, -.Inf, .nan, 1.2.3, 1_000, 'true', 2026-10-01]")
Output
Sequence([Null, Null, Bool(True), Bool(False), Int(42), Int(-7), Int(31), Int(15), Float(1.5), Float(6.02e23), Float(inf), Float(-inf), Float(nan), Text("1.2.3"), Text("1_000"), Text("true"), Text("2026-10-01")])

The last four values show what stays text: a version number such as 1.2.3, a number with _ separators, anything in quotes, and dates (YAML 1.2 has no timestamp type). tRUE and 0X1 are text too, because the core schema allows only the spellings in the table. In a tree, quote a value whenever it must stay text; Yaml.decode_str(source, props) reads any non-null plain scalar into a Str field as written.

Yaml.to_inspect prints a tree in Roc notation, escaping line breaks, tabs and other control characters so each value stays on one line.

3.5.5. Write multi-line text§

Use a block scalar for text that spans lines. | (literal) keeps line breaks; > (folded) joins lines with spaces and keeps a blank line as a paragraph break. A chomping indicator controls the final line break: none keeps one, - removes it, and `` keeps every trailing blank line. A digit sets the content indentation, so the text can start with spaces:

show!(
	\\literal: |
	\\  line one
	\\  line two
	\\folded: >
	\\  joined
	\\  into one line
	\\
	\\  new paragraph
	\\stripped: |-
	\\  no final newline
	\\kept: |+
	\\  trailing blank lines kept
	\\
	\\indented: |2
	\\    starts with two spaces
)
Output
Mapping([{ key: "literal", value: Text("line one\nline two\n") }, { key: "folded", value: Text("joined into one line\nnew paragraph\n") }, { key: "stripped", value: Text("no final newline") }, { key: "kept", value: Text("trailing blank lines kept\n\n") }, { key: "indented", value: Text("  starts with two spaces") }])

3.5.6. Write lists§

A sequence item may start with a mapping on the same line as its - (compact), may hold another sequence the same way, and may sit at the same indentation as its parent key (indentless):

show!(
	\\steps:
	\\- run: build
	\\  name: compile
	\\- - nested
	\\  - compact
)
Output
Mapping([{ key: "steps", value: Sequence([Mapping([{ key: "run", value: Text("build") }, { key: "name", value: Text("compile") }]), Sequence([Text("nested"), Text("compact")])]) }])

Short lists and mappings can also use flow style on one line, as in ports: [80, 443] or labels: { tier: web }.

3.5.7. Quote strings§

A single-quoted string has no escapes except '' for one quote. A double-quoted string supports every YAML 1.2 escape, including \t, \n, \xXX, \uXXXX and \UXXXXXXXX. A # inside quotes is text; outside quotes and after a space it starts a comment:

show!(
	\\single: 'it''s # not a comment'
	\\plain: text # this is a comment
)
show!(
	"double: \"tab\\there, \\u00e9, \\x41\"",
)
Output
Mapping([{ key: "single", value: Text("it's # not a comment") }, { key: "plain", value: Text("text") }])
Mapping([{ key: "double", value: Text("tab\there, é, A") }])

3.5.8. Highlight the original source§

Use Yaml.tokenize_str(text) when you need to display YAML as written. Unlike a decoded tree, its result retains whitespace, comments, quotes, line endings and punctuation. Yaml.tokenize_bytes(bytes) also accepts arbitrary bytes, including invalid UTF-8. Both return this shape:

{
    source : List(U8),
    tokens : List(Yaml.Token),
    diagnostics : List(Yaml.Diagnostic),
    status : [Complete, Recovered, Limited],
}

Each token has kind, start, end, role and scalar fields. start and end are U64 byte offsets into source, with start included and end excluded. They are byte coordinates, not character indexes: a UTF-8 é occupies two bytes and CRLF occupies two bytes. Nonempty tokens cover the entire input in order without gaps or overlap; empty input has no tokens. There is no synthetic end-of-file token. Concatenating the slices reproduces the input byte for byte, including malformed bytes.

The scanner distinguishes these token kinds:

SourceKinds
TriviaWhitespace, Comment
Scalar textPlainScalar, SingleQuotedScalar, DoubleQuotedScalar
Block scalarsBlockScalarHeader, BlockScalarContent
Block punctuationMappingValue, SequenceEntry, ExplicitKey
Flow punctuationFlowSequenceStart, FlowSequenceEnd, FlowMappingStart, FlowMappingEnd, FlowEntry
Names and stream markersAnchor, Alias, Tag, Directive, DocumentStart, DocumentEnd
RecoveryInvalid

role is Key, Value or Other. scalar is Text, Boolean, Null or Number; it describes lexical spelling without constructing a semantic value or imposing the tree parser’s integer range. Quoted and block scalar contents stay text. Plain scalars follow YAML 1.2 spelling: on is text, false is boolean, and null is null. A mapping key’s role takes precedence for highlighting, so on: and true: both get PropertyName.

Yaml.highlight_category(token) maps a token to Trivia, CommentText, PropertyName, StringText, NumericLiteral, BooleanLiteral, NullLiteral, Symbol, Name, TypeName or InvalidText. Use these categories as theme keys in either HTML or PDF output. The helper returns no rendered markup; your output writer copies the original slices and escapes or encodes them for its own output format.

This example uses CRLF and non-ASCII text, a workflow key, a URL fragment, and a shell script in a block scalar:

workflow = "# CI\r\non: [push]\r\nname: 'Check café'\r\nurl: https://example.com/manual#setup\r\ncontinue-on-error: false\r\nrun: |-\r\n  # Shell text, not a YAML comment\r\n  echo ok\r\n"

# Each piece retains bytes from the original source, including whitespace.
# Both an HTML writer and a PDF writer can consume these same pieces.
source_pieces = |tokenized| tokenized.tokens.map(
	|token| {
		category: Yaml.highlight_category(token),
		bytes: tokenized.source.sublist({ start: token.start, len: token.end - token.start }),
	},
)

copy_source = |tokenized| source_pieces(tokenized).fold([], |bytes, piece| bytes.concat(piece.bytes))

Passing each piece to an output writer keeps the same category and byte range in both renderers. copy_source is a minimal sink that discards the theme and joins the original bytes; a production writer can stream each slice directly. Inspecting a few categories:

tokenized = Yaml.tokenize_str(workflow)
Stdout.line!("preserved: ${Str.inspect(copy_source(tokenized) == workflow.to_utf8())}")?
for token in tokenized.tokens {
	bytes = tokenized.source.sublist({ start: token.start, len: token.end - token.start })
	if bytes == "on".to_utf8() or bytes == "false".to_utf8() or bytes == "https://example.com/manual#setup".to_utf8() {
		Stdout.line!("${Str.from_utf8(bytes) ?? ""}: ${Str.inspect(Yaml.highlight_category(token))}")?
	}
}
Output
preserved: True
on: PropertyName
https://example.com/manual#setup: StringText
false: BooleanLiteral

# starts a comment only in a YAML comment position. Quoted #, a URL’s #setup fragment, and the shell comment inside run: |- stay scalar content. Literal |, folded >, and chomping or indentation indicators remain in their original spelling. GitHub Actions ${{ ... }} expressions remain YAML scalar text; an application can add expression colouring as a second overlay within the returned spans.

3.5.8.1. Recovery and semantic validation§

Tokenization does not require successful decoding. It recognizes anchors, aliases, tags and stream markers even where the semantic parser rejects them. Complete means the lexical scan completed without a diagnosed problem; it does not establish that the YAML is semantically valid. Duplicate keys and unsupported decoding features still need a semantic check when you intend to load configuration.

Each diagnostic has a code and half-open start/end byte coordinates. Codes are UnclosedQuote, InvalidEscape, UnclosedCollection, UnexpectedDelimiter, InvalidIndentation, InvalidBlockHeader, InvalidCharacter, InvalidUtf8 and ResourceLimit. Recovered reports lexical problems while keeping usable tokens and all original bytes. Limited reports more than 256 simultaneously open flow collections. The scanner emits an Invalid token covering the remaining input and a ResourceLimit diagnostic, so callers can display those bytes unchanged. This lexical limit is separate from semantic decoding’s 100-level nesting limit. An unclosed quoted scalar retains the bytes through end of input as one quoted token and reports UnclosedQuote. Invalid escapes stay inside the quoted token with diagnostics. Unmatched closing flow delimiters remain punctuation tokens; unclosed collections report their opening spans through end of input. These recovery choices keep a broken example displayable; they do not repair it or supply a valid semantic value.

# Tokenization succeeds as a source operation even when decoding cannot.
broken = Yaml.tokenize_str("name: \"unfinished")

invalid_utf8 = Yaml.tokenize_bytes([0xFF, 0x0D, 0x0A])

expect copy_source(broken) == broken.source
expect !broken.diagnostics.is_empty()
expect copy_source(invalid_utf8) == [0xFF, 0x0D, 0x0A]
expect !invalid_utf8.diagnostics.is_empty()

If a renderer cannot use a token’s category, it can always copy its source slice with the default text style. Keep source as bytes when exact preservation matters: converting invalid UTF-8 to a string with replacement characters changes those bytes. A text output format may instead show a replacement glyph while retaining the original byte coordinates for reporting diagnostics.

3.5.9. What semantic decoding rejects§

The following are outside the supported subset. Each one is an InvalidYaml error, never a silently different value:

Mapping keys are kept as their source text, not resolved like values. 1 and 01 are different keys, and true is the string key "true".

3.5.10. Show where an error is§

InvalidYaml carries a Yaml.Error diagnostic with problem, location, and path. Complete source parsing provides Text({ offset, line, column }): a zero-based original byte offset and one-based line and byte column, including BOM and CRLF bytes. Missing cursor positions explicitly use Unavailable(NotRetained). Match the native Yaml.Problem union to distinguish failures. Limit payloads include the typed resource, maximum and observed count; invalid characters retain a bounded source excerpt. Use Str.inspect(error.problem) when displaying a diagnostic at an application boundary:

report : Str -> Str
report = |text| {
	match Yaml.parse_str(text, {}) {
		Ok(value) => Yaml.to_inspect(value)
		Err(InvalidYaml(error)) => "line ${source_position(error.location).line.to_str()}, column ${source_position(error.location).column.to_str()}: ${Str.inspect(error.problem)}"
	}
}

Stdout.line!(report("server:\n\tport: 80"))?
Stdout.line!(report("base: &defaults\n  port: 80"))?
Stdout.line!(report("one: 1\n---\ntwo: 2"))?
Stdout.line!(report("name: a\nname: b"))?
Stdout.line!(report("list: [1,\n  2]"))?
Stdout.line!(report("1: one\n01: zero-one"))?
Output
line 2, column 1: TabIndentation
line 1, column 7: ReferencesUnsupported
line 2, column 1: MultipleDocumentsUnsupported
line 2, column 1: DuplicateKey("name")
line 1, column 7: UnclosedFlowCollection
Mapping([{ key: "1", value: Text("one") }, { key: "01", value: Text("zero-one") }])

For the full module interface, see the Yaml API reference.

3.5.11. Performance§

Parsing takes time linear in the size of the document, at about 51–55 MB/s on an Apple M2 from 4 KiB to 1 MiB: a 4 KiB configuration in under 0.1 ms and a 1 MiB one in about 20 ms. That is about as fast as yaml-rust2 (Rust), 1.6 times faster than serde_yaml, 2.6 times faster than Go’s yaml.v3 and seven times faster than PyYAML with libyaml. Nesting is limited to 100 levels. Decoding into a record builds the same document first, then walks it once. See Performance for the measurements and the comparison with other libraries.

3.6. Read XML documents§

This chapter shows how to parse an XML document into a tree, find elements, attributes and text in it, and report malformed input with a line and column. It also shows how to read untrusted or very large documents, resolve namespaces, and write a document back out. It is for Roc programmers who have added parser to their app’s dependencies and need data out of XML such as feeds, SVG files, office documents or configuration.

The parser checks that a document is well-formed according to XML 1.0 and gives you the tree after the usual XML clean-up: references replaced by their text, line endings normalised, comments dropped. It does not validate against a schema or DTD, and it rejects documents that contain a <!DOCTYPE>; see what is not supported before using it on documents from other tools.

Every example on this page comes from docs/examples/xml-guide.roc, which CI runs, and imports parser.Xml. Resolved namespace operations live under Xml.Namespaced.

3.6.1. Parse a document and find elements§

Xml.parse_str takes the whole document and returns an Xml record with the document’s root element and its declaration. Each node in the tree is one of:

Node := [
    Element({ name : Str, attributes : List(Attribute), children : List(Node) }),
    Text(Str),
]

An Element holds its name, its attributes in source order, and its children. There is no query language, but Xml.Node has four helpers for the common lookups, and you can pattern match on the tree for anything else:

describe_entry : Xml.Node -> Str
describe_entry = |entry| {
	id = entry.attribute("id") ?? "?"
	titles = Str.join_with(entry.children_named("title").map(|title| title.text()), "")

	"entry ${id}: ${titles}"
}

Given this document:

feed_text =
	\\<?xml version="1.0" encoding="UTF-8"?>
	\\<feed lang="en">
	\\  <!-- newest first -->
	\\  <entry id="2"><title>Fish &amp; Chips</title></entry>
	\\  <entry id="1"><title><![CDATA[<Hello>]]> &#x1F600;</title></entry>
	\\</feed>

parse it and describe each entry element under the root:

match Xml.parse_str(text, {}) {
	Ok(xml) =>
		for entry in xml.root.children_named("entry") {
			# Keep the displayed output portable across documentation fonts.
			Stdout.line!(describe_entry(entry).replace_each("\u(1F600)", "\\u(1F600)"))?
		}

	Err(InvalidXml(problem)) => Stdout.line!("invalid XML: ${Str.inspect(problem.problem)}")?
}
Output
entry 2: Fish & Chips
entry 1: <Hello> \u(1F600)

The whitespace between elements is kept as Text nodes; children_named skips them because it only returns elements. &amp;, the CDATA section and the character reference &#x1F600; have already been turned into ordinary text.

3.6.2. What the tree contains§

The tree is what an XML processor reports, not the literal source:

This document has every one of these in a single element:

print_tree!("<p class='a\tb'>x &lt; y &#65;&#x42;\r\n<![CDATA[<raw>]]><!-- gone -->z<br/></p>")?

Printing the root with Str.inspect gives:

Output
Element({ attributes: [{ name: "class", value: "a b" }], children: [Text("x < y AB
<raw>z"), Element({ attributes: [], children: [], name: "br" })], name: "p" })

3.6.3. Report malformed input§

When a document is not well-formed, Xml.parse_str returns InvalidXml(diagnostic), with a typed problem, structured location and logical path. A LineColumn or Text location has one-based lines and columns, and columns count UTF-8 bytes, so a column after non-ASCII text is larger than the number of characters a user sees.

check : Str -> Str
check = |text| {
	match Xml.parse_str(text, {}) {
		Ok(_) => "well-formed"
		Err(InvalidXml(error)) => "${Str.inspect(error.location)}: ${Str.inspect(error.problem)}"
	}
}

Stdout.line!(check("<a>\n  <b>\n</a>"))?
Stdout.line!(check("<a>&nbsp;</a>"))?
Stdout.line!(check("<a x='1' x='2'/>"))?
Stdout.line!(check("<a/><b/>"))?
Stdout.line!(check("<!DOCTYPE html><html/>"))?
Output
Text({ column: 1, line: 3, offset: 10 }): EndTagMismatch({ actual: "a", expected: "b" })
Text({ column: 4, line: 1, offset: 3 }): UnknownEntity("nbsp")
Text({ column: 10, line: 1, offset: 9 }): DuplicateAttribute("x")
Text({ column: 5, line: 1, offset: 4 }): TrailingContent
Text({ column: 1, line: 1, offset: 0 }): DoctypeUnsupported

If you are combining XML with other parsers, Xml.parser({}) constructs a Parser(Parser.Utf8.Bytes, Xml, Xml.Problem). It leaves any input after the document for the next parser. Its failures are ParseError(diagnostic), with the same native problem as Xml.parse_str. Byte parsing reports InputOffset; .parse_str reports Text({ offset, line, column }). Match diagnostic.problem to handle a particular failure, or render it with Str.inspect when displaying it to a person.

3.6.4. Read untrusted documents§

Xml.parse_str(source, {}) sets no resource limits. For untrusted input, select { limits: Bounded(reader_props) }. Xml.ReaderProps has defaults: max_depth (1,024), max_elements (4,194,304) and max_attributes per element (1,024). A document that would exceed one fails with InvalidXml(diagnostic) and problem LimitExceeded({ resource, max, observed: AtLeast(max + 1) }) at the start tag or attribute that would exceed it, before building that value:

check_untrusted : Str -> Str
check_untrusted = |text| {
	match Xml.parse_str(text, { limits: Bounded({ max_depth: 3, max_elements: 100 }) }) {
		Ok(_) => "accepted"
		Err(InvalidXml(error)) => match error.problem {
			LimitExceeded({ resource, max, .. }) => "${Str.inspect(error.location)}: more than ${max.to_str()} (${Str.inspect(resource)})"
			_ => "${Str.inspect(error.location)}: ${Str.inspect(error.problem)}"
		}
	}
}

Stdout.line!(check_untrusted("<a><b><c/></b></a>"))?
Stdout.line!(check_untrusted("<a>\n  <b><c><d/></c></b>\n</a>"))?
Output
accepted
Text({ column: 9, line: 2, offset: 12 }): more than 3 (Depth)

3.6.5. Read a large document an element at a time§

Xml.reader returns an Xml.Reader positioned before the root element. Each call to next returns the next Event, Start({ name, attributes }), End(name) or Text(text), with the reader after it, then Done at the end of the document, or Failed(error). The events match the tree: <a/> is a Start and an End, text is merged as in the tree, and comments are dropped. Directly after a Start, element builds that element’s subtree and continues after its end tag, so a program can keep one small tree at a time instead of the whole document:

## The titles of a feed's entries, read one entry at a time: only the entry
## being read is ever a tree.
entry_titles : Str -> Try(List(Str), Xml.ReadError)
entry_titles = |text| {
	var $reader = Xml.reader(text, {})?
	var $titles = []
	while True {
		match $reader.next() {
			Event(Start({ name: "entry", .. }), at_entry) =>
				match at_entry.element() {
					Element(entry, after) => {
						$titles = $titles.concat(entry.children_named("title").map(|title| title.text()))
						$reader = after
					}

					Failed(error) => return Err(error)
					NotAfterStart => return Ok($titles)
				}

			Event(_, rest) => {
				$reader = rest
			}

			Done => return Ok($titles)
			Failed(error) => return Err(error)
		}
	}
}

Stdout.line!(Str.inspect(entry_titles("<feed><entry><title>One</title></entry><entry><title>Two</title></entry></feed>")))?
Output
Ok(["One", "Two"])

To build a model of your own instead of Xml.Node values, call element_with after a Start, with functions that make your type from each start tag, each text and each finished element. It reads the subtree as fast as Xml.parse_str builds a tree, and a start function may reject an element with your own error, reported with its location.

Keep only the latest reader, as here, and return from the Done arm rather than setting a flag: then each step updates the reader in place, and a loop that keeps no events holds working memory for the open elements, in addition to the complete input. The same limits apply as for bounded Xml.parse_str, and reader.metadata().location identifies the most recent event.

3.6.6. Read XML from byte chunks§

Construct Xml.decoder(props), then call .read_iter(chunks) for pure byte chunks or .read_stream(chunks) for a file, stdin or another effectful source. Use Xml.Namespaced.decoder(props) to resolve names with the same interface. Each reader emits Value(Start(...)), Value(End(...)) and Value(Text(...)), followed by Finished(report) after successful source exhaustion. <a/> emits both a start and an end event. Comments and processing instructions are validated and omitted.

The complete xml-streaming.roc program counts Atom entries from stdin. Its loop matches expanded names, so the source can use either a default namespace or a prefix:

var $entries = 0.U64
for! result in (Xml.Namespaced.decoder({ max_depth: 128 })).read_stream(Stdin.chunks()) {
	match result? {
		Value(Start({ name: { namespace: Uri("http://www.w3.org/2005/Atom"), local: "entry", .. }, .. })) => {
			$entries = $entries + 1
		}
		Finished(_) => Stdout.line!("${$entries.to_str()} entries")?
		_ => {}
	}
}

Run it from the repository root with roc examples/xml-streaming.roc and supply a complete Atom feed on stdin. A feed with two Atom entry elements prints 2 entries after the entire input has passed validation.

Consume through Finished to validate closing tags and the trailing document content. A source failure is returned under SourceErr; it never finalizes a truncated document. Malformed XML, namespace problems and resource limits stop the reader. XML structure has no record-skipping recovery policy.

The chunk readers accept UTF-8 bytes and an optional UTF-8 BOM. A chunk can end inside a Unicode character, CRLF, reference, attribute, comment, declaration or CDATA terminator. A declaration naming a different encoding is an error; decode other encodings before using this interface. Empty chunks do not signal EOF.

Unlike Xml.reader, chunk readers can emit several adjacent Text events. Text can also be separated at comments, processing instructions and CDATA boundaries. Concatenate text when the application needs the tree reader’s coalesced result. Each event contains complete UTF-8 characters and decoded references; line ending normalization is preserved across chunk boundaries.

Xml.StreamProps sets finite limits for depth (1,024), total elements (4,194,304), attributes per element (1,024), token bytes (65,536), text fragment bytes (65,536) and retained open-element context bytes (4,194,304). Tokens include complete tags, comments, processing instructions and references; CDATA content is emitted in bounded text fragments. max_text_bytes must be at least four. The context budget charges each open element’s complete start tag, including namespace declarations. A resource limit is terminal. Retaining output or earlier decoder states adds to the application’s memory use.

For a custom pull loop, start with decoder = Xml.decoder(props) and call decoder.step({ bytes, end: More }) until the source ends, then use end: Final. Continue with the returned state descriptor and respect its consumed byte count. The return value follows the shared chunk decoder contract. decoder.metadata() provides the latest event’s structured location and the parsed declaration. Complete-buffer readers expose the same information through reader.metadata(). Xml.Namespaced.decoder provides the namespace-aware version. XML failures use InvalidXml(diagnostic) with a native Xml.Problem; namespace failures use InvalidNamespace(diagnostic). Their payloads retain names, characters, limits and observed counts without requiring diagnostic text.

3.6.7. Resolve namespaces§

Xml.Namespaced processes namespaces following Namespaces in XML 1.0. Each element and attribute name becomes { namespace, local, prefix }, where namespace is Uri(uri) or NoNamespace. A binding covers its element and descendants, xmlns="" removes the default namespace, unprefixed attributes are in no namespace, and xml is always bound. The xmlns declarations become each element’s bindings. Look elements up by namespace and local name, so the same code reads both spellings of an Atom feed:

atom : Xml.Namespaced.Namespace
atom = Uri("http://www.w3.org/2005/Atom")

describe_atom : Str -> Str
describe_atom = |text| {
	match Xml.Namespaced.parse_str(text, {}) {
		Ok(doc) => {
			titles = doc.root.children_named(atom, "title").map(|title| title.text())
			"${Str.inspect(doc.root.name())} ${Str.inspect(titles)}"
		}

		Err(InvalidNamespace(error)) => "${Str.inspect(error.location)}: ${Str.inspect(error.problem)}"
		Err(InvalidXml({ problem, .. })) => Str.inspect(problem)
	}
}

Stdout.line!(describe_atom("<feed xmlns='http://www.w3.org/2005/Atom'><title>News</title></feed>"))?
Stdout.line!(describe_atom("<a:feed xmlns:a='http://www.w3.org/2005/Atom'><a:title>News</a:title></a:feed>"))?
Stdout.line!(describe_atom("<feed><x:title/></feed>"))?
Output
Ok({ local: "feed", namespace: Uri("http://www.w3.org/2005/Atom"), prefix: Unprefixed }) ["News"]
Ok({ local: "feed", namespace: Uri("http://www.w3.org/2005/Atom"), prefix: Prefix("a") }) ["News"]
Text({ column: 7, line: 1, offset: 6 }): UndeclaredPrefix("x")

A namespace problem, such as an undeclared prefix, a reserved prefix bound to the wrong namespace or two attributes with the same expanded name, is InvalidNamespace(diagnostic) at the start tag. Xml.Namespaced mirrors Xml: .parse_str(source, { limits: Bounded(reader_props) }) takes the same limits, and Xml.Namespaced.reader resolves names while it reads, so only one tree is built.

3.6.8. Write a document§

document.encode(write_props) writes a namespaced tree as XML 1.0 bytes. Use Str.from_utf8_lossy at a text presentation boundary when needed. By default it writes the tree’s own prefixes and declarations, so a parsed document is written as it was read. prefixes: Table(entries) writes every name with its namespace’s prefix from the table instead, declared once; an empty prefix makes a namespace the default. Text and attribute values are escaped so that they read back unchanged. A tree that cannot be written, with a name that is not allowed, a character XML 1.0 does not allow, or a prefix not bound where it is used, fails with Unrenderable(diagnostic), including its typed problem and logical path.

## Writes an Atom feed with Atom as the default namespace, whatever prefix it
## was read with.
rewrite_atom : Str -> Str
rewrite_atom = |text| {
	match Xml.Namespaced.parse_str(text, {}) {
		Ok(doc) =>
			match doc.encode({ declaration: Omitted, prefixes: Table([{ prefix: "", namespace: "http://www.w3.org/2005/Atom" }]) }).map_ok(Str.from_utf8_lossy) {
				Ok(written) => written
				Err(Unrenderable({ problem, .. })) => Str.inspect(problem)
			}

		Err(_) => "invalid"
	}
}

Stdout.line!(rewrite_atom("<a:feed xmlns:a='http://www.w3.org/2005/Atom'><a:title>Fish &amp; Chips</a:title></a:feed>"))?
Output
<feed xmlns="http://www.w3.org/2005/Atom"><title>Fish &amp; Chips</title></feed>

3.6.9. What is not supported§

These limits are by design:

Document type declarations

A <!DOCTYPE ...> is rejected. As a result only the five predefined entities exist, and any other reference, such as &nbsp;, is an error. Replace HTML entities with character references (&#160;) before parsing.

Namespaces in Xml

Xml keeps names exactly as written: svg:path is an element named "svg:path", and xmlns declarations are ordinary attributes. Use Xml.Namespaced to resolve them.

Declared encodings

The input is a Roc Str, which is already UTF-8. An encoding="..." in the declaration is reported in declaration but not used. Convert other encodings to UTF-8 before parsing.

Validation

Only well-formedness is checked. Required elements, attribute types and schemas are not.

For the full module interfaces, see the Xml and Xml.Namespaced API references.

3.6.10. Performance§

Xml.parse_str builds the tree at about 120–135 MB/s on an Apple M2, and the rate stays flat from 4 KiB to 1 MiB documents: the parser is linear in the input size. On the generated documents that is about 2.2 times as fast as Go’s encoding/xml, and 1.4–2 times slower than Rust’s quick-xml and roxmltree. Character data and attribute values are found 16 bytes at a time with SIMD, the input is validated as UTF-8 once, and names, text and attribute values with nothing to unescape are slices of the input string, so they keep the input alive. Elements are parsed with an explicit stack, so 5,000 levels of nesting are safe, and duplicate-attribute checks are linear: an element with 5,000 attributes parses 19 times faster than with the Rust libraries. Decoding text and attribute values is linear however they mix references, line endings and CDATA, and the xml-ceilings fuzz target holds every reading path to allocation ceilings linear in the document.

Reading every event with Xml.reader takes about 70% of the time Xml.parse_str takes to build the whole tree. Xml.Namespaced.parse_str builds its tree with element_with and runs at about 45% of Xml.parse_str’s speed on an OOXML-like document, since it splits and resolves every name and its names and nodes are larger; it holds about twice the memory. That is as fast as parsing with Xml and then resolving names over the tree, while checking every namespace rule and holding one tree instead of two. See Performance for the measurements and caveats.

3.7. Parse HTML, SVG and MathML§

Use Html, Svg and MathML when an application needs markup as Roc values: checked literals, imported files, runtime documents, or a fixed template whose holes contain your application’s values. The modules share resolved names, indexed trees, source spans and typed diagnostics. They do not depend on a DOM, a renderer, or a reactive framework.

The complete program in examples/markup-literals.roc lowers a parsed drawing into an independent consumer tree. Its text binding contains <path/> and remains text. The same program is exercised as docs/examples/markup-literals.roc.

3.7.1. Choose a document or fragment§

ModuleDefault syntaxDocumentFragment
HtmlHTMLAn HTML documentChildren of body, or a configured context
SvgXMLAn svg root in the SVG namespaceChildren in the SVG namespace
MathMLXMLA math root in the MathML namespaceChildren in the MathML namespace

parse_str(source, props) reads a complete document with defaulted constructor props. parse_fragment(source, props) accepts multiple roots, including text, and returns a nominal Fragment. A fragment’s single_element() requires one element and no other non-whitespace root content; comments and processing instructions do not count as roots.

A document parsed with XML syntax needs its namespace declaration, such as <svg xmlns="http://www.w3.org/2000/svg">. A Svg.Fragment starts with that namespace already in scope, so <path d="M0 0L10 10"/> needs no wrapper. Explicit namespace declarations inside a fragment still apply.

A literal’s expected type establishes compile-time checking:

fraction : MathML.Fragment
fraction = "<mfrac><mn>1</mn><mn>2</mn></mfrac>"

panel : Html.Fragment
panel = "<section hidden='until-found'><p>Find this panel</p></section>"

cx = "42"

caption = "<path/> stays text"

drawing : Svg.Template(Str)
drawing =
	\\<svg viewBox="0 0 100 100">
	\\  <circle cx="${cx}" cy="50" r="4" />
	\\  <text>${caption}</text>
	\\</svg>

Use Parser.Quotation.require(Svg.validate({ source: text, name: "drawing.svg" })) for an imported complete document, or Svg.Fragment.validate for an imported fragment. Html and MathML provide the same document conversion. Dynamic input should retain its fallible parsing result rather than call Parser.Quotation.require.

Parsing failures carry InvalidMarkup(diagnostic). Use diagnostic.location and diagnostic.path to locate a failure. Match diagnostic.problem, a Markup.Problem, to handle them without inspecting diagnostic strings. Child failures carry a ChildGrammar; root failures preserve expanded names; resource failures carry the resource tag and maximum. Template failures retain the hole index and an InterpolationProblem tag. XML syntax failures preserve the underlying XML problem. Use Str.inspect(error.problem) at a display boundary.

3.7.2. Select XML or HTML syntax deliberately§

The syntaxes can produce different trees from similar text. XML preserves exact case and resolves explicit namespace declarations. HTML adjusts SVG spellings such as viewbox to viewBox, inserts permitted implied elements, and applies HTML’s SVG/MathML integration rules. For example, HTML children of foreignObject enter the HTML namespace automatically; XML requires a declaration.

Set syntax: HtmlSyntax on SVG or MathML props for browser fragment rules. Set syntax: XmlSyntax on HTML props for XHTML-style XML rules. The original source bytes remain available regardless of the selected syntax.

For literals that require canonical foreign spelling, set strict_foreign_spelling: True. In HTML syntax this rejects SVG or MathML names whose authored spelling differs from the constructed name, such as viewbox instead of viewBox, while preserving normal HTML case folding inside integration points. NonCanonicalName reports the authored and expected spellings and the exact name’s UTF-8 byte span. The default permits browser name adjustment; XML syntax keeps its existing case-sensitive rules.

For HTML fragments, context_namespace and context_element select the parent context. A table row fragment uses context_element: "tbody"; SVG children use the SVG namespace and context_element: "svg". The scripting prop defaults to True for browser parsing of noscript; set it to False to model a scripting-disabled document. It does not execute scripts.

3.7.3. Bind typed holes without reparsing§

Template(a) accepts homogeneous hole values of any type a, including an application’s signal or handler wrapper. Roc calls from_interpolation on the fixed List(Str) during compilation. The returned assembler accepts Iter(a) and retains the parsed plan and the values separately.

Holes may occupy character data or an entire quoted attribute value. A hole cannot change an element name, attribute name, namespace declaration, markup delimiter, comment, or raw-text body (including script, style and noscript). It cannot split a character reference or occupy only part of a fixed attribute value. Namespace-switching attributes such as MathML annotation-xml’s encoding must remain fixed.

A template’s original input is its List(Str) of fixed segments. The opaque plan retains those boundaries; plan.metadata() provides its segments, parsed source and typed slots. The source contains the concatenated fixed text. The Roc expressions between segments belong to the compiler, not the markup source representation.

The metadata’s slots identify text nodes or attribute positions by index and contain a typed expected grammar. Fixed values are validated during parsing; a future hole value is not validated merely because its slot is legal. A consumer can validate that value, retain a more specific producer type, or apply its own policy when binding. A homogeneous hole type does not prove that every value is appropriate for every syntactic position.

parse_segments(segments, props) builds a plan explicitly. Use plan.bind(values.iter()) to check a manual binding count. There is one logical binding per segment boundary. Multiple slots can share that binding index when HTML construction copies selected option content into selectedcontent; apply the value to every matching slot. Binding reports the first excess value without exhausting an unbounded iterator. Compiler-provided assemblers capture the values directly. Neither operation parses the values as markup or reparses the fixed source.

The example’s consumer applies bindings to resolved tree positions:

lower : Markup.Tree, U64, Markup.Template.Bound(Str) -> ConsumerNode
lower = |tree, index, bound| {
	bindings = bound.metadata()
	plan = bindings.plan.metadata()
	node = tree.nodes.get(index) ?? { kind: Text(""), attributes: [], children: [], origin: Implied(0) }
	match node.kind {
		Text(text) => {
			slot = plan.slots.find_first(|s| s.target == TextSlot({ node: index }))
			Text(
				match slot {
					Ok(s) => bindings.values.get(s.index) ?? ""
					Err(_) => text
				},
			)
		}
		Element(name) => {
			attrs = node.attributes.map_with_index(
				|attr, attribute| {
					slot = plan.slots.find_first(|s| s.target == AttributeSlot({ node: index, attribute }))
					{
						local: attr.name.local,
						value: match slot {
							Ok(s) => bindings.values.get(s.index) ?? ""
							Err(_) => attr.value
						},
					}
				},
			)
			namespace = match name.namespace {
				Uri(uri) => uri
				NoNamespace => ""
			}
			Element({ namespace, local: name.local, attributes: attrs, children: node.children.map(|child| lower(tree, child, bound)) })
		}
		_ => Text("")
	}
}

3.7.4. Validate and handle errors§

The format APIs return InvalidMarkup(diagnostic). Its location carries source coordinates when available; lines and UTF-8 byte columns are one-based. diagnostic.problem distinguishes syntax, namespace, root, element/attribute vocabulary, content-model, value-grammar, interpolation, binding-count and resource-limit failures. InvalidValue includes its authored value and a typed grammar such as Length, PathData or Keyword(values).

Markup.validate_tree(parsed, props) applies the shared authoring rules to an already parsed tree and checks every concrete value. Template parsing validates its own legal holes before producing an opaque plan.

The implemented checks include:

These checks are not full CSS, JavaScript, URL, or document-wide accessibility validation. Embedded programs and CSS expressions remain opaque languages. Content MathML requires the explicit extension options. The independent review harness records which validation rules each oracle can actually check; browser acceptance alone is not proof of valid authored values.

Strict syntax and validation are the defaults. The options are independent:

PropEffect
recovery: BrowserUse HTML’s repaired tree and retain diagnostics. XML remains well-formedness based.
exact_structure: TrueRequire authored HTML nesting, explicit non-void ends and no structural repairs, before derived projection.
strict_foreign_spelling: TrueRequire canonical authored SVG/MathML names in HTML syntax, independently of structural repairs.
allow_unknown_elements: TruePreserve extension elements.
allow_unknown_attributes: TruePreserve extension attributes.
check_values: FalseRetain values without the implemented attribute-value checks.

Recovery does not disable authoring checks. For example, an unknown element may need both browser recovery and the corresponding extension prop. Normal HTML optional tags are accepted in strict mode when the standard permits them. Use exact authored structure for an additional literal or prerender policy; failures include the causing source span. Checks run on authored content before selected-option projection, so generated copies cannot hide an invalid authored child. For syntax and browser tree construction without authoring checks, use Html.parse_source(source, { mode: Fragment, options: props }); mode defaults to Document.

Resource props bound source bytes, depth, elements, all nodes, attributes per element, diagnostics, slots and parser work. Defaults are 16 MiB of source, depth 1,024, 4,194,304 elements/nodes, 1,024 attributes per element and diagnostics, and 65,536 physical slot targets, including generated copies. Work is bounded relative to input length. Exceeding a limit returns a classified error. XML rejects DTDs and never resolves external data.

3.7.5. Retain source and inspect the tree§

A parsed value’s parsed.source retains original bytes. Its tokens partition those bytes into lexical regions; spelling, quotation marks, character references, line endings, whitespace and malformed content remain retrievable. Markup.Source.from_bytes also retains invalid UTF-8. Lossless source retention is separate from semantic parsing, whose text and attributes are decoded and normalized according to the selected syntax.

The tree contains resolved element names, attribute lists, text, comments, processing instructions and HTML document types. Child indices refer to the same tree’s node list. Authored(span) identifies original bytes; Implied(offset) identifies an insertion point chosen by tree construction. Cloned({ source, at }) identifies a generated copy, retaining its authored source span and the destination insertion point. Use tree.text(index) to join descendant character data.

Keep URI, prefix and local name separately when lowering a tree. Markup.Name validates computed DOM names without changing XML source grammar; Markup.Name supplies namespace-aware vocabulary, attribute and text-behavior facts. Html.RepairContext checks bounded HTML repairs for explicitly named DOM nodes. Markup.Context checks authoring placement for a new child or text value against a retained enclosing context without reparsing or walking ancestors. The consumer-fidelity review checks prefixes, qualified spellings, namespace declarations, binding identity and adoption.

Read original bytes to reproduce authored source exactly. Reconstructing a tree or changing a binding does not promise byte-identical serialization. Source-edit operations and a general HTML serializer are not part of these APIs.

3.7.6. Run independent checks locally§

The deterministic conformance check runs against pinned independent parsers:

blueprint run markup

The repository keeps 82 curated cases and an upstream manifest with a revision and file hashes. Run the full upstream tree and strict-syntax checks locally:

python3 scripts/review_markup.py suite

This downloads verified upstream inputs and generates the 1,967-case corpus in ignored scratch storage. Both scripting modes produce 3,898 executions; cached inputs support subsequent offline runs. Generated test data stays out of Git.

Fuzz campaigns and markup benchmarks are local work, with no scheduled runs:

scripts/fuzz.roc smoke html-raw svg-raw svg-path mathml-raw markup-source markup-structured markup-limits markup-segments
python3 scripts/review_markup.py campaign --smoke
python3 scripts/review_markup.py browser --engines chromium firefox webkit
python3 scripts/bench_markup.py --allocations --check-scaling --compile-literals

For runtime throughput comparisons against Rust, Go and Python, follow the markup runtime comparison. Those parsing measurements exclude compilation and report bytes per second.

Node/npm supply the pinned HTML and browser adapters. Semantic checks use a hash-pinned Nu checker on Java 17 or newer and a pinned MathML schema with lxml. All downloaded oracle packages and generated reports live in ignored scratch storage. Reports distinguish parser disagreements, oracle limitations, crashes, timeouts and infrastructure failures.

3.8. Validate computed DOM names§

Use Markup.Name.from_qualified({ namespace, spelling, context }) when application code computes a namespace-aware element or attribute name. It returns the shared Markup.Name identity { namespace, prefix, local } used by markup trees and Xml.Namespaced.Name, without requiring a known vocabulary entry.

The Element context models createElementNS; Attribute models createAttributeNS and setAttributeNS. An empty namespace URI becomes NoNamespace. The constructor splits at the first colon, validates prefix and local spelling, then checks reserved XML and XMLNS namespace rules. Failures use InvalidDomName(diagnostic), whose problem is InvalidCharacter or InvalidNamespace with the supplied spelling and context or namespace. The location is Unavailable(GeneratedValue) because this name has no source text.

html = Uri("http://www.w3.org/1999/xhtml")

svg = Uri("http://www.w3.org/2000/svg")

computed_svg = Markup.Name.from_qualified({ namespace: svg, spelling: "s:a=b", context: Element })

plain_html : Markup.Name
plain_html = { namespace: html, prefix: Unprefixed, local: "h:div" }

qualified_html = Markup.Name.from_qualified({ namespace: html, spelling: "h:div", context: Element })

plain_href : Markup.Name
plain_href = { namespace: NoNamespace, prefix: Unprefixed, local: "xlink:href" }

qualified_href = Markup.Name.from_qualified({ namespace: Uri("http://www.w3.org/1999/xlink"), spelling: "xlink:href", context: Attribute })

These are the DOM construction rules. They permit some names XML source cannot express: a=b is a valid computed element local name, but <a=b/> is not XML. XML parsers enforce their own source grammar; computed-name validation does not make a name serializable as XML.

3.8.1. Preserve explicit identity§

A Markup.Name can also retain an already-resolved identity directly. For example, an HTML createElement("H:DIV") result has HTML namespace, Unprefixed, and local h:div. Namespace-aware construction of h:div instead produces prefix h and local div. Keep those distinct identities even though .qualified() returns the same spelling.

name.qualified() formats the supplied prefix and local fields. It does not infer a prefix from colons in an unprefixed local name, validate a name, or reconstruct its namespace. Keep all three fields when lowering a name.

An unqualified attribute whose local name is xlink:href differs from an XLink-namespaced attribute with prefix xlink and local href. Update and remove attributes using namespace and local identity. Choose the downstream DOM constructor that preserves those explicit fields and verify its result, as shown in the consumer-fidelity review.

3.9. Query markup facts for a consumer§

Use Markup.Name when compiling a parsed tree into another representation or choosing how to bind an attribute value. The facts share the parser’s vocabulary and preserve the element namespace and the attribute’s expanded identity. Consumers do not need a second table of element, ARIA or handler names.

is_known_element tests the built-in HTML, SVG and MathML vocabulary. name.is_custom_element() checks HTML custom-element names together with the HTML namespace. Unknown names can still be constructed with Markup.Name, and parser extension options remain available independently of these predicates.

Markup.element_names(namespace) supplies the canonical local spellings for that namespace’s built-in authoring vocabulary. Markup.aria_attribute_names supplies known unnamespaced ARIA spellings, including retained deprecated names. Both lists are unique and sorted by ASCII spelling. SVG case is preserved, so a consumer can suggest linearGradient without maintaining a second spelling table:

svg_candidates = Markup.element_names(Uri(Markup.svg_namespace))

suggestion = svg_candidates.find_first(|name| name.with_ascii_lowercased() == "lineargradient") ?? "none"

aria_candidates = Markup.aria_attribute_names

An absent or unknown namespace has an empty element list. Custom and extension names are not finite candidate sets. These lists describe the current supported vocabulary, rather than every name that DOM constructors or browser recovery can retain. Enumeration and membership use the same parser-owned vocabulary.

3.9.1. Attribute values§

element.is_boolean_attribute({ attribute }) identifies HTML presence/absence booleans on their relevant elements. It does not turn SVG or MathML true/false values, or ARIA states, into HTML booleans. Namespaced attributes are distinct from unnamespaced attributes with the same local spelling.

enumerated_values returns known single-value keywords, including shared ARIA states for HTML, SVG and MathML. For example, hidden has empty, hidden and until-found keywords; it is not a presence/absence boolean. NotEnumerated means no keyword fact is provided: numbers, token lists and unknown metadata also return it. The result does not model browser missing-value or invalid-value defaults. Preserve the original value rather than lowercasing it indiscriminately.

attribute_grammar exposes the grammar used by the current parser checks. Consumer metadata can be more detailed than those checks: an attribute whose parser grammar is a generic language can still have boolean or enumerated facts. The coverage manifest describes the parser’s validation boundaries.

3.9.2. HTML text and void behavior§

is_void_element uses the HTML namespace and local name. An SVG element named br is not an HTML void element. element.text_behavior({ scripting }) preserves Data, Rcdata, RawText, ScriptData and Plaintext; it does not collapse script and plaintext into a generic raw-text mode. HTML noscript uses RawText when scripting is enabled and Data otherwise. Foreign content uses Data, and XML source keeps its separate grammar.

3.9.2.1. Initial newlines when emitting HTML§

element.drops_initial_newline() identifies HTML pre, listing and textarea. After their emitted HTML start tag, the parser can consume one immediately following LF character token. This describes HTML parsing, independently of whether an authoring vocabulary accepts a historical name. XML parsing and ordinary DOM text mutation do not have this rule. Merely choosing a textarea fragment context does not process a start tag or activate the rule.

For a serializer producing several bindings, element.initial_newline() seeds an opaque state. Pass actual emitted token events through state.step(token); it returns the next state, adjusted token and an action of Skipped or Preserved. Query .is_pending() when needed. An empty text binding emits no token, so it retains the pending state. A comment consumes the pending position, so the next text’s LF is retained. Nonempty text also consumes it, removing at most one initial LF.

The events contain decoded text after HTML input newline preprocessing and character-reference decoding. A numeric reference to CR is still CR; literal source CR/CRLF has already become LF. In RCDATA, serialized comment spelling is text beginning with <, rather than a comment token, and that character consumes the pending position. Escaping and the element’s text behavior remain separate serializer responsibilities. These facts do not suppress syntax diagnostics; choose recovery independently, as described in the recovery and authored-structure controls.

This executable example protects a desired initial LF under pre and textarea, but emits no extra LF after an actual pre comment:

html_name : Str -> Markup.Name
html_name = |local| { namespace: Uri(Markup.html_namespace), prefix: Unprefixed, local }

# These sample values contain no markup requiring escaping.
# Use the state after the tokens the serializer actually emitted.
protect_text = |state, value| if state.step(Characters(value)).action == Skipped {
	"\n${value}"
} else value

desired = "\nvalue"

pre_text = protect_text((html_name("pre")).initial_newline(), desired)

textarea_text = protect_text((html_name("textarea")).initial_newline(), desired)

after_comment = ((html_name("pre")).initial_newline()).step(Comment)

comment_text = protect_text(after_comment.state, desired)

empty_binding = ((html_name("pre")).initial_newline()).step(Characters(""))

The example also parses the emitted strings and checks that their text values round-trip. The HTML Standard specifies the next-token rule and initial-newline authoring convention.

3.9.3. A consumer policy for handler attributes§

element.is_event_handler({ attribute }) checks the element namespace and the attribute namespace/local name. It recognizes MathML onclick even inside an SVG metadata extension subtree, and SVG oninput. An attribute named online is not a handler. A namespaced attribute with local onclick, or an unnamespaced attribute with local e:onclick, is also distinct from the unnamespaced handler.

Ordinary parsers accept valid handler attributes. A consumer can choose an additional literal policy, as this executable example does:

# This consumer permits extension attributes, but forbids known inline handler attributes.
# It is a consumer policy; ordinary SVG and MathML parsing preserves handlers.
NoHandlers := { parsed : Markup.Parsed }.{
	from_quote : Str -> Try(NoHandlers, [BadQuotedBytes(Str)])
	from_quote = |source| {
		fragment = Svg.Fragment.validate({ source, options: { allow_unknown_attributes: True } }).map_err(quote_error)?
		check(fragment.parsed).map_err(quote_error)
	}

	check : Markup.Parsed -> Try(NoHandlers, [InlineHandler(Parser.Diagnostic({ element : Markup.Name, attribute : Markup.Name }))])
	check = |parsed| {
		for node in parsed.tree.nodes {
			match node.kind {
				Element(element) => {
					for attribute in node.attributes {
						if element.is_event_handler({ attribute: attribute.name }) {
							position = parsed.source.position(attribute.span.start)
							return Err(InlineHandler(Parser.Diagnostic.{ problem: { element, attribute: attribute.name }, location: Text({ offset: attribute.span.start, line: position.line, column: position.column }), path: [Key(attribute.name.qualified())] }))
						}
					}
				}
				_ => {}
			}
		}
		Ok(NoHandlers.{ parsed })
	}
}

quote_error = |failure| Parser.Quotation.error({ format: "NoHandlers", message: Str.inspect(failure) })

The runtime check preserves a typed InlineHandler diagnostic with names, location and logical path. Its quotation hook renders that failure at the compiler boundary. Other consumers keep ordinary parsing and name methods:

consumer : NoHandlers
consumer = "<metadata><extension online='custom' customonline='also custom'/></metadata>"

# A namespaced onclick is a different expanded attribute identity.
namespaced : NoHandlers
namespaced = "<g xmlns:e='urn:extension' e:onclick='data'/>"

These facts describe recognized names, not a promise that every browser fires an event on every element. The local consumer-fidelity review records exact engine differences and verifies the reported MathML and SVG handler cases. The HTML event-handler rules, SVG interfaces and MathML interfaces define the namespace boundaries.

3.10. Check repairs when constructing DOM nodes§

Use Html.RepairContext to check a bounded set of HTML tree-construction repairs before appending an explicitly named DOM element or text node. Its names have already been resolved, as with createElementNS: namespace, prefix and local name stay exactly as supplied. No case folding, namespace inference, name validation or vocabulary check occurs. No-namespace names, extension namespace URIs, prefixes and unknown names are accepted as identities. An HTML-namespace math stays HTML; an explicitly HTML-namespaced child below SVG foreignObject stays HTML.

Html.RepairContext.from_parent({ parent: name }) starts at any explicit enclosing boundary and always returns a context. It assumes no outer ancestors or previous siblings. Keep the context returned by parent_context.child(child_name) beside its child node. Reuse the original parent context for siblings or replacement children. Both operations retain only the immediate parent and finite inherited facts; their work is independent of tree depth. They never walk or retain an ancestor list.

The runnable example demonstrates paragraph closure, explicit table sections, attribute-dependent inputs and extension names:

checks : {} -> Try({}, [InvalidConstruction(Parser.Diagnostic(Html.RepairProblem))])
checks = |_| {
	body = Html.RepairContext.from_parent({ parent: html("body") })
	paragraph = body.child(html("p"))?
	span = paragraph.child(html("span"))?
	match span.child(html("div")) {
		Err(InvalidConstruction({ problem: Repair({ rule: CloseParagraph, .. }), .. })) => {}
		_ => crash "expected paragraph closure"
	}

	# Reuse the parent context for siblings and subsequent replacements.
	other_paragraph = body.child(html("p"))?
	other_paragraph.text("A reactive value")?
	table = body.child(html("table"))?
	match table.child(html("tr")) {
		Err(InvalidConstruction({ problem: Repair({ rule: InsertTableSection, .. }), .. })) => {}
		_ => crash "expected implied tbody"
	}
	section = table.child(html("tbody"))?
	_row = section.child(html("tr"))?

	# A name-only check cannot distinguish hidden and ordinary table inputs.
	match table.child(html("input")) {
		Err(InvalidConstruction({ problem: Unsupported({ reason: AttributeDependentTableInput, .. }), .. })) => {}
		_ => crash "expected attribute-dependent result"
	}

	# DOM names are explicit; no spelling or namespace is inferred.
	custom = { namespace: Uri("urn:widgets"), prefix: Prefix("w"), local: "Panel" }
	widget = body.child(custom)?
	widget.text("Custom text")?
	raw_math = body.child(html("math"))?
	_div = raw_math.child(html("div"))?
	Ok({})
}

3.10.1. Handle the result§

context.child(name) returns the child’s context or InvalidConstruction(diagnostic). context.text(decoded_value) returns an empty record or the same error envelope; it does not create a node or escape text.

diagnostic.problem == Repair(detail) reports a covered repair. detail.parent is the exact immediate parent name, and detail.child is Element(name) or Text. Its rule is one of:

RuleCovered condition
CloseParagraphA block-like start tag closes an open paragraph in button scope.
InsertTableSectionA row directly in a table requires an implied section.
InvalidTableChildA child is moved, ignored or requires an implied table part in the current table insertion mode.
TablePartOutsideTableA table part in ordinary body context is ignored.
NestedAnchorAn active anchor exists after the last formatting marker.
NestedFormAn existing form pointer prevents another form outside template handling.
NestedButtonA button in scope is closed before another button starts.
CloseListItemA new list item closes the previous item before a list-scan boundary.
CloseDescriptionItemA new dd or dt closes a previous description item.
CloseOptionAn option/group or select child implies the current option-related end tag.
CloseHeadingA heading starts with another heading as the immediate parent.
ChildOfVoidAn HTML void element is given an element or any text node, including empty text.
ElementInTextContentAn element child is given to an HTML raw-text, RCDATA or plaintext parent.
FosterTableTextText cannot remain below its intended restricted table parent.

An input in table, section or row insertion mode returns Unsupported({ parent, child, reason: AttributeDependentTableInput }). type=hidden changes tree construction, so its name alone cannot establish a repair. Handle Unsupported separately from Repair.

Unrestricted text returns without scanning the new value. Restricted table parents accept HTML whitespace, including form feed, and scan only that value. A form inserted directly in a table is not pushed onto the parser stack, so subsequent children and nonempty text cannot remain inside that form. The optional scripting field of constructor props defaults to Enabled; it controls noscript text handling and propagates to descendants.

3.10.2. Scope of the check§

A successful check does not certify a complete serialization round trip. The listed repairs operate on a fresh enclosing boundary and finite inherited scope facts. Sibling/insertion history, document and frameset modes, foreign content breakouts and namespace adjustments, ruby rules, nested selects, formatting adoption on end tags and attribute-dependent behavior are outside this bounded check. Templates establish scope and formatting boundaries, but these contexts do not model changing template insertion modes across siblings. Source escaping, initial-newline stripping, null preprocessing and tokenizer end delimiters remain serializer responsibilities.

Use Markup.Context for source-authoring placement, canonical HTML identities and HTML/SVG/MathML content models. Use Markup.Name to validate computed names independently. Use Html.parse_source with browser recovery to inspect complete HTML tree construction, or the format parser or Markup.validate_tree for authoring validation. Html.RepairContext does not replace those checks.

3.11. Check authoring placement before constructing DOM§

Use Markup.Context for canonical source-authoring identities and HTML, SVG or MathML content rules when a renderer constructs nodes directly or changes one reactive child or text value. For bounded HTML repairs on already-resolved DOM names, use Html.RepairContext. A context records the enclosing placement rules; checking a new value needs no source string, parsed tree or ancestor walk. Names and attributes use explicit namespace identities, as in Markup.Tree. Pass canonical names from a parsed tree, or validate computed names with Markup.Name. These queries check placement; vocabulary membership remains available through Markup.Name.

Seed a known enclosing boundary with Markup.Context.from_parent({ parent, attributes, syntax, scripting }). Call context.child({ name, attributes }) for each new element. Keep the returned context beside the child node and use it for that node’s children and later text updates. context.text(decoded_text) checks text placement. Attribute values are decoded strings; a colon in a spelling does not supply a namespace.

The complete runnable example is examples/markup-context.roc. Its name helpers set the HTML, SVG or MathML namespace explicitly:

# Seed a known enclosing boundary once. Keep each returned context beside its
# constructed node so a later reactive update checks only the changed value.
checks : {} -> Try({}, [InvalidPlacement(Parser.Diagnostic(Markup.ContextProblem))])
checks = |_| {
	body = Markup.Context.from_parent({ parent: html_name("body"), attributes: [] })?
	button = body.child({ name: html_name("button"), attributes: [] })?
	label = button.child({ name: html_name("span"), attributes: [] })?
	label.text("Save")?

	# The button restriction survives the intervening span.
	nested_link = label.child({ name: html_name("a"), attributes: [{ name: plain_name("href"), value: "/save" }] })
	if nested_link.is_ok() {
		crash "interactive descendant accepted"
	}

	# An explicit section avoids the implied tbody required by HTML parsing.
	table = body.child({ name: html_name("table"), attributes: [] })?
	if table.child({ name: html_name("tr"), attributes: [] }).is_ok() {
		crash "implicit tbody accepted"
	}
	section = table.child({ name: html_name("tbody"), attributes: [] })?
	row = section.child({ name: html_name("tr"), attributes: [] })?
	cell = row.child({ name: html_name("td"), attributes: [] })?
	cell.text("A reactive value")?

	# SVG and MathML use the same explicit namespace identities as parsed trees.
	drawing = body.child({ name: svg_name("svg"), attributes: [] })?
	group = drawing.child({ name: svg_name("g"), attributes: [] })?
	_circle = group.child({ name: svg_name("circle"), attributes: [] })?
	formula = body.child({ name: math_name("math"), attributes: [] })?
	expression = formula.child({ name: math_name("mrow"), attributes: [] })?
	identifier = expression.child({ name: math_name("mi"), attributes: [] })?
	identifier.text("x")?
	if expression.text("unwrapped text").is_ok() {
		crash "MathML row text accepted"
	}
	Ok({})
}

The inherited button restriction survives the intervening span, so adding an interactive link is rejected. The table example requires an explicit tbody before tr. SVG and MathML contexts apply the same content rules as their validators; an mi permits text while an mrow needs MathML child elements.

3.11.1. Reuse the enclosing boundary§

A seed describes the parent and establishes a boundary for inherited rules. It cannot infer ancestors outside that boundary. Propagate returned contexts from the enclosing boundary instead of seeding each descendant independently. For a replacement child, reuse its parent’s context. If placement-relevant attributes change, derive a new child context before checking its descendants. No existing DOM node is mutated by these queries.

The default syntax is HTML and scripting is enabled, matching the parser’s browser context settings. XML syntax uses namespace-aware content checks without HTML tree-construction repairs. These queries operate on decoded node values; source syntax, escaping and input-stream diagnostics remain parser or serializer responsibilities.

3.11.2. Handle rejection and unsupported contexts§

Failures use InvalidPlacement(diagnostic). Its problem tags InvalidChild and InvalidText identify a placement rejected by the content rules. WouldRepair identifies a browser repair that would change balanced construction. UnexpectedNamespace gives the required namespace. UnsupportedContext explicitly identifies a context that needs history beyond these finite checks. Handle that result rather than treating it as acceptance.

These are checks for one balanced element or text placement. They do not prove whole-parent arity, sibling counts, document completeness, attribute value grammars or source escaping. Use the format’s parser or Markup.validate_tree for complete markup validation. Contexts needing insertion or sibling history, such as templates and framesets, report UnsupportedContext.

3.12. Exact authored HTML structure§

Set exact_structure: True in Html.Props when source nesting must match the constructed tree, for example before compiling a literal into DOM constructors. The default remains normal HTML parsing. This policy is independent of recovery: enabling browser recovery does not permit structural repairs when exact structure is requested.

Html.parse_fragment("<table><tbody><tr><td>x</td></tr></tbody></table>", {
    exact_structure: True,
})

Every non-void element needs an explicit matching end tag. HTML void elements such as br need none; foreign SVG and MathML elements may use self-closing syntax. Implicit elements, implicit closes, ignored tags, reparenting, formatting reconstruction and discarded or duplicated attributes are rejected. Document elements must be authored, including any required html, head and body wrappers. Fragment context nodes are synthetic and do not need to appear in the source.

The diagnostic’s problem uses HtmlStructure with a typed reason and the causing half-open UTF-8 byte span. Its structured location identifies that span’s start. For example, <p><div></div></p> identifies the opening div, while a missing end tag identifies the end of input.

Case folding, character-reference decoding, source CR/CRLF normalization and normal first-newline handling of pre, listing and textarea remain available. To retain non-structural parse diagnostics, combine recovery: Browser with exact_structure: True. For example, <p>&#13;</p> retains decoded CR text and an InvalidCharacterReference diagnostic; strict recovery rejects it. A literal source CR or CRLF becomes LF before reference decoding. Decoded CR references are not normalized again. These policies follow the HTML input preprocessing and numeric reference rules.

Discarded insignificant HTML whitespace is permitted; fostered or moved content is rejected. The authored check runs before selectedcontent projection, so legitimate derived copies and template binding fan-out remain supported. exact_structure permits normal foreign-name adjustments. To reject spelling changes such as viewbox to viewBox, also enable strict_foreign_spelling: True. Its NonCanonicalName diagnostic identifies the exact name span and expected spelling. HTML names inside integration points still use normal HTML case folding. XML syntax already requires explicit nesting and is unaffected by these HTML policies.

3.13. Verify DOM consumer fidelity§

Use the local consumer review when a downstream renderer constructs DOM nodes from parsed markup or binds values into a parsed template. It checks names, namespace declarations, DOM interfaces and adoption into another document.

3.13.1. Run the review§

The review requires Node, the pinned Playwright browsers and the Roc compiler used by the package. Browser dependencies are installed in the ignored oracle cache. Generated probes and reports also stay in ignored local storage.

python3 scripts/review_markup.py \
  --work .roc-parser-tmp/markup/consumer \
  consumer --engines chromium firefox webkit

Read consumer.json in the work directory. A changed namespace, prefix, qualified spelling, namespace declaration or binding target fails the review. Each constructed tree has a prerender_sha256 and fidelity_sha256; adoption must leave the projection and its digest unchanged. These are digests of the consumer tree, including values and attribute order, rather than digests of the original source spelling.

The report separates implementation gaps from mismatches. The pinned Firefox 159.0 browser does not execute an unnamespaced oninput handler on an SVG g; Chromium and WebKit do. That exact engine/version observation is reported as oracle_gap. The negative online attribute probe dispatches line and must not execute. Other handler disagreements fail the review. The report also records auxiliary event and computed-name observations without treating browser acceptance as an authoring rule. A constructor that cannot preserve an explicit name returns consumer_gap and fails the consumer comparison.

3.13.2. Preserve explicit names§

The consumer-fidelity projection carries every name as:

{"namespace":"http://www.w3.org/1999/xhtml","prefix":null,"local":"h:div","qualified":"h:div"}

namespace and prefix are nullable. local is never split on a colon. qualified equals local for a null prefix, or prefix + ":" + local otherwise. An element is ["element", name, attributes, children]; each attribute is [name, value]. Namespace declaration attributes are included. Text, comments, processing instructions and doctypes retain their separate node kinds.

For example, HTML createElement("h:div") produces the name above, while createElementNS("http://www.w3.org/1999/xhtml", "h:div") produces prefix h and local div. Both have the same qualified spelling. A renderer must choose its constructor using the explicit fields and verify the resulting identity. It must not select a namespace or a prefix from a colon in a string.

The reviewed consumer uses namespace constructors for qualified names. Literal colon-containing names with a null namespace use the XML document’s non-namespace constructor and are adopted into the target document. An unprefixed HTML element whose local name contains a colon uses HTML createElement. Every constructor result is checked against all four name fields. Some computed identities have no preserving constructor in a browser: they are reported rather than rewritten or reparsed. For example, Chromium 158 drops the last component from createElementNS(svgNamespace, "p:a:b"), while the reviewed Firefox and WebKit retain local a:b.

Attribute updates and removal use expanded identity: (namespace, local). An unnamespaced attribute with local xlink:href can coexist with an XLink attribute with local href, even though both have qualified spelling xlink:href. Binding updates the existing attribute’s value and preserves its prefix. removeAttributeNS(null, "xlink:href") removes only the literal attribute; removeAttributeNS(xlinkNamespace, "href") removes only the XLink attribute.

3.13.3. Interpret the two projections§

The WPT/tree-construction projection compares namespace URI, local name, values and tree shape using the fixture format’s normalization. It is suitable for those parser tests. The separate consumer projection retains prefixes, qualified spellings and all xmlns attributes, so it distinguishes h:div from q:div, a:href from b:href, and changed namespace bindings.

The consumer constructs nodes from the Roc tree and applies typed bindings as text or attribute values. It never reparses source or binding text. XML fragment references inherit their namespace from a wrapper whose own declarations are excluded from the fragment result. Browser parsing, DOM construction and selected-content projection remain separate checks; a browser lacking selected-content cloning is recorded as that specific oracle gap.

3.14. Read and write JSON§

This chapter shows how to read a JSON document into a value whose shape you do not have to know in advance, walk it, report mistakes with a line and column, choose how strict to be, and write a value back out. It is for Roc programmers who have added parser to their app’s dependencies and need to handle JSON that a record type cannot describe: software bills of materials, JSON Schema files, configuration with comments, or data that must pass through unchanged.

By default the parser is strict: it accepts only the JSON of RFC 8259, and also rejects repeated keys and nesting deeper than 128 levels. Options relax it for common variants, such as comments, trailing commas and a byte order mark, and add checks, such as size limits and I-JSON numbers. It reads complete documents held in memory. For NDJSON or large top-level arrays, use chunked record reading and writing.

The complete-document examples on this page come from docs/examples/json-guide.roc, which CI checks and runs, and imports parser.JsonValue.

3.14.1. The builtin Json or JsonValue§

Roc auto-imports a builtin module named Json. It decodes a document straight into a record, list or other type that you name in an annotation, in the way Choosing how to read your input describes. Use it when you know the shape of the document and a Roc type can hold all of it.

Use JsonValue when a record type cannot describe the document, or would lose part of it:

The module is called JsonValue because the builtin already has the name Json: import parser.Json would leave the alias unbound. Its error tag is InvalidJsonValue for the same reason. The builtin reports invalid input as InvalidJson with a Str, and one tag cannot carry two payload types, so the different name lets one function use ? on both modules.

3.14.2. Parse a document and walk it§

JsonValue.parse_str reads a whole document and returns a JsonValue, or InvalidJsonValue with the position of the first problem. A JsonValue is one of:

JsonValue := [
    Null,
    Bool(Bool),
    Number(Str),
    String(Str),
    Array(List(JsonValue)),
    Object(List({ key : Str, value : JsonValue })),
]

An Object keeps its members in source order. A Number holds the number’s text exactly as written; Numbers keep their text explains why and how to convert it. JsonValue has methods for the common lookups:

This CycloneDX bill of materials gives each component’s licence either as an SPDX id or as an expression, and names its components with a bom-ref key that no Roc record field could have:

sbom_text =
	\\{
	\\  "bomFormat": "CycloneDX",
	\\  "specVersion": "1.6",
	\\  "components": [
	\\    {
	\\      "bom-ref": "pkg:roc/parser@2.1.0",
	\\      "name": "roc-parser",
	\\      "version": "2.1.0",
	\\      "licenses": [{ "license": { "id": "UPL-1.0" } }]
	\\    },
	\\    {
	\\      "bom-ref": "pkg:roc/unicode@4.3.0",
	\\      "name": "unicode",
	\\      "version": "4.3.0",
	\\      "licenses": [{ "expression": "UPL-1.0 OR MIT" }]
	\\    }
	\\  ]
	\\}

These functions list the components with their licences, trying both shapes:

## A component's licence: an SPDX id, or an SPDX expression.
licence : JsonValue -> Str
licence = |component| {
	match component.get_path(["licenses", "0", "license", "id"]) {
		Ok(String(id)) => id
		_ =>
			match component.get_path(["licenses", "0", "expression"]) {
				Ok(String(expression)) => expression
				_ => "no licence"
			}
	}
}

## One line per component of a CycloneDX document.
components : Str -> Try(List(Str), [InvalidJsonValue(JsonValue.Error), Missing, WrongType])
components = |text| {
	bom = JsonValue.parse_str(text, {})?
	found = bom.get("components")?
	list = found.as_list()?
	Ok(
		list.map(
			|component| {
				name = component.get("name") ?? Null
				version = component.get("version") ?? Null
				"${name.as_str() ?? "?"} ${version.as_str() ?? "?"}: ${licence(component)}"
			},
		),
	)
}

print_components!(sbom_text)?
Stdout.line!(Str.inspect(JsonValue.parse_str(sbom_text, {}).map_ok(JsonValue.keys)))?
Output
roc-parser 2.1.0: UPL-1.0
unicode 4.3.0: UPL-1.0 OR MIT
Ok(["bomFormat", "specVersion", "components"])

get_path uses each key as it is written, so a key that contains /, . or ~ needs no escaping. A segment applied to an array must be a decimal index without a sign or leading zeros, as in a JSON Pointer.

For anything the methods do not cover, match on the variants. This function collects every string in a document with the keys and indexes that lead to it:

## Every string in a value, with the keys and indexes that lead to it.
strings : JsonValue, Str -> List(Str)
strings = |value, path| {
	match value {
		String(text) => ["${path} = ${text}"]
		Array(items) => {
			var $found = []
			var $index = 0.U64
			for item in items {
				$found = $found.concat(strings(item, "${path}/${$index.to_str()}"))
				$index = $index + 1
			}
			$found
		}

		Object(members) => {
			var $found = []
			for member in members {
				$found = $found.concat(strings(member.value, "${path}/${member.key}"))
			}
			$found
		}

		Null | Bool(_) | Number(_) => []
	}
}

Keeping only the lines that mention bom-ref prints:

Output
/components/0/bom-ref = pkg:roc/parser@2.1.0
/components/1/bom-ref = pkg:roc/unicode@4.3.0

3.14.3. Show where an error is§

InvalidJsonValue carries a JsonValue.Error diagnostic with problem, location, and path. Parsed text has location: Text({ offset, line, column }): a zero-based original byte offset and one-based line and column. Columns count bytes from the start of the line, and a line ends at LF, CR or CRLF. The position is the start of the problem: the first character of a bad number or literal, the comma before a closing bracket, the slash of a comment, or the opening quote of a repeated key.

Match JsonValue.Problem tags for program decisions. DuplicateKey(key) and ConflictingDuplicateKey(key) carry the decoded key. LimitExceeded({ resource, max, observed }) identifies the typed resource, its configured maximum, and an exact count or lower bound. NonInteroperableNumber({ text, reason }) preserves the exact numeric lexeme and distinguishes an unsafe integer, excessive significant digits, non-finite value, double overflow or double underflow. Str.inspect(problem) provides a diagnostic for logs and command-line tools.

report : Str -> Str
report = |text| {
	match JsonValue.parse_str(text, {}) {
		Ok(_) => "valid"
		Err(InvalidJsonValue(error)) => "${source_position(error.location).line.to_str()}:${source_position(error.location).column.to_str()}: ${Str.inspect(error.problem)}"
	}
}

Stdout.line!(report("{\n  \"name\": \"web\",\n  \"port\": 08080\n}"))?
Stdout.line!(report("[\"café\", tru]"))?
Stdout.line!(report("{\"name\": 'web'}"))?
Stdout.line!(report("[1, 2,]"))?
Stdout.line!(report("// settings\n{}"))?
Stdout.line!(report("{\"a\": 1} {\"b\": 2}"))?
Stdout.line!(report("\"\\ud800\""))?
Output
3:11: LeadingZero
1:11: InvalidLiteral
1:10: ExpectedValue
1:6: TrailingComma
1:1: CommentsNotAllowed
1:10: UnexpectedContent
1:2: UnpairedSurrogate

The second error is at column 11 although tru is the tenth character, because é is two bytes in UTF-8. JSON has no single-quoted strings and no comments, and a document holds exactly one value, so text after the first value is an error rather than a second document; Combine with other parsers shows how to read several values. The last input is a \u escape for half of a UTF-16 surrogate pair, with no other half. RFC 8259 leaves such escapes to the implementation. A Roc Str cannot hold one, so JsonValue rejects it unless you ask for U+FFFD instead (see Choose how strict to be).

3.14.4. Numbers keep their text§

JSON does not limit the size or precision of numbers, and parsers disagree about what 18446744073709551616 or 0.1000000000000000055511151231257827 means: many turn every number into a 64-bit float and lose digits. JsonValue does not decide for you. A Number holds the text from the input, so a number read and written back keeps every digit, its sign and the spelling of its exponent. Convert it when you need arithmetic:

## How each accessor reads a number's text.
convert : JsonValue -> Str
convert = |value| {
	text =
		match value {
			Number(lexeme) => lexeme
			_ => "not a number"
		}
	"${text}: as_i64 ${Str.inspect(value.as_i64())}, as_u64 ${Str.inspect(value.as_u64())}, as_dec ${Str.inspect(value.as_dec())}"
}

for text in ["42", "-0", "1.50", "1e3", "18446744073709551616", "1e400"] {
	Stdout.line!(convert(JsonValue.parse_str(text, {}) ?? Null))?
}
Stdout.line!(Str.inspect(JsonValue.parse_str("1.50", {}) == JsonValue.parse_str("1.5", {})))?
Stdout.line!(Str.inspect(F64.from_str("1.50")))?
Output
42: as_i64 Ok(42), as_u64 Ok(42), as_dec Ok(42.0)
-0: as_i64 Ok(0), as_u64 Ok(0), as_dec Ok(0.0)
1.50: as_i64 Err(WrongType), as_u64 Err(WrongType), as_dec Ok(1.5)
1e3: as_i64 Err(WrongType), as_u64 Err(WrongType), as_dec Ok(1000.0)
18446744073709551616: as_i64 Err(OutOfRange), as_u64 Err(OutOfRange), as_dec Ok(18446744073709551616.0)
1e400: as_i64 Err(WrongType), as_u64 Err(WrongType), as_dec Err(OutOfRange)
False
Ok(1.5)

Because values compare by their text, 1.50 and 1.5 are different JsonValue values, as the last line but one shows. Convert both with as_dec to compare them as numbers.

3.14.5. Repeated keys§

RFC 8259 says that the names within an object should be unique, and leaves open what a repeated name means. Readers differ: most keep the last value, some keep the first, and some reject the document. Two programs can then read different data from the same file, so by default JsonValue rejects a repeated key. Keys are compared after their escapes are decoded, so "extension" and "\u0065xtension" are the same key.

The duplicates option chooses what a repeated key means:

Reject

The default. Any repeated key is an error, reported at the repeated key.

CollapseIdentical

Keep one member when every copy has an equal value, and report an error when the values differ. Values are compared as JsonValue values: numbers by their text and objects member by member in order, so 1 and 1.0, or the same members in another order, differ.

KeepFirst

Keep the first member and ignore later copies.

KeepLast

Keep the first member’s position with the last copy’s value, as JavaScript’s JSON.parse and Python’s json.loads do.

## Read `text` with each duplicate mode, and write what each one kept.
modes : Str -> List(Str)
modes = |text| {
	options : List((Str, JsonValue.Props))
	options = [
		("Reject", JsonValue.Props.{}),
		("CollapseIdentical", JsonValue.Props.{ duplicates: CollapseIdentical }),
		("KeepFirst", JsonValue.Props.{ duplicates: KeepFirst }),
		("KeepLast", JsonValue.Props.{ duplicates: KeepLast }),
	]
	options.map(
		|(name, chosen)| {
			match JsonValue.parse_str(text, chosen) {
				Ok(value) => "${name}: ${JsonValue.encode(value, {}).map_ok(|bytes| Str.from_utf8(bytes) ?? crash "JSON encoding produced invalid UTF-8") ?? "?"}"
				Err(InvalidJsonValue(error)) => "${name}: ${source_position(error.location).line.to_str()}:${source_position(error.location).column.to_str()}: ${Str.inspect(error.problem)}"
			}
		},
	)
}

print_lines!(modes("{\"id\": 1, \"name\": \"web\", \"id\": 2}"))?
print_lines!(modes("{\"extension\": {\"a\": 1}, \"\\u0065xtension\": {\"a\": 1}}"))?
Output
Reject: 1:26: DuplicateKey("id")
CollapseIdentical: 1:26: ConflictingDuplicateKey("id")
KeepFirst: {"id":1,"name":"web"}
KeepLast: {"id":2,"name":"web"}
Reject: 1:25: DuplicateKey("extension")
CollapseIdentical: {"extension":{"a":1}}
KeepFirst: {"extension":{"a":1}}
KeepLast: {"extension":{"a":1}}

CollapseIdentical exists for documents that repeat a key by mistake without changing its meaning. The published SPDX 3.0.1 JSON schema is one: it defines extension and prop_Element_extension twice in the same object, with equal values each time. Strict parsing rejects it; CollapseIdentical reads it and would still report a copy with a different value. The conformance review reads the pinned schema this way (see JSON).

3.14.6. Choose how strict to be§

JsonValue.parse_str(text, props) takes a JsonValue.Props record whose fields all have defaults, so you pass only the fields you change. parse_str(text, {}) defaults to strict RFC 8259, no repeated keys, and at most 128 levels of nesting. A limit or check that fails is reported as an InvalidJsonValue error at the offending value, like any other error.

Table 1. Limits
FieldDefaultEffect
max_bytesno limitThe most bytes in the supplied input, including surrounding whitespace and any BOM. Checked before constructing a value. The parser combinator applies this limit to its entire supplied buffer, including any unconsumed suffix.
max_depth128The most arrays and objects nested inside each other. A document that is a single number or string has depth 0.
max_itemsno limitThe most items in one array or members in one object, counted as written, before repeated keys are handled.
max_string_bytesno limitThe most UTF-8 bytes in one string or key, after its escapes are decoded.
max_number_digitsno limitThe most characters in one number, counting the sign, the decimal point and the exponent.
Table 2. Relaxations
FieldDefaultEffect
commentsFalseAccept // line comments and /* */ block comments wherever whitespace may appear. Comments are not kept.
trailing_commasFalseAccept a comma after the last item of an array or the last member of an object.
bomFalseSkip a byte order mark (U+FEFF) at the very start of the document.
non_finiteFalseAccept NaN, Infinity and -Infinity, kept as Number text.
control_charactersFalseAccept unescaped characters U+0000 to U+001F, such as a tab, inside strings and keys.
lone_surrogatesRejectWith Replace, a \u escape for half of a surrogate pair becomes U+FFFD instead of an error. A complete pair is always decoded.
Table 3. Checks
FieldDefaultEffect
duplicatesRejectWhat a repeated key means; see Repeated keys.
top_levelAnyObjectOrArray or Object requires the document to be that shape.
interoperable_numbersFalseRefuse numbers that an IEEE 754 double cannot hold, as I-JSON (RFC 7493) requires: integers whose magnitude exceeds 2^53 - 1, more than 17 significant digits, and values that overflow or that underflow to zero.

The parser keeps its nesting on an explicit stack, so a higher max_depth cannot overflow the call stack while parsing. Other operations do recurse over a value: comparing values with == or CollapseIdentical, hashing, and Str.inspect. The writer refuses more than 128 levels. Raise the limit only as far as your documents need.

3.14.6.1. Read JSON with comments§

Configuration files such as tsconfig.json and editor settings use JSON with comments and trailing commas, often called JSONC:

tsconfig_text =
	\\{
	\\  // Compiler options for the example project.
	\\  "compilerOptions": {
	\\    "target": "ES2022", /* modern output */
	\\    "strict": true,
	\\  },
	\\  "include": ["src/**/*.ts",],
	\\}

## JSON with comments, as in `tsconfig.json` and editor settings.
read_jsonc : Str -> Try(JsonValue, [InvalidJsonValue(JsonValue.Error)])
read_jsonc = |text| JsonValue.parse_str(text, { comments: True, trailing_commas: True })

Reading tsconfig_text with read_jsonc, and then strictly with parse_str, and writing the first result with encode({}):

Output
{"compilerOptions":{"target":"ES2022","strict":true},"include":["src/**/*.ts"]}
2:3: CommentsNotAllowed

/* inside a string, as in "src/**/*.ts", is text, not a comment. An unterminated block comment is reported where it opens. These two options do not make the parser read JSON5; see What is not supported.

3.14.6.2. Read files with a byte order mark§

Some Windows editors start a UTF-8 file with a byte order mark. parse_str rejects it, at line 1, column 1. bom: True skips it, and only at the start of the document. This reader also requires the document to be an object, as a configuration file must be:

## A configuration file that may start with a byte order mark, as files
## saved by some Windows editors do, and must hold an object.
read_config : Str -> Try(JsonValue, [InvalidJsonValue(JsonValue.Error)])
read_config = |text| JsonValue.parse_str(text, { bom: True, top_level: Object })

show!(read_config("\u(FEFF){\"port\": 8080}"))?
show!(JsonValue.parse_str("\u(FEFF){\"port\": 8080}", {}))?
show!(read_config("[8080]"))?
Output
{"port":8080}
1:1: BomNotAllowed
1:1: TopLevelRequired(Object)

3.14.6.3. Read NaN and Infinity§

Python’s json.dumps writes NaN, Infinity and -Infinity for floats that JSON cannot express, unless it is told not to. non_finite: True reads them as Number text. They are not numbers that as_dec, as_i64 or as_u64 can convert, and the writer refuses them, so what encode writes is always strict JSON:

## Read metrics written by Python's `json.dumps`, which writes `NaN` and
## `Infinity` for floats that JSON cannot express.
print_loss! : Str => Try({}, _)
print_loss! = |text| {
	match JsonValue.parse_str(text, { non_finite: True }) {
		Ok(metrics) => {
			loss = metrics.get("loss") ?? Null
			Stdout.line!(Str.inspect(loss))?
			Stdout.line!(Str.inspect(loss.as_dec()))?
			Stdout.line!(Str.inspect(JsonValue.encode(metrics, {}).map_ok(|bytes| Str.from_utf8(bytes) ?? crash "JSON encoding produced invalid UTF-8")))?
		}

		Err(_) => Stdout.line!("invalid JSON")?
	}
	Ok({})
}

For {"loss": NaN, "best": -Infinity} this prints:

Output
Number("NaN")
Err(OutOfRange)
Err(InvalidJsonValue({ location: Unavailable(GeneratedValue), path: [], problem: InvalidNumber({ bytes: [78, 97, 78], total_bytes: 3 }) }))

3.14.6.4. Limit untrusted input§

A document from the network can be built to make a reader do too much work or hold too much memory. Set limits that suit your documents, require the top-level shape you expect, and keep repeated keys rejected:

## Options for documents from an untrusted source.
untrusted : JsonValue.Props
untrusted = JsonValue.Props.{
	max_depth: 32,
	max_items: 1000,
	max_string_bytes: 4096,
	max_number_digits: 40,
	top_level: Object,
	interoperable_numbers: True,
}

show!(JsonValue.parse_str("{\"ids\": [1, 2, 3]}", untrusted))?
show!(JsonValue.parse_str("{\"id\": 9007199254740993}", untrusted))?
show!(JsonValue.parse_str("{\"a\": \"${Str.repeat("x", 5000)}\"}", untrusted))?
show!(JsonValue.parse_str("[1, 2, 3]", untrusted))?
Output
{"ids":[1,2,3]}
1:8: NonInteroperableNumber({ reason: UnsafeInteger, text: "9007199254740993" })
1:7: LimitExceeded({ max: 4096, observed: AtLeast(4097), resource: StringBytes })
1:1: TopLevelRequired(Object)

9007199254740993 is a valid JSON number, but a JavaScript program would read it as 9007199254740992; interoperable_numbers refuses it so that every reader agrees. The options do not limit the size of the whole document: check Str.count_utf8_bytes before parsing if you need to.

3.14.7. Write JSON§

value.encode(props) returns UTF-8 bytes. Its JsonValue.WriteProps fields default to compact JSON:

Set max_bytes in WriteProps to bound the exact UTF-8 output size, including escaped strings, keys and indentation. It defaults to no limit. The writer checks the size before allocating its output and returns InvalidJsonValue with LimitExceeded({ resource: DocumentBytes, max, observed }) if the budget would be exceeded. It adds no final newline: reserve one byte from your budget if the calling protocol appends one.

expect JsonValue.parse_str("null", { max_bytes: 4 }) == Ok(Null)
expect JsonValue.encode(String("é"), { max_bytes: 4 }).map_ok(|bytes| Str.from_utf8(bytes) ?? crash "JSON encoding produced invalid UTF-8") == Ok("\"é\"")
expect JsonValue.encode(String("é"), { max_bytes: 3 }).map_ok(|bytes| Str.from_utf8(bytes) ?? crash "JSON encoding produced invalid UTF-8") == Err(InvalidJsonValue(Parser.Diagnostic.{ problem: LimitExceeded({ resource: DocumentBytes, max: 3, observed: AtLeast(4) }), location: Unavailable(GeneratedValue), path: [] }))

For example, a downstream SBOM tool can use a 16 MiB budget for both parsing and writing while keeping its own depth and component limits. Number text is preserved, so large identifiers and precise decimal values do not pass through floating point.

Numbers are written as their text. In strings, only the quote, the backslash and control characters are escaped: \b, \f, \n, \r and \t for those characters and \u00XX (lowercase) for the rest. Other characters, including / and non-ASCII text, are written as they are. The output is byte for byte what Python’s json.dumps(value, ensure_ascii=False, sort_keys=..., indent=...) writes, with the separators (",", ":") when compact and (",", ": ") when indented, for the same number text; the conformance review checks this.

release : JsonValue
release = Object([
	{ key: "name", value: String("roc-parser \"JSON\" café") },
	{ key: "version", value: String("2.2.0") },
	{ key: "sizes", value: Array([Number("1.50"), Number("2E3")]) },
	{ key: "draft", value: Bool(False) },
	{ key: "assets", value: Object([]) },
])

Stdout.line!(JsonValue.encode(release, {}).map_ok(|bytes| Str.from_utf8(bytes) ?? crash "JSON encoding produced invalid UTF-8") ?? "not writable")?
Stdout.line!(JsonValue.encode(release, { indent: 2, sort_keys: True }).map_ok(|bytes| Str.from_utf8(bytes) ?? crash "JSON encoding produced invalid UTF-8") ?? "not writable")?
Stdout.line!(Str.inspect(JsonValue.encode(Array([Number("0x10")]), {}).map_ok(|bytes| Str.from_utf8(bytes) ?? crash "JSON encoding produced invalid UTF-8")))?
Stdout.line!(Str.inspect(JsonValue.encode(Object([{ key: "a", value: Null }, { key: "a", value: Null }]), {}).map_ok(|bytes| Str.from_utf8(bytes) ?? crash "JSON encoding produced invalid UTF-8")))?
Output
{"name":"roc-parser \"JSON\" café","version":"2.2.0","sizes":[1.50,2E3],"draft":false,"assets":{}}
{
  "assets": {},
  "draft": false,
  "name": "roc-parser \"JSON\" café",
  "sizes": [
    1.50,
    2E3
  ],
  "version": "2.2.0"
}
Err(InvalidJsonValue({ location: Unavailable(GeneratedValue), path: [], problem: InvalidNumber({ bytes: [48, 120, 49, 48], total_bytes: 4 }) }))
Err(InvalidJsonValue({ location: Unavailable(GeneratedValue), path: [], problem: DuplicateKey("a") }))

A value built in your code can hold something that JSON cannot express, and the writer refuses it rather than write text that would read back as something else. All writer problems are carried by InvalidJsonValue, with location: Unavailable(GeneratedValue) in the shared diagnostic envelope. Invalid number excerpts own at most 40 bytes and retain the original byte length.

InvalidNumber(excerpt)

A Number whose text is not a JSON number, such as 0x10, +1, 1. or text with spaces around it, including NaN and Infinity.

DuplicateKey(key)

A key repeated in one object.

LimitExceeded({ resource: Nesting, max, observed })

More than the supported nesting depth.

InvalidJsonValue with LimitExceeded({ resource: DocumentBytes, max, observed })

The serialized output would exceed the byte budget.

So what the writer returns always reads back with parse_str as the value you wrote, or, with sort_keys, as its sorted copy. It does not keep the input’s layout: comments, whitespace and the spelling of escapes are gone, so "\u00e9" is written "é".

3.14.8. Check JSON during compilation§

Annotate a string literal with the JsonValue type, and the compiler calls JsonValue.from_quote on it while compiling your program. The literal must be strict JSON; anything else is a compiler error, and the constant needs no parsing at run time:

lock : JsonValue
lock =
	\\{"name": "app",
	\\ "checksum": 340282366920938463463374607431768211455,
	\\ "greeting": "caf\\u00e9 \\ud83d\\ude00"}

In a Roc string literal a backslash starts a Roc escape, in multi-line strings too, so write each backslash of a JSON escape twice: JSON’s \u00e9 is \\u00e9, and \n is \\n. A single \u00e9 is a compiler error, because Roc’s own escape is written \u(E9); a single \n puts a real line break inside the JSON string, which strict JSON rejects.

For a file, import its text as a Str and require a checked conversion in a top-level declaration, as Parse and validate during compilation describes:

import "assets/json-guide-settings.jsonc" as settings_text : Str

settings : JsonValue
settings = Parser.Quotation.require(
	JsonValue.validate({
		source: settings_text,
		name: "json-guide-settings.jsonc",
		options: { comments: True, trailing_commas: True },
	}),
)

JsonValue.validate takes source, a name used in errors (default "<literal>"), and options, the same JsonValue.Props that parse_str takes (default {}). JSON has no separate lint checks: its extra checks are these options. There is no validate_as; to decode JSON into a record, use the builtin Json.

Stdout.line!(Str.inspect(lock.get("checksum")))?
# Display the non-BMP scalar as an escape while keeping its decoded value.
Stdout.line!(Str.inspect(lock.get("greeting")).replace_each("\u(1F600)", "\\u(1F600)"))?
Stdout.line!(Str.inspect(settings.get_path(["files.exclude", "**/.roc-parser-tmp"])))?
Stdout.line!(Str.inspect(JsonValue.validate({ source: "{\"a\": [1,]}", name: "list.json" })))?
Output
Ok(Number("340282366920938463463374607431768211455"))
Ok(String("café \u(1F600)"))
Ok(Bool(True))
Err(InvalidJsonValue({ location: Text({ column: 9, line: 1, offset: 8 }), path: [Source("list.json")], problem: TrailingComma }))

The last line shows a runtime validation failure, including its source name and text position. Compiler quotations render the same structured problem as text.

3.14.9. Combine with other parsers§

JsonValue.parser(props) is a Parser(Parser.Utf8.Bytes, JsonValue, JsonValue.Problem) for use with the combinators. It reads the whitespace before a value, one strict JSON value, and the whitespace after it, and leaves the rest of the input for the next parser. JsonValue.parser(props) takes the same options as parse_str. Failures are ParseError({ problem, location, path }), with the same structured problem as parse_str and the byte offset of the problem.

JSON Lines (newline-delimited JSON) logs hold one value per line, which Parser.many reads:

## Every value of a JSON Lines (newline-delimited JSON) log.
json_lines : Str -> Try(List(JsonValue), [ParseError(Parser.Diagnostic(Parser.Problem(JsonValue.Problem)))])
json_lines = |text| (Parser.many(JsonValue.parser({}))).parse_str(text)

Stdout.line!(Str.inspect(json_lines("{\"event\": \"start\"}\n{\"event\": \"stop\"}\n")))?
Stdout.line!(Str.inspect(json_lines("{\"event\": \"start\"}\n{\"event\": }\n")))?
Output
Ok([Object([{ key: "event", value: String("start") }]), Object([{ key: "event", value: String("stop") }])])
Err(ParseError({ location: Text({ column: 11, line: 2, offset: 29 }), path: [], problem: ExpectedValue }))

Because each value takes the whitespace after it, this reader also accepts several values on one line; it does not check that each value has a line of its own.

3.14.10. Read and write records in chunks§

Use JsonValue.record_decoder(props) when NDJSON logs or a top-level JSON array are too large to hold in memory. Call reader.read_iter(chunks) for an Iter(List(U8)), or reader.read_stream(chunks) for a Stream(Try(List(U8), source_error)). Both produce Value(JsonValue) events and a final Finished(report) inside Try. Chunk boundaries may split UTF-8, escapes, numbers or nested values. Empty chunks do not signal EOF. Consume through Finished to validate the array’s closing bracket and any trailing input.

The default framing: Ndjson reads one complete value per LF-delimited line, accepts CRLF, ignores lines containing only JSON whitespace, and accepts a final value without LF. Each line must contain exactly one value. With framing: Array, each item of the outer array becomes a value; nested objects and arrays within an item are collected before it is emitted. An empty array yields only Finished. Arbitrary top-level objects do not have a chunked event interface.

The json field takes JsonValue.Props for each emitted value. Thus max_depth excludes the outer array wrapper, max_items limits containers inside each item rather than the number of streamed items, and top_level checks each item. Array wrapper comments, a leading BOM, and a trailing comma follow the corresponding JSON options; a BOM is accepted only at the start of the array document. For NDJSON, each nonblank line is a separate JSON document and uses those options independently.

max_record_bytes defaults to 4 MiB and bounds the encoded unfinished value before parsing it. Readers retain one record and the current source chunk; retaining emitted values adds application memory. A returned string or number may share its record’s bytes, but does not retain preceding records. Use a pull loop to process and discard values instead of collecting the whole stream.

Configure the shared error policy with errors: { policy: ReportInvalidRecords } in the reader props. NDJSON can report or skip a rejected line because its framing is independent. Array syntax errors, invalid encoding, resource limits and source failures are terminal. Diagnostics identify the record and its original one-based line and byte column. Source failures remain SourceErr and never produce Finished.

Create a writer with JsonValue.record_encoder({ chunk_bytes, ..props }). Call writer.write_iter(values) or writer.write_stream(source). Writers emit compact NDJSON by default; framing: Array writes an enclosing array. Writers validate each complete value before its first bytes, retain one encoded record, and emit chunks no larger than chunk_bytes. Their max_record_bytes bounds encoded JSON for one value, excluding the single LF, comma or bracket framing byte. It is checked before rendering. sort_keys: True sorts object members. A source or encoding failure can leave earlier records written; an array is closed only after successful source exhaustion.

The complete json-streaming.roc example reads NDJSON from stdin and writes one compact JSON array to stdout. It connects reader.values_stream(source) to writer.write_stream(values), so rejected records and missing completion remain errors.

3.14.11. What is not supported§

These limits are by design:

Document events

JsonValue reads a complete document and returns a tree; it has no event or token interface. JsonValue.record_decoder incrementally emits NDJSON records or top-level array items, but collects each item as a tree. Numbers and unescaped strings share their input memory, so complete-document values can keep the whole document alive and streamed values can keep their record alive.

JSON5

Only the relaxations listed in Choose how strict to be are available. Single-quoted strings, unquoted keys, hexadecimal numbers, leading or trailing decimal points, a leading ``, and line breaks escaped inside strings are always errors.

Type-directed decoding

There is no JsonValue.decode or validate_as. To decode JSON into your own record type, use the builtin Json.

Other encodings

The input is UTF-8. JsonValue.parser(props), which takes bytes, rejects a value that contains bytes that are not UTF-8. Convert UTF-16 and UTF-32 documents first.

Queries and schemas

get_path takes a list of keys and indexes, not a JSON Pointer string, and there is no JSONPath or JSON Schema validation.

Preserved layout

Comments, whitespace and the spelling of escapes are dropped, and the writer has no option to escape non-ASCII text.

For the full module interface, see the JsonValue API reference.

3.14.12. Performance§

The parser scans the input bytes once, keeping its nesting on an explicit stack as the XML parser does, so deep nesting cannot overflow the call stack. Repeated keys are found by comparing keys in objects of up to 16 members and with a hash table in larger ones. The module’s tests parse an object with 20,000 members and an array of 100,000 numbers, and the fuzz targets run with a time limit per input, to catch quadratic behaviour. Number text, and strings without escapes, are slices of the input string rather than copies. The benchmark harness compares JsonValue.parse_str with serde_json, Go’s encoding/json and Python’s json; see Benchmarks and Compare equivalent work.

3.15. Markdown§

This chapter shows how to turn Markdown text into a tree of Roc values and how to walk that tree to produce your own output, such as HTML or a table of contents. It is a set of how-to guides for readers who already know basic Roc and have added roc-parser to an app. By the end you can parse a document, read its blocks and inline content, pull out YAML frontmatter, and write a renderer.

The Markdown module follows CommonMark 0.31.2 plus the GitHub Flavored Markdown (GFM) extensions for tables, task list items, strikethrough and extended autolinks. It also reads a leading --- frontmatter block.

3.15.1. Parse a document into blocks§

Parse a whole document with Markdown.parse_str(source, props). It returns a List(Markdown): one value per top-level block, in document order.

Markdown has no syntax errors. Every input is a valid document, so Markdown.parse_str returns the blocks directly rather than a Try; text that looks like broken syntax becomes ordinary paragraph text. To use the document parser inside your own combinators, use Markdown.parser(props), a Parser(Parser.Utf8.Bytes, List(Markdown), []). Its receiver runners share the common parser diagnostic envelope.

The following app parses a document that uses each kind of block and prints each block with Str.inspect, which shows what the parser produced.

source =
	\\# Shopping
	\\
	\\Buy these *today*:
	\\
	\\- [x] apples
	\\- [ ] pears
	\\
	\\> Quoted
	\\
	\\```roc
	\\main = 1
	\\```
	\\
	\\| Item | Qty |
	\\| :--- | --: |
	\\| pear | 2 |
	\\
	\\***
	\\
	\\<div>raw</div>

main! = |_args| {
	blocks = Markdown.parse_str(source, {})
	for block in blocks {
		Stdout.line!(Str.inspect(block))?
	}
	loose_list!()
}

A list in which no items are separated by blank lines is tight; otherwise it is loose. The loose field records this, which matters when you render HTML: a tight list normally shows its item paragraphs without <p> tags. Ordered lists keep their starting number:

loose_list! = || {
	blocks = Markdown.parse_str("3. first\n\n4. second\n", {})
	for block in blocks {
		Stdout.line!(Str.inspect(block))?
	}
	Ok({})
}

Output:

Heading({ level: One, content: [Text("Shopping")] })
Paragraph([Text("Buy these "), Emphasis([Text("today")]), Text(":")])
ListBlock({ kind: Unordered, loose: False, items: [{ blocks: [Paragraph([Text("apples")])], task: Checked }, { blocks: [Paragraph([Text("pears")])], task: Unchecked }] })
Blockquote([Paragraph([Text("Quoted")])])
Code({ info: "roc", pre: "main = 1
" })
Table({ header: [[Text("Item")], [Text("Qty")]], align: [Left, Right], rows: [[[Text("pear")], [Text("2")]]] })
ThematicBreak
HtmlBlock("<div>raw</div>
")
ListBlock({ kind: Ordered({ start: 3 }), loose: True, items: [{ blocks: [Paragraph([Text("first")])], task: NoTask }, { blocks: [Paragraph([Text("second")])], task: NoTask }] })

The block variants are:

VariantContents
Heading({ level, content })An ATX (#) or Setext (underlined) heading. level is One to Six; level.to_str() gives "1" to "6".
Paragraph(inlines)A paragraph’s inline content.
Blockquote(blocks)The blocks inside a > quote.
ListBlock({ kind, loose, items })kind is Unordered or Ordered({ start }). Each item has blocks and a task of NoTask, Unchecked ([ ]) or Checked ([x] or [X]). Item text is always wrapped in Paragraph, even in a tight list.
Code({ info, pre })A fenced or indented code block. info is the info string after the opening fence (empty for indented code); pre is the literal content, ending in a newline.
ThematicBreakA ***, --- or ___ line.
Table({ header, align, rows })A GFM table. Each cell is a List(Inline). align holds one Default, Left, Center or Right per column.
HtmlBlock(raw)Raw HTML, including its trailing newline, exactly as written.
Frontmatter(raw)The raw text of a leading frontmatter block; only ever the first block. See Read frontmatter.

Block quotes and lists nest at most 1,000 levels deep. Deeper > and list markers are read as paragraph text. Emphasis, strikethrough, links and images also nest at most 1,000 levels deep (counted separately); the delimiters and brackets of deeper ones are read as text. Code that walks the tree recursively therefore cannot run out of stack on hostile input.

The syntax tree types support == and hashing, so blocks and inlines can be Dict keys and Set elements.

Link reference definitions ([label]: /url) do not appear in the tree. The parser resolves them into the links that use them.

3.15.2. Parse inline content§

Inline content is the text inside a paragraph, heading or table cell. When you already have a single line or fragment, such as a title from a database, parse it directly with Markdown.parse_inlines, which returns a List(Inline). Markdown.inline_parser is the same parser for use in combinators.

show_inlines! = |text| {
	inlines = Markdown.parse_inlines(text)
	for inline in inlines {
		Stdout.line!(Str.inspect(inline))?
	}
	Ok({})
}

show_inlines!("*em* **strong** ~~gone~~ `x + 1`")?
show_inlines!("[Roc](https://roc-lang.org \"Home\") and ![a cat](cat.png)")?
show_inlines!("<https://example.com> www.example.com me@example.com")?
show_inlines!("one\ntwo  \nthree")?

Output:

Emphasis([Text("em")])
Text(" ")
Strong([Text("strong")])
Text(" ")
Strikethrough([Text("gone")])
Text(" ")
InlineCode("x + 1")
Link({ label: [Text("Roc")], target: { href: "https://roc-lang.org", title: Ok("Home") } })
Text(" and ")
Image({ alt: [Text("a cat")], target: { href: "cat.png", title: Err(Missing) } })
Link({ label: [Text("https://example.com")], target: { href: "https://example.com", title: Err(Missing) } })
Text(" ")
Link({ label: [Text("www.example.com")], target: { href: "http://www.example.com", title: Err(Missing) } })
Text(" ")
Link({ label: [Text("me@example.com")], target: { href: "mailto:me@example.com", title: Err(Missing) } })
Text("one
two")
HardBreak
Text("three")

The output shows these rules:

A reference link such as [the guide][guide] points to a definition elsewhere in the document. Markdown.parse_inlines sees only the fragment you give it, so it cannot resolve the reference and leaves the brackets as text. Parse the whole document with Markdown.parse_str instead:

document =
	\\See [the guide][guide] and ![logo][].
	\\
	\\[guide]: https://example.com/guide "The guide"
	\\[logo]: /logo.png

blocks = Markdown.parse_str(document, {})
for block in blocks {
	Stdout.line!(Str.inspect(block))?
}

Output:

Paragraph([Text("See "), Link({ label: [Text("the guide")], target: { href: "https://example.com/guide", title: Ok("The guide") } }), Text(" and "), Image({ alt: [Text("logo")], target: { href: "/logo.png", title: Err(Missing) } }), Text(".")])

Reference labels match case-insensitively using Unicode case folding, so [Straße] matches a definition labelled [STRASSE].

3.15.3. Read frontmatter§

Many static-site tools put metadata at the top of a Markdown file between two --- lines. When the document’s first line is exactly --- and a later line is exactly ---, Markdown.parse_str returns the lines between them as the first block, Frontmatter(raw), and parses the rest as Markdown. Markdown.frontmatter(blocks) returns that text, or Err(Missing) when the document has none. The parser does not interpret the text. Pass it to the YAML parser (see Read YAML configuration and frontmatter) to read the fields:

post =
	\\---
	\\title: Hello
	\\draft: false
	\\---
	\\# Hello
	\\
	\\First post.

main! = |_args| show!(post)

show! = |text| {
	blocks = Markdown.parse_str(text, {})
	match Markdown.frontmatter(blocks) {
		Ok(raw) => {
			Stdout.line!("raw: ${Str.inspect(raw)}")?
			meta = Yaml.parse_str(raw, {})?
			Stdout.line!("meta: ${meta.to_inspect()}")?
			# The frontmatter is the first block.
			Stdout.line!("body blocks: ${(blocks.len() - 1).to_str()}")?
		}

		Err(Missing) => Stdout.line!("no frontmatter")?
	}
	Ok({})
}

Output:

raw: "title: Hello
draft: false
"
meta: Mapping([{ key: "title", value: Text("Hello") }, { key: "draft", value: Bool(False) }])
body blocks: 2

Without a closing --- line, the opening --- is ordinary Markdown: a thematic break, or a Setext heading underline.

3.15.4. Walk the tree to produce output§

To produce output, write two functions: one that matches each block variant and one that matches each inline variant. Each function calls the other for nested content. This section builds a small HTML renderer and a table-of-contents extractor for the following release notes:

blocks = Markdown.parse_str(source, {})
Stdout.write!(render_blocks(blocks))?
Stdout.line!("--- contents ---")?
for line in table_of_contents(blocks) {
	Stdout.line!(line)?
}

Escape the characters that are significant in HTML:

escape : Str -> Str
escape = |text| {
	text
		.replace_each("&", "&amp;")
		.replace_each("<", "&lt;")
		.replace_each(">", "&gt;")
		.replace_each("\"", "&quot;")
}

Render inline content:

render_inlines : List(Markdown.Inline) -> Str
render_inlines = |inlines| {
	var $out = ""
	for inline in inlines {
		$out = $out.concat(render_inline(inline))
	}
	$out
}

render_inline : Markdown.Inline -> Str
render_inline = |inline| {
	match inline {
		Text(text) => escape(text)
		Strong(children) => "<strong>${render_inlines(children)}</strong>"
		Emphasis(children) => "<em>${render_inlines(children)}</em>"
		Strikethrough(children) => "<del>${render_inlines(children)}</del>"
		InlineCode(code) => "<code>${escape(code)}</code>"
		Link({ label, target }) => "<a href=\"${escape(target.href)}\"${title_attr(target.title)}>${render_inlines(label)}</a>"
		Image({ alt, target }) => "<img src=\"${escape(target.href)}\" alt=\"${escape(plain_text(alt))}\"${title_attr(target.title)} />"
		HardBreak => "<br />\n"
		# Raw HTML passes through unchanged; see the warning above.
		HtmlInline(raw) => raw
	}
}

title_attr : Try(Str, [Missing]) -> Str
title_attr = |title| {
	match title {
		Ok(text) => " title=\"${escape(text)}\""
		Err(Missing) => ""
	}
}

Render blocks. A tight list item is rendered without its <p> wrapper:

render_blocks : List(Markdown) -> Str
render_blocks = |blocks| {
	var $out = ""
	for block in blocks {
		$out = $out.concat(render_block(block))
	}
	$out
}

render_block : Markdown -> Str
render_block = |block| {
	match block {
		Heading({ level, content }) => {
			n = level.to_str()
			"<h${n}>${render_inlines(content)}</h${n}>\n"
		}

		Paragraph(inlines) => "<p>${render_inlines(inlines)}</p>\n"
		Blockquote(children) => "<blockquote>\n${render_blocks(children)}</blockquote>\n"
		ListBlock({ kind, loose, items }) => {
			tag =
				match kind {
					Unordered => "ul"
					Ordered(_) => "ol"
				}
			var $body = ""
			for item in items {
				# A tight list shows its paragraphs without <p> tags.
				inner =
					match item.blocks {
						[Paragraph(inlines)] if !loose => render_inlines(inlines)
						_ => render_blocks(item.blocks).trim()
					}
				$body = $body.concat("<li>${inner}</li>\n")
			}
			"<${tag}>\n${$body}</${tag}>\n"
		}

		Code({ pre, .. }) => "<pre><code>${escape(pre)}</code></pre>\n"
		ThematicBreak => "<hr />\n"
		HtmlBlock(raw) => raw
		# Tables, frontmatter and the remaining variants are left out of this sketch.
		_ => ""
	}
}

A table of contents only needs headings and their plain text:

plain_text : List(Markdown.Inline) -> Str
plain_text = |inlines| {
	var $out = ""
	for inline in inlines {
		piece =
			match inline {
				Text(text) => text
				InlineCode(code) => code
				Strong(children) | Emphasis(children) | Strikethrough(children) => plain_text(children)
				Link({ label, .. }) => plain_text(label)
				Image({ alt, .. }) => plain_text(alt)
				HardBreak => " "
				HtmlInline(_) => ""
			}
		$out = $out.concat(piece)
	}
	$out
}

table_of_contents : List(Markdown) -> List(Str)
table_of_contents = |blocks| {
	var $lines = []
	for block in blocks {
		match block {
			Heading({ level, content }) => {
				indent =
					match level {
						One => ""
						Two => "  "
						_ => "    "
					}
				$lines = $lines.append("${indent}- ${plain_text(content)}")
			}

			_ => {}
		}
	}
	$lines
}

Output:

<h1>Release notes</h1>
<p>Version <strong>2</strong> is out. Read the <a href="/changes" title="All changes">changelog</a>.</p>
<h2>Fixed</h2>
<ul>
<li>Faster <code>parse</code></li>
<li>Safer <b>HTML</b> &amp; entities</li>
</ul>
<h2>Known issues</h2>
<p>None.</p>
--- contents ---
- Release notes
  - Fixed
  - Known issues

A complete renderer would also handle Table, use Code’s info for a language- class, and pass Ordered({ start }) through as the start attribute.

3.15.5. Conformance and limitations§

The API reference lists every type and function in the module.

3.15.6. Performance§

Markdown.parse_str parses about 30 MB/s on an Apple M2, building the block tree and every inline: a typical README in about 0.15 ms and a 1 MiB document in about 32 ms. That is about 2.7 times slower than goldmark (Go) and comrak (Rust), 7 times slower than pulldown-cmark, and 10 times faster than markdown-it-py. Parsing stays linear on runs of unmatched emphasis and brackets and on deeply nested block quotes and lists. See Performance for the measurements and caveats.

3.16. Reading AsciiDoc§

AsciiDoc parses an in-memory string into a document title and typed blocks, and renders embedded HTML. Paragraphs retain their inline source. Parsing normalizes line endings to LF. Like Asciidoctor, it removes trailing ASCII whitespace from each line of AsciiDoc source: the document and any included .adoc, .asciidoc, .asc, .ad or .txt file. Lines from any other included file, such as code or program output, keep their trailing whitespace wherever they land, and directives in them, such as ifdef:: or include::, are ordinary text.

3.16.1. Parsing and rendering§

AsciiDoc.parse_str(source, props) returns the parsed document. Props fields have defaults: includes remain links unless expand_includes: True, and includes supplies virtual sources as { path, source } records. Includes never read files or the network. The parser supports nested includes, line and tag selectors, conditionals, attribute definitions and include level offsets. Call document.render_html() to render embedded HTML, or AsciiDoc.render_inline(source, attributes) for independent inline text.

For example, supply a virtual chapter in the constructor props:

document = AsciiDoc.parse_str("include::chapter.adoc[]", {
    includes: [{ path: "chapter.adoc", source: "Chapter text" }],
    expand_includes: True,
})?
html = document.render_html()

Listing, source, literal and verse blocks and paragraphs honour tabsize as Asciidoctor does. With the block’s own tabsize attribute or, when it has none, the document’s, each tab expands to the next multiple of that many columns, counting each character as one column. Zero, a negative number or text that isn’t a number expands nothing. An indented literal paragraph and passthrough content keep their tabs. A block’s indent applies after the expansion.

Block forms include sections, paragraphs, lists and description lists, listing and literal blocks, compound blocks, admonitions, tables, media and block metadata. Rendering handles inline formatting, macros, substitutions, cross references, section IDs, numbering, tables of contents, captions and footnotes. Rich blocks retain context, style, ID, attributes and nested blocks alongside the simpler paragraph, section, listing and literal variants.

Rich.attrs uses an immutable AsciiDoc.Attributes scope. Parsed nodes share their document attribute snapshot and keep local metadata separately, preserving attribute order, duplicates and empty values without copying the document bindings into each node. render_inline_attributes(text, node.attrs) renders against that scope. The existing render_inline(text, bindings) accepts a plain list for independent inline input.

Use materialize_attributes(node.attrs) when a flat list is required for serialization or editing; this explicitly enumerates the ordered bindings. Parsed source updates share balanced name indexes; materializing an updated scope sorts its bindings by their retained insertion positions, taking O(N log N) work at this explicit boundary. Unchanged seed scopes reuse their already ordered immutable bindings. For a manually constructed block, use attrs: AsciiDoc.attributes(bindings). To replace bindings, materialize and edit the list, then construct a new scope with AsciiDoc.attributes; the original snapshot remains unchanged.

Input is bounded to 256 KiB, expanded input to 1 MiB and structural nesting to 64 levels. Virtual includes default to a maximum depth of 16. Invalid input or resource-limit failures return Unsupported or InvalidAsciiDoc with a structured { problem, location, path } diagnostic. Source positions use Text({ offset, line, column }), with a zero-based UTF-8 byte offset and one-based line and column. Include paths are represented by Source(path); limits retain their typed resource, maximum and observed count. An initial UTF-8 byte order mark is ignored.

source = "= Guide\n\nIntroduction\n\n== Getting started\n\nHello\n"

describe : Str -> Str
describe = |text| {
	match AsciiDoc.parse_str(text, {}) {
		Ok(document) => "${document.title}: ${document.blocks.len().to_str()} top-level blocks"
		Err(Unsupported(error)) => "Unsupported on line ${source_position(error.location).line.to_str()}: ${Str.inspect(error.problem)}"
		Err(InvalidAsciiDoc(error)) => "Invalid on line ${source_position(error.location).line.to_str()}: ${Str.inspect(error.problem)}"
	}
}

main! = |_| Stdout.line!(describe(source))

This complete example prints:

Guide: 2 top-level blocks

3.16.2. Reusing include selection§

A tool that expands some includes itself, for example to record where each listing came from, can call select_include instead of reimplementing the selection rules. It takes the included source and the directive’s attribute list, such as lines=1..4, tag=example,indent=0 or tags=**;!hidden. It returns the selected text, the source line number of each selected line, and a list of problems. include:: expansion calls the same code, so the two agree.

Selection is Asciidoctor 2.0.23’s, as lenient as it is. A missing tag selects nothing. lines=4abc selects line 4. A reversed range selects nothing, so lines=4..2 on its own selects every line. lines= overrides tag= and tags= whenever it is present, even with an empty value. Tags nest, and indent=0 removes leading tabs as well as spaces, one character each.

Three problems are Asciidoctor’s own warnings: MissingTag, UnclosedTag and UnmatchedEnd. The other two, InvalidLines and EmptySelection, flag selections that Asciidoctor makes without a warning. A strict caller rejects any problem, and a lenient caller ignores them. The API reference for AsciiDoc.IncludeProblem defines each one.

select_include reads its source as Asciidoctor reads an included file that isn’t AsciiDoc, such as code for a listing. When include:: expands an AsciiDoc file, it first removes trailing whitespace from each line, as Asciidoctor does, so a line of only spaces doesn’t count toward indent=.

In a document that sets tabsize, indent= first expands tabs. For such a document, call select_include(source, attributes, { tabsize: 4 }) with the document’s tab size. Asciidoctor ignores a tabsize= in the directive’s own attribute list, and so do both functions.

This example selects from a small Rust file:

fn greet(name: &str) {
    // tag::body[]
    let message = format!("Hello, {name}!");
    println!("{message}");
    // end::body[]
}
## Explain a problem, as a strict documentation build would.
describe : AsciiDoc.IncludeProblem -> Str
describe = |problem| {
	match problem {
		MissingTag(name) => "tag '${name}' never opens"
		UnclosedTag(name) => "tag '${name}' is not closed"
		UnmatchedEnd(name) => "end of '${name}' matches no open tag"
		InvalidLines(entry) => "lines entry '${entry}' is invalid"
		EmptySelection => "nothing is selected"
	}
}

## Select like `include::greet.rs[attributes]`, rejecting any problem.
strict : Str -> Str
strict = |attributes| {
	selection = AsciiDoc.select_include(greet_rs, attributes, {})
	if selection.problems.is_empty() {
		"${attributes} selects lines ${Str.inspect(selection.lines)}:\n${selection.text}"
	} else {
		"${attributes} is rejected: ${Str.join_with(selection.problems.map(describe), "; ")}"
	}
}

main! = |_| {
	for attributes in ["lines=3..4", "tag=body,indent=0", "tag=bdy", "lines=4..3"] {
		Stdout.line!(strict(attributes))?
	}
	Ok({})
}

It prints:

lines=3..4 selects lines [3, 4]:
    let message = format!("Hello, {name}!");
    println!("{message}");
tag=body,indent=0 selects lines [3, 4]:
let message = format!("Hello, {name}!");
println!("{message}");
tag=bdy is rejected: tag 'bdy' never opens; nothing is selected
lines=4..3 is rejected: lines entry '4..3' is invalid

The review fixtures in scripts/asciidoc/include-selection-fixtures.json compare these selections with Asciidoctor’s, including the inputs behind each problem.

3.16.3. Oracle coverage and implementation coverage§

The development oracle pins Ruby Asciidoctor to version 2.0.23 and verifies the gem’s SHA-256 before installing it. Its JSON protocol exposes a structural projection, richer block metadata, rendered HTML and warnings. The structural projection flattens Asciidoctor’s preamble wrapper and retains raw block source. It deliberately does not claim an inline semantic tree.

Authored fixtures assert reference behavior independently of the Roc parser. Strict review checks both the structural projection and rendered HTML against the pinned oracle. HTML comparison ignores layout indentation while retaining attributes, inline text and significant whitespace. Reference-accepted inputs that the implementation rejects fail the strict gate.

The oracle runs without ambient file or network access. Tests can supply a bounded virtual include map; the oracle applies its own include processing, including line and tag selectors, to those sources. Repository chapters are also checked individually, with unresolved includes left as links.

3.16.4. Reproduce the checks§

Use the latest Roc nightly, plus Ruby and Python:

python3 scripts/asciidoc/setup_oracle.py
mkdir -p .roc-parser-tmp/asciidoc
$ROC build --opt=dev scripts/asciidoc/probe.roc --output=.roc-parser-tmp/asciidoc/probe
python3 scripts/review_asciidoc.py check --require-complete --compare-html --probe .roc-parser-tmp/asciidoc/probe --generated 200 --mutations 200 --corpus docs
scripts/fuzz.roc smoke asciidoc-raw asciidoc-structured

The review writes machine-readable evidence and retains failing inputs for replay under .roc-parser-tmp/asciidoc/. Use --oracle-only to validate the reference fixtures without a Roc probe. The native raw target checks crashes, error locations and LF/CRLF agreement; the structured target builds expected block trees independently from its input choices.

3.16.5. Local performance checks§

For fast local iteration, reuse the Ruby side of an existing saved run:

python3 scripts/check_asciidoc_performance_cached.py \
  --baseline /path/to/saved/results.json \
  --roc "$ROC" \
  --replay path/to/small-cases.json --output /tmp/asciidoc-cached-dev-1

When no binary is supplied, the command builds optimized native code with the pinned --roc compiler. To reuse an existing build, pass --candidate-binary with its genuine matching build receipt. The saved report SHA is always recorded; --baseline-sha256 optionally pins an expected value. The cached runner starts only that native worker. It checks fresh projection and DOM against the saved Ruby correctness hashes before and after timing, using the complete request (including ordered attributes and virtual includes) and mode. It defaults to quick 5 ms / three-sample native batches. --id ID can select a saved case without writing a replay file; repeat it for several IDs.

Treat ratios as rough internal guidance: large gains and geometric scaling warnings matter more than small near-parity differences. Saved Ruby sample quality, original failures, machine descriptions and reference code hashes remain visible; noisy or short historical samples receive warnings. New requests or changed semantics require a refresh. Noisy native samples remain visible too. Native requests time out after 10 seconds by default (--timeout overrides this). The first worker error is saved and stops the run; unprocessed inputs remain explicit. Include several sizes of each geometric family in a replay to inspect scaling; isolated maximum-size cases cannot establish growth.

Refresh with the existing fresh check_asciidoc_performance.py --replay ... command. Cached runs never claim fresh paired Oracle timing qualification and do not overwrite saved reports. This is an optional local development shortcut; it does not add CI jobs or change parser behavior.

Fresh reference measurements are available when refreshing the cache or investigating a meaningful slowdown:

The performance harness builds optimized native code and compares parsing and parsing plus HTML rendering with the same pinned Asciidoctor. Its default ceiling is 1.0 times the oracle duration for each input. Persistent workers keep IPC, JSON serialization, warmup and correctness checks outside timed batches.

python3 scripts/check_asciidoc_performance.py --roc "$ROC" --deadline-seconds 10800

The fresh campaign generates 2,000 seeded documents plus 500 raw mutations, runs the authored fixtures, and tests geometric sizes from 32 to 1,024 for paragraphs, sections, footnotes, cross references, lists, tables, attributes and delimiters. Footnotes (including table cells), named references, growing counter state (including formatted paragraphs and underscored names) and chained attribute definitions also run at 2,048 and 4,096 entries. Conditional presence and value tests exercise repeated lookups in a growing attribute environment. Explicit attribute substitutions, attribute passthroughs and explicit macro footnotes also run through 4,096 entries to exercise growing state in fallback substitution paths. Counter and set state mixed with immediate quoted passes, math and ordinary URLs also runs through 4,096 entries to catch repeated attribute materialization. Static header attributes mixed with URL paragraphs, URL list items, listing blocks, formatted section titles and AsciiDoc table cells check shared snapshot copies and repeated option lookups through 4,096 entries. The table-cell family explicitly disables headers and starts cell contents immediately after |. Static attribute environments with repeated and distinct image targets also run through 4,096 blocks to guard media option lookup and attribute sharing. Repeated source definitions followed by URLs or text, varied-name unset/readd cycles and many tiny tables currently use the bounded audit sizes 128, 256 and 512. Additional bounded families keep counters or set values live while unrelated source definitions change, including an explicit counter reset. A separate bounded family resets every live counter through a source definition and increments it again, then probes both endpoint counters. Alternating counter paragraphs and simple Body. paragraphs exercise transitions between Rich and ordinary parsed nodes while dynamic names grow, retaining counter, body and final-value predicates at the same bounded sizes. Ordered Options variants supply a same-value standard attribute or change and restore that attribute. Both retain ordered requests and the same HTML predicates to check initialization and baseline tracking across scope transitions. These families guard revision history, dynamic-state reconciliation and shared table scopes. Compact generators append standard-scope controls, small and ordered Options boundaries, and larger counter-reset and section-title cases. Title variants include ordinary and escaped quoted passes, role-prefixed passthroughs, explicit links, angle-wrapped HTTP/HTTPS links and literal angle prefixes, through 4,096 entries. These are strict performance targets, including retained failures; their presence does not claim passing timings. The extended guards exercise missing and newline-rejected literal closers, shared quote-role closers, Unicode role prefixes, malformed title macros and listing callouts through 4,096 entries. Callout cases include missing >, Unicode HTML-like text, many numbered callouts on separate lines or one line, and a growing placeholder-prefix collision. Two default-substitution families retain normal HTML-comment callouts and escaped numeric callouts through 4,096 entries. Deterministic parameters keep cached comparisons reproducible. These remain local scaling inputs and do not expand the two-case CI smoke. Larger counter-preview, colliding attribute-name and alternating Options inputs retain the exact requests that exposed repeated copying and rescanning. Source-scaled guards above 512 are generated only when the largest requested size is at least 1,024. Seeded samples also explore sizes between the scaling points; --generated 0 disables them along with the generated document campaign. Use --sizes 128 256 512 for a bounded local scaling campaign, or --replay with a retained exact-source case to isolate a measured failure. Stateful pass previews mixed with unrelated sets check work that can otherwise be discarded before shared-state evaluation. Positional mail subjects and bodies exercise protected-tag restoration with growing counter state. Table footnotes also run at 8,192 entries to check footer scaling. Nested containers use depths from 2 to 32. Scaling checks flag growth above the configured exponent. Reports record source hashes, compiler, machine, samples and replay commands. Failing inputs and scaling pairs are retained under .roc-parser-tmp/asciidoc-performance/. Replay cases may supply an ordered attributes list of {name, value} string records to exercise AsciiDoc.Options.attributes. Nonempty options are included in request fingerprints and retained replay cases; absent and empty options use the original request format and fingerprints. Entries keep their order and last duplicate wins. The pinned Ruby worker applies these as soft initial attributes, permitting subsequent document definitions to override them as the Roc parser does. Supplied values override the worker’s fixed initial attributes. Use ordinary lowercase attribute names for cross-engine parity; Asciidoctor’s API also interprets special attribute-name suffixes and normalizes names.

Scaling uses source UTF-8 byte length by default. Replay cases whose growing input is an ordered Options list or include map can set "scaling_input": "request". This measures the canonical compact UTF-8 JSON of source, includes, expansion mode and nonempty ordered attributes. Case IDs, measurement modes, iteration counts and warmup counts do not contribute to size. The flag changes only the scaling metric; worker requests and their fingerprints stay the same. Reports record the chosen basis and both measured input sizes. A family cannot mix source and request metrics, and cases cannot supply an arbitrary numeric size. The speed ceiling and scaling exponent stay unchanged.

Use --seed to explore another deterministic campaign, --corpus for local documents, and --replay to repeat a retained failure. --smoke runs two tiny correctness and protocol cases without asserting timing; CI runs this smoke check. The full timing gate is intended for a quiet local machine. For a stronger measurement of a retained input, use:

python3 scripts/check_asciidoc_performance.py --roc "$ROC" --replay path/to/cases.json --window-ms 50 --samples 7 --stable-all --progress

The standalone attribute-alias replay retains nine long stored values and late syntax-rejection inputs that exposed slow fallback scanning. It keeps their original ordered caller attributes and sources. Run it locally with a fresh output directory:

python3 scripts/check_asciidoc_performance.py --roc "$ROC" --replay scripts/asciidoc/performance-attribute-alias-controls.json --modes parse render --window-ms 150 --samples 15 --stable-all --batch-policy per-engine --max-ratio 1.0 --max-scaling-exponent 1.3 --output .roc-parser-tmp/asciidoc-alias-controls-attempt1

These are individual timing controls with no geometric scaling claim. They do not change the normal guard inventory or the two-case CI smoke check.

Document models are generated in Python. Each input then runs through both engines for fresh correctness checks, calibration, warmup and timing samples. The timing window is a minimum duration; it does not cap a single operation. A Ruby parse that takes seconds still runs in full for warmup and each sample. Use --progress to see the active case, engine and measurement phase. The default per-response wall budget is 180 seconds, allowing slow oracle warmup, calibration and fresh projection/HTML checks outside the timed interval. This response budget does not change the 1.0 times speed ceiling or sampling. The default full-campaign deadline remains 7,200 seconds; the command above allows 10,800 seconds for the expanded local corpus. Every request retains its fresh correctness checks.

The Ruby worker releases request-local correctness documents, projections, responses and include-source maps after each reply, including error replies. It clears the thread-local include map at the same boundary. This cleanup runs outside the elapsed interval; it prevents a prior correctness graph from remaining live during the next timed request. It does not force garbage collection inside measured operations.

The default --batch-policy per-engine calibrates an independent operation count for each engine. Every elapsed batch is divided by that engine’s count; full-duration measurements retain the requested minimum window for both engines, the requested sample count and alternating engine order. Fresh projection and HTML checks run before and after every case/mode measurement. Version 1 workers also return full output for each response; version 2 positive replies contain verified counts, checksum and elapsed time. Duration or noise refinement increases only affected engine counts within the existing attempt and iteration limits. Reports use schema version 2 and record both counts for every attempt; retained replay commands preserve the policy. Use --batch-policy common to reproduce the historical shared-count policy. Independent counts avoid repeating a slow oracle operation just because the native engine needs a larger batch. This reduces incidental oracle GC and rare-event averaging compared with longer shared-count batches; it is a new sampling policy, not a claim of identical statistical confidence. Historical reports remain unchanged and identify their original driver and policy.

Add --progress to flush the current case, mode, calibration engine and sample/attempt counts to stderr before each worker request. Completion and qualification messages identify long oracle responses without changing timed worker loops. The flag is opt-in and retained by replay commands.

The performance workers use reply protocol version 2. For every case and mode, the driver requests fresh full projection and HTML output before and after measurement, compares both engines and each engine’s before/after output, and checks the pinned oracle identity. Each zero-iteration reply also reports the metric from one separate fresh operation. Positive-iteration replies contain counts, checksum and elapsed time, avoiding an additional untimed conversion for each calibration or sample response. Every timed operation and warmup still constructs a fresh document; the driver requires its engine’s exact checksum (iterations + warmup) * operation_checksum.

A compact timing reply is accepted only inside the matching checked input, mode and worker-instance context, which expires on error or restart. Version 1 workers remain supported with their full per-response output comparisons. Reports retain before/after proof digests and raw timing attempts; correctness failures also retain full replies under the output’s correctness/ directory. Input fingerprints and retained replay sources are unchanged. This protocol does not reduce samples, duration windows, coverage or performance thresholds. The two-case CI smoke exercises the same correctness boundaries and compact protocol without asserting speed.

3.17. HTTP messages§

This chapter shows how to parse raw HTTP/1.x bytes into Roc records: a request or response line, its header fields, and its body. It is a set of how-to guides for readers who know basic Roc and are handling bytes from a socket, a test fixture or a capture file. By the end you can read requests and responses, decode chunked bodies, read messages incrementally, split pipelined messages, embed the parsers in a larger grammar, and know which inputs the parser refuses and why.

The HTTP module follows the message syntax of RFC 9112 and the field syntax of RFC 9110. It parses messages; it does not open connections or send anything.

3.17.1. Parse a request§

Parse a request with HTTP.Request.parse_bytes(bytes, props). It takes the raw bytes as a List(U8) (use text.to_utf8() for a Str) and returns a complete HTTP.Request, rejecting trailing input:

request_text = "POST /notes HTTP/1.1\r\nHost: example.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nhello"

request : HTTP.Request
request = HTTP.Request.parse_bytes(request_text.to_utf8(), {})?
Stdout.line!("method: ${method_name(request.method)}")?
Stdout.line!("target: ${request.target}")?
Stdout.line!("version: ${request.version.major.to_str()}.${request.version.minor.to_str()}")?
Stdout.line!("body: ${Str.from_utf8(request.body) ?? "<binary>"}")?

Output:

method: POST
target: /notes
version: 1.1
body: hello

A request is a record with these fields:

Turning a Method back into its name covers every case with one Extension branch:

## A method as it appears on the request line.
method_name : HTTP.Method -> Str
method_name = |method| {
	match method {
		Get => "GET"
		Head => "HEAD"
		Post => "POST"
		Put => "PUT"
		Delete => "DELETE"
		Connect => "CONNECT"
		Options => "OPTIONS"
		Trace => "TRACE"
		Patch => "PATCH"
		Extension(name) => name
	}
}

purge = HTTP.Request.parser({}).run("PURGE /cache/a HTTP/1.1\r\nHost: example.com\r\n\r\n".to_utf8())?
Stdout.line!("method: ${Str.inspect(purge.value.method)}")?

Output:

method: Extension("PURGE")

3.17.1.1. Read header fields§

Look a field up with message.header(name), which compares names case-insensitively (HTTP field names are) and returns the first match, or Err(Missing):

content_type = request.header("content-type") ?? "none"
accept = request.header("Accept") ?? "none"
Stdout.line!("content type: ${content_type}, accept: ${accept}")?

Output:

content type: text/plain, accept: none

Header names keep the case they were sent in. Values have surrounding spaces and tabs removed. A field that appears more than once appears in headers once per occurrence, so walk headers yourself to see every value.

3.17.2. Parse a response§

Parse a response with HTTP.Response.parse_bytes(bytes, props). status_code is a U16 and reason is the reason phrase, which may be empty:

response_text = "HTTP/1.1 404 Not Found\r\nContent-Length: 9\r\n\r\nNot here."

response = HTTP.Response.parse_bytes(response_text.to_utf8(), {})?
Stdout.line!("status: ${response.status_code.to_str()} ${response.reason}")?
Stdout.line!("body: ${Str.from_utf8(response.body) ?? "<binary>"}")?

Output:

status: 404 Not Found
body: Not here.

3.17.3. Understand how the body is found§

The headers decide where the body ends (RFC 9112 section 6.3). The parser applies these rules in order:

  1. A response with a 1xx, 204 or 304 status has no body.

  2. Transfer-Encoding: chunked means the body is a series of chunks, which the parser decodes into one body.

  3. Content-Length: n means the body is exactly n bytes. If the input has fewer, parsing fails.

  4. With neither field, a request has no body, and a response’s body runs to the end of the input, because the server marks the end by closing the connection.

3.17.3.1. Decode a chunked body§

Chunk extensions (;name=value after a chunk size) and trailer fields (fields after the final 0 chunk) are checked for valid syntax and then discarded. Only the decoded content is kept.

chunked_text = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nWiki\r\n6;note=x\r\npedia \r\n0\r\nExpires: never\r\n\r\n"

chunked = HTTP.Response.parser({}).run(chunked_text.to_utf8())?
Stdout.line!("decoded body: ${Str.from_utf8(chunked.value.body) ?? "<binary>"}")?

Output:

decoded body: Wikipedia 

3.17.4. Read pipelined messages§

A client may send several requests on one connection without waiting for responses. HTTP.Request.parser(props).run(bytes) consumes one message prefix and returns the following bytes as rest; run it again on rest for the next message. To read a whole buffer of requests at once, use HTTP.parse_requests, which fails unless the buffer ends exactly at the end of a request:

pipelined = "GET /a HTTP/1.1\r\nHost: example.com\r\n\r\nGET /b HTTP/1.1\r\nHost: example.com\r\n\r\n".to_utf8()

first = HTTP.Request.parser({}).run(pipelined).map_err(|error| Pipeline(error))?
second = HTTP.Request.parser({}).run(first.rest).map_err(|error| Pipeline(error))?
Stdout.line!("first: ${first.value.target}, second: ${second.value.target}, left over: ${second.rest.len().to_str()} bytes")?

all = HTTP.parse_requests(pipelined, {})?
Stdout.line!("targets: ${Str.join_with(all.map(|r| r.target), ", ")}")?

Output:

first: /a, second: /b, left over: 0 bytes
targets: /a, /b

A response with no framing fields reads to the end of the input, so it is always the last message in a buffer.

3.17.5. Read messages from byte chunks§

Use HTTP.Request.decoder(props).read_stream(chunks) to process a request pipeline from a file or socket without collecting its bodies. chunks is a Stream(Try(List(U8), source_error)); the pure equivalent is HTTP.Request.decoder(props).read_iter(chunks). Empty chunks do not mean EOF, and chunk boundaries may occur inside any HTTP line, UTF-8 header value, or body.

The reader emits Value(RequestHead(head)), followed by zero or more Value(Body(bytes)) fragments, any Value(Trailer(header)) fields, and Value(MessageEnd). It then starts the next pipelined request. Bodies are binary bytes, with chunked transfer framing removed. No complete body is retained. A zero-length body produces no Body event.

MessageEnd validates one message. The final Finished(report) validates the source through EOF; drain the reader through it to detect an incomplete next message or malformed suffix. Stopping after a head or body fragment leaves the remaining input unvalidated. A source failure is returned as Err(SourceErr(original_error)) and never completes a truncated message. HTTP framing and limit failures are terminal Err(InvalidHttp(error)) values, with byte offsets counted from the start of the source. HTTP readers never skip rejected messages or attempt to resume at another line.

The complete program examples/http-streaming.roc reads stdin and prints each message’s body byte count. For example:

printf 'POST /notes HTTP/1.1\r\nHost: a\r\nContent-Length: 2\r\n\r\nhi' | roc examples/http-streaming.roc

Output:

request /notes
body bytes: 2
finished; protocol bytes: 0

3.17.5.1. Associate responses with requests§

Use HTTP.Response.decoder({ methods, reader: props }).read_stream(chunks) or .read_iter(chunks) for responses. The decoder requires an Iter(HTTP.Method) containing the request methods in response order. A response without a matching method is an error. An informational response such as 100 or 103 produces its own head and MessageEnd, while retaining the method for the final response. Methods are pulled only when a response starts; unused methods are neither pulled nor checked at source EOF.

Responses to HEAD, informational responses, 204, and 304 have no body. A response without length or transfer framing streams its body until source EOF, then emits MessageEnd and Finished. These readers retain the strict framing checks of the complete-input API, including rejecting conflicting framing fields even when response context implies an empty body.

A 101 response or successful CONNECT response ends HTTP parsing. After its head and MessageEnd, the reader emits ProtocolSwitch, then bounded ProtocolData(bytes) events containing the remaining bytes unchanged, through source EOF. The application is responsible for validating the protocol upgrade and interpreting those bytes. They are never parsed as another HTTP message.

The example accepts get, head, or connect to read responses for one request of that method. For a response to HEAD:

printf 'HTTP/1.1 200 OK\r\nContent-Length: 999\r\n\r\n' | roc examples/http-streaming.roc head

The reported body byte count is zero; the advertised length is metadata.

3.17.5.2. Bound metadata and output fragments§

HTTP.ReadProps defaults to 65,536 head bytes, 8,192 bytes per line, 100 header fields, 65,536 trailer-section bytes, 100 trailer fields, and 65,536 bytes per body or protocol-data fragment. The byte limits include CRLF for complete heads and trailer sections; max_line_bytes excludes CRLF and also bounds chunk-size lines and their extensions. Each message starts new head and trailer budgets. max_body_chunk_bytes must be positive.

The props are max_head_bytes, max_line_bytes, max_headers, max_trailer_bytes, max_trailers, and max_body_chunk_bytes. Limits fail before retaining the byte or emitting the field that exceeds the budget. Host, Content-Length, and Transfer-Encoding are rejected in trailers. Retained parser memory depends on these metadata bounds and the current source chunk. Keeping emitted values intentionally adds to application memory use. These limits do not bound the total body length, connection lifetime, or time spent waiting for input; enforce those in the application when needed.

For a custom driver, HTTP.Request.decoder(props) or HTTP.Response.decoder({ methods, reader: props }) returns a configured reader. decoder.step({ bytes, end: More }) returns at most one event and the number of bytes consumed from that call. Supply Final only when the source has ended, and continue stepping to the terminal result. The shared chunk reader contract describes consumption and completion in detail.

3.17.6. Use the parsers inside a larger grammar§

HTTP.Request.parser(props) and HTTP.Response.parser(props) are the same parsers as a Parser(Parser.Utf8.Bytes, _, HTTP.Problem), for when a message is one part of a larger format. They leave the bytes after the message for the next parser:

## A capture file: a one-line label, then the raw request.
capture : Parser(Parser.Utf8.Bytes, { label : Str, request : HTTP.Request }, _)
capture =
	Parser.const(|label| |request| { label, request })
		.keep(Parser.chomp_while(|byte| byte != '\n').map(|bytes| Str.from_utf8_lossy(bytes)))
		.skip(Parser.Utf8.codeunit('\n'))
		.keep(HTTP.Request.parser({}).map_problem(|problem| HttpProblem(problem)))

saved = (capture).parse_str("health check\nGET /health HTTP/1.1\r\nHost: example.com\r\n\r\n").map_err(|error| Capture(error))?
Stdout.line!("${saved.label}: ${saved.request.target}")?

Output:

health check: /health

They fail with ParseError({ problem, location, path }), preserving the native HTTP.Problem tag. Byte runners identify generic input positions with InputOffset(offset); string runners provide original byte, line and column coordinates.

3.17.7. Rejected messages and request smuggling§

Request smuggling happens when two programs, such as a proxy and your server, disagree about where one message ends and the next begins. An attacker can then hide a second request inside the first one’s body. To avoid this, the parser rejects any message whose framing it would otherwise have to guess.

The parser rejects a message that has any of the following:

The status line has rules of its own: the status code is exactly three digits and at least 100, and the space before the reason phrase may be left out when the phrase is empty.

The complete-message constructors fail with InvalidHttp(Error), where an Error is { problem, location, path }: the tag identifies the broken rule and location: ByteOffset(offset) identifies the original byte. A framing error points at the start of the field line responsible; a missing Host field is reported at offset 0, the start of the message.

smuggled = "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 3\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n"

match HTTP.Request.parser({}).run(smuggled.to_utf8()) {
	Ok(_) => Stdout.line!("accepted")?
	Err(ParseError({ location: InputOffset(offset), problem, .. })) => Stdout.line!("rejected at byte ${offset.to_str()}: ${Str.inspect(problem)}")?
	Err(ParseError(error)) => Stdout.line!(Str.inspect(error))?
}

Output:

rejected at byte 55: both Transfer-Encoding and Content-Length

The fuzz/HttpSmugglingProperty.roc fuzz target generates messages built from these constructs and checks that each one is rejected; see the fuzzing guide.

3.17.8. Out of scope§

The API reference lists the HTTP types and parsers.

3.17.9. Performance§

A typical request or response head parses in 1.5–2 µs on an Apple M2, as fast as Go’s net/http and about 3 times slower than Rust’s httparse, which checks less (see Compare equivalent work). Line ends, tokens and field values are checked 16 bytes at a time with SIMD. A Content-Length body is returned without copying, so body size barely affects the time; a chunked body is assembled once, in linear time even with thousands of one-byte chunks (20,000 of them in 0.7 ms). See Performance for the measurements.

3.18. Parsing Roc source§

This chapter is for authors of highlighters and source analysis tools. RocSyntax parses Roc source and returns source tokens, a syntax tree and diagnostics. The implementation is pure Roc; the compiler reference worker is development tooling and is not a dependency of applications using the module. Legacy Rust compiler syntax is outside its scope.

The syntax tree represents expressions, interpolated and multiline strings, typed literals, binding patterns, declarations, control flow, records, nominal construction, imports, type annotations and declarations, constraints and associated items. Module, hosted, app, package and platform headers retain their exposures, dependencies and target configuration. The compatibility section identifies the changing compiler grammar used for validation.

3.18.1. Parse and inspect tokens§

This complete example prints the parse status and the byte ranges of numeric tokens. The documentation check compiles and runs it against the package source.

main! = |_args| {
	parsed = RocSyntax.parse_str("answer = 1 + 2 * 3\n", {})
	Stdout.line!("status: ${Str.inspect(parsed.status)}")?
	Stdout.line!("diagnostics: ${parsed.diagnostics.len().to_str()}")?
	for token in parsed.tokens {
		if token.kind == Number {
			text = Str.from_utf8(parsed.source.sublist({ start: token.start, len: token.end - token.start })) ?? ""
			Stdout.line!("number ${text} at bytes ${token.start.to_str()}..${token.end.to_str()}")?
		}
	}
	Ok({})
}

Output:

status: Complete
diagnostics: 0
number 1 at bytes 9..10
number 2 at bytes 13..14
number 3 at bytes 17..18

3.18.2. Source locations and trees§

Tokens retain half-open UTF-8 byte spans: start includes the first byte and end points immediately after the token. Comments and whitespace remain in the token stream, so tooling can preserve the exact source even when it is incomplete. Keep the original source alongside the result when resolving spans.

Each diagnostic uses { problem, location, path }. Its typed code is diagnostic.problem.code, and its half-open source interval is diagnostic.problem.span. location identifies the original byte position; consumers can distinguish failures without matching rendered strings.

The tree uses node indices and a separate child-index buffer. Follow a node’s child range through that buffer to visit its children; do not assume adjacent nodes are siblings. This representation supports large inputs without allocating a separate recursive container for every syntax construct.

Byte offsets are not character columns. An editor adapter must convert positions to the coordinate convention requested by its client. Build a line-start index once when converting many locations in the same source.

3.18.3. Highlighting and lint rules§

A highlighter can start from tokens for comments, keywords, strings, literals and punctuation. Syntax structure adds context for declarations, calls and expressions. Preserved tokens continue to be useful when a node is malformed.

Use RocSyntax.tokenize_str or tokenize_bytes when only lexical highlighting is needed. They return source-backed tokens and diagnostics without constructing a tree. RocSyntax.highlight_category(token.kind) maps each token to a presentation category without allocating strings or choosing theme colors. The runnable examples/roc-syntax.roc demonstrates both entry points and ships in the examples archive, where it is also checked against the bundled package.

Lint rules can traverse the tree and report diagnostics at source spans. Grouping and operator precedence are represented structurally. Rules that need binding or type information must add scope analysis or use compiler information; parsing alone does not establish that a program typechecks.

Check the parse status and diagnostics before applying a rule that assumes a complete tree. Recovery returns partial structure for malformed input. Nesting at the configured limit, or a document above the token limit, returns Limited; tokens remain available for highlighting. Code completion and highlighting should tolerate partial results.

3.18.4. Compatibility and validation§

The syntax reference is compiler commit f7bef6b38f23797eb34b291ced98af71ea37dd3b from upstream main. The implementation compiler is not pinned: CI builds and tests the package with the latest Roc nightly, and each validation run records the nightly it used. This identifies a changing language implementation, rather than a stable language specification. Updating the syntax reference is a separate maintenance decision.

The differential corpus records individual syntax rules and their coverage. For accepted input every compiler AST node must have a candidate node of the corresponding kind with the same byte span, apart from the leading . the compiler keeps in a name’s token and the arrow it keeps in front of a call. Raw fuzzing checks source preservation and structural invariants; structured generation checks independently constructed syntax. roc-syntax-spans holds any source to span invariants that need no reference: in a complete parse a node covers whole tokens and both halves of each delimiter pair, and in any parse children keep to their own bytes. roc-syntax-span-model writes files while noting each node’s exact span, including around comments and newlines inside delimiters, and the validation run reviews its corpus against the compiler. Performance runs use optimized builds and measure scaling as documents grow.

3.18.5. Run the feedback loop§

Install the latest Roc nightly, Zig 0.17.0 and Python 3, or let blueprint run roc-syntax provide Zig (Contributing to roc-parser). From the repository root, run:

ROC=/path/to/roc ZIG=/path/to/zig python3 scripts/check_roc_syntax.py

The command checks the package, builds the pinned compiler oracle and optimized Roc probe, checks the live protocol, compares curated cases plus 1,000 independently generated files, blocks, annotations and expressions, checks dedicated type, platform and adversarial grammar fixtures, compares acceptance across the complete pinned snapshot corpus, and compares tokens and normalized trees for every compiler-accepted input. It checks editor recovery, runs both fuzz smoke campaigns, compares mutated inputs with the compiler oracle, and checks performance scaling. It rebuilds candidate probes and records source and executable hashes. Its validation.json, step logs, differential failures and performance results live under .roc-parser-tmp/roc-syntax-validation/.

The first oracle build fetches the pinned compiler Git objects when no sibling compiler repository is available. --roc-source /path/to/roc selects an existing Git repository; its working files are ignored. Build caches stay under the validation output directory. Compiler backend libraries are not required.

Use --generated 10000 --mutation-runs 10000 --fuzz-runs 10000 for a larger campaign. Add --allocations to collect allocation/reallocation call counts. For performance comparisons, save the first run’s performance.json and pass it as --baseline /path/to/before.json on a later run on the same machine and compiler. An allocation call count does not measure bytes copied or peak memory; inspect timing growth too.

For the local per-input speed ratchet against the actual compiler parser, add --reference-performance full. Both workers parse the same immutable source repeatedly in optimized builds; startup, JSON transport and tree projection are outside the timed region. Reports contain per-input medians, samples and ratios, and retain failing inputs for replay. The target ceiling is --max-reference-ratio 1.5. Every input in the full comparison meets it on the development machine; the ratchet reports any input that regresses past it. See the measurement protocol and replay commands.

CI uses only --reference-performance smoke: two tiny inputs with a short measurement window and no speed-ratio assertion. On Linux the scaling check records the comment family but does not assert its ratio: the basic-cli host’s musl allocator can unmap and remap a page on every parse at some sizes, which hides the parser’s own scaling (basic-cli#530). The full comparative ratchet is a local development tool. Deep synthetic operator chains explicitly report partial depth-limited structural eligibility checks; ordinary conformance gates continue to require complete normalized tree comparisons.

Pull requests use seed 1 for reproducible differential generation. Scheduled and manual exploration use the GitHub run ID as a fresh reproducible seed; --seed RUN_ID reproduces that run locally. Reports retain complete source, models, commands and failures in the syntax validation artifact for 14 days. Promote minimized failures into committed fixtures before that artifact expires. Fresh seeds explore additional combinations; they do not prove grammar coverage. Scheduled exploration runs 10,000 generated cases and 10,000 mutations, restores the raw fuzz corpus and runs a fresh smoke campaign before oracle comparisons.

Generated models include headers, imports, type declarations, constraints and statement sequences. Six arithmetic operators use only the parentheses needed to preserve the independently constructed model. Use --expression-depth, --pattern-depth, --type-depth and --statements to tune bounded generation; reports record those budgets. See the model families and remaining omissions.

The editor recovery gate checks damaged files with healthy declarations before and after each edit, including UTF-8 comments and headers. Surviving declarations must retain their exact byte spans and independently specified trees. Diagnostics must stay inside the damaged region. Compiler rejection confirms that an edit is malformed; compiler recovery layout is not used as the candidate’s expected tree. These contracts cover specific edit classes rather than promising recovery from every possible incomplete program.

The oracle mutation lane edits accepted seeds from all four fixture sets and the complete upstream corpus. Every mutation must agree on acceptance; accepted mutations must also agree on tokens, normalized trees and node spans. The lane additionally compares unchanged UTF-8 inputs harvested from the raw fuzz corpus when present. Inputs that are not valid UTF-8 are compared too, and exclusions are counted explicitly. Mutation provenance records exact byte edits, seeds and original source so each failure can be replayed without the original corpus. See mutation campaign commands and limits.

To import the larger upstream source corpus, run:

python3 scripts/import_roc_syntax_snapshots.py --roc-source /path/to/roc

The importer reads the pinned commit’s immutable snapshot blobs and records source hashes, parse modes and skipped snapshot kinds. This is an exploratory coverage corpus, including invalid and historical examples. It is not a list of programs the current compiler promises to accept. Feed the generated fixtures to scripts/review_roc_syntax.py with the built probe and oracle commands. Every disagreement remains available for inspection; unknown projections and unsupported syntax must not be presented as structural agreement.

For each parser improvement, add a curated case for the relevant rule, check its normalized structure against the reference, extend the independent generator where useful, and rerun scaling when the parser’s traversal or allocation changes. Preserve minimized fuzz failures as source fixtures so the check survives corpus and generator changes.

3.19. Combinators by task§

This chapter is for readers who have finished Your first parser and are writing a parser of their own. It groups Parser combinators and Parser.Utf8 primitives by the job they do, and explains the three rules that most often surprise people: how alternatives backtrack, when repetition stops, and why parsers read bytes rather than characters. The API reference has the exact signature of every function.

The examples come from combinators-behaviour.roc; every expect in them passes.

3.19.1. Running a parser§

FunctionUse it to
p.parse_str(str)Parse a whole Str. Returns Ok(value) or Err(ParseError({ problem, location, path })), where location identifies the furthest failure. Text runners report Text({ offset, line, column }); raw runners use InputOffset(offset). Input left over is a failure too.
p.parse(bytes)The same for a List(U8).
p.run_str(str), p.run(bytes)Parse the start of the input and return { value, rest }, where rest is what is left. Use these to read one message from a stream that holds several.
p.parse(input), p.run(input)Run a parser whose input is not text, such as the CSV.Record that record parsers read. Call them as methods: p.parse(input).

3.19.2. Reading text§

All of these read from Parser.Utf8.Bytes, which is List(U8).

ParserReads
Parser.Utf8.codeunit(b)Exactly the byte b, such as Parser.Utf8.codeunit('=').
Parser.Utf8.codeunit_satisfies(pred)One byte that pred accepts.
Parser.Utf8.any_codeunit({})Any one byte.
Parser.Utf8.string(s), Parser.Utf8.utf8(bytes)Exactly the given text, returned as a Str or as bytes.
Parser.Utf8.digit({}), Parser.Utf8.digits({})One ASCII digit, or a run of them, as a U64. digits fails on a number too large for a U64.
Parser.chomp_while(pred)Bytes while pred accepts them. Always succeeds, possibly reading nothing.
Parser.chomp_until(b)Bytes up to, but not including, the byte b. Fails if b never appears.
Parser.Utf8.rest_str({}), Parser.Utf8.rest({})All remaining input, as a Str (failing if it is not valid UTF-8) or as bytes.

3.19.3. Sequencing and transforming§

Start a sequence with a value such as Parser.const(|a| |b| ...).

CombinatorDoes
Parser.const(v)Reads nothing and produces v, which can be a function that receives the results of subsequent keep calls.
p.keep(q)Runs q after p and passes its result to the function p produced.
p.skip(q)Runs q after p and discards q’s result.
p.map(f), Parser.map2, Parser.map3Transforms one, two or three results with an ordinary function.
p.between(open, close)Reads open, p, close and keeps only p’s result.
p.ignore()Keeps the input p reads but replaces its result with {}.
p.flatten()Turns a parser producing Try(a, Parser.Problem(p)) into one producing a, where Err(problem) becomes a parse failure with that structured tag.

flatten is how you validate a value while parsing. Here a number above 65535 is rejected with a tag carrying the authored value and allowed maximum:

# Validate a value and turn a rejection into a parse failure.
port : Parser(Parser.Utf8.Bytes, U64, _)
port =
	Parser.Utf8.digits({})
		.map(
			|n| if n <= 65535 {
				Ok(n)
			} else {
				Err(PortOutOfRange({ value: n, max: 65535 }))
			},
		)
		.flatten()

expect (port).parse_str("8080") == Ok(8080)
expect (port).parse_str("70000") == Err(ParseError(Parser.Diagnostic.{ problem: PortOutOfRange({ value: 70000, max: 65535 }), location: Text({ offset: 0, line: 1, column: 1 }), path: [] }))

The third Parser parameter is the grammar’s problem union. Primitive UTF-8 constructors extend Parser.Utf8.Problem(p) with your tags, preserving each branch’s payload through composition. Parameterless primitives are factories, such as Parser.Utf8.digits({}), so they can adopt the surrounding grammar’s problem type. An infallible top-level parser can use [] as its problem parameter.

A native format parser has its own closed problem union. When combining it with another grammar, use map_problem to retain its domain as a structured payload: Yaml.parser({}).map_problem(|problem| YamlProblem(problem)). This changes only problem identity; byte offsets and furthest-failure selection remain intact. A caller can match YamlProblem(detail) without parsing text.

3.19.4. Alternatives and optional parts§

CombinatorDoes
Parser.one_of(list)Tries each parser in order; the first success wins. On failure, reports the furthest failure, using the later failure on a tie.
Parser.one_of(list), Parser.alt(p, q)The same furthest-failure behavior for any input type.
Parser.maybe(p)Produces Ok(value) if p succeeds, or Err(Missing) without reading anything if it fails.
Parser.fail(problem)Always fails with the structured problem. Useful as the last alternative.
# An optional leading sign, then digits.
signed : Parser(Parser.Utf8.Bytes, I64, _)
signed =
	Parser.const(
		|sign| |n| {
			match sign {
				Ok(_) => -(n.to_i64_wrap())
				Err(Missing) => n.to_i64_wrap()
			}
		},
	)
		.keep(Parser.maybe(Parser.Utf8.codeunit('-')))
		.keep(Parser.Utf8.digits({}))

expect (signed).parse_str("-42") == Ok(-42)
expect (signed).parse_str("42") == Ok(42)

3.19.4.1. Alternatives always backtrack§

When an alternative fails, the next one starts from the same position the first one started from, however much input the failed one had examined. There is no "commit" point, so you never need a try wrapper as in some other parser libraries:

# Both alternatives start with "ab". When the first fails at "d", the
# second starts again from the beginning of the same input.
abc_or_abd : Parser(Parser.Utf8.Bytes, Str, _)
abc_or_abd = Parser.one_of([Parser.Utf8.string("abc"), Parser.Utf8.string("abd")])

expect (abc_or_abd).parse_str("abd") == Ok("abd")

The cost is that the first success wins, even when a later alternative would have read more. Put longer or more specific alternatives first:

# The first alternative that succeeds wins, even if a later one would
# consume more input. Put the longer keyword first.
keyword_short_first : Parser(Parser.Utf8.Bytes, Str, _)
keyword_short_first = Parser.one_of([Parser.Utf8.string("in"), Parser.Utf8.string("int")])

keyword_long_first : Parser(Parser.Utf8.Bytes, Str, _)
keyword_long_first = Parser.one_of([Parser.Utf8.string("int"), Parser.Utf8.string("in")])

expect (keyword_short_first).run_str("int").map_ok(|r| r.rest) == Ok("t")
expect (keyword_long_first).parse_str("int") == Ok("int")

With the short keyword first, "int" reads as "in" and leaves t behind.

Failures from alternatives retain the furthest location; the later failure wins only when both reach the same position. Distinct prefixes still make grammars easier to understand and avoid repeated work.

3.19.5. Repetition§

CombinatorProduces
Parser.many(p)Zero or more results of p.
Parser.one_or_more(p)One or more results; fails if the first p fails.
p.sep_by(sep)Zero or more results of p separated by sep. The separators are dropped.
p.sep_by_one_or_more(sep)The same, requiring at least one.

These combinators use the input length to detect progress.

3.19.5.1. Repetition stops when an element makes no progress§

Repetition ends at the first element that fails, or that succeeds without reading any input. Without the second condition, repeating a parser that can succeed on empty input, such as chomp_while or maybe(p), would loop forever:

# chomp_while always succeeds, possibly consuming nothing. `many` stops at
# the first element that makes no progress instead of looping forever.
runs : Parser(Parser.Utf8.Bytes, List(List(U8)), [])
runs = Parser.many(Parser.chomp_while(|b| b == 'a'))

expect (runs).run_str("aab").map_ok(|r| r.rest) == Ok("b")

3.19.5.2. A failed element ends the repetition, not the parse§

When an element fails part-way through, repetition ends before that element and leaves its input unread. The repetition itself succeeds:

# A repeated element that fails part-way is not an error for `many`:
# repetition ends before that element, and its input is left unconsumed.
pair : Parser(Parser.Utf8.Bytes, U64, _)
pair = Parser.Utf8.digits({}).skip(Parser.Utf8.codeunit(';'))

expect (Parser.many(pair)).run_str("1;2;3x").map_ok(|r| r.rest) == Ok("3x")

The failure of the element that stopped the repetition is kept, though. So when you parse a whole input with parser.parse_str, a bad element in the middle of a list is reported by its own problem tag and offset. Report parse errors shows how to turn that into a useful message.

3.19.6. Recursive structures§

A parser for a nested format refers to itself. Make it a function, because only functions can refer to themselves, and wrap the call in Parser.lazy so that it is built only when it is needed:

# A tree is a number or a bracketed, comma-separated list of trees: [1,[2,3]]
Tree := [Leaf(U64), Node(List(Tree))]

tree : {} -> Parser(Parser.Utf8.Bytes, Tree, _)
tree = |{}|
	Parser.one_of([
		Parser.Utf8.digits({}).map(|n| Leaf(n)),
		Parser.lazy(|_| tree({}))
			.sep_by(Parser.Utf8.codeunit(','))
			.between(Parser.Utf8.codeunit('['), Parser.Utf8.codeunit(']'))
			.map(|children| Node(children)),
	])

leaves : Tree -> U64
leaves = |t| {
	match t {
		Leaf(_) => 1
		Node(children) => children.fold(0, |sum, child| sum + leaves(child))
	}
}

expect (tree({})).parse_str("[1,[2,3],[]]").map_ok(leaves) == Ok(3)

Two top-level parsers can refer to each other in the same way. One lazy in the cycle is enough: here expr uses term directly, and term reaches back to expr through lazy:

# A sum of two terms, where a term is a number or a bracketed sum: 1+(2+3)
term : Parser(Parser.Utf8.Bytes, U64, _)
term =
	Parser.Utf8.digits({})
		.alt(Parser.lazy(|_| expr).between(Parser.Utf8.codeunit('('), Parser.Utf8.codeunit(')')))

expr : Parser(Parser.Utf8.Bytes, U64, _)
expr =
	Parser.const(|x| |y| x + y)
		.keep(term)
		.skip(Parser.Utf8.codeunit('+'))
		.keep(term)

expect (expr).parse_str("1+(2+3)") == Ok(6)

Without any lazy, the compiler rejects the pair: only functions can be recursive, and each parser is a value.

3.19.7. Bytes, not characters§

The Parser.Utf8 parsers read UTF-8 code units, which are bytes. An ASCII character is one byte, but other characters take two to four. é, for example, is the two bytes 0xC3 0xA9:

# Parsers read UTF-8 code units (bytes), not characters. "é" is two bytes.
expect Parser.Utf8.any_codeunit({}).run("é".to_utf8()).map_ok(|result| result.value) == Ok(0xC3)
expect Parser.Utf8.any_codeunit({}).run_str("é").is_err()
expect (Parser.Utf8.string("é")).parse_str("é") == Ok("é")

This has three consequences:

3.19.8. Parsers over other input§

Parser(input, a, problem) works for any input type, not only bytes. The CSV module’s record parsers read a CSV.Record, a list of fields, and CSV.field(p) runs a byte parser p on one field. To write a primitive parser for your own input type, use Parser.custom with a function from input to Ok({ value, rest }) or Err(ParseError(Parser.Diagnostic.{ problem, location: InputOffset(offset), path: [] })). Use a typed native problem union; the runner adds generic parser failures and preserves explicit locations and logical paths.

3.20. Parse a format of your own§

This guide shows how to turn a line-based text format into typed Roc values, with tests for each part and error messages that name the line at fault. It assumes you know the combinators from Your first parser.

The running example is an application log with one entry per line:

08:00 INFO started
12:30 WARN disk almost full

The finished program is custom-format-log.roc.

3.20.1. 1. Write down the result types§

Decide what a caller should get back before writing any parser. Use tags for fixed sets of words and records for groups of fields:

Level : [Info, Warn, Error]

Time : { hour : U64, minute : U64 }

Entry : { time : Time, level : Level, message : Str }

3.20.2. 2. Write one parser per part§

Give each part of the line its own parser, named after what it reads. Small parsers are easier to test, and their names make the larger parser read like the format’s description:

two_digits : Parser(Parser.Utf8.Bytes, U64, _)
two_digits =
	Parser.const(|tens| |ones| tens * 10 + ones)
		.keep(Parser.Utf8.digit({}))
		.keep(Parser.Utf8.digit({}))

time : Parser(Parser.Utf8.Bytes, Time, _)
time =
	Parser.const(|hour| |minute| { hour, minute })
		.keep(two_digits)
		.skip(Parser.Utf8.codeunit(':'))
		.keep(two_digits)
		.map(
			|t| if t.hour < 24 and t.minute < 60 {
				Ok(t)
			} else {
				Err(NoSuchTime)
			},
		)
		.flatten()

level : Parser(Parser.Utf8.Bytes, Level, _)
level =
	Parser.one_of([
		Parser.const(Info).skip(Parser.Utf8.string("INFO")),
		Parser.const(Warn).skip(Parser.Utf8.string("WARN")),
		Parser.const(Error).skip(Parser.Utf8.string("ERROR")),
	])

message : Parser(Parser.Utf8.Bytes, Str, _)
message = Parser.Utf8.rest_str({})

Notes on the choices made here:

3.20.3. 3. Combine the parts§

entry : Parser(Parser.Utf8.Bytes, Entry, _)
entry =
	Parser.const(|t| |l| |m| { time: t, level: l, message: m })
		.keep(time)
		.skip(Parser.Utf8.codeunit(' '))
		.keep(level)
		.skip(Parser.Utf8.codeunit(' '))
		.keep(message)

3.20.4. 4. Test each parser§

Put expect tests next to the parsers, covering at least one accepted and one rejected input for each:

expect (time).parse_str("09:05") == Ok({ hour: 9, minute: 5 })
expect (time).parse_str("24:00") == Err(ParseError(Parser.Diagnostic.{ problem: NoSuchTime, location: Text({ offset: 0, line: 1, column: 1 }), path: [] }))
expect (level).parse_str("WARN") == Ok(Warn)

expect
	(entry).parse_str("12:30 WARN disk almost full")
		== Ok({ time: { hour: 12, minute: 30 }, level: Warn, message: "disk almost full" })

expect (entry).parse_str("12:30 DEBUG hello").is_err()

Run them:

roc test custom-format-log.roc

The compiler reports how many tests passed. A failing test prints the expression and the values on each side of ==.

3.20.5. 5. Split the input into records yourself§

For a format with one record per line, split the text into lines and parse each line separately, rather than describing the whole file with sep_by. You get three benefits:

Problem : { line : U64, problem : Parser.Problem(Parser.Utf8.Problem([NoSuchTime])) }

parse_log : Str -> Try(List(Entry), Problem)
parse_log = |text| {
	var $entries = []
	var $number = 0
	for line in text.split_on("\n") {
		$number = $number + 1
		match (entry).parse_str(line) {
			_ if line.is_empty() => {}
			Ok(e) => {
				$entries = $entries.append(e)
			}
			Err(ParseError({ problem, .. })) => return Err({ line: $number, problem })
		}
	}
	Ok($entries)
}

This splits on \n only. If your files can come from Windows, also strip a trailing \r from each line.

3.20.6. 6. Use it§

level_name : Level -> Str
level_name = |l| {
	match l {
		Info => "info"
		Warn => "warning"
		Error => "error"
	}
}

summary : Str -> Str
summary = |text| {
	match parse_log(text) {
		Ok(entries) =>
			Str.join_with(entries.map(|e| "${level_name(e.level)} at ${e.time.hour.to_str()}h: ${e.message}"), "\n")
		Err({ line, problem }) => "line ${line.to_str()}: ${Str.inspect(problem)}"
	}
}

main! = |_args| {
	Stdout.line!(summary("08:00 INFO started\n12:30 WARN disk almost full\n"))?
	Stdout.line!(summary("08:00 INFO started\n\n25:00 ERROR late\n"))
}

Run the program:

roc custom-format-log.roc

Output:

info at 8h: started
warning at 12h: disk almost full
line 3: NoSuchTime

The second log has a blank second line, which is skipped, and a time that does not exist on the third line, which is reported with that line number.

3.20.7. When the format is not line-based§

If records can span lines, as in a nested or bracketed format, you cannot split the input first. Describe the whole input with one parser instead:

3.21. Report parse errors§

Parser failures carry descriptive tags and structured payloads. Match the tags when deciding what to do; render text when displaying a failure. Locations, configured limits, expected types and offending source values remain available without extracting them from a diagnostic string.

The examples below come from errors-report.roc. Their complete output is checked against errors-report.expected.

3.21.1. Shared diagnostic metadata§

A diagnostic is Parser.Diagnostic(problem), a transparent nominal record with problem, location and path. The problem remains a descriptive tag union owned by the format. Source strings carry names, keys or authored values; callers never need to match prose to distinguish failures.

LocationMeaning
Text({ offset, line, column })Original byte offset and one-based physical line and byte column
ByteOffset(offset)Original byte offset when text coordinates are not retained
LineColumn({ line, column })Known text coordinates without an original byte offset
InputOffset(index)Element index in the generic parser’s input, which may not be bytes
Unavailable(reason)NotRetained, GeneratedValue or NotApplicable; no fabricated zero position

The logical path is independent of the physical location. Its segments are Record(number), Field(number), Index(index), Key(key) and Source(name). Record and field numbers are one-based; indexes are zero-based. A decoded value can therefore retain a key or field identity even when source bytes are gone.

OperationFailure
Format parse_str, parse_bytes and validate constructorsA native tag such as InvalidYaml(error), InvalidXml(error), InvalidNamespace(error), InvalidJsonValue(error), InvalidCsv(error) or InvalidHttp(error)
parser.parse, parser.run, parser.parse_str, parser.run_strParseError(diagnostic) with the grammar’s problem union plus generic runner failures
CSV positional row parserIts generic runner preserves InvalidCsv(error) or InvalidRow(error) inside diagnostic.problem
value.encode(props)A typed format error describing why a programmatically constructed value cannot be written
Markdown.parse_strNone: every input is a Markdown document

Resource failures retain max and observed. Exact(count) is a complete count; AtLeast(count) records the first proven violation without scanning the remaining input. Offending scalar values use owned bounded excerpts containing at most forty bytes and the original total_bytes; keeping a failure does not retain the complete input buffer. Complete identifiers remain available when identity is needed.

Quotation hooks render typed failures into BadQuotedBytes(Str) because compiler diagnostics require text. Runtime validation retains native tags. Failure unions compose with ?, including application and I/O errors. InvalidJsonValue remains distinct from builtin Json’s InvalidJson.

3.21.2. Show the source location§

Print one-based locations in file:line:column: explanation form. The example uses Str.inspect(problem) at the display boundary; applications can instead match selected problem tags and provide their own wording or translations.

location_text : Parser.Location -> Str
location_text = |location| match location {
	Text({ line, column, .. }) => "${line.to_str()}:${column.to_str()}"
	LineColumn({ line, column }) => "${line.to_str()}:${column.to_str()}"
	ByteOffset(offset) => "byte ${offset.to_str()}"
	InputOffset(offset) => "input ${offset.to_str()}"
	Unavailable(reason) => "location unavailable (${Str.inspect(reason)})"
}

render_error : Str, Parser.Diagnostic(problem) -> Str
render_error = |name, error| {
	path = if (error.path.is_empty()) "" else {
		shown_path = Str.inspect(error.path)
		" (path ${shown_path})"
	}
	location = location_text(error.location)
	problem = Str.inspect(error.problem)
	"${name}:${location}: ${problem}${path}"
}

yaml_message : Str, Str -> Str
yaml_message = |file_name, source| match Yaml.parse_str(source, {}) {
	Ok(_) => "${file_name}: ok"
	Err(InvalidYaml(error)) => render_error(file_name, error)
}

xml_message : Str, Str -> Str
xml_message = |file_name, source| match Xml.parse_str(source, {}) {
	Ok(_) => "${file_name}: ok"
	Err(InvalidXml(error)) => render_error(file_name, error)
}

json_message : Str, Str -> Str
json_message = |file_name, source| match JsonValue.parse_str(source, {}) {
	Ok(_) => "${file_name}: ok"
	Err(InvalidJsonValue(error)) => render_error(file_name, error)
}

config.yaml:2:7: UnclosedFlowCollection
feed.xml:2:10: EndTagMismatch({ actual: "feed", expected: "entry" })
package.json:2:15: TrailingComma

Limits and optional checks report structured failures through the same located envelope. Unsupported YAML features and XML namespace failures retain their own problem tags; applications can distinguish them from malformed syntax.

3.21.3. CSV: report the record and field§

CSV paths identify the original record and field, including headers and blank records. Physical coordinates identify the failed field. Custom field parsers retain their typed problem and diagnostic metadata under FieldProblem and ParserFailure; the row adapter adds the record context.

Row : { name : Str, count : U64 }

row : Parser(CSV.Record, Row, _)
row =
	CSV.record(|name| |count| { name, count })
		.keep(CSV.field(CSV.string({})))
		.keep(CSV.field(CSV.u64({})))

csv_message : Str -> Str
csv_message = |source| {
	match CSV.rows_parser({ row: row }).parse_str(source) {
		Ok(rows) => "${rows.len().to_str()} rows"
		Err(ParseError({ problem: InvalidCsv(error), .. })) => render_error("CSV", error)
		Err(ParseError({ problem: InvalidRow(error), .. })) => render_error("CSV row", error)
		Err(ParseError(error)) => render_error("CSV parser", error)
	}
}

CSV:2:7: Syntax(UnterminatedQuotedField) (path [Record(2), Field(2)])
CSV row:2:7: ParserFailure(FieldProblem({ location: ByteOffset(0), path: [], problem: ExpectedValue({ expected: U64, source: { bytes: [109, 97, 110, 121], total_bytes: 4 } }) })) (path [Record(2), Field(2)])
CSV row:1:10: UnreadFields({ found: 3, read: 2 }) (path [Record(1), Field(3)])

Native scalar failures retain the expected type and at most 40 bytes of the failed cell. CSV.decode_str also reports native MissingRequiredField(name) when a required column is absent. Custom parser payloads retain the information supplied by that parser.

3.21.4. HTTP: reject invalid framing§

HTTP problems distinguish malformed syntax, ambiguous framing and configured resource limits. A server can reject an invalid request and close the connection while recording the problem and byte offset for diagnosis.

http_message : Str -> Str
http_message = |source| {
	match HTTP.Request.parse_bytes(source.to_utf8(), {}) {
		Ok(request) => "request for ${request.target}"
		Err(InvalidHttp(error)) => render_error("400 Bad Request", error)
	}
}

400 Bad Request:byte 0: MissingHost

HTTP.Request.parse_bytes(bytes, {}) validates one complete message. HTTP.Request.parser({}).run(bytes) returns a prefix and its remaining bytes; the request decoder validates a complete pipeline, including its final boundary.

3.21.5. Streaming and custom parsers§

Chunk readers forward native format failures directly. SourceErr(cause) preserves an I/O failure unchanged; it never finalizes truncated input as EOF. A safely framed invalid record can produce Rejected(nativeError) when recovery is configured. Resource limits, broken framing and invalid encoding remain terminal. Adapter failures are structured DecoderProtocol or RecoveryLimitExceeded diagnostics. See Read and write documents in chunks.

Define domain failures with payloads, such as InvalidTime({ hour, minute }). A custom primitive returns Err(ParseError(Parser.Diagnostic.{ problem, location: InputOffset(offset), path: [] })). Alternatively validate a parsed value with .map(...).flatten(). The runner preserves the furthest failure and explicit logical context; parse_str and run_str turn inferred byte indexes into original text positions.

When combining a native format parser with another grammar, use .map_problem(|problem| JsonProblem(problem)) to give its problems a domain tag. This changes problem identity while preserving diagnostic locations and paths. Primitive factories such as Parser.Utf8.digits({}) and CSV.string({}) infer the surrounding extensible problem type.

3.22. Parse and validate during compilation§

Compile-time parsing and useful validation are enduring design goals for every format in roc-parser. A quoted document or an adjacent imported file should become a ready-to-use Roc value, with invalid input explained during compilation. The same pure parser supports runtime input; checked conversions add configurable checks without changing the ordinary parsing APIs.

3.22.1. Quoted documents§

Annotate a quotation with its target type. Roc calls that type’s from_quote method during compilation. A malformed document, a failed decoding, or an enabled lint returns Err(BadQuotedBytes(message)), which Roc reports as a compiler error. Successful quotations become parsed constants.

Person : { name : Str, age : U8 }

people : CSV.Decoded(Person)
people =
	\\name,age
	\\Ada,36

table : CSV
table =
	\\name,age
	\\Ada,36

people.value holds typed records; table.records holds the raw CSV fields as bytes, including the header. Raw CSV does not assume its first row is a header.

Quotation typeAccess to the parsed result
YamlThe document tree itself
Yaml.Decoded(a).value, decoded as a
QIF.events, ordered raw section/account/record events
OFX.statements, supported bank/card statements
CSV.records, raw CSV records
CSV.Decoded(row).value, a list of decoded rows
XmlThe document’s .root and .declaration
JsonValueThe JSON value itself
Markdown.Document.blocks, a list of Markdown blocks
AsciiDoc.title and .blocks
HTTP.Request, HTTP.ResponseThe request or response itself
RocSyntax.source, .tokens, .nodes, and the other parsed fields

YAML’s supported subset and XML’s well-formedness rules apply to quotations too. XML quotations do not validate an XSD or process namespaces. Roc source quotations check supported syntax, not names or types. HTTP quotations contain exactly one complete message, with the CRLF line endings its grammar requires. JSON quotations are strict RFC 8259 and reject repeated keys. Roc processes backslash escapes in multi-line strings too, so write a JSON escape such as \u00e9 with a doubled backslash, \\u00e9.

3.22.2. Adjacent files§

Keep the dedicated file extension for editor highlighting. Import the file as Str, then require a checked conversion in a top-level declaration:

import "assets/compile-time-settings.yaml" as settings_text : Str

Config : { name : Str, port : U16 }

config : Config
config = Parser.Quotation.require(
	Yaml.validate_as({
		source: settings_text,
		name: "compile-time-settings.yaml",
		unknown_keys: Reject,
	}),
)

The imported file is read and the constant is parsed during compilation. The compiled program needs neither the file nor a runtime parser call for that constant. Roc file imports accept Str or List(U8); they do not invoke a custom type’s from_quote method directly.

Parser.Quotation.require unwraps a successful checked conversion and renders its structured failure if the conversion fails. Use it for demanded top-level constants. With dynamic input, keep the validator’s Try result and handle its error: a failing require call that executes at runtime crashes at runtime. Uninstantiated generic code is not a promise that its dependent quotations have been checked.

3.22.3. Props and extra checks§

Validators take a props record. source is required; name defaults to "<literal>". Other fields have defaults, so callers supply only overrides:

answer = Parser.Quotation.require(
	Yaml.validate({
		source: "answer: yes",
		lints: { legacy_booleans: False },
	}),
)

This accepts the YAML string yes deliberately. Without the override, the legacy-boolean check rejects it. Quote yes to retain text, or use true or false for a Boolean.

Each format has validate; YAML and CSV also have validate_as for type-directed decoding. JSON has validate but no validate_as; decode JSON into records with Roc’s builtin Json. HTTP provides HTTP.Request.validate and HTTP.Response.validate. Runtime validators return the natural parsed value and format-tagged structured diagnostics. Only from_quote and explicit quotation adapters render those failures as BadQuotedBytes(Str).

The initial extra checks are enabled by default. Set an individual Boolean to False to accept that construct. Invalid syntax, type mismatches, unsupported features, and resource limits remain errors regardless of lint settings.

FormatLint fieldRejects
YAMLlegacy_booleansUnquoted yes, no, on, or off scalar values, ignoring case
CSVduplicate_headers, empty_headersDuplicate or empty names when a header mode is selected
XMLnon_utf8_declarationA non-UTF-8 declaration in already-decoded text
Markdownunclosed_fencesA code fence closed implicitly rather than by a closing marker
AsciiDocunexpanded_includesAn active include directive left unexpanded
HTTPrepeated_content_lengthRepeated identical Content-Length values
Roc sourceplaceholdersPlaceholder ... expressions

JSON has no lint fields. Its props take options, the same JsonValue.Props record that JsonValue.parse_str(source, props) takes, so JSONC, a byte order mark, a duplicate-key policy such as CollapseIdentical, size limits and I-JSON numbers are chosen the same way at compile time as at run time (see Choose how strict to be).

Markdown’s ordinary parser accepts every string. Its fence check is an additional policy, not a claim that CommonMark requires closing fences. Checks use parsed context, so quoted strings, comments, escaped directives, and code are handled according to their format.

YAML typed props support keys (SnakeCase, KebabCase, or CamelCase) and unknown_keys (Skip or Reject), defaulting to SnakeCase and Skip. CSV props support header: Absent, Exact, or Normalized: raw validation defaults to Absent, typed validation to Exact. Normalized header checks use normalized names. Raw results retain the original header fields. AsciiDoc props expose explicit include sources and attributes, a depth limit of 16, and expand_includes: False by default. Validation never discovers or reads include files itself.

3.22.4. Simple, useful errors§

Runtime diagnostics carry problem, location, and path; unavailable positions are explicit. Source names and logical field or record positions remain structured. Compiler quotations render the source and format, any available line and byte column, and the first problem. For example:

settings.yaml:2:7: YAML: UnclosedFlowCollection

CSV errors also identify the record and field. Missing required fields are named even when there is no source coordinate. Syntax is checked before extra checks, and extra checks before decoding. Checks report the first relevant source location. These messages are a starting point that can improve without changing how callers select validation policy.

3.22.5. Your own quotation type§

Parser.Quotation.check({ parser, source }) adapts a combinator parser, requiring complete consumption and retaining its furthest-error location:

Count := { value : U64 }.{
	from_quote : Str -> Try(Count, [BadQuotedBytes(Str)])
	from_quote = |source| Parser.Quotation.check({ parser: Parser.Utf8.digits({}), source }).map_ok(|value| Count.{ value })
}

count : Count
count = "42"

For a format-specific application type, call a configured validate_as from from_quote, add any pure domain checks, and render failures with Parser.Quotation.error at that compiler boundary. Keep native typed problems and their available locations in runtime APIs.

The complete example is checked, tested, and run in CI. Each format’s quotation and imported-file rejection contracts are checked by the compiler tests.

3.22.6. Checked web markup and typed templates§

Html, Svg and MathML provide checked document and Fragment literals, imported-source validation, and Template(a) interpolation. Fixed markup is parsed during compilation; typed hole values remain separate from the plan. See Parse HTML, SVG and MathML for legal hole positions, syntax selection, validation limits, and a tested downstream consumer example.

4. Reference§

4.1. Modules§

Find the public owner for a value or task in this table. The API reference gives every signature; the format chapters describe supported syntax and resource limits.

4.1.1. The modules at a glance§

The package exposes fifteen roots. Shared parser primitives and quotation helpers live under Parser; format-specific constructors live with their values.

ModuleUse it toStart from
AsciiDocParse and preprocess AsciiDocAsciiDoc.parse_str(source, {})
CSVRead raw fields or decode recordsCSV.parse_str(source, {}), CSV.decode_str(source, {})
HTTPRead request and response messagesHTTP.Request.parse_bytes(bytes, {}), HTTP.Response.parser({})
HtmlCheck HTML documents, fragments and templatesHtml.parse_str, Html.parse_fragment, Html.RepairContext
JsonValueRead lossless JSON trees and record streamsJsonValue.parse_str, JsonValue.record_decoder, value.encode({})
MarkdownParse CommonMark and GFMMarkdown.parse_str(source, {})
MarkupUse shared names, source trees, contexts and templatesMarkup.Name, Markup.Context, Markup.Template.Plan
MathMLCheck MathML documents and fragmentsMathML.parse_str, MathML.parse_fragment
OFXRead and write financial statementsOFX.parse_str, OFX.decoder, OFX.encoder
ParserBuild parsers and shared streaming descriptorsParser.const, Parser.Utf8, Parser.Quotation
QIFRead and write Quicken recordsQIF.parse_str, QIF.decoder, QIF.encoder
RocSyntaxRetain Roc tokens, trees and diagnosticsRocSyntax.parse_str, RocSyntax.parse_bytes
SvgCheck SVG markup and path dataSvg.parse_str, Svg.Path.parse_str
XmlRead raw or namespace-resolved XMLXml.parse_str, Xml.Namespaced.parse_str, Xml.reader
YamlRead YAML trees or decode configuration recordsYaml.parse_str, Yaml.decode_str

4.1.2. How the modules fit together§

Constructors take a source and a defaulted props record, such as Xml.parse_str(source, {}). Configure that same operation through its props; there is no separate parse_with variant. A parser constructor such as Xml.parser({}) returns a reusable value: parser.parse_str(source) consumes all input, while parser.run_str(source) returns { value, rest }.

Failures carry Parser.Diagnostic(problem): { problem, location, path }. Native formats wrap this in their own failure tag, such as InvalidXml. Generic parser runners use ParseError. See Report parse errors for locations, logical paths, bounded source excerpts and rendering.

Streaming uses the same receiver methods for every format. Construct a decoder, then call decoder.read_iter(chunks), decoder.read_stream(source) or decoder.collect(chunks). Configure recovery in the constructor’s errors props. Encoders provide write_iter and write_stream; see Read and write documents in chunks.

Checked quotations use from_quote. Runtime validate returns native typed failures; Parser.Quotation.require asserts a checked constant, and quotation hooks render compiler text at that boundary. See Parse and validate during compilation.

The following complete program demonstrates the entry points:

# Parser and Parser.Utf8: build a parser from combinators, run it on a Str.
pair : Parser(Parser.Utf8.Bytes, (U64, U64), _)
pair =
	Parser.const(|a| |b| (a, b))
		.keep(Parser.Utf8.digits({}))
		.skip(Parser.Utf8.codeunit(','))
		.keep(Parser.Utf8.digits({}))

# CSV: decode each row into a record type the compiler infers from use.
people : Str -> Try(List({ name : Str, age : U64 }), [InvalidCsv(CSV.Error)])
people = |text| CSV.decode_str(text, {})

# HTTP: parse one message; bytes after it are left for the next message.
http_target : Str -> Str
http_target = |text| {
	match HTTP.Request.parse_bytes(text.to_utf8(), {}) {
		Ok(request) => request.target
		Err(InvalidHttp(e)) => "invalid request at ${Str.inspect(e.location)}"
	}
}

# Xml and Yaml: whole-document functions that report line and column.
xml_root : Str -> Str
xml_root = |text| {
	match Xml.parse_str(text, {}) {
		Ok(doc) => Str.inspect(doc.root)
		Err(InvalidXml(e)) => "${Str.inspect(e.location)}: ${Str.inspect(e.problem)}"
	}
}

yaml_value : Str -> Str
yaml_value = |text| {
	match Yaml.parse_str(text, {}) {
		Ok(value) => value.to_inspect()
		Err(InvalidYaml(e)) => "${Str.inspect(e.location)}: ${Str.inspect(e.problem)}"
	}
}

# Yaml can also decode straight into a record.
config : Str -> Try({ title : Str, draft : Bool }, [InvalidYaml(Yaml.Error)])
config = |text| Yaml.decode_str(text, {})

# JsonValue: a lossless tree; numbers keep their text, and it writes JSON too.
json_sorted : Str -> Str
json_sorted = |text| {
	match JsonValue.parse_str(text, {}) {
		Ok(value) => value.encode({ sort_keys: True }).map_ok(Str.from_utf8_lossy) ?? "not writable"
		Err(InvalidJsonValue(e)) => "${Str.inspect(e.location)}: ${Str.inspect(e.problem)}"
	}
}

# Markdown: parsing never fails, so `parse_str` returns the blocks directly.
markdown_blocks : Str -> Str
markdown_blocks = |text| Str.join_with(Markdown.parse_str(text, {}).map(Str.inspect), "\n")

Ok((3, 4))
Ok([{ age: 36, name: "Ada" }, { age: 41, name: "Alan" }])
/index.html
Element({ attributes: [{ name: "lang", value: "en" }], children: [Text("hi")], name: "greeting" })
Text({ column: 7, line: 1, offset: 6 }): EndTagMismatch({ actual: "a", expected: "b" })
Mapping([{ key: "title", value: Text("Notes") }, { key: "draft", value: Bool(False) }])
Ok({ draft: False, title: "Notes" })
{"name":"web","version":2.10}
Heading({ level: One, content: [Text("Title")] })
Paragraph([Text("Some "), Emphasis([Text("text")]), Text(".")])

4.1.3. Parser§

Parser(input, value, problem) describes a computation over input. Compose it with receiver operations such as keep, skip, map, many and sep_by. Parser.one_of chooses alternatives, and Parser.custom adapts a primitive with a structured diagnostic. See Your first parser and Combinators by task.

4.1.4. Parser.Utf8§

Parser.Utf8 owns byte, string and digit primitives. Parser.Utf8.Bytes is List(U8). Text runners are methods of the parser value, so a parser built from these primitives uses parser.parse_str(source) or parser.run_str(source).

4.1.5. CSV§

CSV.parse_str(source, {}) returns a nominal document with raw .records. CSV.decode_str(source, {}) infers a record type from use. For positional conversion, build a CSV.record parser with CSV.field and pass it to CSV.rows_parser({ row }). Its generic runner returns ParseError, preserving InvalidCsv or InvalidRow inside the problem union. See Read CSV data.

4.1.6. HTTP§

HTTP.Request and HTTP.Response own complete parse_bytes constructors and prefix parser constructors. A parsed message offers message.header(name). Their decoders emit message heads, body fragments, trailers and boundaries; response decoding also takes request methods. The parser operates on bytes and performs no network I/O. See HTTP messages.

4.1.7. JsonValue§

JsonValue.parse_str(source, props) reads a tree while preserving member order and number spelling. Props select explicit relaxations and resource bounds. Use receiver methods such as get, at, get_path and as_i64 to inspect a value. value.encode(props) returns validated UTF-8 bytes; JsonValue.record_decoder and JsonValue.record_encoder provide record streams. See Read and write JSON.

4.1.8. Markdown§

Markdown.parse_str(source, {}) returns blocks directly: every input is valid Markdown. Markdown.parse_inlines reads inline content. Tree values retain links and raw HTML for the application to handle. See Markdown.

4.1.9. Xml§

Xml.parse_str(source, {}) returns raw XML names, a declaration and a root. Use Xml.Namespaced.parse_str(source, {}) when expanded namespace identities matter. Both support limits: Bounded(readerProps) for untrusted documents, pull readers and chunk decoders. Readers expose { location, declaration } through reader.metadata(). Namespace-resolved documents provide document.encode(writeProps). See Read XML documents.

Html, Svg and MathML return shared Markup.Source and Markup.Tree values. Use source.position(offset), tree.single_root() and tree.text(root). Markup.Name carries explicit namespace, local and prefix identity and offers vocabulary queries as receiver methods. Markup.Context checks authored placement; Html.RepairContext checks HTML repair semantics. Template plans live under Markup.Template, expose read-only metadata and bind with plan.bind(values.iter()). See the markup chapters for those distinct tasks.

4.1.10. Yaml§

Yaml.parse_str(source, {}) returns a configuration tree. Yaml.decode_str decodes directly into records, lists, dictionaries or tuples; optional values use Try(value, [Missing]). Yaml.value_parser(props) offers the same typed decoding through generic parser receivers. Native failures, including missing required keys, use InvalidYaml. See Read YAML configuration and frontmatter.

4.1.11. Choosing between modules§

Use Choosing how to read your input to choose type-directed decoding, a document tree, combinators or ordinary string operations. Conformance records each format’s supported specification and known gaps.

4.2. Conformance§

This chapter is for readers deciding whether a format parser is accurate enough for their input, and for contributors who need to reproduce the numbers. For each format it names the specification the parser follows, the reference implementation (the oracle) its results are compared with, the results last measured, and the known gaps.

4.2.1. How conformance is checked§

Each format has a review script in scripts/ that feeds a fixed set of inputs to a small Roc program (the probe) and to a pinned reference implementation, normalises both results and compares them. The inputs come from the format’s official test suite where one exists, plus hand-written cases in scripts/<format>/cases.json.

Every case where the library and the oracle disagree on purpose is listed in a known-failures.json file with its reason. A review fails when a case disagrees and is not listed, or when a listed case starts to agree (the list is stale). The scripts never update the list themselves.

Where the oracle is itself wrong, the script records an oracle disagreement and uses the specification’s expected result instead; these cases are reported but are not failures.

The AsciiDoc parser has a separate development harness: scripts/review_asciidoc.py. It pins Asciidoctor 2.0.23 and checks independently authored semantic assertions, a normalized block-source projection and embedded HTML. The local task uses --require-complete --compare-html: reference-accepted parser rejections, projection differences and HTML differences fail. Generated models, reproducible mutations and repository document harvesting extend the authored cases. Reports retain original inputs, source hashes and failing reproducers. The separate local performance harness compares optimized native parsing and rendering with the same oracle and checks geometric scaling.

4.2.2. HTML, SVG and MathML§

scripts/review_markup.py check compares deterministic mixed-markup fixtures against a pinned parse5 HTML parser and a namespace-aware Expat XML parser. The local suite command downloads hash-verified WPT inputs and generates its full tree-construction corpus in ignored storage. Git retains the curated cases, upstream revision, source hashes and license. Script-executing tests are excluded because these pure parsers do not execute JavaScript; scripting-mode parser tests are included. Upstream expected trees take precedence when parse5 lags the selected standard, and those disagreements remain visible in reports.

Semantic checks have separate evidence: a pinned Nu checker, a W3C MathML Core schema with pinned lxml, and independently authored value/structure fixtures. scripts/markup/oracle-limitations.json records validator and schema gaps. Neither a browser accepting malformed markup nor an oracle’s limited validation establishes complete authoring conformance. Local fuzz and browser campaigns are described in Parse HTML, SVG and MathML.

4.2.3. Results§

Run the reviews below for the source and compiler you intend to use. Reports identify accepted inputs, known divergences, unexpected failures and crashes. A passing finite corpus establishes agreement for those inputs and policies; it does not establish complete implementation of a specification. The per-format sections explain supported behavior and intentional differences.

4.2.4. Reproducing the results§

The review scripts need Python 3, the pinned oracles, and a Roc compiler. Set ROC to the compiler you want to test with. All cross-language comparisons are local-only. blueprint run conformance installs every oracle into one environment and runs the fast reviews. To run them by hand, install each oracle into its own virtual environment under .roc-parser-tmp/, which git ignores:

export ROC=/path/to/roc
python3 -m venv .roc-parser-tmp/venv-yaml
.roc-parser-tmp/venv-yaml/bin/pip install --no-deps -r scripts/yaml/requirements.txt
python3 -m venv .roc-parser-tmp/venv-http
.roc-parser-tmp/venv-http/bin/pip install --no-deps -r scripts/http/requirements.txt
python3 -m venv .roc-parser-tmp/venv-md
.roc-parser-tmp/venv-md/bin/pip install --no-deps -r scripts/markdown/requirements.txt

Then run one command per format. CSV, XML and JSON need only the standard library. The XML script downloads the W3C suite on first use and checks its SHA-256; the JSON script downloads its five pinned documents the same way (--no-documents skips them).

python3 scripts/review_csv.py
python3 scripts/review_xml.py check --baseline scripts/xml/known-failures.json
python3 scripts/review_json.py check --baseline scripts/json/known-failures.json
.roc-parser-tmp/venv-http/bin/python scripts/review_http.py
.roc-parser-tmp/venv-md/bin/python scripts/review_markdown.py check
.roc-parser-tmp/venv-md/bin/python scripts/review_markdown_blocks.py check --baseline scripts/markdown/blocks/known-failures.json
.roc-parser-tmp/venv-yaml/bin/python scripts/review_yaml.py check --baseline scripts/yaml/known-failures.json

Each script prints a summary, such as the following for HTTP:

134 cases, 22 known divergences, 0 failures, 0 crashes, 0 stale (h11 0.16.0)

The XML, YAML, JSON and Markdown block scripts also write a JSON report, with every case’s expected and actual result, under .roc-parser-tmp/. The JSON script writes .roc-parser-tmp/json-review/check.json.

4.2.5. CSV§

The parser follows RFC 4180 with the relaxations most CSV readers share: LF and lone CR line breaks as well as CRLF, an optional final line break, rows of different lengths, and any UTF-8 in unquoted fields. The CSV module documentation lists the dialect exactly.

scripts/review_csv.py compares the raw records with Python’s csv.reader in strict mode, on the fixed cases and 2000 random inputs from a fixed seed.

Known differences, by design:

4.2.6. HTTP§

The parser follows the message syntax of RFC 9112 and the field syntax of RFC 9110. It is a message parser: it reads bytes that are already in memory and does not manage connections.

scripts/review_http.py compares the verdict, start line, fields, decoded body and leftover bytes with h11. Most of the 22 listed divergences are places where RFC 9112 allows a parser to reject something and this one does, as llhttp does by default while h11 accepts it: line folding, bare LF line endings, control characters in field values, Transfer-Encoding in an HTTP/1.0 message, and a message with both Transfer-Encoding and Content-Length. Rejecting these is what prevents request smuggling. The others are by design:

Limitations:

4.2.7. JSON§

The parser follows RFC 8259. By default it is strict and also rejects repeated keys; options add relaxations and checks (see Choose how strict to be).

scripts/review_json.py compares the probe with Python’s standard json module, configured so that it models JsonValue.Options:

Every accepted value is also written compact, with indent: 2 and with sort_keys: True, and compared byte for byte with what json.dumps writes. Rejections are compared as verdicts, and an error’s line and column must lie inside the input. Each case also checks that JsonValue.parser_with agrees with JsonValue.parse_str.

The inputs are:

All 720 cases pass, with no known failures and no oracle disagreements. The 35 implementation-defined suite cases are recorded, never counted as failures. 10 are accepted: numbers too large or too small for a double, whose text is kept. 25 are rejected: 10 lone or malformed surrogate escapes, which a Roc Str cannot hold (lone_surrogates: Replace reads them as U+FFFD); 13 inputs that are not UTF-8, such as UTF-16, ISO-8859-1, overlong sequences and encoded surrogates; a byte order mark, which bom: True accepts; and 500 nested arrays, past the default 128-level limit.

Two suite cases are policy deviations. y_object_duplicated_key and y_object_duplicated_key_and_value are valid RFC 8259 text, but strict parsing rejects a repeated key on purpose. The review checks them with duplicates: KeepLast, which is how Python reads them, and lists them under policy_deviations in its report.

Limitations: the input must be UTF-8, the relaxations stop well short of JSON5. Incremental parsing accepts NDJSON or top-level array records through JsonValue.record_decoder; complete values require their full document. The JsonValue module documentation lists every option.

4.2.8. Markdown§

The parser follows CommonMark 0.31.2 with the GitHub Flavored Markdown extensions for tables, task list items, strikethrough and extended autolinks. It also reads a frontmatter block between --- lines at the start of a document.

Two scripts check it:

All 76 known failures of the block check are differences of representation or of the oracle, not wrong structure:

The known-failures file does not yet give each entry its reason; the grouping above comes from comparing the expected and actual trees.

Markdown has no syntax errors, so every input produces a document.

4.2.9. XML§

The parser checks well-formedness as defined by XML 1.0 (Fifth Edition) for documents without a document type declaration. It does not validate.

scripts/review_xml.py compares verdicts and trees with expat and selects the W3C conformance cases inside the supported subset: UTF-8 text, XML 1.0, no <!DOCTYPE>. The two known failures are:

The 11 oracle disagreements are cases where expat uses older name-character tables than the Fifth Edition; the suite’s verdict is used.

Limitations: document type declarations and therefore all entities other than the five predefined ones are rejected, namespaces are not processed, the input must be a Str (already decoded), and comments and processing instructions are dropped from the tree.

4.2.10. YAML§

The parser implements a subset of YAML 1.2 for configuration files and frontmatter, and resolves plain scalars with the core schema.

scripts/review_yaml.py runs every yaml-test-suite case and the hand-written cases, and compares the value with ruamel.yaml’s pure-Python parser. Of the 803 cases:

Two of the 206 rejections, limits/flow-depth-100 and limits/flow-depth-512, come from the 100-level nesting limit, which now also applies to flow collections; scripts/yaml/known-failures.json records them with that reason. The 58 oracle disagreements are cases where ruamel.yaml differs from the test suite’s expected result; the suite wins.

Limitations: besides the features above, integers must fit in I64, tabs may not be used for indentation, and nesting deeper than 100 levels is rejected. The Yaml module documentation describes the subset.

4.2.11. Property testing§

Conformance checks compare chosen inputs with an oracle. Alongside them, every parser runs under coverage-guided property tests built with roc-fuzz, which generate inputs, including malformed and pathological ones, and check properties such as: the parser never crashes or hangs, rendering a value and parsing it again gives the same value, and results agree with a simple model. The local review scripts can compare a fuzzing corpus with the oracles. CI runs native Roc property campaigns, with smoke runs on pull requests and longer runs each night; those campaigns do not invoke the cross-language review scripts. Property testing describes the targets and how to run them.

4.2.12. QIF and OFX banking records§

scripts/review_qif.py check compares bank/cash/card records with pinned Quiffen and exercises both writer directions. Dates use an explicit day-first policy in the comparison. Exact decimal values and account/category semantics are compared separately from source layout and numeric spelling.

scripts/review_ofx.py check compares the supported banking/card statement subset with pinned ofxtools and ofxparse. The corpus exercises SGML/XML and UTF-8/Windows-1252 combinations. Models preserve identifiers, exact monetary values, timestamps, statement context and supported extension data. Unsupported message sets and ambiguous SGML extensions are explicit parser limits.

Both reviews expose seeded campaign, corpus and replay commands. The --smoke campaign is a short local integration check; longer runs retain inputs and diagnostics under .roc-parser-tmp/. Agreement on this finite corpus establishes supported-subset evidence, not complete QIF or OFX specification coverage.

4.3. Compatibility§

This chapter is for readers choosing a release, upgrading, or matching a Roc compiler to the package. It states which compiler each release is built for, what a version number promises, where the package runs, and what changed in 2.0.0.

4.3.1. Roc compiler§

Roc has no stable release yet, and its compiler changes the language and standard library often. roc-parser follows the latest nightly rather than pinning one:

Use the latest nightly with the latest release. If a newer nightly breaks a release you depend on, the nightly its notes name under "Compilers" is known to work; the release notes also say when a migration changed the package’s API.

roc version

4.3.2. Version numbers§

Releases follow semantic versioning:

For a parser, which inputs it accepts and the values it produces matter as much as its type signatures. Read the release notes for behaviour changes even when the API is unchanged.

Roc packages are fetched by URL, so a release is a bundle URL whose name is the hash of its content. Copy it from the GitHub releases page; each release also publishes an SBOM and signed provenance.

4.3.3. Platforms§

The package is plain Roc with no platform-specific code: it does no input or output, so it works with any Roc platform, on any system where that platform and the Roc compiler run. CI builds and tests it on Linux x86-64, and the conformance results in Conformance were measured on macOS with Apple silicon.

4.3.4. Dependencies§

The package depends on one other package:

PackageVersionUsed for
roc-lang/unicode4.3.3Unicode case folding, general categories and scalar handling in the Markdown module

package/main.roc names its release URL. Roc fetches it when it fetches roc-parser, so applications need not add it themselves.

4.3.5. The builtin Json module§

Roc auto-imports a builtin module named Json, which decodes JSON into records. roc-parser’s JSON module is therefore named JsonValue: an import parser.Json would leave the alias unbound. For the same reason its errors use the tag InvalidJsonValue: the builtin reports invalid input as InvalidJson with a Str, and keeping the tags apart lets one function pass errors from both on with ? (see The builtin Json or JsonValue).

4.3.6. Upgrading older releases§

The 2.0.0 upgrade guide maps the names used by 1.x clients to the redesigned format modules. For changes since 5.0.0, use the 6.0.0 migration notes, including typed parser problems, primitive factories and the shared streaming API.

4.4. Performance§

Use this chapter to assess memory, bound untrusted input and choose what to measure. Throughput depends on the compiler, document shape and result your application needs. See Benchmarks for reproducible comparisons.

4.4.1. Summary§

Measure the package release and compiler you will deploy, using representative inputs and increasing sizes. Keep elapsed time, allocation traffic and resident memory separate. Benchmark reports record their source and toolchain identities; old measurements do not establish the performance of a current nightly.

4.4.2. Choose an appropriate workload§

roc-parser is a pure-Roc package that returns typed values and structured errors. CSV, QIF and OFX provide incremental readers and writers through builtin Iter and Stream; see Read and write documents in chunks. Their whole-document convenience functions collect results in memory. The other formats require complete input. XML provides event readers over an in-memory source. Measure the representation and source lifetime needed by your application.

A parser returning a full tree does different work from a tokenizer returning borrowed events. Compare the representation and validation your caller actually needs, rather than selecting a library from a single throughput ratio.

4.4.3. Bound work on untrusted input§

The format parsers use byte scanners, explicit state and bounded nesting. Per-input deadlines in the property tests detect excessive work; a bounded search does not prove a complexity bound for every input.

Recursive custom combinators still use the call stack. Impose a nesting limit for untrusted recursive formats and test the limit and the first rejected input.

4.4.4. Memory behaviour§

For whole-document APIs, the complete input and parsed values contribute to peak memory. Chunk readers retain the current source chunk, bounded unfinished syntax and format context; collecting their results adds retained output. Allocation traffic also depends on how many fields, nodes and decoded strings are created; byte length alone does not describe the workload.

Text without escapes can share the input: XML text and attributes, YAML scalars and keys, JSON strings and number text, and HTTP field values use slices where possible. Rewritten escapes, entities and block scalars need new bytes. A Content-Length body shares input memory; a chunked body is assembled.

A small retained slice can keep a large input alive. Copy values deliberately when the caller must release the original document. Measure retained memory separately from cumulative requested bytes and process RSS.

4.4.5. Compare equivalent work§

Benchmark drivers read input before the timed loop, consume the result and record failures. Review their source when comparing libraries:

Count only inputs accepted under the comparison’s declared policy. Report rejections and resource limits separately from successful timings.

4.4.6. Costs to measure§

Building an owned result tree allocates and later frees lists, strings and records. Sharing introduces reference-count work. Repeated conversion or reconstruction of text can add copies even when the parser’s scanner is linear.

Profile the deployed compiler and backend before naming a cause. Keep a minimal control that distinguishes scanning, decoding and result construction. Changes to compiler ownership analysis can alter allocation costs without changing the high-level Roc source.

4.4.7. Byte scanning and slices§

Parser.Utf8.ByteClass describes a set of bytes. Parser.Utf8.find_any, Parser.Utf8.skip_class, Parser.Utf8.find_line_end and Parser.Utf8.span_class examine runs with U8x16, then handle remaining bytes with a scalar loop. Build reusable classes outside hot loops. The gain depends on the distance between matches and the amount of result construction after each match.

The parsers track byte offsets and return slices where possible. Converting bytes with Str.from_utf8 validates UTF-8; XML can instead cut names and text from an already validated string with boundary-checked byte slices. Sharing avoids copies but retains the original allocation while a slice is reachable.

4.4.8. Tips for your own parsers§

4.5. Glossary§

The words this manual uses with a specific meaning, in alphabetical order. Terms that belong to one format name the format in parentheses.

4.5.1. Backtracking§

Returning to an earlier position in the input to try another alternative after one fails. Parser.alt and one_of always backtrack: each alternative starts from the input the first one was given, however much the failed one read.

4.5.2. Chomping (YAML)§

How a block scalar (| or >) treats the line breaks at its end. Clip (the default) keeps one, strip (-) keeps none, and keep (``) keeps them all. Unrelated to Parser.chomp_while and chomp_until, which consume input while or until a condition holds.

4.5.3. Combinator§

A function that builds a parser from other parsers, such as keep, skip, many or one_of. A parser for a whole format is built by combining small ones. See Modules.

4.5.4. Consumption§

The part of the input a parser has read when it succeeds. The rest is passed to the next parser. A parser that succeeds without consuming anything makes no progress; many and sep_by stop repeating when an element makes no progress, so they cannot loop forever.

4.5.5. Core schema (YAML)§

The YAML 1.2 rules that decide what an unquoted (plain) scalar means: null and ~ are null, true and false are Booleans, and numbers such as 12, 0o14, 0xC, 1.5 and .inf are integers or floats. Anything else is a string. Quoted scalars are always strings.

4.5.6. Flanking (CommonMark)§

The rule that decides whether a run of * or _ can open or close emphasis. A run is left-flanking when it is not followed by whitespace and, if it is followed by punctuation, is preceded by whitespace or punctuation; right-flanking is the mirror image. So *foo* is emphasis but * foo * is not.

4.5.7. Format§

In type-directed decoding, a type whose methods (parse_str, parse_record_field, parse_list_start and so on) read one value at a time from the input. CSV.Format(errors) and Yaml.Format(errors) are formats; ordinary callers use them through CSV.decode_str and Yaml.decode_str. Custom scalar decoders name the format and their complete error row in parser_for. See parser_for.

4.5.8. Framing (HTTP)§

How the receiver finds where a message body ends: by Transfer-Encoding: chunked, by Content-Length, or, for a response with neither, by the end of the connection. See smuggling.

4.5.9. Furthest failure§

Of all the failures seen while parsing, the one that read furthest into the input, including failures that an alternative or a repetition recovered from. The runners report it as ParseError({ problem, location, path }), so the error points at the bad element and not at the input left over after it.

4.5.10. I-JSON§

The Internet JSON profile of RFC 7493: JSON restricted so that every reader gets the same data. Among other rules, numbers should fit an IEEE 754 double and keys must be unique. JsonValue.parse_str checks its number rule with interoperable_numbers: True, and rejects repeated keys by default. See Choose how strict to be.

4.5.11. Input§

The data a parser reads, given by the first type parameter of Parser(input, a, problem). For text it is Parser.Utf8.Bytes, a list of UTF-8 bytes; for CSV record decoding it is a CSV.Record, a list of fields.

4.5.12. JSONC§

JSON with comments: JSON that also allows // and /* */ comments and, in most tools, trailing commas, as in tsconfig.json and editor settings. JsonValue.parse_str reads it with comments: True and trailing_commas: True. It is not JSON5. See Choose how strict to be.

4.5.13. Known failure§

A conformance case where the library and the oracle disagree on purpose, listed with its reason in a known-failures.json file. See Conformance.

4.5.14. Method§

A function that belongs to a type, defined in the .{ } block after the type declaration and called with a dot on a value: parser.keep(field) calls Parser.keep(parser, field). This manual writes calls in that method-call style. The compiler finds the method from the value’s type, which is static dispatch.

4.5.15. Opaque nominal type§

A nominal type declared with ::, whose backing representation is hidden outside its module, so values can only be built and read through its methods. Parser(input, a, problem) is one. A nominal type declared with :=, such as Yaml, JsonValue or Markdown, shows its tags, so you can match on them.

4.5.16. Oracle§

A trusted reference implementation whose results a test compares with the library’s, such as Python’s csv module for CSV or expat for XML. When the oracle is itself wrong, the specification decides. See Conformance.

4.5.17. Parseable§

A where-clause alias, row.Parseable(errs), that CSV.decode_str and the other decoding functions use to say "a type that this format can decode" without naming the format’s internal state type. You rarely write it yourself.

4.5.18. Parser§

A value of type Parser(input, a, problem): a description of how to read an a from the start of an input. Running it returns the value and the unconsumed input, or a ParseError with a typed problem, location and logical path.

4.5.19. parser_for§

The method the compiler derives for records, tuples, lists and the builtin scalar types, which reads a value of that type with a given format. It is how CSV.decode_str and Yaml.decode_str decode into the type your annotation names. A field of type Try(a, [Missing]) is optional. A missing required field is reported by the format with a typed problem and a path identifying the field.

4.5.20. Parsing incomplete§

The parser succeeded but did not consume the whole input. Functions such as parser.parse_str, which expect to read everything, report it as a ParseError at the furthest failure, or as UnexpectedInput at the leftover.

4.5.21. Progress§

See consumption.

4.5.22. Property test§

A test that generates many inputs and checks a rule that must hold for every one of them, such as "the parser never crashes" or "parsing a rendered value gives back the value". This package runs its property tests with coverage guidance from the roc-fuzz library. See Property testing.

4.5.23. Request smuggling (HTTP)§

An attack in which two programs that read the same bytes, such as a proxy and a server, disagree on where one message ends. The attacker hides a second request inside the first one’s body. A parser prevents it by rejecting any message whose framing could be read two ways.

4.5.24. Soft line break (CommonMark)§

A line break inside a paragraph that is not a hard break. HTML renders it as a space; this library keeps it as \n in the Text node.

4.5.25. Static dispatch§

Choosing which function a method call runs from the type of the value, at compile time. It is how value.to_inspect() finds the Yaml version for a Yaml value, and how CSV.decode_str finds the parser_for of your row type.

4.5.26. Try§

Roc’s builtin type for a result that may fail: Try(ok, err) is Ok(value) or Err(problem). Every fallible function in this package returns one. An optional value is Try(a, [Missing]).

4.5.27. Type module§

A module named after the one type it defines, with that type’s methods in the .{ } block of its declaration. Parser, Yaml, Xml, JsonValue and Markdown and CSV are type modules. HTTP uses an empty tag union to group its Request, Response and related types. Parser.Utf8 groups text primitives within Parser.

4.5.28. Well-formed (XML)§

Following the syntax rules of XML 1.0: one root element, properly nested and matching tags, quoted and unique attributes, and only declared entities. Well-formedness says nothing about which elements are allowed; that is validity, which needs a schema or DTD and which this package does not check.

4.5.29. Where clause§

The part of a type annotation that lists the methods a type variable must have, such as where [input.len : input -> U64] on Parser.many, which needs only the length of its input to detect progress.

5. Contributing§

5.1. Contributing to roc-parser§

This chapter is for people who want to change roc-parser: fix a bug, improve a format parser, or improve this manual. It assumes you can read Roc and use Git and Python 3. When you finish it you will have a working checkout, know which checks CI runs, and know what a pull request needs before it can be merged.

Related chapters: Property tests and conformance reviews explains the fuzz targets and conformance reviews, Add a format module walks through adding a new format module, and Compiler, releases, settings and security reports covers the compiler, releases and repository settings.

5.1.1. Report a problem or propose a change§

Report bugs and propose changes in the roc-parser issue tracker. For a parsing bug, include the roc-parser release or commit, the output of roc version, and the smallest input that shows the problem. For a change to a public API or to the syntax a format accepts, open an issue first so the shape can be agreed before you write the code.

Do not report a suspected vulnerability in a public issue. Follow the private process in the security policy instead.

5.1.2. Development setup§

Install:

roc-parser does not pin a compiler. CI tests every change with the latest nightly, which it resolves at the start of each run, so install a new nightly when one is published. Check the one you have:

roc version

Output, with the date and commit of your nightly:

Roc compiler version nightly-2026-10-21-5349569

Download blueprint for your system from the release and check it against the release’s blueprint.sha256 before you run it. For an Apple silicon Mac:

gh release download 0.13.0 --repo lukewilliamboswell/roc-blueprint --pattern blueprint-aarch64-darwin --pattern blueprint.sha256
shasum -a 256 --check --ignore-missing blueprint.sha256
chmod +x blueprint-aarch64-darwin
mv blueprint-aarch64-darwin ~/.local/bin/blueprint

Blueprint.roc declares the tasks and what each needs. blueprint downloads the tools it pins in Blueprint.lock, such as Zig for the RocSyntax oracle, and expects the others, such as roc, python3, ruby and docker, on your PATH; blueprint requirements lists them. Nix supplies the benchmark environment (Reproducible environment with Blueprint) and the optional local oracle-ruby activation. The default check needs no reference runtimes or Nix. Local AsciiDoc comparisons require Ruby 3.4 or newer and RubyGems.

The scripts find the compiler through the ROC environment variable and fall back to roc on PATH. Leave ROC unset when you run blueprint, which reads it to choose the compiler that evaluates Blueprint.roc itself.

5.1.3. Repository map§

PathPurpose
package/The library: Parser, and one module per format
examples/Small apps that use the package source (../package/main.roc); releases attach them pinned to the bundle URL
fuzz/Property-test targets, seed inputs and dictionaries (Property tests and conformance reviews)
scripts/Roc orchestration, independent Python oracle harnesses, and documentation and release-inventory helpers
scripts/<format>/Conformance probe, cases and known-failures baseline for each format
scripts/tests/Unit tests for the scripts, including the repository policy
tests/spec.jsonWhat each example prints, checked on every pull request and shipped with each release’s examples
docs/This manual, in AsciiDoc
docs/releases/Release notes, one AsciiDoc file per release
Blueprint.roc, Blueprint.lockThe development environments and the named tasks CI runs

5.1.4. Run the tests§

Each module carries its tests as expect blocks next to the code. Run them all through the package:

roc test package/main.roc

Output:

All (253) tests passed in 535.8 ms.

The number grows as tests are added.

5.1.4.1. Run what CI runs§

blueprint run check runs everything a pull request must pass, the same way the check job of ci.yml does. It takes a quarter of an hour or more on a laptop; its scratch files and evidence go under .roc-parser-tmp/:

blueprint run check

It runs scripts/check_all.roc, which works through these sections in order and stops at the first failure. Name sections to run only those, for example blueprint run check -- fuzz bench, and list them with blueprint run check -- --list:

SectionWhat it runs
scriptsThe fixed tooling compiler version, Roc orchestration tests, checks and formatting, and the independent Python harness tests including repository policy
packagescripts/test_package.roc: roc check, roc fmt --check and roc test on the package, the compile-time literal checks and roc docs; then a release dry run, which bundles the package, builds the examples archive with the bundle’s release URL, and checks, builds and runs every case of its spec.json against that bundle served on a loopback address
examplesEvery example in examples/ against the working tree, compared with the output recorded in tests/spec.json
docsThe manual’s Roc examples and the API reference audit (Build the documentation)
fuzzBuilds seven shared fuzz executables and runs each scheduled property on 2,000 inputs from its seeds (Property tests and conformance reviews)
benchA dev-build smoke run of the roc-parser benchmark drivers (Benchmarks)

Roc commands use Weaver for argument parsing, with their schemas in scripts/src/CommandLine.roc. Use --help on a command to list its options, for example blueprint run fuzz -- --help. Options accept both --name value and --name=value. Put extra compiler arguments for blueprint run bundle after --, following the bundle command’s own options.

The scheduled fuzz.yml workflow runs ten-minute native Roc campaigns for each registered CI property, reusing binaries compiled once in a shared job. Cross-language correctness and performance comparisons run locally through the named conformance, markup, asciidoc, roc-syntax, bench and bench-quick Blueprint tasks. They are excluded from CI, nightly compatibility checks and release validation.

Harness unit tests retain fake workers and authored adapter fixtures. Tests that launch installed reference implementations require an explicit local opt-in and their dependencies:

ROC_PARSER_RUN_ORACLE_TESTS=1 python3 -m unittest discover -s scripts/tests -p 'test_*.py'

The opt-in is disabled when CI is set.

When you change what an example prints, record the new output and review the diff:

blueprint run examples -- --update

5.1.4.2. Repository policy§

scripts/tests/test_repository_policy.py enforces these rules. A pull request that breaks one fails CI:

scripts/test_automation.roc also requires every fuzz/*.roc target to be registered in scripts/src/FuzzPlan.roc, and the campaign matrix in fuzz.yml to match that registry. Repository policy guards the default check and workflow entry points against cross-language execution.

5.1.5. Build the documentation§

The manual is AsciiDoc under docs/. The docs task uses the shared native roc-docs renderer to build the HTML manual and searchable PDF, generates the API reference, and packages the combined site:

blueprint run docs

The site and release archives are under .docs-out/release/. To stamp a release version, pass -- --release-version V. The renderer and its fonts, source highlighting and diagram support are downloaded through Blueprint and checked against Blueprint.lock.

The API reference is generated from the doc comments in package/ with roc docs. Build it, or check that it builds without writing anything:

blueprint run api-docs -- --version unreleased
blueprint run api-docs -- --check

Code examples in the manual are compiled and run, so a changed API breaks the build rather than silently leaving a stale example:

blueprint run doc-examples

The docs section of blueprint run check runs the examples and the API check. Every pull request also builds the manual, HTML and PDF, in the dry run of the Release workflow; download the release-site artifact of that run to preview it.

When you write a chapter, follow the shared documentation guide: give each page one purpose (tutorial, how-to, reference or explanation), name the reader and the outcome at the top, label commands and their output separately, and put any warning before the step it applies to.

5.1.6. Before you open a pull request§

Pull requests need passing CI: check, which runs blueprint run check, and Build the release, the dry run of the Release workflow, as well as codeql. A new Roc nightly can break check without any fault in your change; see Compiler and nightly. Human review is encouraged; answer review comments before asking for another review after a substantial change.

5.1.7. License§

By contributing, you agree that your contribution is licensed under the Universal Permissive License v1.0.

5.1.8. Write documentation§

Write for a current user or contributor task. Remove superseded explanations and keep release changes with their release. Follow the shared documentation guidance.

5.2. Property tests and conformance reviews§

This chapter is for contributors who change a parser and need to know whether the change is correct. The first half explains how roc-parser checks its parsers beyond the expect tests: property tests driven by a fuzzer, and reviews against a mature reference implementation. The second half is a set of how-to sections: run the targets, reproduce and minimise a failure, turn it into a regression, and update a conformance baseline.

It assumes the setup in Contributing to roc-parser, including the latest Roc nightly, which CI uses too. Every command below uses that compiler:

export ROC=/path/to/roc_nightly/roc

5.2.1. Why property tests§

An expect test checks one input that somebody thought of. Parsers fail on the inputs nobody thought of: a carriage return on its own, a quote in the middle of a field, 100,000 nested brackets. A property test states something that must hold for every input, and a fuzzer searches for an input where it does not.

The fuzzer is roc-fuzz, a Roc platform that embeds libFuzzer. It is coverage guided: it keeps inputs that reach new code and mutates them further, so it finds deep paths far faster than random input would. The targets are library quality tests, not a security programme. The established targets run on pull requests and nightly in CI. The HTML/SVG/MathML targets are local-only: smoke all includes them, while CI uses smoke --ci all and the scheduled matrix excludes them.

A failing property is a bug in the parser, or occasionally in the property. Either way the outcome is a fix: see Fix the cause, do not gate it.

Keep the committed seeds small. A generator should describe a class of inputs and a property should check its contract across sizes and variations. Prefer round trips, independent models, metamorphic relations and resource bounds to large lists of prescribed input/output pairs. Shrink failures and keep a small explicit regression witness when it explains a contract the generator would otherwise miss.

Expanded corpora, timing samples and comparison scaffolds belong in ignored .roc-parser-tmp/ storage. Generate scaling documents from parameters. Recreate external suites from pinned revisions with verified hashes, retaining the acquisition recipe and pins in Git. Replacing a dataset must preserve its useful checks; moving assertions out of the repository is not a substitute for them.

5.2.2. Three kinds of target§

Each fuzz/*Property.roc module owns one property. The *Properties.roc applications use roc-fuzz 0.7.0 Fuzz.suite to group independent properties into shared executables. Markup has a local suite; allocation checks that require a different compiler profile retain separate executables. Each property retains its name, examples, corpus and reports; selection happens through the executable’s --target option rather than a byte added to the input.

CSV retention uses its own CsvRetention executable while the compiler’s hosted-box row-join bug affects combined suites. It keeps the same backend, opaque measurement ownership fence, diagnostic assertions and allocation ceilings. OFX allocation checks use a separate specializing executable while the November 18 Records build abort requires Boxy for the other Records properties. The OFX Boxy build exceeds an existing allocation ceiling; separating it preserves that check.

Properties come in three kinds, which differ in what the fuzzer’s bytes mean and therefore in what the property can check.

Raw targets (xml-raw, xml-reader, xml-limits, xmlns-raw, yaml-raw, yaml-source, json-raw, json-options, csv-raw, http-raw, markdown-inline, markdown-document, string-primitives):: The bytes are the input document, often invalid UTF-8. Nothing is known about the expected result, so the target checks properties that hold for any document: the parser does not crash or hang, an error points inside the input, LF/CRLF/CR line endings give the same result, appending a final line break changes nothing, and the result survives re-serialisation. These are called metamorphic relations: they relate two runs of the parser instead of comparing one run with a known answer. Many raw targets also switch to a pathological generator (deep nesting, thousands of attributes, long delimiter runs) when the input starts with a marker byte, so that the per-input --timeout catches quadratic behaviour.

The XML pull reader and namespaces have their own raw targets:

xml-ceilings is a measuring target: two bytes choose a document shape and size, and Fuzz.measure! requires the allocator traffic and held bytes of Xml.parse_str, a loop over Xml.Reader.next and Xml.Namespaced.parse_str to stay under ceilings linear in the document (and, for the reader, in its depth). Its 22 declared examples are a small and a large document of each shape, and its whole input space is a few thousand documents. Ceilings only move down; a pull request that raises one states why.

The yaml-source target checks the highlighting API separately from semantic parsing: token spans partition every original byte, diagnostics stay in range, key categories agree with roles, and repeated tokenization is deterministic. It reuses the YAML seed corpus and dictionary. Run a short check with scripts/fuzz.roc smoke yaml-source, or explore more locally with scripts/fuzz.roc campaign yaml-source --seconds 600.

Structured targets (csv-roundtrip, csv-decode, xml-roundtrip, xml-malformed, xmlns-roundtrip, yaml-roundtrip, yaml-block, json-roundtrip, http-roundtrip, http-smuggling, markdown-inline-ast, markdown-blocks, markdown-refdefs, parser-combinators):: The bytes are choices. A generator reads them to build a value (a table, an XML tree, an HTTP message, a Markdown syntax tree) and a way to write it down, and builds the expected parse alongside the text from the specification, never by calling the parser. The target then requires the parser to return exactly that value. Mutating generators such as xml-malformed and http-smuggling instead inject one known violation into a valid document and require the parser to reject it. parser-combinators builds a random combinator expression and compares the real parser with a small reference interpreter of the documented semantics.

Seeded targets (csv, xml, yaml, http-request, http-response)

The original targets read a string through roc-fuzz’s Fuzz.str generator. They start from reviewable seeds in fuzz/seeds/<name>.json, which the runner encodes for Fuzz.str at run time, and use the token lists in fuzz/dictionaries/ to mutate towards real syntax. Seed strings are limited to 255 UTF-8 bytes by that encoding.

Raw targets also take seeds and a dictionary, written as plain text. Structured targets start from an empty corpus, because their bytes are not text.

The three JSON targets divide the work this way:

5.2.3. Oracles§

A property test can only be as good as its expected answer. Structured targets build that answer from the specification, but a generator can share a misunderstanding with the parser. So each format is also compared with a mature, independent implementation, its oracle, pinned to an exact version:

FormatOracleSpecification and suite
CSVPython’s csv module in strict modeRFC 4180, plus the documented dialect
XMLexpat (Python’s pyexpat)XML 1.0 5th edition, W3C XML Conformance Test Suite 2013-09-23
YAMLruamel.yamlYAML 1.2, the yaml-test-suite
JSONPython’s json module, with the parse options modelled in review_json.pyRFC 8259, JSONTestSuite, and pinned SPDX, CycloneDX, Syft and Trivy documents
HTTPh11RFC 9112 and RFC 9110; cases pin where RFC 9112 is stricter than h11
Markdowncmark-gfm 0.29.0.gfm.13 (via cmarkgfm), with markdown-it-py to adjudicateCommonMark 0.31.2 spec examples and GFM extensions

Oracles have bugs too. When roc-parser and the oracle disagree, the review reports it, and a person decides which one the specification supports. Known oracle quirks are named in the review scripts and in the baselines, with the section of the specification that settles them.

5.2.4. Run the property tests§

AsciiDoc adds asciidoc-raw for byte-input robustness and error locations, and asciidoc-structured for independently constructed block trees. Its broader oracle fixtures, generated corpus and mutation exploration are described in Reading AsciiDoc. Inline source agreement alone does not establish agreement on Asciidoctor substitutions or rendered HTML.

Run every target for a short, iteration-bounded smoke test. This is what the fuzz section of blueprint run check runs on a pull request; it builds shared executables with two compiler jobs, then runs each property in turn:

scripts/fuzz.roc smoke all

Run one or more targets by name, and lower the iteration count for a quick check while you work:

scripts/fuzz.roc smoke csv --runs 200

The last lines of the output are the target’s summary and its report:

csv: passed; baseline not_checked; examples 0; exploration passed (attempted 200, accepted 200, rejected 0)
Report: .roc-fuzz/reports/934809-521896414/report.json

Run a longer, time-bounded campaign on the target closest to your change. The scheduled fuzz.yml workflow runs a 600-second campaign for every target each night, on the latest Roc nightly:

scripts/fuzz.roc campaign markdown-inline-ast --seconds 600

--max-input-size, --timeout and --memory-limit override a target’s registered bounds, and --no-build reuses the family’s verified binary from the last run. A family name such as Records selects all its properties. --platform PATH replaces the released roc-fuzz platform with a local entry point for upstream development; its sources and native host enter the build identity.

The runner keeps everything under .roc-parser-tmp/fuzz/:

5.2.5. Reproduce and minimise a failure§

A failing run prints the crash message and the paths of its report and of the saved input, original-input.bin. In CI, the fuzz-failure-<target> artifact holds the binary, the corpus and the run directory. Use the same target name for every step.

  1. Show the input as the target decodes it. For a structured target this is the generated document and its expected value:

    scripts/fuzz.roc show csv-roundtrip INPUT --no-build
    
  2. Replay it to confirm that it still fails:

    scripts/fuzz.roc replay csv-roundtrip INPUT --no-build
    
  3. Minimise it into a smaller input that fails the same way:

    scripts/fuzz.roc minimize csv-roundtrip INPUT OUTPUT --no-build
    

INPUT can be any file, including one from the corpus. Showing a corpus entry of the seeded csv target prints the string it decodes to:

scripts/fuzz.roc show csv .roc-parser-tmp/fuzz/corpus/csv/13d7ab0a19cc0856931196f0e8bac548e045234b --no-build

Output (the last line):

"a"

Each command writes a report under a .roc-fuzz/ directory, which Git ignores.

5.2.6. Add a regression§

Once you understand the minimised input:

  1. Write the smallest expect in the module that fails for the same reason, and fix the parser until it passes. The expect is the regression; it runs in every build.

  2. For a seeded or raw target, add the readable input to fuzz/seeds/<name>.json so the fuzzer starts near the bug in future runs.

  3. If the generator of a structured target avoided the input’s shape, remove that restriction so the target covers it from now on.

  4. Keep the raw minimised input and the target binary outside the repository when exact reproduction of a historical failure matters.

5.2.7. Fix the cause, do not gate it§

When a property fails, change the parser, not the test. Do not add a condition to a target that skips the failing shape, a known-gaps flag, or a baseline entry, to make CI green. A gate hides the bug from every later run, and the fuzzer will not tell you when it comes back.

There are two legitimate exceptions, and both need a written reason:

Several gates that once existed were removed by fixing the parser, which is how the targets found the CSV final-record bug, the YAML block scalar chomping bugs and the quadratic Markdown inline cases.

5.2.8. Run a conformance review§

Each format has a review script that runs a small probe app, scripts/<format>/probe.roc, which prints the parser’s result as JSON, and compares it with the oracle. Install the pinned oracle into a virtual environment first:

python3 -m venv .venv
.venv/bin/python -m pip install --no-deps -r scripts/yaml/requirements.txt

scripts/http/, scripts/markdown/ and scripts/yaml/ each have a requirements.txt with every dependency pinned, so --no-deps installs exactly the reviewed versions; CSV, XML and JSON use the Python standard library. Cross-language comparisons run locally. blueprint run conformance does all of this in one environment: with no arguments it runs the fast checks, and all adds the robustness campaigns:

.venv/bin/python scripts/review_yaml.py check --baseline scripts/yaml/known-failures.json
.venv/bin/python scripts/review_yaml.py campaign --smoke
python3 scripts/review_xml.py check --baseline scripts/xml/known-failures.json
python3 scripts/review_json.py check --baseline scripts/json/known-failures.json
python3 scripts/review_json.py campaign --smoke
python3 scripts/review_csv.py
.venv/bin/python scripts/review_http.py

The campaign commands are robustness campaigns. review_json.py campaign gives 60% of its time budget to libFuzzer runs of the three JSON targets, whose raw targets also start from every short case and suite input. It gives the rest to a seeded differential loop that generates documents and option sets, mutates them, and compares the probe with the oracle. It writes a report, and a reproducer for each kind of disagreement, under .roc-parser-tmp/json-review/campaign/. --smoke runs a short, iteration-bounded local comparison; for a longer run, pass a budget and a seed, and replay a reproducer once you have a fix:

python3 scripts/review_json.py campaign --seconds 1800 --seed 94
python3 scripts/review_json.py replay .roc-parser-tmp/json-review/campaign/reproducers/FILE.json

The CSV review runs the hand-written cases, 2000 seeded random inputs and, with --corpus DIR, a fuzz corpus. Its last line summarises the run:

cases=2035 unexpected=0 known=0 fixed_known=0

The Markdown reviews compare against the CommonMark spec examples and cmark-gfm:

.venv/bin/python -m pip install --no-deps -r scripts/markdown/requirements.txt
.venv/bin/python scripts/review_markdown.py check
.venv/bin/python scripts/review_markdown_blocks.py check --baseline scripts/markdown/blocks/known-failures.json

Several scripts can also check a structured target’s generator against the oracle, which is how a generator’s own mistakes are caught: review_xml.py corpus <target> <corpus>, review_json.py corpus <target> <corpus>, review_http.py --corpus DIR --fuzz-show BINARY --fuzz-target http-roundtrip, review_markdown.py generated BINARY CORPUS and review_markdown_blocks.py crosscheck BINARY CORPUS --fuzz-target markdown-blocks.

5.2.9. Known-failures baselines§

Each format keeps a baseline at scripts/<format>/known-failures.json (for Markdown blocks, scripts/markdown/blocks/known-failures.json): the cases where roc-parser and the oracle are allowed to disagree. Every entry carries a reason, of one of these kinds:

A review fails when a case disagrees and is not in the baseline, and reports any baseline entry that now passes as stale. When your change makes a known failure pass, remove the entry. When it introduces a new disagreement, fix the parser; add an entry only for one of the reasons above. review_yaml.py, review_json.py and review_markdown_blocks.py can rewrite the baseline with --record-known-failures PATH; review the diff entry by entry before you commit it.

5.2.10. OpenSSF Scorecard§

The OpenSSF Scorecard Fuzzing check detects OSS-Fuzz, ClusterFuzzLite and a few language-specific frameworks, but not Roc targets. A low Fuzzing score is a detection gap, not evidence that these campaigns did not run.

5.2.11. Local web-markup campaigns§

The html-raw, svg-raw, svg-path, mathml-raw, markup-source, markup-structured, markup-limits and markup-segments targets exercise malformed input, independently generated structure, source partitioning, bounds and typed holes. They are registered with scheduled: False. Run them locally using the commands in Parse HTML, SVG and MathML. Their seeds and dictionaries are reviewable source; corpus/report output remains in ignored scratch storage.

5.2.12. Incremental document parsing§

stream-contract compares shared adapters against an independent byte-record model under arbitrary splits and injected source errors. stream-ceilings measures distinct generated chunks inside the allocator measurement and discards completed output. CSV, QIF and OFX add format-specific chunk equivalence, round-trip, malformed-record and resource targets. They run in PR smoke and scheduled campaigns through the same registry as the existing formats.

The financial reviews use pinned independent Python libraries and cross-writer checks. Record-error recovery is checked against independently filtered expected models; broken framing remains terminal. Source chunks, syntax boundaries and encoding boundaries are independent fuzz choices.

5.2.13. Check diagnostic ownership§

Run scripts/fuzz.roc smoke diagnostic-lifetimes locally to check that native JSON, YAML and CSV diagnostic payloads survive after their input is dropped without retaining unrelated source bytes. Two-byte seeds choose a failure kind and generated padding size; the property checks complete payload identity and retained allocation size. XML error lifetimes are also checked by xml-stream-ceilings alongside its reader and namespace bounds.

5.3. Add a format module§

This how-to is for contributors who want to add a ready-made parser for a new text format, alongside CSV, Xml, Yaml, JsonValue, Markdown and HTTP. It assumes Contributing to roc-parser and Property tests and conformance reviews, and that you have opened an issue to agree that the format belongs in the package. When you finish, the new module has the same evidence behind it as the existing ones: tests, a review against a mature implementation, property tests in CI, benchmarks, and a chapter in this manual.

The steps use a hypothetical format called Toml as the example. Follow them in order: each step relies on the one before.

5.3.1. 1. Decide what you support§

Write down, before any code, which specification and version the module follows, and which parts of it the module supports. Every existing module supports a documented subset and rejects the rest with an error, rather than misreading it. For example Xml rejects a DOCTYPE, and Yaml rejects multi-line flow collections. A rejected input is a clear limit; a misread one is a bug that nobody notices.

Pick the oracle now too: a widely used implementation that you can pin to an exact version and call from Python, such as tomllib for TOML. The oracles of the existing formats are listed in Property tests and conformance reviews.

5.3.2. 2. Create the module§

  1. Add package/Toml.roc. Start it with a ## doc comment that names the specification, lists what is supported and what is rejected, and describes the error type. This comment becomes the module’s page in the API reference.

  2. Give every exposed value a ## doc comment with its purpose and, where it helps, a short expect example.

  3. Expose the module in package/main.roc and name it in that file’s doc comment. roc test package/main.roc, which scripts/test_package.roc runs, then runs its tests with the rest.

  4. Check that the name does not clash with a builtin module that Roc imports automatically. Roc has a builtin Json, so import parser.Json would leave the alias unbound; that is why the JSON module is JsonValue.

  5. Make it a type module and follow the conventions of the existing ones:

    • Toml.parse_str : Str, Toml.Props -> Try(Toml, [InvalidToml(Toml.Error)]), where Toml.Error is Parser.Diagnostic(Toml.Problem), carrying a typed problem, the most precise location and a logical path. Define a Toml.Problem union with structured payloads for source values, limits, and expected tokens. Keep native problems closed; Parser.custom widens them internally for composition. Render diagnostics at application boundaries;

    • Toml.parser : Toml.Props -> Parser(Parser.Utf8.Bytes, Toml, Toml.Problem) for callers who compose parsers, failing with ParseError({ problem, location, path });

    • Try(a, [Missing]) for every optional value, and methods in the .{ } block, called in method-call style in examples;

    • fallible parsing and validation functions return errors on every input; explicit constant assertions such as Parser.Quotation.require are documented separately.

  6. If records are a natural target, also implement a format for type-directed decoding behind CSV.decode_str and Yaml.decode_str, and expose Toml.decode_str with a Parseable where-clause alias. The parsers chapter of the Roc language reference lists the methods a format provides.

Prefer a scanner over the input bytes for document-level parsing, with an explicit stack for nesting. The combinator style is convenient for small grammars, but every rewritten module (CSV, XML, HTTP, Markdown) moved to scanners to stay linear in the input size and to avoid deep recursion, and JsonValue was written as one from the start.

Use one nominal Props record with defaulted fields for resource bounds, variants and validation policy. parse_str(source, {}) should select the format’s documented defaults. Add receiver transformations to the values they operate on; avoid a second parse_with entry point or a standalone public implementation module. Expose helper types through the format that owns them.

Runtime validate returns the same native typed diagnostics as parsing. Implement from_quote on the nominal document type by calling validation and rendering BadQuotedBytes(Str) only at the compiler boundary. Keep source names and format options in defaulted validation props. Add validate_as when type-directed decoding is supported. See Parse and validate during compilation.

For chunked formats, construct the shared descriptor with Parser.decoder or Parser.encoder, and expose the constructor through the format. Reuse common errors: { policy, max_errors } and chunk_bytes props. Preserve native format failures and source I/O causes instead of adding adapter wrappers. See Read and write documents in chunks for the decoder protocol and completion contract.

5.3.3. 3. Write the tests§

Put focused expect witnesses next to the code they cover. Prefer properties with small deterministic seeds and reviewable generators for broad syntax coverage: round trips, independent models, metamorphic relations and resource bounds. Shrink failures before retaining a witness. Exercise each configured limit at and past its boundary without checking in expanded scaling inputs.

Add compiler fixtures for valid and invalid quotations and imported text, including default checks and overrides, to scripts/literals/cases.json, and add import parser.Toml to the app header that app_source in scripts/check_literals.roc writes. The script checks rejection by both roc check and roc build, and verifies the intended error message rather than accepting any compiler failure.

Run the package tests:

$ROC test package/Toml.roc
$ROC test package/main.roc

5.3.4. 4. Add a probe and a review script§

A conformance review runs the same inputs through your module and the oracle and compares the results. It needs three pieces under scripts/toml/, modelled on scripts/csv/, the smallest of the existing reviews:

probe.roc

A basic-cli app that reads one document on standard input and prints one JSON line: the parsed value or an error. Use the public API only.

cases.json

Small explicit witnesses with a name and an input for contracts that generated checks would obscure. Generate broader cases from parameters. Recreate external conformance suites from pinned URLs and SHA-256 hashes in ignored scratch storage.

known-failures.json

The baseline. It starts empty; every entry added later needs a reason (see Property tests and conformance reviews).

Then add scripts/review_toml.py. It builds the probe with $ROC (falling back to roc on PATH), runs every case through the probe and the oracle, converts both results to the same shape, and exits non-zero on any disagreement that is not in the baseline. Report baseline entries that now pass as stale. If the oracle is a third-party package, pin it, and all of its dependencies, in scripts/toml/requirements.txt so that pip install --no-deps -r installs exactly the reviewed versions.

Acquire external suites from pinned revisions and verified hashes into ignored scratch storage. Commit the acquisition recipe, pins and licence information, not expanded suites. Keep small explicit regression witnesses when they clarify a contract; use deterministic generators and independent properties for broad coverage. Label intentional policy deviations with their reason.

Run it, fix the parser until it passes, and add a unit test under scripts/tests/ for any logic in the script that is not a direct call to the oracle.

5.3.5. 5. Add property-test targets§

Add at least two targets under fuzz/, as described in Property tests and conformance reviews:

toml-raw.roc

Raw bytes. Check that the parser never crashes, that errors point inside the input, and the metamorphic relations that hold for the format, such as line-ending equivalence. Bound pathological shapes with the runner’s --timeout.

toml-roundtrip.roc

A structured generator. Build a value and a way of writing it from the bytes, build the expected result from the specification without calling the parser, and require the parser to return exactly that value.

Check the generator against the oracle on a corpus before you trust it: add a corpus mode to the review script, like review_xml.py corpus.

Then add a robustness campaign to the review script: a campaign command, like review_yaml.py campaign and review_json.py campaign. It spends part of a time budget on libFuzzer runs of your targets and the rest on a seeded differential loop that generates and mutates documents, runs each through the probe and the oracle, and writes a report and a reproducer for every kind of disagreement under .roc-parser-tmp/. Give it --smoke for a short, iteration-bounded run in CI, --seconds and --seed for local runs, and a replay command for a saved reproducer.

5.3.6. 6. Register the targets§

  1. Add each target to FuzzPlan.entries in scripts/src/FuzzPlan.roc, with its seed encoding (raw for raw bytes, none for a structured target), seeds, dictionary, input size and timeout.

  2. Add reviewable seeds in fuzz/seeds/toml.json and syntax tokens in fuzz/dictionaries/toml.dict for the raw target. Add an import and a seed_sources entry in scripts/src/FuzzPlan.roc for the new seed JSON. Seed JSON and encoding bounds are validated at compile time.

  3. Add each target to the target matrix of the fuzz job in .github/workflows/fuzz.yml, in the same order as the scheduled entries in FuzzPlan.entries. Register the review in reviews in scripts/src/HarnessPlan.roc, with its command and requirements file, and the campaign’s --smoke run as a second, scheduled entry, as json and json-robustness do.

scripts/test_automation.roc fails if a fuzz/*.roc file is not registered, or if the workflow matrix lists the targets differently, or in a different order, from the registry. Every pull request runs CI-enabled native targets through blueprint run check. Check the registries and run the new targets:

python3 -m unittest discover -s scripts/tests -p "test_*.py"
scripts/fuzz.roc smoke toml-raw toml-roundtrip

5.3.7. 7. Add a benchmark§

Follow "Add a benchmark" in Benchmarks: add bench/roc/toml.roc, which calls the public entry point through Bench.run!, a branch in bench/roc/alloc.roc, a deterministic generator and pathological entries in scripts/bench.py, small deterministic scaling inputs, and vetted independent comparators pinned exactly. Register each language explicitly; leave a comparator list empty when no qualified implementation covers the workload, and document that gap. scripts/tests/test_bench.py requires a Roc driver and at least one comparator for each format. Check that the pathological documents scale linearly; the allocation counts from --allocations are a steadier signal than the timings:

python3 scripts/bench.py --quick --formats toml
python3 -m unittest discover -s scripts/tests -p "test_bench.py"

5.3.8. 8. Document the module§

  1. Add a chapter docs/toml.adoc that shows how to parse a document, how to handle errors, and what is and is not supported. Include it from the manual’s index with leveloffset=+1, like the other format chapters.

  2. Make every code example in the chapter a complete program that scripts/check_doc_examples.roc compiles and runs, so it cannot drift from the API.

  3. Add the module wherever the manual lists the formats: the format table in README.md, the lists in docs/index.adoc, and modules.adoc, overview.adoc, errors.adoc, compile-time-parsing.adoc, conformance.adoc, property-testing.adoc and performance.adoc.

  4. Add the module to the release notes for the next version in docs/releases/.

Build and check the documentation:

blueprint run api-docs
blueprint run doc-examples
blueprint run docs

5.3.9. Checklist§

5.4. Benchmarks§

This chapter is for contributors who want to measure a change, add a benchmark or reproduce the figures in Performance. It covers the harness, the corpora, the comparison programs, the environments the benchmarks run in, and how to read a report.

5.4.1. Run the benchmarks§

You need a recent Roc nightly; the report records which one. Go, Rust (cargo, Rust 1.98 or newer for the financial comparators) and Python 3 are needed only to create or qualify the corresponding reference measurements. Use Python 3.10–3.13 for Quiffen; Blueprint’s benchmark environment supplies 3.13. Ordinary runs rebuild and measure Roc, then reuse qualified reference timings. A missing or incompatible reference is reported as ReferenceUnavailable; it is never silently rebuilt or remeasured.

$ export ROC=~/roc_nightly-macos_apple_silicon-2026-10-21-5349569/roc
$ python3 scripts/bench.py --quick --refresh-references  # explicitly create reference timings
$ python3 scripts/bench.py --quick --label candidate     # rerun only Roc; reuse references
$ python3 scripts/bench.py --allocations                  # full settings require their own reference cache

--quick measures each format’s samples, its small generated document and one pathological document, with three short batches. Use it to check that nothing is broken, not to compare numbers. The full run takes about half an hour on a laptop when refreshing all implementations. Cached reference timings are reused without building or benchmarking Go, Rust or Python. Cheap runtime/version probes qualify the cache; they do not parse the benchmark corpus.

Narrow a run with these options:

--formats csv,xml

Only these formats.

--langs roc,go

Only these implementations (roc, rust, go, python).

--only large,sample-svg

Only documents with these names or kinds (sample, small, medium, large, pathological).

--label before

Write the report to .roc-parser-tmp/bench/before/, so two runs can be compared side by side.

--repetitions, --target-ms, --timeout

Batch count, approximate batch duration and the time limit for one batch.

--corpus-only

Write the corpus and its manifest, then stop.

--refresh-references

Explicitly build and measure selected reference libraries, then store their complete measurement receipts. This is the only mode that creates a comparator Python environment or installs its dependencies.

--reference-cache-dir

Share a cache across worktrees or output directories. The default is .roc-parser-tmp/bench/reference-cache, separate from report labels.

To compare two revisions of the parser, check each one out in turn and run the harness with a different --label; the report records the commit and whether the tree was dirty. Use the same reference cache for both revisions; parser, Roc compiler, and Roc benchmark-driver changes do not invalidate its entries.

Reference keys include the exact input SHA-256, comparator code and dependency pins, installed runtime/library versions, build recipe, ownership/workload, measurement settings, and machine/CPU-affinity/governor/resource configuration. Changing any of these requires an explicit reference refresh. Go builds and identity probes use GOTOOLCHAIN=local; select an installed Go compiler on PATH, because cache qualification must not download toolchains. Rust identity probes also disable automatic toolchain installation.

Cache files retain calibration, warmup and every measured batch, original commands, timestamps and source-report provenance. Corrupt, stale or incomplete receipts are rejected. Reports mark cache hits and preserve their original measurement date: reused timings are historical measurements, not fresh timing samples. They have no time-based expiry; refresh explicitly when new reference measurements are needed. This measurement cache is separate from the shared correctness oracle cache: performance depends on runtime, hardware and measurement conditions as well as the input.

Reference receipts persist in the local cache. A different machine configuration reports ReferenceUnavailable and requires an explicit reference refresh rather than reusing timings from another machine. Cross-language comparisons run locally.

5.4.2. Measure streaming performance and elapsed-time targets§

Measure record and event consumption separately from whole-document collection:

$ blueprint run streaming-bench -- --only medium --refresh-references --label streaming-before
$ blueprint run streaming-bench -- --only medium --label streaming-after --max-slowdown 1.5

The first command explicitly builds and measures the pinned Rust references; the second rebuilds Roc and reuses matching reference receipts. The JSON corpora contain the generated SBOM’s component records as an array or NDJSON, preserving number spellings. XML consumes decoded events without a tree. HTTP includes header-heavy request pipelines; large opaque bodies are separate workloads and cannot stand in for header parsing. The source is preloaded, with I/O excluded from the timer. --chunk-bytes sets Roc’s source chunk width (4,096 by default); file-source memory is checked by the stream allocation fuzz targets instead.

The references use serde_json owned values discarded one record at a time, quick-xml borrowed events, and httparse borrowed heads with body/trailer framing. XML’s event checksum coalesces adjacent text and decodes references and line endings. The report keeps ownership/workload labels; a matching checksum does not establish identical validation or lifetime guarantees. Xml.reader is also timed as a complete-source event reader for comparison. Consumer checksums must agree. JSON’s original-number and serde-normalized number spellings use separate expected checksums computed from an independent input model outside the timer; an unexplained mismatch cannot pass.

--max-slowdown 1.5 requires each selected non-pathological Roc measurement to take at most 1.5 times the fastest qualified reference’s elapsed time. It works for ordinary whole-document benchmark runs too. Collection and consumption are compared separately. A missing reference, failed Roc parse, or timing spread above 25% cannot pass; a noisy fastest reference must be remeasured rather than replaced by a slower library. The command exits with status one when a target fails or lacks qualified evidence. Detailed results and target decisions are saved in results.json and report.md.

On macOS the harness cannot pin CPU affinity. Run final measurements serially on an otherwise idle machine, retain the reported timing spread, and compare revisions on the same hardware and toolchains. The README runner below uses CPU affinity on Linux for its published figures.

Keep raw timing reports and reference receipts in the ignored .roc-parser-tmp/bench/ directory. Share reports as review or release attachments when needed; commit the harness and concise result summaries rather than machine-specific measurements.

5.4.3. Reproduce the README throughput table§

On Linux, choose an available logical CPU and run:

$ python3 scripts/readme_throughput.py --cpu 3
$ python3 scripts/readme_throughput.py --cpu 3 --refresh-references  # explicit initial reference measurement

The runner builds Roc drivers serially with --debug and the default speed optimization, then pins the timed runs to that CPU. It parses one deterministic document for each concrete README format, constructs the result and consumes a checksum. CSV collects string rows with CSV.parse_string_records; QIF and OFX use whole-document collection. The markup formats use validated fragments with explicit namespace roots for SVG and MathML. Documents are about 64 KiB, except HTTP, which uses a 555-byte browser GET request with realistic headers and no body. Opaque body bytes would inflate throughput without adding parsing work. AsciiDoc parses without rendering, and Roc source produces a complete lossless syntax tree. These are representative workloads, not cross-format speed comparisons.

Each number is decimal MB/s from the median of seven calibrated batches targeting 200 ms after warmup. The iteration cap can produce shorter batches. Reading stdin and printing results are outside the timer. Rejected inputs, unstable checksums and runs with more than 25% timing spread cannot supply a published number. The runner leaves inputs, build logs and full measurement receipts in .roc-parser-tmp/readme-throughput/. The local results.json records its source hash, compiler, hardware, input hashes and samples; changing Roc requires a new run. The default rebuilds and measures Roc and reuses qualified reference timings. --refresh-references explicitly builds and remeasures the reference parsers, replacing their cached measurements. Refresh needs the Go/Rust/Python comparison toolchains, Ruby 3.4 or newer, and Zig 0.17.0 for the pinned compiler parser; blueprint shell provides the pinned Zig. --python selects an existing interpreter with the standard comparator pins; markup references use a separate virtual environment. --reference-cache-dir selects the shared measurement cache.

The table selects the fastest successful collection reference from the measured candidates. Borrowed results remain labeled, and known canonical/checksum failures cannot win. Namespace-resolved DOM references for HTML, SVG and MathML perform tree construction; Roc additionally retains source and performs authoring validation. QIF’s borrowed Rust reference uses floating-point amounts. Other parsers also construct their own tree models, including the compiler’s tokenizer/AST versus RocSyntax’s lossless indexed tree. These distinctions are retained with library versions and original reference receipts; no aggregate speed ratio assumes identical semantics or ownership. Current Roc DOM outputs are checked against cached independent canonical evidence on every run.

5.4.4. How a measurement works§

Every implementation, in Roc or in another language, is a small program that follows one protocol. It reads a document from standard input, starts a monotonic clock, parses the document a given number of times, stops the clock, and prints one JSON line:

{"iterations":120,"elapsed_ns":20414000,"successes":120,"checksum":43811}

Process start-up, reading the input and printing are outside the timed region. Collection comparators build their in-memory result and fold over it into a checksum. Comparators labelled consume discard each completed record and reuse buffers where the library permits. Roc’s CSV, QIF and OFX consumption drivers pass preloaded input through the public streaming APIs in 64 KiB chunks, validate each record, and consume the same decoded field checksums as their collection drivers without collecting a document. Compare collection and consumption workloads separately; the financial references currently provide collection comparisons only.

These drivers preload the source: their RSS includes the complete input and does not establish bounded file-source memory. Use bench/roc/csv-file-stream.roc for a file/stdin source that retains bounded read buffers and includes reading in its timing. Its checksum weights raw field bytes directly, including bytes that are not valid UTF-8.

QIF compares typed bank transactions with Quiffen and Rust qif_parser. Rust QIF retains borrowed strings and uses f64 amounts rather than Roc’s exact monetary values. It is reported as collect-borrowed and excluded from ratios against owned collection. Rust roxmltree also borrows from the input and is excluded from owned-collection ratios. Either can appear as the fastest measured collection reference when the table labels these differences explicitly. The report lists each implementation’s representation and workload. OFX compares bank/card statements with ofxtools, ofxparse, ofxgo, and Rust ofx-rs. Financial checksums include transaction counts and weighted UTF-8 payee/name and memo bytes; exact amounts and dates are checked by the correctness reviews. CSV checksums include every decoded field byte. The Go QIF slot has no comparator because the surveyed Go reader does not provide the required coverage.

When several Rust toolchains are installed, select the intended one explicitly, for example RUSTUP_TOOLCHAIN=1.98.1 python3 scripts/bench.py --formats qif,ofx. The report records the selected compiler and locked library versions.

For each document and implementation, scripts/bench.py:

  1. runs one parse to estimate its cost, and picks the number of parses per batch so a batch takes about --target-ms (200 ms, or 20 ms with --quick);

  2. runs one warmup batch and discards it;

  3. runs --repetitions batches (7, or 3 with --quick), each in a fresh process;

  4. records the median time per parse, the minimum, maximum, standard deviation and spread ((max - min) / median), throughput in MB/s (1,000,000 bytes per second, from the median), and the peak resident set size of the batch processes;

  5. with --allocations, replays the document once through bench/roc/alloc.roc, a diagnostic built with roc build --fuzz, and records how many times the Roc parser called roc_alloc for it, using the target’s measure command.

Peak RSS is the whole process, so it includes the language runtime: about 1.5 MiB for Roc and Rust, 12–20 MiB for Go and Python, before any document is read. Compare it within a language or across document sizes, not as an absolute cost of a parser.

5.4.5. Corpora§

The corpus is generated by build_corpus in scripts/bench.py every run. Each generated document uses a random number generator seeded from its own name, so the bytes, and the SHA-256 recorded for each one in the report, are identical on every machine and run. Changing a generator changes the hashes, which marks results as not comparable with earlier ones.

Each format has five kinds of document:

sample

Small real-world-shaped files in bench/corpus/samples/: a CSV order export, this repository’s fuzz workflow (YAML), a design-tool-style SVG, a CycloneDX 1.6 software bill of materials (JSON), this repository’s README, and an HTTP browser request and JSON API response (kept in HTTP_SAMPLES so their CRLFs survive). They are frozen copies; do not update them.

small, medium, large

Generated documents of about 4 KiB, 64 KiB and 1 MiB in the shape of typical data: CSV rows with quoted fields and embedded newlines, YAML service configuration with flow collections and block scalars, an XML catalogue with attributes and entities, a CycloneDX-style JSON bill of materials whose components carry licences, nested properties, integers beyond 64 bits, long decimals and escaped or non-ASCII strings, a Markdown document with headings, lists, code, quotes and tables, and HTTP POST requests with a JSON body and chunked HTML responses.

pathological

Inputs that stress one mechanism: very wide CSV rows and long runs of escaped quotes, YAML nesting at the 100-level limit and long flow sequences, deeply nested XML and thousands of attributes, JSON arrays nested to the 128-level limit, a 200,000-number array, objects with 20,000 keys or 20,000 empty-array members, strings full of escapes or surrogate pairs and a 100,000-digit number, unclosed Markdown emphasis and nested brackets, and HTTP messages with many headers or thousands of one-byte chunks. Several of these were super-linear before the fuzzing work described in Bound work on untrusted input.

The corpus and a manifest.json with every document’s size and hash are written to .roc-parser-tmp/bench/<label>/corpus/, so you can feed a document to a driver by hand:

$ .roc-parser-tmp/bench/full/bin/roc-yaml 100 < .roc-parser-tmp/bench/full/corpus/yaml/generated-medium.yaml

5.4.6. Roc drivers§

bench/roc/ holds one driver per format, each a few lines calling the format’s public entry point: CSV.parse_str, Yaml.parse_str, Xml.parse_str, JsonValue.parse_str, Markdown.parse_str, and HTTP.Request.parse_bytes or HTTP.Response.parse_bytes, each with explicit props. The timing loop is shared in bench/roc/Bench.roc. The harness builds each driver with roc build and its default speed optimization. Measure production throughput with that optimized binary.

When an entry point is renamed, only the call in that format’s driver (and the matching line in alloc.roc) changes.

5.4.7. Comparison programs§

LanguageWhereLibraries, pinned by
Rustbench/compare/rust/csv, yaml-rust2, serde_yaml, quick-xml, roxmltree, serde_json, pulldown-cmark, comrak and httparse, pinned exactly in Cargo.toml and Cargo.lock; built with cargo build --release --locked, thin LTO and one codegen unit.
Gobench/compare/go/encoding/csv, gopkg.in/yaml.v3, encoding/xml, encoding/json, goldmark and net/http, pinned by go.mod and go.sum.
Pythonscripts/bench_python.py and bench/compare/python/requirements.txtcsv, PyYAML (pure Python and libyaml), xml.etree, json, markdown-it-py and h11. The program is under scripts/ because repository policy keeps every Python file there. The harness installs the pins into a virtual environment in ~/.cache/roc-parser/bench-venv (outside the checkout, for the same reason); pass --python to use another interpreter.

Each program builds the same kind of in-memory structure as roc-parser:

These choices, and where they still differ, are listed in Compare equivalent work. Read them before quoting a ratio.

To add a comparator, add a function to the language’s program, register its name in COMPARATORS in scripts/bench.py with a one-line description of what it measures, pin the library, and run --quick --langs <lang> to check that it accepts the samples.

5.4.8. Add a benchmark§

scripts/tests/test_bench.py checks that the corpus is deterministic, that every format has every kind of document, and that each format has a driver and comparators; run it with python3 -m unittest discover -s scripts/tests.

5.4.9. Read a report§

Each run writes three things to .roc-parser-tmp/bench/<label>/:

results.json

Metadata (date, machine and CPU, OS, Roc, Go, Rust and Python versions, every crate, module and package version, the commit and whether the tree was dirty, the corpus manifest with hashes, and what each implementation measures), one record per document and implementation, and the comparison table.

report.md

The same as Markdown tables.

logs/

Build output and allocation diagnostics.

The comparison table gives, per format and library, the geometric mean over the non-pathological documents that both implementations accepted of the ratio of median times. "roc-parser is 3x slower" means the library parsed those documents three times as fast. Pathological documents are left out of the mean because they measure worst cases, which differ by design between parsers; read them individually.

In the per-document table, check these before trusting a number:

5.4.10. Continuous integration§

Cross-language performance and correctness comparisons are local-only. CI, nightly compatibility checks and releases do not build or run the references.

Every pull request runs --quick --langs roc --fail-on-error as the bench section of blueprint run check (see Contributing to roc-parser). It fails only when a Roc driver does not build, crashes, or exceeds its 60-second batch limit, which catches a parser that has turned quadratic without gating on noisy timings.

5.4.11. Reproducible environment with Blueprint§

Blueprint.roc at the repository root describes the bench environment for roc-blueprint 0.13.0: Go, Rust (cargo and rustc), Python and Git from Nix, at the nixpkgs revision the committed Blueprint.lock records, and Roc from your machine. It declares x86_64-linux and aarch64-darwin (Apple silicon Macs), and needs Nix with flakes enabled. Install the blueprint binary as Contributing to roc-parser describes, put the latest Roc nightly on PATH as roc, and run:

$ blueprint run bench-quick              # small corpus
$ blueprint run bench                    # full comparison, with --allocations
$ blueprint run bench -- --only csv      # extra arguments go to scripts/bench.py
$ blueprint shell bench                  # every toolchain on PATH
$ blueprint update                       # refresh Blueprint.lock

The first run downloads the toolchains; later runs start in a few seconds. Generated files go to .blueprint/, which is not committed. Leave ROC unset when you run blueprint: Blueprint reads ROC to choose the compiler that evaluates Blueprint.roc itself.

Compiler. Roc is not pinned. The local tasks use roc from your PATH; the report names the compiler measured. Use the latest nightly to match package CI.

Go, Rust and Python follow the nixpkgs revision in Blueprint.lock. Library versions are pinned by Cargo.lock, go.sum and requirements.txt as above, so a Blueprint run and a plain run measure the same library code. The Python packages go into one virtual environment per interpreter, under ~/.cache/roc-parser/.

Provenance. The bench and bench-quick tasks pass --blueprint to scripts/bench.py. The report’s Environment line, and metadata.environment in results.json, then record the SHA-256 of Blueprint.lock and the nixpkgs revision it pins; a run without the flag says outside blueprint. Both record the path of each toolchain, so you can check that it came from /nix/store.

Without Nix, install the toolchains yourself and run scripts/bench.py as shown in Benchmarks; the report records every version either way.

5.4.12. Profile one workload§

For a closer look at one parser, build its driver with debug information and sample it. On macOS, samply works on the optimised binaries:

$ "$ROC" build --debug bench/roc/yaml.roc --output=.roc-parser-tmp/yaml-prof
$ samply record .roc-parser-tmp/yaml-prof 1000 < .roc-parser-tmp/bench/full/corpus/yaml/generated-medium.yaml

Some Roc functions keep hashed linker names even with --debug; nm -n maps recorded offsets back to symbols.

scripts/bench_yaml.py is an older, YAML-only harness kept for comparing two revisions of the YAML parser with --replace-dep (--parser-root <checkout>/package) on the YAML microbenchmark fixtures, and for its allocation counter, fuzz/yaml-alloc.roc. Its reports go to .roc-parser-tmp/yaml-bench/<label>/.

5.4.13. Markup runtime comparison§

Compare HTML, SVG and MathML runtime parsing against the pinned Rust, Go and Python DOM libraries with scripts/compare_markup_performance.py. Install Python with venv support, Cargo and Go, and use a Roc compiler built in ReleaseFast mode. Preparation installs the hash-pinned Python dependencies and builds the drivers; measurement runs separately:

python3 scripts/compare_markup_performance.py \
  --roc /path/to/roc --compiler-build-mode ReleaseFast prepare
python3 scripts/compare_markup_performance.py run

These are local runs. Reports and dependency caches stay under .roc-parser-tmp/markup/dom-compare/; pass --work before each subcommand to use another directory. Keep sources unchanged between preparation and timing: the harness checks source and binary hashes and retains the compiler identity, dependency locks, inputs, canonical trees and every sample.

The full comparison covers 16 geometric and document inputs. Every adapter must first agree with an independent complete-tree model on namespace URI, local names, attributes, exact text and child order. The projection consistently omits namespace declarations, comments, processing instructions and doctype metadata. Seven warmed samples use shuffled implementation order. Parsing, DOM allocation, consumption and ordinary result destruction or collection are timed; compilation, startup, imports, input reads and decoding precede the timer. Roc applications use --debug and the default speed optimization.

results.json records bytes_per_second; divide by 1,000,000 for MB/s. Higher throughput is better. The default gate requires the full Roc parser’s median elapsed time to be at most 1.5 times the fastest eligible reference on every input; use run --threshold 1.0 to require parity. A failing gate returns a nonzero exit and retains the results. The full Roc lane includes source tracking and authoring validation; the raw lane is diagnostic and does not replace it. Compilation and checked-literal costs from scripts/bench_markup.py are separate measurements.

5.4.14. Roc syntax reference runtime comparison§

Run scripts/compare_roc_syntax_performance.py locally to compare the pure Roc parser with the pinned compiler parser on each identical input. Build the oracle with scripts/roc_syntax/build_oracle.py --optimize ReleaseFast first, then pass its executable through --oracle-command and the ordinary Roc syntax correctness worker through --probe-command. The comparison builds its own Roc driver using the default speed optimization; --candidate-binary explicitly reuses an existing optimized driver and marks its build provenance unverified in the report.

python3 scripts/compare_roc_syntax_performance.py \
  --roc /path/to/roc \
  --probe-command '/path/to/roc scripts/roc_syntax/probe.roc' \
  --oracle-command .roc-parser-tmp/syntax-oracle/install/bin/roc-syntax-oracle

The default local run covers 33 geometric and document inputs. Repeat --fixtures path.json to include selected curated or extracted corpora, retaining each mode. Every input must first agree on acceptance and complete consumption; accepted inputs must also agree on their normalized tree. The oracle bounds its projection depth at 256; documented depth cutoffs match the corresponding candidate subtree and are counted explicitly in the report. Those inputs are labelled partial_depth_limited, not complete structural validation; this local timing eligibility rule does not relax the ordinary grammar gates. Unknown grammar variants remain errors. Timing excludes JSON, source encoding, startup and I/O. Each parser repeatedly tokenizes, parses, and releases its result while observing only constant-time result counts. Neither parser performs a full AST traversal inside this timing lane; the existing geometric benchmark remains separate.

Batches calibrate to at least 20 milliseconds, warm up, and collect five samples. Reports retain every sample, source hash, build receipt, median ratio and a conservative ratio lower bound (fastest Roc sample / slowest reference sample). A one-microsecond reference floor avoids division by zero and exaggerated tiny input ratios. The default --max-reference-ratio 1.5 requires runtime within 1.5 times the optimized reference on every input. Exceeding its lower-bound ratio returns a nonzero exit and saves the exact request and timings under failures/ for replay. On 19 October 2026 all 33 inputs passed the 1.5 lower-bound ratio on a loaded host (load average about 11). Medians on that host still exceeded 1.5 for some adjacent-statement and unclosed inputs, so compare retired instructions when the host is busy: those families run 1.1 to 1.35 times the reference’s instructions. Use --replay failures/<hash>-<mode>.json to time a saved input again.

--smoke selects only two tiny inputs and one one-millisecond sample, checks the protocol and correctness, and disables ratio assertions. Both smoke and full reference comparisons are explicit local activities. Build the reference oracle with ReleaseFast, also required for a full ratio comparison. Its build receipt must match the binary hash and immutable compiler revision. The smoke permits an explicit ReleaseSafe build for optional safety checks.

5.5. Compiler, releases, settings and security reports§

This chapter is for maintainers with write access to the repository. It covers which Roc compiler the repository uses, how a release is cut and what it publishes, the repository settings the workflows rely on, and how security reports are handled. It assumes Contributing to roc-parser.

roc-parser follows the roc-automation model for Roc packages, with the deviations listed in Repository tooling. The model’s manual, at the revision this repository uses, explains the reasons behind each rule: the three workflows, optional workflows and repository settings.

5.5.1. Compiler and nightly§

Roc has no stable release yet, and roc-parser does not pin a nightly. Every workflow resolves the latest release of roc-lang/nightlies once per run and passes that tag to roc-automation’s setup-roc-nightly. The installer checks the compiler archive against the size and SHA-256 published for that release. Every job uses the compiler selected for its run:

There is no compiler pin file and no automated update controller: the compiler moves by itself every day, and nothing else in the repository changes because of it. Package modules and maintained applications follow that compiler; orchestration scripts use the fixed tooling compiler described below.

5.5.1.1. Nightly compatibility§

nightly.yml calls roc-automation’s shared nightly compatibility workflow every day at 13:17 UTC. It downloads the examples archive of the latest final release, checks, builds and runs every case of its spec.json on the latest nightly on Linux x86-64 and macOS arm64, and keeps one tracking issue: opened by the first failure, commented on by later ones, closed by the next pass. A failure means the published release needs a patch release that works with the new compiler.

A run can be dispatched by hand with a nightly-tag and a release-tag to reproduce a past combination; such a run never opens or closes the issue.

5.5.1.2. The tools that do not move§

These change only through a pull request a maintainer reviews:

5.5.2. Repository tooling§

The check, package, bundle, documentation-example and release drivers are executable Roc scripts under scripts/, run with the fixed roc-stable compiler fetched by Blueprint. Routine validation uses compilation caches and explicit dev builds; instrumented fuzz binaries use the default LLVM build mode. Cache bypasses require a concrete reason and a removal condition. Package commands use the separate roc on PATH. The released release-package utility from tools-0.10.1 builds the bundle, examples archive, API archive and notes, and tests archived examples against the exact bundle. The shared release workflow composes it with the manual and the bundle’s SBOM. These local commands publish nothing. Independent Python, Ruby and Zig oracle harnesses compare parser behavior. scripts/automation-policy.json lists every runtime exception and each probe or compiler fixture that must use the floating nightly. The policy check rejects unlisted files, stale exceptions, tooling pin drift and workflow pin drift. The shared working-tree and archive checks both use tests/spec.json. Their pinned validator retains uncached example builds as an independent source/bundle verification boundary. This exception ends when reviewed shared tooling supports cached checks with each receipt bound to that input identity.

The manual builds as HTML and PDF with roc-docs from tools-0.10.1. Blueprint fetches the native binary and checks its digest against Blueprint.lock. Use the documentation commands in Contributing to roc-parser to reproduce the build.

Write upcoming release changes in docs/releases/unreleased.adoc; rename that file to the chosen version when preparing its release record.

5.5.3. Release a new version§

Releases follow semantic versioning: a release that changes the result of a public function on any input, or removes or changes a public signature, is a major release.

5.5.3.1. Before you start§

  1. Confirm that main is green: CI, and the Release dry run of the last pull request.

  2. Choose the version with roc bump, which compares the public API with the latest release and says how far the version must move:

    roc bump --old https://github.com/lukewilliamboswell/roc-parser/releases/download/<latest>/<hash>.tar.zst package/main.roc
    

    The shared release gate accepts a requested major greater than the latest final major before API extraction: every possible API change fits that bump. For a same-major release, it runs roc bump --expect <version> and stops if the comparison fails or understates the change. Compilation, the full package checks and exact release-candidate tests remain required for every release. Validation allows 75 minutes so the full checks complete before assembly.

  3. Write the release notes as docs/releases/<version>.adoc, usually by renaming docs/releases/unreleased.adoc, and link them from docs/releases/index.adoc. Put the upgrade impact first. Show how to depend on the release with the placeholder {package-url}, which the workflow replaces with the bundle’s URL; notes that do not use it are refused. Use headings, paragraphs, lists, [source] blocks, simple tables and links: the notes become the GitHub release description as Markdown, and other markup is refused.

  4. Merge the notes through a normal pull request. Its Release dry run renders the notes of the highest version under docs/releases/, which are these.

5.5.3.2. Run the release§

Run the Release workflow (release.yml) from the Actions tab on main, with version set to the new version, for example 2.1.0, or 2.1.0-rc.4 for a release candidate. Leave nightly-tag empty to resolve the latest compiler, or name an exact nightly to reproduce reviewed compiler evidence. The reusable CI job runs the native package checks, mocked harness tests and Roc-only smoke workloads before release assembly. It passes the validated compiler identity to the build, candidate tests and documentation jobs. The pinned roc-automation release-package.yml workflow validates the version, builds the package and tests the unchanged release candidate. It owns SBOM validation, attestations and publication. Its publishing jobs run no repository code.

The sbom-composition task accounts for package source files and the reviewed Unicode inputs in scripts/release/dependencies.json. It checks archive members against checkout SHA-256 digests; the shared tools validate the graph and bind the sidecars to the actual bundle.

Every pull request runs the shared release workflow with 0.0.1-dryrun and publishes nothing. A separate read-only job builds the manual and unmodified compiler API documentation using the release’s resolved nightly. A final release deploys that site to GitHub Pages.

5.5.3.3. What a release contains§

AssetContents
<hash>.tar.zstThe package bundle that users name in their app header.
roc-parser-examples-<version>.zipThe examples, with their parser dependency pinned to the bundle’s URL and nothing else changed, a README, and spec.json: what each example prints, from tests/spec.json, in the form roc-automation’s nightly compatibility runner reads.
roc-parser-docs-<version>.zipThe unmodified roc docs API reference, under the version directory. The manual is a separate PDF asset; GitHub Pages serves the manual and API reference together for the latest final release.
roc-parser-<version>.pdfThe manual as one PDF.
<hash>.tar.zst.spdx.json, <hash>.tar.zst.spdx-2.3.json, <hash>.tar.zst.cdx.jsonThe source inventory in SPDX 3.0.1, SPDX 2.3 and CycloneDX 1.7 formats, bound to the bundle’s SHA-256 and accounting for its source and dependencies.
<hash>.tar.zst.sbom-manifest.jsonThe manifest binding the bundle and its SPDX 3.0.1 and CycloneDX sidecars by SHA-256.
LICENSEThe package license.

GitHub stores build-provenance attestations for the release assets.

Nothing in the repository changes after a release. The examples in examples/ always use the package source (parser: "../package/main.roc"); only the archive attached to the release points them at its URL.

5.5.3.4. After the release§

  1. Check the release page: the notes, with the bundle URL and the compilers, and every asset above.

  2. Verify the provenance of the bundle you downloaded:

    gh attestation verify <hash>.tar.zst --repo lukewilliamboswell/roc-parser
    
    # Download the attestation for offline use.
    gh attestation download <hash>.tar.zst --repo lukewilliamboswell/roc-parser
    
  3. For a final release, open the published manual and API reference and check that they show the new version, and dispatch Nightly compatibility to test the new release on the latest nightly.

If publication stops part way, follow Recover an interrupted publication. Never publish a draft by hand or replace an asset; changed bytes are a new version.

5.5.4. Repository settings§

The workflows rely on these settings, which a maintainer applies on GitHub; roc-automation’s check-settings task, run from a checkout of roc-automation, reports which are in place:

5.5.5. Security reports§

The policy is in SECURITY.md: security fixes are made for main and the latest release, and reports arrive privately through GitHub private vulnerability reporting, never in public issues.

When a report arrives:

  1. Acknowledge it within 7 calendar days and give an initial assessment within 14.

  2. Reproduce it with the reporter’s input. If it is a crash, hang or excessive resource use, add the input to the matching fuzz target’s seeds or the module’s tests in the private fix branch, as described in Property tests and conformance reviews.

  3. Prepare the fix in the private security advisory’s temporary fork, and agree a disclosure date with the reporter. The target is coordinated disclosure within 90 days.

  4. Release the fix as a new version, then publish the advisory, crediting the reporter if they agree.

6. Background§

6.1. Where the design comes from§

roc-parser is not a new idea. Parser combinators have been studied and used for fifty years, and almost every choice in the Parser and Parser.Utf8 modules repeats, adapts or deliberately declines something from that history. This chapter traces those ideas to their sources, so you can see why the library behaves as it does and where it differs from the libraries you may already know. It assumes you have read Your first parser; you do not need it to use the package.

The examples come from background-lineage.roc; every expect in it passes.

6.1.1. A parser is a function§

The founding idea is that a parser is an ordinary function: it takes the input, reads something from the front of it, and returns what it read together with the input that is left. Because parsers are values, functions that take parsers and return new parsers (the combinators) can express sequencing, choice and repetition directly, so the parser has the shape of the grammar it reads.

William Burge described such a set of combinators in his 1975 book Recursive Programming Techniques. Philip Wadler’s 1985 paper "How to replace failure by a list of successes" showed how a lazy functional language can represent a parser’s possible outcomes as a list: an empty list is failure, and several elements are the several ways the input could be read. Graham Hutton’s 1992 paper "Higher-order functions for parsing" turned these ideas into a complete, teachable library and became the standard reference.

roc-parser keeps the function and drops the list. A Parser(input, a, problem) wraps a function from input to a Try holding either the value and the rest of the input, { value, rest }, or a ParseError with a problem tag and an offset. Parser.custom takes such a function directly, so you can always step outside the combinators, and Parser.run runs a parser and returns the same shape:

# A parser is a function from input to a result and the rest of the input.
# `custom` wraps such a function directly.
take : U64 -> Parser(Parser.Utf8.Bytes, Parser.Utf8.Bytes, _)
take = |n|
	Parser.custom(
		|input|
			if input.len() >= n {
				Ok({ value: input.take_first(n), rest: input.drop_first(n) })
			} else {
				Err(ParseError(Parser.Diagnostic.{ problem: MissingBytes(n), location: InputOffset(0), path: [] }))
			},
	)

# Monadic style: the next parser depends on a value already read. Here a
# length prefix says how many bytes follow.
length_prefix : Parser(Parser.Utf8.Bytes, U64, _)
length_prefix = Parser.Utf8.digits({}).skip(Parser.Utf8.codeunit(':'))

counted : Parser(Parser.Utf8.Bytes, Str, _)
counted = length_prefix.and_then(take).map(Str.from_utf8_lossy)

expect (counted).parse_str("3:abc") == Ok("abc")
expect (counted).parse_str("3:ab").is_err()

A parser returns one result, not a list. Wadler’s list of successes allows ambiguous grammars, where one input has several readings, but the formats this package reads are designed to have exactly one reading. A single result is cheaper, and it gives one clear place to put an error.

6.1.2. Sequencing: monads and applicatives§

Running one parser and then another needs a way to combine their results. Hutton and Erik Meijer’s "Monadic parser combinators" (1996) and the shorter "Monadic parsing in Haskell" (1998) observed that parsers form a monad: the sequencing operation, usually called bind or and_then, passes the value from the first parser to a function that chooses the second. That is what counted above does with and_then: the length it reads decides how many bytes the next parser takes.

Most of a grammar does not need that power. In "Applicative programming with effects" (2008), Conor McBride and Ross Paterson named the weaker applicative interface: a pure value lifted into the parser, and a way to apply a parser of functions to a parser of arguments. Their paper points to parsing as a case where monads are more than you need, citing S. Doaitse Swierstra and Luc Duponcheel’s 1996 combinators, whose structure is known before any input is read. Applicative parsers cannot make the grammar depend on earlier values, and in return they read as a description of the input.

Parser.const with keep and skip is exactly that applicative interface, with the names Evan Czaplicki’s elm/parser gave its pipeline operators: |= keeps a result and |. ignores one. You list the pieces of the input in order, and the curried constructor receives the ones you keep:

# Applicative style: a constructor function, then one `keep` per field and
# one `skip` per piece of punctuation. No step depends on an earlier value.
Point : { x : U64, y : U64 }

point : Parser(Parser.Utf8.Bytes, Point, _)
point =
	Parser.const(|x| |y| { x, y })
		.skip(Parser.Utf8.codeunit('('))
		.keep(Parser.Utf8.digits({}))
		.skip(Parser.Utf8.codeunit(','))
		.keep(Parser.Utf8.digits({}))
		.skip(Parser.Utf8.codeunit(')'))

expect (point).parse_str("(3,4)") == Ok({ x: 3, y: 4 })

roc-parser builds map and keep on this same application step, and also offers the monadic and_then method for the cases that need it, such as counted. Prefer keep and skip when the grammar allows: a parser that uses only them can be read top to bottom as a description of its input.

6.1.3. Choice and backtracking§

A choice between alternatives must decide what happens when the first one fails after reading part of the input. The libraries in this lineage give three answers.

Full backtracking

The next alternative starts again from the original input. Wadler’s lists and Hutton’s combinators work this way, and so does Bryan O’Sullivan’s attoparsec, which keeps the whole input so that it can backtrack arbitrarily.

Committed choice

Daan Leijen and Erik Meijer’s Parsec (2001) tries the second alternative only if the first failed without consuming input. Their paper gives two reasons: naive backtracking holds on to the input and leaks space, and when the first alternative has already read part of the input, its failure is usually the error worth reporting. When you do want to backtrack, you wrap the first alternative in try. Elm’s parser takes the same default for the same reason, with backtrackable in place of try; its README uses the input [ 1, 23zm5, 3 ], where the useful error is at the z, not at the [.

Ordered choice

Bryan Ford’s parsing expression grammars (PEGs, 2004) define choice as prioritized: A / B tries A, and tries B from the same position only if A fails. Once A succeeds, B is never considered, even if the rest of the parse then fails.

roc-parser’s Parser.alt and Parser.one_of are PEG ordered choice. A failed alternative is always retried from the original input, so there is no try, and the first success wins:

# Ordered choice, as in a PEG: the first alternative that succeeds wins, and
# a failed alternative is retried from the same input.
sign : Parser(Parser.Utf8.Bytes, [Plus, Minus, PlusPlus], _)
sign =
	Parser.one_of([
		Parser.const(PlusPlus).skip(Parser.Utf8.string("++")),
		Parser.const(Plus).skip(Parser.Utf8.string("+")),
		Parser.const(Minus).skip(Parser.Utf8.string("-")),
	])

expect (sign).parse_str("++") == Ok(PlusPlus)
expect (sign).parse_str("-") == Ok(Minus)

The trade-offs follow from the history. Without a commit point, you never have to decide where to put try, and a grammar reads the same as its PEG. The costs are the ones Parsec was designed to avoid: a grammar whose alternatives share long prefixes re-reads them, and without help a failure deep inside an early alternative would be replaced by the failures of the later ones. The furthest-failure rule described below provides that help, and Combinators by task shows how to order alternatives so that re-reading rarely matters.

6.1.4. Repetition that always ends§

PEG repetition is greedy: e* matches as many e as it can and never gives any back. Parser.many, one_or_more and sep_by behave the same way, which is why a failed element ends the repetition rather than the parse.

Repeating a parser that can succeed without reading anything, such as chomp_while or maybe(p), would loop forever. Parsec refuses at run time, with the error "combinator 'many' is applied to a parser that accepts an empty string". roc-parser instead stops the repetition at the first element that makes no progress, and returns what it has collected. The check compares input lengths, so it costs O(1) per element and needs only the len method from the input type, which the where clause on many states. A Roc program should not crash on its input, so a well-defined result is better than an error that only appears for some inputs.

6.1.5. Error reporting§

The early papers say little about errors. Parsec made them a design goal: on failure it reports the position and the set of things that would have been accepted there. Mark Karpov’s megaparsec, a fork of Parsec, keeps that and, when it merges the errors of two branches, prefers the one that got further into the input. Elm’s parser adds context: a parser can say "I am reading a list", so the error can name what was being read and not just the row and column, which the Elm compiler uses for its error messages.

roc-parser adopts megaparsec’s rule. Parsers track the furthest failure, the failure that read furthest into the input, and every runner reports it as ParseError({ problem, location, path }). Generic input offsets count input elements; text runners retain original byte offsets and line/column coordinates. A repetition that stops at a bad element remembers that element’s failure, so if the parse then fails, the error points at the bad element rather than at the input left over after the repetition:

# Furthest failure: `many` stops at the bad second point, but the error the
# runner reports is that point's own failure, at the byte where it happened.
points : Parser(Parser.Utf8.Bytes, List(Point), _)
points = Parser.many(point)

expect (points).parse_str("(1,2)(3,x)") == Err(ParseError(Parser.Diagnostic.{ problem: ExpectedDigit, location: Text({ offset: 8, line: 1, column: 9 }), path: [] }))

The alternative that got furthest is almost always the one the author of the input meant, so this recovers most of the precision of committed choice while keeping ordered choice. ParseError is an open tag union, so ? passes it through a function that returns other errors too.

The format modules go further on their own: Yaml and Xml report line and column, CSV the record and field, and HTTP the byte offset, each in an error tag named after the format. Report parse errors shows how to present them.

6.1.6. Bytes, not characters§

Haskell’s early combinator libraries read lists of characters. The libraries built for speed read bytes: attoparsec has a ByteString interface, and Geoffroy Couprie’s nom for Rust describes itself as "byte-oriented" and "zero-copy". The Roc language reference gives the same advice for Roc: in the strings chapter of the language reference, it recommends working in UTF-8 bytes when implementing a parser.

So the input type of the text parsers, Parser.Utf8.Bytes, is List(U8), and 'a' is a byte. Every format in this package is defined over ASCII delimiters, which in UTF-8 are single bytes that never appear inside a multi-byte character, so reading bytes is both correct and fast. Text is turned back into Str only once a whole field has been read.

6.1.7. Recursion and laziness§

A grammar for nested data refers to itself: a list contains values, and a value can be a list. In Haskell this costs nothing, because laziness delays building a parser until it is used. Roc evaluates eagerly, so a parser that refers to itself would be built forever. Parser.lazy takes a function that builds the parser and calls it only when the parser runs, exactly as elm/parser’s lazy : (() -> Parser a) -> Parser a does for eager Elm. Combinators by task shows it in use.

Left recursion, where a rule starts with itself (expr = expr "-" term), is a different problem. A top-down parser calls the rule again before reading anything and never stops. Ford’s PEG paper notes that left recursion is not available in PEGs. Research has since shown how to support it in combinators, for example Richard Frost, Rahmatullah Hafiz and Paul Callaghan’s 2008 "Parser combinators for ambiguous left-recursive grammars", and Joshua Barretto’s chumsky for Rust offers left recursion and memoization as opt-in features. roc-parser supports neither. Write the repetition explicitly instead: a term followed by many of "-" term.

6.1.8. Packrat parsing and performance§

Backtracking can re-run the same parser at the same position many times, and in the worst case the time grows exponentially with the input. Ford’s packrat parsing (2002) fixes this by memoizing every rule’s result at every position, which guarantees linear time at the cost of memory proportional to the input times the number of rules.

roc-parser does not memoize. For the formats it targets, careful ordering of alternatives keeps backtracking shallow, and memoization would cost memory on every input to protect against grammars that are rare in practice. Where speed matters most, the package goes further: Add a format module explains that the CSV, XML, HTTP and Markdown modules moved their document-level parsing from combinators to byte scanners with an explicit stack, to stay linear in the input size and avoid deep recursion. Combinators remain the interface for composing them and for writing your own parsers.

6.1.9. Combinators and parser generators§

The other tradition is the parser generator. Stephen C. Johnson’s yacc (1975) reads a grammar file and generates an LALR(1) parser; Terence Parr’s ANTLR (described by Parr and Russell Quong in 1995) generates LL(k) parsers, with its later versions using more powerful strategies. A generator analyses the whole grammar before any input is read, so it can report ambiguities and conflicts at build time, it accepts left-recursive rules, and the parsers it generates are fast.

Combinators trade that analysis for being ordinary code. The grammar is written in the host language, can be tested piece by piece with expect, uses the host’s types, and needs no build step. You can add a combinator of your own whenever the grammar needs one, such as a parser whose next step depends on a value already read. Choosing how to read your input covers when that trade is worth making.

6.1.10. Testing against the specification§

The newest layer in this lineage is testing. Koen Claessen and John Hughes’s QuickCheck (2000) introduced property-based testing: state a property that should hold for every input, and let the tool generate inputs to try to break it. roc-parser’s quality process follows that approach. Fuzz targets check properties such as "parsing never crashes", and review scripts compare every format with a mature independent implementation, its oracle, on generated and conformance-suite inputs. Property tests and conformance reviews describes the process.

6.1.11. How roc-parser compares§

IdeaSourceroc-parser
Parser as a function returning the rest of the inputBurge 1975, Wadler 1985, Hutton 1992Yes, with one result instead of a list of successes
Applicative sequencingSwierstra and Duponcheel 1996, McBride and Paterson 2008, elm/parserconst, keep and skip
Monadic sequencingHutton and Meijer 1996 and 1998and_then, and custom for anything else
ChoiceParsec 2001 (committed, with try); Ford 2004 (ordered)Ordered choice that always backtracks; no try
Repetition of empty-matching parsersParsec raises an errorStops at the first element that makes no progress
Error reportingParsec, megaparsec (longest match), elm/parser (context)Furthest failure with a byte offset; line and column in the formats
Input typeattoparsec, nom (bytes)UTF-8 bytes, as the Roc language reference recommends
Memoization, left recursionFord 2002; Frost, Hafiz and Callaghan 2008; chumskyNot supported; write repetition explicitly
QualityClaessen and Hughes 2000Fuzzing, property tests and oracle comparisons

6.1.12. Further reading§

Burge, W. H. (1975). Recursive Programming Techniques. Addison-Wesley. Open Library record

The first published set of parsing combinators, in a book about programming with recursion and higher-order functions. Out of print.

Wadler, P. (1985). How to replace failure by a list of successes. Functional Programming Languages and Computer Architecture, LNCS 201, 113–128. doi:10.1007/3-540-15975-4_33

Represents failure and backtracking with lazy lists of results; the technique behind the first functional parser libraries.

Hutton, G. (1992). Higher-order functions for parsing. Journal of Functional Programming, 2(3), 323–343. doi:10.1017/S0956796800000411

The classic introduction to combinator parsing, building sequencing, choice and repetition from a handful of primitives.

Hutton, G., and Meijer, E. (1996). Monadic parser combinators. Technical report NOTTCS-TR-96-4, University of Nottingham. PDF on Graham Hutton’s site

A long tutorial that recasts combinator parsing in monadic style; its introduction surveys the earlier work.

Hutton, G., and Meijer, E. (1998). Monadic parsing in Haskell. Journal of Functional Programming, 8(4), 437–444. doi:10.1017/S0956796898003050

The short, widely taught version of the same ideas.

Swierstra, S. D., and Duponcheel, L. (1996). Deterministic, error-correcting combinator parsers. Advanced Functional Programming, LNCS 1129, 184–207. doi:10.1007/3-540-61628-4_7

Non-monadic combinators whose fixed structure allows analysis and error correction; the early example of applicative parsing.

Leijen, D., and Meijer, E. (2001). Parsec: Direct style monadic parser combinators for the real world. Technical report UU-CS-2001-27, Utrecht University. Microsoft Research publication page

Committed choice, try, and error messages that name the position and the expected input. The library is on Hackage as parsec.

McBride, C., and Paterson, R. (2008). Applicative programming with effects. Journal of Functional Programming, 18(1), 1–13. doi:10.1017/S0956796807006326

Defines applicative functors and names parsing as a case that needs less than a monad; the theory behind keep and skip.

Ford, B. (2002). Packrat parsing: simple, powerful, lazy, linear time. Proceedings of the Seventh ACM SIGPLAN International Conference on Functional Programming (ICFP), 36–47. doi:10.1145/581478.581483

Memoizes a backtracking parser to guarantee linear time.

Ford, B. (2004). Parsing expression grammars: a recognition-based syntactic foundation. Proceedings of the 31st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL), 111–122. doi:10.1145/964001.964011

Prioritized choice and greedy repetition as a grammar formalism; the closest formal model of roc-parser’s one_of and many.

Frost, R. A., Hafiz, R., and Callaghan, P. (2008). Parser combinators for ambiguous left-recursive grammars. Practical Aspects of Declarative Languages (PADL), LNCS 4902, 167–181. doi:10.1007/978-3-540-77442-6_12

One way to support left recursion in combinators, the feature roc-parser leaves out.

Claessen, K., and Hughes, J. (2000). QuickCheck: a lightweight tool for random testing of Haskell programs. Proceedings of the Fifth ACM SIGPLAN International Conference on Functional Programming (ICFP), 268–279. doi:10.1145/351240.351266

Property-based testing, the basis of this package’s fuzz and property tests.

Johnson, S. C. (1975). Yacc: Yet Another Compiler-Compiler. Bell Laboratories. Online copy

The parser generator that set the pattern for grammar files compiled to LALR(1) tables.

Parr, T. J., and Quong, R. W. (1995). ANTLR: A predicated-LL(k) parser generator. Software: Practice and Experience, 25(7), 789–810. doi:10.1002/spe.4380250705

The first journal description of ANTLR, a widely used LL parser generator.

Libraries

  • elm/parser by Evan Czaplicki: parser pipelines (|= and |.), no backtracking by default, and context in error messages. Roc is, in the words of its FAQ, "a direct descendant of Elm".

  • megaparsec: Parsec’s successor, with typed errors and longest-match error merging.

  • attoparsec: fast parsing of bytes and text with arbitrary backtracking and incremental input.

  • nom, winnow (a fork of nom) and chumsky: Rust combinator libraries. nom is byte-oriented and zero-copy, winnow is a fork of nom that keeps the fundamentals in one crate, and chumsky focuses on error recovery.

7. Release notes§

7.1. Published releases§

Use the published release records for package URLs, compiler requirements and upgrade instructions. Choose a final release for normal use; preview releases may change their APIs.

Read the 6.0.0 upgrade notes for unified streaming, structured errors and parser migration, financial formats, HTML/SVG/MathML parsing and templates, XML readers and namespaces, AsciiDoc fixes, and current compiler requirements.

Read the 5.0.0 upgrade notes for its Roc compiler and application-platform requirements.

Read the 4.0.0 upgrade notes for JSON byte limits, writer error handling and Roc syntax span changes.

The working-tree changes point to the release being prepared.

7.1.1. Release candidates§

Read the 6.0.0-rc.5 preview notes for November 18 compiler compatibility and native contributor tooling.

A release marked prerelease is a preview. Read that release’s notes and keep its package and examples together when testing it.

Read the 6.0.0-rc.4 preview notes for the uniform API, structured diagnostics and shared streaming methods.