语法

2022.06.17

本文按照YAML官方文档1.2.2版本翻译总结而成

https://yaml.org/spec/1.2.2/#chapter-4-syntax-conventions

The following chapters formally define the syntax of YAML character streams, using parameterized BNF productions. Each BNF production is both named and numbered for easy reference. Whenever possible, basic structures are specified before the more complex structures using them in a “bottom up” fashion.

The productions are accompanied by examples which are presented in a two-pane side-by-side format. The left-hand side is the YAML example and the right-hand side is an alternate YAML view of the example. The right-hand view uses JSON when possible. Otherwise it uses a YAML form that is as close to JSON as possible.

4.1. Production Syntax

Productions are defined using the syntax production-name ::= term, where a term is either:

Note: Quantified terms are always greedy.

The order of precedence is parenthesization, then quantification, then concatenation, then alternation.

Some lines in a production definition might have a comment like:

These comments are meant to be informative only. For instance a comment that says # not followed by non-ws char just means that you should be aware that actual production rules will behave as described even though it might not be obvious from the content of that particular production alone.

4.2. Production Parameters

Some productions have parameters in parentheses after the name, such as s-line-prefix(n,c). A parameterized production is shorthand for a (infinite) series of productions, each with a fixed value for each parameter.

For instance, this production:

Is shorthand for:

And this production:

Is shorthand for:

The parameters are as follows:

4.3. Production Naming Conventions

To make it easier to follow production combinations, production names use a prefix-style naming convention. Each production is given a prefix based on the type of characters it begins and ends with.