forked from neuroo/json-ast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.js
208 lines (169 loc) · 4.98 KB
/
ast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
export const nodeTypes = {
DOCUMENT : 'document',
COMMENT : 'comment',
OBJECT : 'object',
PROPERTY : 'property',
KEY : 'key',
ARRAY : 'array',
VALUE : 'value',
STRING : 'string',
NUMBER : 'number',
TRUE : 'true',
FALSE : 'false',
NULL : 'null',
ERROR : 'error'
};
// All elements in the tree will extend the `JsonNode` base class
export class JsonNode {
constructor(_type = nodeTypes.ERROR) {
this._type = _type || nodeTypes.ERROR;
this._position = null;
}
get type() { return this._type; }
set position(_pos) { this._position = _pos || null; }
get position() { return this._position; }
accept(visitor) { visitor.visit(this); }
static toObject(jsonNode) { return JSON.parse(JSON.stringify(jsonNode)); }
static toString(jsonNode) { return JSON.stringify(jsonNode); }
static toJSON(jsonNode) { return toJSON(jsonNode); }
};
export class JsonDocument extends JsonNode {
constructor() {
super(nodeTypes.DOCUMENT);
this._child = null;
this._comments = [];
}
set child(_child) { this._child = _child; }
get child() { return this._child; }
get comments() { return this._comments; }
};
export class JsonObject extends JsonNode {
constructor() {
super(nodeTypes.OBJECT);
this._properties = [];
this._comments = [];
}
get properties() { return this._properties; }
get comments() { return this._comments; }
};
export class JsonProperty extends JsonNode {
constructor() {
super(nodeTypes.PROPERTY);
this._key = null;
this._value = null;
}
get key() { return this._key; }
set key(_key) { this._key = _key; }
get value() { return this._value; }
set value(_value) { this._value = _value; }
};
export class JsonArray extends JsonNode {
constructor() {
super(nodeTypes.ARRAY);
this._items = [];
this._comments = [];
}
get items() { return this._items; }
get comments() { return this._comments; }
};
export class JsonValue extends JsonNode {
constructor(_value = null, _type = nodeTypes.VALUE) {
super(_type);
this._value = _value;
};
get value() { return this._value; }
set value(_value) { this._value = _value; }
};
export class JsonKey extends JsonValue {
constructor(_value = null) { super(_value, nodeTypes.KEY); };
};
export class JsonComment extends JsonValue {
constructor(_value = null) { super(_value, nodeTypes.COMMENT); }
};
export class JsonString extends JsonValue {
constructor(_value = null) { super(_value, nodeTypes.STRING); }
};
export class JsonNumber extends JsonValue {
constructor(_value = null) { super(_value, nodeTypes.NUMBER); }
};
export class JsonTrue extends JsonValue {
constructor() { super(true, nodeTypes.TRUE); };
};
export class JsonFalse extends JsonValue {
constructor() { super(false, nodeTypes.FALSE); };
};
export class JsonNull extends JsonValue {
constructor() { super(null, nodeTypes.NULL); };
};
const nodeTypeObjectMapping = {
[nodeTypes.DOCUMENT] : JsonDocument,
[nodeTypes.COMMENT] : JsonComment,
[nodeTypes.OBJECT] : JsonObject,
[nodeTypes.PROPERTY] : JsonProperty,
[nodeTypes.KEY] : JsonKey,
[nodeTypes.ARRAY] : JsonArray,
[nodeTypes.VALUE] : JsonValue,
[nodeTypes.STRING] : JsonString,
[nodeTypes.NUMBER] : JsonNumber,
[nodeTypes.TRUE] : JsonTrue,
[nodeTypes.FALSE] : JsonFalse,
[nodeTypes.NULL] : JsonNull
};
//
// Utility methods to construct the objects
//
export class NodeFactory {
constructor() {}
static fromType(objectType, _value = null) {
let clazz = nodeTypeObjectMapping[objectType];
if (clazz === null)
throw new Error(`AST node of type ${objectType} cannot be found`);
return _value !== null ? new clazz(_value) : new clazz();
}
}
function toJSON(jsonNode) {
if (!(jsonNode instanceof JsonNode))
throw new Error('JSON conversion only accepts a kind of JsonNode');
// Just a recursive, slow implementation to a JavaScript object from this
// JsonNode
function recursiveNodeConversion(rootNode, parentObject) {
let result = null;
switch (rootNode.type) {
case nodeTypes.DOCUMENT:
return recursiveNodeConversion(rootNode.child);
case nodeTypes.OBJECT: {
result = {};
rootNode.properties.forEach((propNode) => {
result[recursiveNodeConversion(propNode.key)] =
recursiveNodeConversion(propNode.value);
});
return result;
}
case nodeTypes.ARRAY: {
result = [];
rootNode.items.forEach(
(itemNode) => { result.push(recursiveNodeConversion(itemNode)); });
return result;
}
case nodeTypes.VALUE:
case nodeTypes.STRING:
case nodeTypes.KEY:
return rootNode.value;
case nodeTypes.NUMBER: {
// typecast
if (rootNode.value instanceof Number)
return rootNode.value;
return (new Number(rootNode.value)).valueOf();
}
case nodeTypes.TRUE:
return true;
case nodeTypes.FALSE:
return false;
case nodeTypes.NULL:
return null;
default:
return undefined;
}
}
return recursiveNodeConversion(jsonNode);
}