-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.js
160 lines (152 loc) · 5.18 KB
/
parse.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
/**
* Provides basic parsing utilities.
*/
/**
* parses a template and returns its syntax tree
* using {@link nodes} and {@link tree}.
*/
exports.parse = function (text, fileName, lineNo) {
var nodes = exports.nodes(text, fileName, lineNo);
var tree = exports.tree(nodes, fileName);
return tree;
};
/** constructs a flat list of inline and block nodes with
* interpolated text based on the text of a template */
exports.nodes = function (text, fileName, lineNo) {
var nodes = [];
var node = {errors:[]};
var lineNo = lineNo || 1;
while (text.length) {
var nextOpen = text.indexOf("{");
if (nextOpen < 0) {
nodes.push(text);
text = "";
} else {
var prefix = text.slice(0, nextOpen);
prefix.replace(/\n/g, function () {
lineNo++;
});
var lastLf = prefix.lastIndexOf("\n");
var columnNo = lastLf < 0 ? 0 : prefix.length - lastLf - 1;
nodes.push(prefix);
text = text.slice(nextOpen);
var match = exports.parseCurly(text, node);
var tag = match[0];
if (/^%.*%$/.test(tag) || /^{.*}$/.test(tag)) {
nodes.push({
"type": match[0].slice(0, 1),
"content": match[0].slice(
1,
match[0].length - 1
),
"fileName": fileName,
"lineNo": lineNo,
"columnNo": columnNo
});
} else {
nodes.push("{" + tag + "}");
}
text = match[1];
}
}
return nodes;
};
/** transforms a flat list of interpolated text and inline
* and block nodes and constructs a syntax tree. */
exports.tree = function (nodes, fileName) {
var root = {
"type": "block",
"content": "root",
"fileName": fileName,
"lineNo": 1,
"columnNo": 0,
"children": []
};
var stack = [root];
nodes.forEach(function (node) {
if (typeof node === "string") {
stack[stack.length - 1].children.push(node);
} else {
if (node.type == "{") {
stack[stack.length - 1].children.push({
"type": "inline",
"content": node.content.trim(),
"fileName": fileName,
"lineNo": node.lineNo,
"columnNo": node.columnNo
});
} else if (/^\s*end/.test(node.content)) {
if (stack.length == 1) {
stack[0].children.push({
"type": "error",
"fileName": fileName,
"lineNo": node.lineNo,
"columnNo": node.columnNo,
"error": "unmatched end tag"
})
} else {
stack.pop();
}
} else {
var node = {
"type": "block",
"content": node.content.trim(),
"fileName": fileName,
"lineNo": node.lineNo,
"columnNo": node.columnNo,
"children": []
};
stack[stack.length - 1].children.push(node);
stack.push(node);
}
}
});
return root;
};
/**
* @param {String} text a string starting with a curly brace
* with a matching, nested curly brace somewhere before the
* end.
* @param {Object} node an object with an `errors` array.
* @returns {[enclosed, remainder]} where `enclosed` is a
* `String` that may contain nested curly braces, and
* remainder is all text that followed.
*/
exports.parseCurly = function (text, node) {
if (!text.slice(0, 1) == "{")
throw new Error("Assertion failed: parseCurly must receive a string that starts with a curly brace.");
text = text.slice(1);
var nextOpen = text.indexOf("{");
var nextClose = text.indexOf("}");
if (nextClose < 0) {
node.errors.push("Unmatched `{` in " + JSON.stringify("{" + text));
return ["", ""];
} else if (nextOpen < 0) {
return [text.slice(0, nextClose), text.slice(nextClose + 1)];
} else {
return exports.parseCurlyScan(text, node);
}
};
/**
*/
// already found {, looking for }, might find { } inside
exports.parseCurlyScan = function (text, node) {
var nextOpen = text.indexOf("{");
var nextClose = text.indexOf("}");
if (nextClose < 0) {
node.errors.push("Unmatched `{` in " + JSON.stringify("{" + text));
return ["", ""];
} else if (nextOpen < 0 || nextClose < nextOpen) {
// assert nextClose > 0
return [text.slice(0, nextClose), text.slice(nextClose + 1)];
} else {
// assert nextOpen < nextClose
var match = exports.parseCurly(text.slice(nextOpen), node);
var inner = match[0];
var after = match[1];
var match = exports.parseCurlyScan(after, node);
var before = match[0];
var outer = match[1];
return [text.slice(0, nextOpen + 1) + inner + "}" + before, outer];
}
};