# AST for YAML See details: [../src/ast.ts](../src/ast.ts) ## Node ```ts interface Node { type: string; loc: SourceLocation; range: [number, number]; } ``` All nodes have `type`, `range`, `loc` and `parent` properties according to [ESLint - The AST specification]. ## Content Nodes ### YAMLMapping ```ts interface YAMLMapping extends Node { type: "YAMLMapping" style: "block" | "flow" pairs: YAMLPair[] parent: YAMLDocument | YAMLPair | YAMLSequence | YAMLWithMeta } ``` This is mappings. - `style` ... If `"block"`, it is [block style mapping]. If `"flow"`, it is [flow style mapping]. e.g. `{ foo: bar }` - `pairs` ... An array of `key: value` pairs. ### YAMLPair ```ts interface YAMLPair extends Node { type: "YAMLPair" key: YAMLMapping | YAMLSequence | YAMLScalar | YAMLAlias | YAMLWithMeta | null value: YAMLMapping | YAMLSequence | YAMLScalar | YAMLAlias | YAMLWithMeta | null parent: YAMLMapping } ``` This is `key: value` pairs. ### YAMLSequence ```ts interface YAMLSequence extends Node { type: "YAMLSequence" style: "block" | "flow" entries: (YAMLMapping | YAMLSequence | YAMLScalar | YAMLAlias | YAMLWithMeta)[] parent: YAMLDocument | YAMLPair | YAMLSequence | YAMLWithMeta } ``` This is sequences. - `style` ... If `"block"`, it is [block style sequence]. If `"flow"`, it is [flow style sequence]. e.g. `{ foo: bar }` - `entries` ... An array of sequence entries. ### YAMLScalar ```ts interface YAMLScalar extends Node { type: "YAMLScalar" style: "plain" | "double-quoted" | "single-quoted" | "literal" | "folded" value: string | number | boolean | null parent: YAMLDocument | YAMLPair | YAMLSequence | YAMLWithMeta } ``` This is scalars. #### YAMLPlainScalar ```ts interface YAMLPlainScalar extends YAMLScalar { style: "plain" strValue: string raw: string } ``` This is [plain style scalars] (unquoted scalars). #### YAMLDoubleQuotedScalar ```ts interface YAMLDoubleQuotedScalar extends YAMLScalar { style: "double-quoted" strValue: string value: string raw: string } ``` This is [double-quoted style scalars]. #### YAMLSingleQuotedScalar ```ts interface YAMLSingleQuotedScalar extends YAMLScalar { style: "single-quoted" strValue: string value: string raw: string } ``` This is [single-quoted style scalars]. #### YAMLBlockLiteralScalar ```ts interface YAMLBlockLiteralScalar extends YAMLScalar { style: "literal" chomping: "clip" | "keep" | "strip" indent: null | number value: string } ``` This is [literal style scalars]. e.g. ```yaml | foo bar ``` - `chomping` - `"clip"` ... The chomping indicator is not specified. - `"strip"` ... The `-` chomping indicator is specified. - `"keep"` ... The `+` chomping indicator is specified. - `indent` ... The specified indentation indicator. It is null if not specified. #### YAMLBlockFoldedScalar ```ts interface YAMLBlockFoldedScalar extends YAMLScalar { style: "folded" chomping: "clip" | "keep" | "strip" indent: null | number value: string } ``` This is [folded style scalars]. e.g. ```yaml > foo bar ``` ### YAMLAlias ```ts interface YAMLAlias extends Node { type: "YAMLAlias" name: string parent: YAMLDocument | YAMLPair | YAMLSequence | YAMLWithMeta } ``` This is [aliases]. ## Tag and Anchor ### YAMLWithMeta ```ts interface YAMLWithMeta extends Node { type: "YAMLWithMeta" anchor: YAMLAnchor | null tag: YAMLTag | null value: YAMLMapping | YAMLSequence | YAMLScalar | YAMLAlias | null parent: YAMLDocument | YAMLPair | YAMLSequence } ``` If it has a tag or anchor, the content node will be wrapped in this node. ### YAMLAnchor ```ts interface YAMLAnchor extends Node { type: "YAMLAnchor" name: string parent: YAMLWithMeta } ``` ### YAMLTag ```ts interface YAMLTag extends Node { type: "YAMLTag" tag: string parent: YAMLWithMeta } ``` ## Document ### YAMLDocument ```ts interface YAMLDocument extends Node { type: "YAMLDocument" directives: YAMLDirective[] content: YAMLMapping | YAMLSequence | YAMLScalar | YAMLAlias | YAMLWithMeta | null parent: YAMLProgram anchors: { [key: string]: YAMLAnchor[] } // YAML version version: string } ``` ### YAMLDirective ```ts interface YAMLDirective extends Node { type: "YAMLDirective" value: string parent: YAMLDocument } ``` This is directives. In the following example, `%YAML 1.2` is represented by this node. ```yaml %YAML 1.2 --- "foo" ``` ### Program ```js extend interface Program { body: YAMLDocument[] } ``` The `body` of the `Program` node generated by this parser is an array of `YAMLDocument`. [ESLint - The AST specification]: https://eslint.org/docs/developer-guide/working-with-custom-parsers#the-ast-specification [block style mapping]: https://yaml.org/spec/1.2/spec.html#id2798057 [flow style mapping]: https://yaml.org/spec/1.2/spec.html#id2790832 [block style sequence]: https://yaml.org/spec/1.2/spec.html#id2797382 [flow style sequence]: https://yaml.org/spec/1.2/spec.html#id2790320 [plain style scalars]: https://yaml.org/spec/1.2/spec.html#id2788859 [double-quoted style scalars]: https://yaml.org/spec/1.2/spec.html#id2787109 [single-quoted style scalars]: https://yaml.org/spec/1.2/spec.html#id2788097 [literal style scalars]: https://yaml.org/spec/1.2/spec.html#id2795688 [folded style scalars]: https://yaml.org/spec/1.2/spec.html#id2796251 [aliases]: https://yaml.org/spec/1.2/spec.html#id2786196