Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80690 views
1
// Generated by LiveScript 1.2.0
2
(function(){
3
var reject, special, tokenRegex;
4
reject = require('prelude-ls').reject;
5
function consumeOp(tokens, op){
6
if (tokens[0] === op) {
7
return tokens.shift();
8
} else {
9
throw new Error("Expected '" + op + "', but got '" + tokens[0] + "' instead in " + JSON.stringify(tokens) + ".");
10
}
11
}
12
function maybeConsumeOp(tokens, op){
13
if (tokens[0] === op) {
14
return tokens.shift();
15
}
16
}
17
function consumeList(tokens, delimiters, hasDelimiters){
18
var result;
19
if (hasDelimiters) {
20
consumeOp(tokens, delimiters[0]);
21
}
22
result = [];
23
while (tokens.length && tokens[0] !== delimiters[1]) {
24
result.push(consumeElement(tokens));
25
maybeConsumeOp(tokens, ',');
26
}
27
if (hasDelimiters) {
28
consumeOp(tokens, delimiters[1]);
29
}
30
return result;
31
}
32
function consumeArray(tokens, hasDelimiters){
33
return consumeList(tokens, ['[', ']'], hasDelimiters);
34
}
35
function consumeTuple(tokens, hasDelimiters){
36
return consumeList(tokens, ['(', ')'], hasDelimiters);
37
}
38
function consumeFields(tokens, hasDelimiters){
39
var result, key;
40
if (hasDelimiters) {
41
consumeOp(tokens, '{');
42
}
43
result = {};
44
while (tokens.length && (!hasDelimiters || tokens[0] !== '}')) {
45
key = tokens.shift();
46
consumeOp(tokens, ':');
47
result[key] = consumeElement(tokens);
48
maybeConsumeOp(tokens, ',');
49
}
50
if (hasDelimiters) {
51
consumeOp(tokens, '}');
52
}
53
return result;
54
}
55
function consumeElement(tokens){
56
switch (tokens[0]) {
57
case '[':
58
return consumeArray(tokens, true);
59
case '(':
60
return consumeTuple(tokens, true);
61
case '{':
62
return consumeFields(tokens, true);
63
default:
64
return tokens.shift();
65
}
66
}
67
function consumeTopLevel(tokens, types){
68
var ref$, type, structure, origTokens, result, finalResult, x$, y$;
69
ref$ = types[0], type = ref$.type, structure = ref$.structure;
70
origTokens = tokens.concat();
71
if (types.length === 1 && (structure || (type === 'Array' || type === 'Object'))) {
72
result = structure === 'array' || type === 'Array'
73
? consumeArray(tokens, tokens[0] === '[')
74
: structure === 'tuple'
75
? consumeTuple(tokens, tokens[0] === '(')
76
: consumeFields(tokens, tokens[0] === '{');
77
finalResult = tokens.length ? consumeElement(structure === 'array' || type === 'Array'
78
? (x$ = origTokens, x$.unshift('['), x$.push(']'), x$)
79
: (y$ = origTokens, y$.unshift('('), y$.push(')'), y$)) : result;
80
} else {
81
finalResult = consumeElement(tokens);
82
}
83
if (tokens.length && origTokens.length) {
84
throw new Error("Unable to parse " + JSON.stringify(origTokens) + " of type " + JSON.stringify(types) + ".");
85
} else {
86
return finalResult;
87
}
88
}
89
special = /\[\]\(\)}{:,/.source;
90
tokenRegex = RegExp('("(?:[^"]|\\\\")*")|(\'(?:[^\']|\\\\\')*\')|(#.*#)|(/(?:\\\\/|[^/])*/[gimy]*)|([' + special + '])|([^\\s' + special + ']+)|\\s*');
91
module.exports = function(string, types){
92
var tokens, node;
93
tokens = reject(function(it){
94
return !it || /^\s+$/.test(it);
95
}, string.split(tokenRegex));
96
node = consumeTopLevel(tokens, types);
97
if (!node) {
98
throw new Error("Error parsing '" + string + "'.");
99
}
100
return node;
101
};
102
}).call(this);
103
104