-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpret.js
171 lines (154 loc) · 4.86 KB
/
interpret.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
'use strict';
const { SOURCES } = require('./source-tracker');
const ast = require('./ast');
/*
Given a parsed AST, this generates the final object represented by the TOML.
If a SourceTracker is provided, it will track the source locations of all
keys and value of all generated objects (i.e., "tables") and arrays.
*/
module.exports = (definitions, tracker) => {
const explicitTables = new Set();
const implicitTables = new Set();
const redefinableTables = new Set();
const appendableArrays = new Set();
return buildRoot();
function buildRoot() {
const root = createTable();
let current = root;
for (const definition of definitions) {
if (definition instanceof ast.ScopeNode) {
if (definition.isArrayTable) {
current = appendTable(root, definition.key.parts, definition.source);
} else {
current = defineTable(root, definition.key.parts, definition.source);
}
} else if (definition instanceof ast.AssignmentNode) {
assign(current, definition.key.parts, definition.value);
} else {
throw new TypeError('Unrecognized definition type');
}
}
return root;
}
function traverse(obj, keyPath, isNewScope = false) {
const length = keyPath.length - 1;
for (let i = 0; i < length; ++i) {
const keyPart = keyPath[i];
const key = keyPart.value;
const value = obj[key];
if (value === undefined) {
const table = createTable();
implicitTables.add(table);
isNewScope && redefinableTables.add(table);
tracker && track(obj, key, keyPart.source, getKeySource(keyPath, i));
obj[key] = table;
obj = table;
} else if (implicitTables.has(value)) {
obj = value;
} else if (isNewScope && explicitTables.has(value)) {
obj = value;
} else if (isNewScope && appendableArrays.has(value)) {
obj = value[value.length - 1];
} else {
const source = getKeySource(keyPath, i);
throw source.error('Cannot redefine existing key').done();
}
}
return obj;
}
function defineTable(obj, keyPath, source) {
obj = traverse(obj, keyPath, true);
const keyPart = keyPath[keyPath.length - 1];
const key = keyPart.value;
const value = obj[key];
if (value === undefined) {
const table = createTable();
explicitTables.add(table);
tracker && track(obj, key, keyPart.source, source);
obj[key] = table;
return table;
} else if (redefinableTables.has(value)) {
redefinableTables.delete(value);
implicitTables.delete(value);
explicitTables.add(value);
tracker && track(obj, key, keyPart.source, source);
return value;
} else {
const source = getKeySource(keyPath);
throw source.error('Cannot redefine existing key').done();
}
}
function appendTable(obj, keyPath, source) {
obj = traverse(obj, keyPath, true);
const keyPart = keyPath[keyPath.length - 1];
const key = keyPart.value;
let value = obj[key];
if (value === undefined) {
value = createArray();
appendableArrays.add(value);
tracker && track(obj, key, keyPart.source, source);
obj[key] = value;
}
if (appendableArrays.has(value)) {
const table = createTable();
tracker && track(value, value.length, source, source);
value.push(table);
return table;
} else {
const source = getKeySource(keyPath);
throw source.error('Cannot redefine existing key').done();
}
}
function assign(obj, keyPath, valueNode) {
obj = traverse(obj, keyPath);
const keyPart = keyPath[keyPath.length - 1];
const key = keyPart.value;
if (obj[key] === undefined) {
tracker && track(obj, key, keyPart.source, valueNode.source);
obj[key] = getValue(valueNode);
} else {
const source = getKeySource(keyPath);
throw source.error('Cannot redefine existing key').done();
}
}
function getValue(node) {
if (!(node instanceof ast.ValueNode)) {
throw new TypeError('Expected node to be a ValueNode');
}
if (node instanceof ast.InlineTableNode) {
const table = createTable();
for (const assignmentNode of node.assignments) {
assign(table, assignmentNode.key.parts, assignmentNode.value);
}
return table;
}
if (node instanceof ast.InlineArrayNode) {
const array = createArray();
for (const valueNode of node.values) {
tracker && track(array, array.length, valueNode.source, valueNode.source);
array.push(getValue(valueNode));
}
return array;
}
if (node.value == null) {
throw new TypeError('Expected ValueNode to have a value');
}
return node.value;
}
function createTable() {
const table = Object.create(null);
tracker && tracker[SOURCES].set(table, new Map());
return table;
}
function createArray() {
const array = [];
tracker && tracker[SOURCES].set(array, new Map());
return array;
}
function track(obj, key, keySource, valueSource) {
tracker[SOURCES].get(obj).set(key, { key: keySource, value: valueSource });
}
};
function getKeySource(keyPath, lastIndex = keyPath.length - 1) {
return keyPath[0].source.to(keyPath[lastIndex].source);
}