source: node_modules/@swagger-api/apidom-ast/README.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.4 KB
Line 
1# @swagger-api/apidom-ast
2
3`@swagger-api/apidom-ast` contains tools necessary for parsing stage of ApiDOM, specifically for syntactic analysis.
4Syntactic analysis will take a stream of tokens and turn it into an AST representation.
5Using the information in the tokens, this phase will reformat them as an AST which represents
6the structure of input string in a way that makes it easier to work with.
7
8`@swagger-api/apidom-ast` currently contains AST nodes for [JSON](https://www.json.org/json-en.html) and [YAML 1.2](https://yaml.org/spec/1.2/spec.html) formats.
9
10## Installation
11
12You can install this package via [npm CLI](https://docs.npmjs.com/cli) by running the following command:
13
14```sh
15 $ npm install @swagger-api/apidom-ast
16```
17
18## Base AST Nodes
19
20Base AST nodes are nodes that are supplementary to any specific AST nodes.
21Having standardized AST of various formats (JSON/YAML) allows us to have common
22syntactic analysis or transformation algorithms.
23These nodes includes [Error](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/src/Error.ts), [Literal](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/src/Literal.ts), [Position](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/src/Position.ts), etc...
24
25Along with base nodes there are [predicates](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/src/predicates.ts) that can assert on these nodes.
26
27## JSON AST Nodes
28
29Convenient for low lever CST parsers that don't come with it's onw AST nodes.
30You can find list of JSON AST nodes in this [directory](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ast/src/json/nodes).
31
32## YAML AST Nodes
33
34Convenient for low lever CST parsers that don't come with it's onw AST nodes.
35You can find list of YAML AST nodes in this [directory](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ast/src/yaml/nodes).
36As YAML is very complex format, along with nodes we also expose implementation of YAML [Failsafe](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ast/src/yaml/schemas/failsafe) and [JSON](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ast/src/yaml/schemas/json) schemas
37along with [formatters for canonical block scalars](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/src/yaml/schemas/canonical-format.ts).
38
39## Traversal
40
41`@swagger-api/apidom-ast` comes with its own traversal algorithm convenient for traversing [CST](https://en.wikipedia.org/wiki/Parse_tree) or [AST](https://en.wikipedia.org/wiki/AST).
42
43### visit
44
45[visit](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/src/visitor.ts#L214) will walk through an CST/AST using a depth first traversal, calling
46the visitor's enter function at each node in the traversal, and calling the
47leave function after visiting that node and all of its child nodes.
48
49By returning different values from the enter and leave functions, the
50behavior of the visitor can be altered, including skipping over a sub-tree of
51the Node (by returning false), editing the Node Tree by returning a value or null
52to remove the value, or to stop the whole traversal by returning [BREAK](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/src/visitor.ts#L64).
53
54When using `visit` to edit an Node Tree, the original Node Tree will not be modified, and
55a new version of the Node Tree with the changes applied will be returned from the
56visit function.
57
58```js
59import { visit } from '@swagger-api/apidom-ast';
60
61const tree = {
62 type: 'root',
63 children: [
64 {
65 type: 'child',
66 value: 'this is child node',
67 children: [],
68 },
69 ],
70};
71
72const keyMap = {
73 root: ['children'],
74};
75
76const visitor = {
77 child(node) {
78 console.dir(node.value); // => 'this is child node'
79 },
80};
81
82const newTree = visit(tree, visitor, { keyMap }); // => tree{...}
83```
84
85Notice how we used 3rd parameter to `visit` function. Actually it can consume number of configuration
86options which can change its behavior.
87
88Configuration option | Type | Default | Description
89--- | --- | --- | ---
90<a name="keyMap"></a>`keyMap` | `Object` | `null` | Defines how nodes map to it's children.
91<a name="state"></a>`state` | `Object` | `{}` | Additional state that is provided to the visitor. State is merged inti visitor object in following manner: `Object.assign(visitor, state)`
92<a name="breakSymbol"></a>`breakSymbol` | `Object` | `{}` | Defines a symbol that can break the traversal. Symbol is compared by strict equality (`===`).
93<a name="visitFnGetter"></a>`visitFnGetter` | `Function` | [getVisitFn](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/src/visitor.ts#L33) | Function that extract appropriate method from the visitor given specific Node type.
94<a name="nodeTypeGetter"></a>`nodeTypeGetter` | `Function` | [getNodeType](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/src/visitor.ts#L67) | Node type extractor function.
95<a name="nodePredicate"><a/>`nodePredicate` | `Function` | [isNode](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/src/visitor.ts#L70) | Predicate that checks if particular Node can be really considered a Node.
96<a name="detectCycles"><a/>`detectCycles` | `Boolean` | `true` | If the structure that needs to be traversed is represented as directed cyclic graph, `visit` will skip Nodes that have already been traversed to avoid infinite recursion.
97
98
Note: See TracBrowser for help on using the repository browser.