-
Notifications
You must be signed in to change notification settings - Fork 25
/
parse.js
36 lines (32 loc) · 936 Bytes
/
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
require("collections/shim");
var grammar = require("./grammar"),
Map = require("collections/map");
var memo = new Map(); // could be Dict
module.exports = parse;
function parse(text, options) {
var cache;
if (Array.isArray(text)) {
return {
type: "tuple",
args: text.map(function (text) {
return parse(text, options);
})
};
} else if (!options && (cache = memo.get(text))) {
return cache;
} else {
try {
var syntax = grammar.parse(text, options || Object.empty);
if (!options) {
memo.set(text,syntax);
}
return syntax;
} catch (error) {
error.message = (
error.message.replace(/[\s\.]+$/, "") + " " +
" on line " + error.line + " column " + error.column
);
throw error;
}
}
}