@speclynx/apidom-parser-adapter-json 1.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +91 -0
- package/LICENSE +202 -0
- package/LICENSES/AFL-3.0.txt +182 -0
- package/LICENSES/Apache-2.0.txt +202 -0
- package/LICENSES/BSD-3-Clause.txt +26 -0
- package/LICENSES/MIT.txt +9 -0
- package/NOTICE +65 -0
- package/README.md +124 -0
- package/dist/167.apidom-parser-adapter-json.browser.js +10 -0
- package/dist/167.apidom-parser-adapter-json.browser.min.js +1 -0
- package/dist/451.apidom-parser-adapter-json.browser.js +10 -0
- package/dist/451.apidom-parser-adapter-json.browser.min.js +1 -0
- package/dist/9786785aaddf11f37840.wasm +0 -0
- package/dist/apidom-parser-adapter-json.browser.js +17850 -0
- package/dist/apidom-parser-adapter-json.browser.min.js +1 -0
- package/package.json +69 -0
- package/src/adapter.cjs +75 -0
- package/src/adapter.mjs +65 -0
- package/src/lexical-analysis/index.cjs +42 -0
- package/src/lexical-analysis/index.mjs +38 -0
- package/src/media-types.cjs +20 -0
- package/src/media-types.mjs +16 -0
- package/src/syntactic-analysis/TreeCursorIterator.cjs +62 -0
- package/src/syntactic-analysis/TreeCursorIterator.mjs +57 -0
- package/src/syntactic-analysis/TreeCursorSyntaxNode.cjs +51 -0
- package/src/syntactic-analysis/TreeCursorSyntaxNode.mjs +47 -0
- package/src/syntactic-analysis/direct/index.cjs +64 -0
- package/src/syntactic-analysis/direct/index.mjs +59 -0
- package/src/syntactic-analysis/direct/visitors/CstVisitor.cjs +163 -0
- package/src/syntactic-analysis/direct/visitors/CstVisitor.mjs +158 -0
- package/src/syntactic-analysis/indirect/index.cjs +56 -0
- package/src/syntactic-analysis/indirect/index.mjs +49 -0
- package/src/syntactic-analysis/indirect/visitors/CstVisitor.cjs +177 -0
- package/src/syntactic-analysis/indirect/visitors/CstVisitor.mjs +172 -0
- package/src/syntactic-analysis/indirect/visitors/JsonAstVisitor.cjs +189 -0
- package/src/syntactic-analysis/indirect/visitors/JsonAstVisitor.mjs +183 -0
- package/types/apidom-parser-adapter-json.d.ts +103 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard").default;
|
|
4
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _apidomAst = require("@speclynx/apidom-ast");
|
|
8
|
+
var _TreeCursorIterator = _interopRequireDefault(require("../TreeCursorIterator.cjs"));
|
|
9
|
+
var _CstVisitor = _interopRequireWildcard(require("./visitors/CstVisitor.cjs"));
|
|
10
|
+
var _JsonAstVisitor = _interopRequireWildcard(require("./visitors/JsonAstVisitor.cjs"));
|
|
11
|
+
/**
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* This version of syntactic analysis does following transformations:
|
|
17
|
+
* `TreeSitter CST -> JSON AST -> ApiDOM`
|
|
18
|
+
*
|
|
19
|
+
* Transient transformation of TreeSitter CST is performed
|
|
20
|
+
* using TreeSitter cursor. TreeSitter cursor is a stateful object
|
|
21
|
+
* that allows us to walk syntax tree containing large number of nodes
|
|
22
|
+
* with maximum efficiency. Using this transient CST transformation
|
|
23
|
+
* gives us double the performance when syntactically analyzing
|
|
24
|
+
* CST into JSON AST.
|
|
25
|
+
*
|
|
26
|
+
* Two traversals passes are needed to get from CST to ApiDOM.
|
|
27
|
+
* This analysis is much slower than the direct one, but allows
|
|
28
|
+
* to do additional analysis magic on JSON AST.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
const analyze = (cst, {
|
|
32
|
+
sourceMap = false
|
|
33
|
+
} = {}) => {
|
|
34
|
+
const cursor = cst.walk();
|
|
35
|
+
const iterator = new _TreeCursorIterator.default(cursor);
|
|
36
|
+
const [rootNode] = Array.from(iterator);
|
|
37
|
+
const cstVisitor = new _CstVisitor.default();
|
|
38
|
+
const astVisitor = new _JsonAstVisitor.default();
|
|
39
|
+
const jsonAst = (0, _apidomAst.visit)(rootNode, cstVisitor, {
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
keyMap: _CstVisitor.keyMap,
|
|
42
|
+
state: {
|
|
43
|
+
sourceMap
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return (0, _apidomAst.visit)(jsonAst.rootNode, astVisitor, {
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
keyMap: _JsonAstVisitor.keyMap,
|
|
49
|
+
nodeTypeGetter: _JsonAstVisitor.getNodeType,
|
|
50
|
+
nodePredicate: _JsonAstVisitor.isNode,
|
|
51
|
+
state: {
|
|
52
|
+
sourceMap
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
var _default = exports.default = analyze;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { visit } from '@speclynx/apidom-ast';
|
|
2
|
+
import TreeCursorIterator from "../TreeCursorIterator.mjs";
|
|
3
|
+
import CstVisitor, { keyMap as cstKeyMap } from "./visitors/CstVisitor.mjs";
|
|
4
|
+
import JsonAstVisitor, { keyMap as astKeyMap, isNode, getNodeType } from "./visitors/JsonAstVisitor.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* This version of syntactic analysis does following transformations:
|
|
10
|
+
* `TreeSitter CST -> JSON AST -> ApiDOM`
|
|
11
|
+
*
|
|
12
|
+
* Transient transformation of TreeSitter CST is performed
|
|
13
|
+
* using TreeSitter cursor. TreeSitter cursor is a stateful object
|
|
14
|
+
* that allows us to walk syntax tree containing large number of nodes
|
|
15
|
+
* with maximum efficiency. Using this transient CST transformation
|
|
16
|
+
* gives us double the performance when syntactically analyzing
|
|
17
|
+
* CST into JSON AST.
|
|
18
|
+
*
|
|
19
|
+
* Two traversals passes are needed to get from CST to ApiDOM.
|
|
20
|
+
* This analysis is much slower than the direct one, but allows
|
|
21
|
+
* to do additional analysis magic on JSON AST.
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
const analyze = (cst, {
|
|
25
|
+
sourceMap = false
|
|
26
|
+
} = {}) => {
|
|
27
|
+
const cursor = cst.walk();
|
|
28
|
+
const iterator = new TreeCursorIterator(cursor);
|
|
29
|
+
const [rootNode] = Array.from(iterator);
|
|
30
|
+
const cstVisitor = new CstVisitor();
|
|
31
|
+
const astVisitor = new JsonAstVisitor();
|
|
32
|
+
const jsonAst = visit(rootNode, cstVisitor, {
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
keyMap: cstKeyMap,
|
|
35
|
+
state: {
|
|
36
|
+
sourceMap
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return visit(jsonAst.rootNode, astVisitor, {
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
keyMap: astKeyMap,
|
|
42
|
+
nodeTypeGetter: getNodeType,
|
|
43
|
+
nodePredicate: isNode,
|
|
44
|
+
state: {
|
|
45
|
+
sourceMap
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
export default analyze;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.keyMap = exports.default = void 0;
|
|
6
|
+
var _apidomAst = require("@speclynx/apidom-ast");
|
|
7
|
+
var _TreeCursorSyntaxNode = _interopRequireDefault(require("../../TreeCursorSyntaxNode.cjs"));
|
|
8
|
+
const keyMap = exports.keyMap = {
|
|
9
|
+
document: ['children'],
|
|
10
|
+
object: ['children'],
|
|
11
|
+
array: ['children'],
|
|
12
|
+
string: ['children'],
|
|
13
|
+
property: ['children'],
|
|
14
|
+
key: ['children'],
|
|
15
|
+
error: ['children']
|
|
16
|
+
};
|
|
17
|
+
class CstVisitor {
|
|
18
|
+
static toPosition(node) {
|
|
19
|
+
const start = new _apidomAst.Point({
|
|
20
|
+
row: node.startPosition.row,
|
|
21
|
+
column: node.startPosition.column,
|
|
22
|
+
char: node.startIndex
|
|
23
|
+
});
|
|
24
|
+
const end = new _apidomAst.Point({
|
|
25
|
+
row: node.endPosition.row,
|
|
26
|
+
column: node.endPosition.column,
|
|
27
|
+
char: node.endIndex
|
|
28
|
+
});
|
|
29
|
+
return new _apidomAst.Position({
|
|
30
|
+
start,
|
|
31
|
+
end
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
document = {
|
|
35
|
+
enter: node => {
|
|
36
|
+
const position = CstVisitor.toPosition(node);
|
|
37
|
+
return new _apidomAst.JsonDocument({
|
|
38
|
+
children: node.children,
|
|
39
|
+
position,
|
|
40
|
+
isMissing: node.isMissing
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
leave: document => {
|
|
44
|
+
return new _apidomAst.ParseResult({
|
|
45
|
+
children: [document]
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
enter(node) {
|
|
50
|
+
// anonymous literals from CST transformed into AST literal nodes
|
|
51
|
+
if (node instanceof _TreeCursorSyntaxNode.default && !node.isNamed) {
|
|
52
|
+
const position = CstVisitor.toPosition(node);
|
|
53
|
+
const value = node.type || node.text;
|
|
54
|
+
const {
|
|
55
|
+
isMissing
|
|
56
|
+
} = node;
|
|
57
|
+
return new _apidomAst.Literal({
|
|
58
|
+
value,
|
|
59
|
+
position,
|
|
60
|
+
isMissing
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
object(node) {
|
|
66
|
+
const position = CstVisitor.toPosition(node);
|
|
67
|
+
return new _apidomAst.JsonObject({
|
|
68
|
+
children: node.children,
|
|
69
|
+
position,
|
|
70
|
+
isMissing: node.isMissing
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
pair(node) {
|
|
74
|
+
const position = CstVisitor.toPosition(node);
|
|
75
|
+
const children = node.children.slice(1);
|
|
76
|
+
const {
|
|
77
|
+
keyNode
|
|
78
|
+
} = node;
|
|
79
|
+
let jsonKeyValue;
|
|
80
|
+
let parseError;
|
|
81
|
+
try {
|
|
82
|
+
jsonKeyValue = JSON.parse(keyNode?.text ?? '""');
|
|
83
|
+
} catch (error) {
|
|
84
|
+
jsonKeyValue = keyNode?.text ?? '';
|
|
85
|
+
parseError = error instanceof Error ? error : undefined;
|
|
86
|
+
}
|
|
87
|
+
const key = new _apidomAst.JsonKey({
|
|
88
|
+
value: jsonKeyValue,
|
|
89
|
+
parseError,
|
|
90
|
+
position: keyNode != null ? CstVisitor.toPosition(keyNode) : undefined,
|
|
91
|
+
isMissing: keyNode != null ? keyNode.isMissing : false
|
|
92
|
+
});
|
|
93
|
+
return new _apidomAst.JsonProperty({
|
|
94
|
+
children: [key, ...children],
|
|
95
|
+
position,
|
|
96
|
+
isMissing: node.isMissing
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
array(node) {
|
|
100
|
+
const position = CstVisitor.toPosition(node);
|
|
101
|
+
return new _apidomAst.JsonArray({
|
|
102
|
+
children: node.children,
|
|
103
|
+
position,
|
|
104
|
+
isMissing: node.isMissing
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
string(node) {
|
|
108
|
+
const position = CstVisitor.toPosition(node);
|
|
109
|
+
let value;
|
|
110
|
+
let parseError;
|
|
111
|
+
try {
|
|
112
|
+
value = JSON.parse(node.text);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
parseError = error instanceof Error ? error : undefined;
|
|
115
|
+
value = node.text;
|
|
116
|
+
}
|
|
117
|
+
return new _apidomAst.JsonString({
|
|
118
|
+
value,
|
|
119
|
+
parseError,
|
|
120
|
+
position,
|
|
121
|
+
isMissing: node.isMissing
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
number(node) {
|
|
125
|
+
const position = CstVisitor.toPosition(node);
|
|
126
|
+
const value = node.text;
|
|
127
|
+
return new _apidomAst.JsonNumber({
|
|
128
|
+
value,
|
|
129
|
+
position,
|
|
130
|
+
isMissing: node.isMissing
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
null(node) {
|
|
134
|
+
const position = CstVisitor.toPosition(node);
|
|
135
|
+
const value = node.text;
|
|
136
|
+
return new _apidomAst.JsonNull({
|
|
137
|
+
value,
|
|
138
|
+
position,
|
|
139
|
+
isMissing: node.isMissing
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
true(node) {
|
|
143
|
+
const position = CstVisitor.toPosition(node);
|
|
144
|
+
const value = node.text;
|
|
145
|
+
return new _apidomAst.JsonTrue({
|
|
146
|
+
value,
|
|
147
|
+
position,
|
|
148
|
+
isMissing: node.isMissing
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
false(node) {
|
|
152
|
+
const position = CstVisitor.toPosition(node);
|
|
153
|
+
const value = node.text;
|
|
154
|
+
return new _apidomAst.JsonFalse({
|
|
155
|
+
value,
|
|
156
|
+
position,
|
|
157
|
+
isMissing: node.isMissing
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
ERROR(node, key, parent, path) {
|
|
161
|
+
const position = CstVisitor.toPosition(node);
|
|
162
|
+
const errorNode = new _apidomAst.Error({
|
|
163
|
+
children: node.children,
|
|
164
|
+
position,
|
|
165
|
+
isUnexpected: !node.hasError,
|
|
166
|
+
isMissing: node.isMissing,
|
|
167
|
+
value: node.text
|
|
168
|
+
});
|
|
169
|
+
if (path.length === 0) {
|
|
170
|
+
return new _apidomAst.ParseResult({
|
|
171
|
+
children: [errorNode]
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
return errorNode;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
var _default = exports.default = CstVisitor;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { JsonArray, JsonDocument, JsonFalse, JsonNull, JsonNumber, JsonObject, JsonKey, JsonProperty, JsonString, JsonTrue, ParseResult, Position, Point, Literal, Error as ErrorNode } from '@speclynx/apidom-ast';
|
|
2
|
+
import TreeCursorSyntaxNode from "../../TreeCursorSyntaxNode.mjs";
|
|
3
|
+
export const keyMap = {
|
|
4
|
+
document: ['children'],
|
|
5
|
+
object: ['children'],
|
|
6
|
+
array: ['children'],
|
|
7
|
+
string: ['children'],
|
|
8
|
+
property: ['children'],
|
|
9
|
+
key: ['children'],
|
|
10
|
+
error: ['children']
|
|
11
|
+
};
|
|
12
|
+
class CstVisitor {
|
|
13
|
+
static toPosition(node) {
|
|
14
|
+
const start = new Point({
|
|
15
|
+
row: node.startPosition.row,
|
|
16
|
+
column: node.startPosition.column,
|
|
17
|
+
char: node.startIndex
|
|
18
|
+
});
|
|
19
|
+
const end = new Point({
|
|
20
|
+
row: node.endPosition.row,
|
|
21
|
+
column: node.endPosition.column,
|
|
22
|
+
char: node.endIndex
|
|
23
|
+
});
|
|
24
|
+
return new Position({
|
|
25
|
+
start,
|
|
26
|
+
end
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
document = {
|
|
30
|
+
enter: node => {
|
|
31
|
+
const position = CstVisitor.toPosition(node);
|
|
32
|
+
return new JsonDocument({
|
|
33
|
+
children: node.children,
|
|
34
|
+
position,
|
|
35
|
+
isMissing: node.isMissing
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
leave: document => {
|
|
39
|
+
return new ParseResult({
|
|
40
|
+
children: [document]
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
enter(node) {
|
|
45
|
+
// anonymous literals from CST transformed into AST literal nodes
|
|
46
|
+
if (node instanceof TreeCursorSyntaxNode && !node.isNamed) {
|
|
47
|
+
const position = CstVisitor.toPosition(node);
|
|
48
|
+
const value = node.type || node.text;
|
|
49
|
+
const {
|
|
50
|
+
isMissing
|
|
51
|
+
} = node;
|
|
52
|
+
return new Literal({
|
|
53
|
+
value,
|
|
54
|
+
position,
|
|
55
|
+
isMissing
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
object(node) {
|
|
61
|
+
const position = CstVisitor.toPosition(node);
|
|
62
|
+
return new JsonObject({
|
|
63
|
+
children: node.children,
|
|
64
|
+
position,
|
|
65
|
+
isMissing: node.isMissing
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
pair(node) {
|
|
69
|
+
const position = CstVisitor.toPosition(node);
|
|
70
|
+
const children = node.children.slice(1);
|
|
71
|
+
const {
|
|
72
|
+
keyNode
|
|
73
|
+
} = node;
|
|
74
|
+
let jsonKeyValue;
|
|
75
|
+
let parseError;
|
|
76
|
+
try {
|
|
77
|
+
jsonKeyValue = JSON.parse(keyNode?.text ?? '""');
|
|
78
|
+
} catch (error) {
|
|
79
|
+
jsonKeyValue = keyNode?.text ?? '';
|
|
80
|
+
parseError = error instanceof Error ? error : undefined;
|
|
81
|
+
}
|
|
82
|
+
const key = new JsonKey({
|
|
83
|
+
value: jsonKeyValue,
|
|
84
|
+
parseError,
|
|
85
|
+
position: keyNode != null ? CstVisitor.toPosition(keyNode) : undefined,
|
|
86
|
+
isMissing: keyNode != null ? keyNode.isMissing : false
|
|
87
|
+
});
|
|
88
|
+
return new JsonProperty({
|
|
89
|
+
children: [key, ...children],
|
|
90
|
+
position,
|
|
91
|
+
isMissing: node.isMissing
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
array(node) {
|
|
95
|
+
const position = CstVisitor.toPosition(node);
|
|
96
|
+
return new JsonArray({
|
|
97
|
+
children: node.children,
|
|
98
|
+
position,
|
|
99
|
+
isMissing: node.isMissing
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
string(node) {
|
|
103
|
+
const position = CstVisitor.toPosition(node);
|
|
104
|
+
let value;
|
|
105
|
+
let parseError;
|
|
106
|
+
try {
|
|
107
|
+
value = JSON.parse(node.text);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
parseError = error instanceof Error ? error : undefined;
|
|
110
|
+
value = node.text;
|
|
111
|
+
}
|
|
112
|
+
return new JsonString({
|
|
113
|
+
value,
|
|
114
|
+
parseError,
|
|
115
|
+
position,
|
|
116
|
+
isMissing: node.isMissing
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
number(node) {
|
|
120
|
+
const position = CstVisitor.toPosition(node);
|
|
121
|
+
const value = node.text;
|
|
122
|
+
return new JsonNumber({
|
|
123
|
+
value,
|
|
124
|
+
position,
|
|
125
|
+
isMissing: node.isMissing
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
null(node) {
|
|
129
|
+
const position = CstVisitor.toPosition(node);
|
|
130
|
+
const value = node.text;
|
|
131
|
+
return new JsonNull({
|
|
132
|
+
value,
|
|
133
|
+
position,
|
|
134
|
+
isMissing: node.isMissing
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
true(node) {
|
|
138
|
+
const position = CstVisitor.toPosition(node);
|
|
139
|
+
const value = node.text;
|
|
140
|
+
return new JsonTrue({
|
|
141
|
+
value,
|
|
142
|
+
position,
|
|
143
|
+
isMissing: node.isMissing
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
false(node) {
|
|
147
|
+
const position = CstVisitor.toPosition(node);
|
|
148
|
+
const value = node.text;
|
|
149
|
+
return new JsonFalse({
|
|
150
|
+
value,
|
|
151
|
+
position,
|
|
152
|
+
isMissing: node.isMissing
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
ERROR(node, key, parent, path) {
|
|
156
|
+
const position = CstVisitor.toPosition(node);
|
|
157
|
+
const errorNode = new ErrorNode({
|
|
158
|
+
children: node.children,
|
|
159
|
+
position,
|
|
160
|
+
isUnexpected: !node.hasError,
|
|
161
|
+
isMissing: node.isMissing,
|
|
162
|
+
value: node.text
|
|
163
|
+
});
|
|
164
|
+
if (path.length === 0) {
|
|
165
|
+
return new ParseResult({
|
|
166
|
+
children: [errorNode]
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return errorNode;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
export default CstVisitor;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.keyMap = exports.isNode = exports.getNodeType = exports.default = void 0;
|
|
5
|
+
var _apidomAst = require("@speclynx/apidom-ast");
|
|
6
|
+
var _apidomCore = require("@speclynx/apidom-core");
|
|
7
|
+
const keyMap = exports.keyMap = {
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
[_apidomAst.ParseResult.type]: ['children'],
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
[_apidomAst.JsonDocument.type]: ['children'],
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
[_apidomAst.JsonObject.type]: ['children'],
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
[_apidomAst.JsonProperty.type]: ['children'],
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
[_apidomAst.JsonArray.type]: ['children'],
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
[_apidomAst.Error.type]: ['children'],
|
|
20
|
+
..._apidomCore.keyMap
|
|
21
|
+
};
|
|
22
|
+
const getNodeType = node => {
|
|
23
|
+
if ((0, _apidomCore.isParseResultElement)(node)) {
|
|
24
|
+
return 'ParseResultElement';
|
|
25
|
+
}
|
|
26
|
+
if ((0, _apidomCore.isElement)(node)) {
|
|
27
|
+
return (0, _apidomCore.getNodeType)(node);
|
|
28
|
+
}
|
|
29
|
+
return (0, _apidomAst.getNodeType)(node);
|
|
30
|
+
};
|
|
31
|
+
exports.getNodeType = getNodeType;
|
|
32
|
+
const isNode = element => (0, _apidomCore.isElement)(element) || (0, _apidomAst.isNode)(element);
|
|
33
|
+
exports.isNode = isNode;
|
|
34
|
+
class JsonAstVisitor {
|
|
35
|
+
sourceMap = false;
|
|
36
|
+
annotations;
|
|
37
|
+
ParseResultElement = {
|
|
38
|
+
leave: element => {
|
|
39
|
+
// mark first-non Annotation element as result
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
const elements = element.findElements(_apidomCore.isPrimitiveElement);
|
|
42
|
+
if (elements.length > 0) {
|
|
43
|
+
const resultElement = elements[0];
|
|
44
|
+
resultElement.classes.push('result');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// provide annotations
|
|
48
|
+
this.annotations.forEach(annotationElement => {
|
|
49
|
+
element.push(annotationElement);
|
|
50
|
+
});
|
|
51
|
+
this.annotations = [];
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
constructor() {
|
|
55
|
+
this.annotations = [];
|
|
56
|
+
}
|
|
57
|
+
document(node) {
|
|
58
|
+
const element = new _apidomCore.ParseResultElement();
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
element._content = node.children;
|
|
61
|
+
return element;
|
|
62
|
+
}
|
|
63
|
+
object(node) {
|
|
64
|
+
const element = new _apidomCore.ObjectElement();
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
element._content = node.children;
|
|
67
|
+
this.maybeAddSourceMap(node, element);
|
|
68
|
+
return element;
|
|
69
|
+
}
|
|
70
|
+
property(node) {
|
|
71
|
+
const element = new _apidomCore.MemberElement();
|
|
72
|
+
|
|
73
|
+
// @ts-ignore
|
|
74
|
+
element.content.key = node.key;
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
element.content.value = node.value;
|
|
77
|
+
this.maybeAddSourceMap(node, element);
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Process possible errors here that may be present in pair node children as we're using direct field access.
|
|
81
|
+
* There are usually 3 children here found: "key", ":", "value".
|
|
82
|
+
*/
|
|
83
|
+
if (node.children.length > 3) {
|
|
84
|
+
node.children
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
86
|
+
.filter(child => child.type === 'error')
|
|
87
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
88
|
+
.forEach(errorNode => {
|
|
89
|
+
this.error(errorNode, node, [], [node]);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return element;
|
|
93
|
+
}
|
|
94
|
+
key(node) {
|
|
95
|
+
const {
|
|
96
|
+
value,
|
|
97
|
+
parseError
|
|
98
|
+
} = node;
|
|
99
|
+
const element = new _apidomCore.StringElement(value);
|
|
100
|
+
if (parseError instanceof _apidomAst.Error) {
|
|
101
|
+
element.setMetaProperty('jsonParse', {
|
|
102
|
+
isError: true,
|
|
103
|
+
errorType: parseError.name,
|
|
104
|
+
errorMessage: parseError.message
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
this.maybeAddSourceMap(node, element);
|
|
108
|
+
return element;
|
|
109
|
+
}
|
|
110
|
+
array(node) {
|
|
111
|
+
const element = new _apidomCore.ArrayElement();
|
|
112
|
+
// @ts-ignore
|
|
113
|
+
element._content = node.children;
|
|
114
|
+
this.maybeAddSourceMap(node, element);
|
|
115
|
+
return element;
|
|
116
|
+
}
|
|
117
|
+
string(node) {
|
|
118
|
+
const {
|
|
119
|
+
value,
|
|
120
|
+
parseError
|
|
121
|
+
} = node;
|
|
122
|
+
const element = new _apidomCore.StringElement(value);
|
|
123
|
+
if (parseError instanceof _apidomAst.Error) {
|
|
124
|
+
element.setMetaProperty('jsonParse', {
|
|
125
|
+
isError: true,
|
|
126
|
+
errorType: parseError.name,
|
|
127
|
+
errorMessage: parseError.message
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
this.maybeAddSourceMap(node, element);
|
|
131
|
+
return element;
|
|
132
|
+
}
|
|
133
|
+
number(node) {
|
|
134
|
+
const element = new _apidomCore.NumberElement(Number(node.value));
|
|
135
|
+
this.maybeAddSourceMap(node, element);
|
|
136
|
+
return element;
|
|
137
|
+
}
|
|
138
|
+
null(node) {
|
|
139
|
+
const element = new _apidomCore.NullElement();
|
|
140
|
+
this.maybeAddSourceMap(node, element);
|
|
141
|
+
return element;
|
|
142
|
+
}
|
|
143
|
+
true(node) {
|
|
144
|
+
const element = new _apidomCore.BooleanElement(true);
|
|
145
|
+
this.maybeAddSourceMap(node, element);
|
|
146
|
+
return element;
|
|
147
|
+
}
|
|
148
|
+
false(node) {
|
|
149
|
+
const element = new _apidomCore.BooleanElement(false);
|
|
150
|
+
this.maybeAddSourceMap(node, element);
|
|
151
|
+
return element;
|
|
152
|
+
}
|
|
153
|
+
literal(node) {
|
|
154
|
+
if (node.isMissing) {
|
|
155
|
+
const message = `(Missing ${node.value})`;
|
|
156
|
+
const element = new _apidomCore.AnnotationElement(message);
|
|
157
|
+
element.classes.push('warning');
|
|
158
|
+
this.maybeAddSourceMap(node, element);
|
|
159
|
+
this.annotations.push(element);
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
error(node, key, parent, path) {
|
|
164
|
+
const message = node.isUnexpected ? `(Unexpected ${node.value})` : `(Error ${node.value})`;
|
|
165
|
+
const element = new _apidomCore.AnnotationElement(message);
|
|
166
|
+
element.classes.push('error');
|
|
167
|
+
this.maybeAddSourceMap(node, element);
|
|
168
|
+
if (path.length === 0) {
|
|
169
|
+
// no document to visit, only error is present in CST
|
|
170
|
+
const parseResultElement = new _apidomCore.ParseResultElement();
|
|
171
|
+
parseResultElement.push(element);
|
|
172
|
+
return parseResultElement;
|
|
173
|
+
}
|
|
174
|
+
this.annotations.push(element);
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
maybeAddSourceMap(node, element) {
|
|
178
|
+
if (!this.sourceMap) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const sourceMap = new _apidomCore.SourceMapElement();
|
|
182
|
+
// @ts-ignore
|
|
183
|
+
sourceMap.position = node.position;
|
|
184
|
+
// @ts-ignore
|
|
185
|
+
sourceMap.astNode = node;
|
|
186
|
+
element.meta.set('sourceMap', sourceMap);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
var _default = exports.default = JsonAstVisitor;
|