1 | # yaml-ast-parser
|
2 |
|
3 | [![Build Status](https://travis-ci.org/mulesoft-labs/yaml-ast-parser.svg?branch=master)](https://travis-ci.org/mulesoft-labs/yaml-ast-parser)
|
4 |
|
5 | This is a fork of JS-YAML which supports parsing of YAML into AST.
|
6 |
|
7 | In additional to parsing YAML to AST, it has following features:
|
8 |
|
9 | * restoration after the errors and reporting errors as a part of AST nodes.
|
10 | * built-in support for `!include` tag used in RAML
|
11 |
|
12 | ## Usage
|
13 | The type information below is relevant when using TypeScript, if using from JavaScript only the field/method information is relevant.
|
14 |
|
15 | `load` method can be used to load the tree and returns a `YAMLNode`.
|
16 |
|
17 | ### YAMLNode
|
18 | `YAMLNode` class is an ancestor for all node kinds.
|
19 | It's `kind` field determine node kind, one of `Kind` enum:
|
20 | * `SCALAR`, `MAPPING`, `MAP`, `SEQ`, `ANCHOR_REF` or `INCLUDE_REF`.
|
21 |
|
22 | After node kind is determined, it can be cast to one of the `YAMLNode` descendants types:
|
23 | * `YAMLScalar`, `YAMLMapping`, `YamlMap`, `YAMLSequence` or `YAMLAnchorReference`.
|
24 |
|
25 | | class | important members |
|
26 | |-------|-------------------|
|
27 | | `YAMLNode` | `startPosition` and `endPosition` provide node range.|
|
28 | | `YAMLScalar` | `string` `value` field |
|
29 | | `YAMLMapping` |`YAMLScalar` `key` and `YAMLNode` `value` fields |
|
30 | | `YAMLSequence` | `YAMLNode[]` `items` field|
|
31 | | `YamlMap` | `YAMLMapping[]` `mappings` field|
|
32 | | `YAMLAnchorReference` | `string` `referencesAnchor` and `YAMLNode` `value`|
|
33 |
|
34 | ### YAMLScalar
|
35 |
|
36 | Scalars are [one of the three main node types defined by YAML](http://www.yaml.org/spec/1.2/spec.html#scalar//) and are effectively leaf nodes.
|
37 |
|
38 | There are many factors that can influence the type of datum represent in scalar node (context, schema, tag, etc.).
|
39 |
|
40 | To help inspection of a `YAMLScalar` to determine its datatype when a document uses the [Core Schema](http://www.yaml.org/spec/1.2/spec.html#id2804923), you can pass the `YAMLScalar` to the `determineScalarType` function. It will return an enum value indicating `null`, `bool`, `int`, `float`, or `string`.
|
41 |
|
42 | Once you know the type, there are also some helper functions to help read the value by passing them the string, `value`: `parseYamlBoolean`, `parseYamlFloat`, and `parseYamlInteger`. |
\ | No newline at end of file |