1 // Generated by LiveScript 1.2.0
3 var reject, special, tokenRegex;
4 reject = require('prelude-ls').reject;
5 function consumeOp(tokens, op){
6 if (tokens[0] === op) {
9 throw new Error("Expected '" + op + "', but got '" + tokens[0] + "' instead in " + JSON.stringify(tokens) + ".");
12 function maybeConsumeOp(tokens, op){
13 if (tokens[0] === op) {
14 return tokens.shift();
17 function consumeList(tokens, delimiters, hasDelimiters){
20 consumeOp(tokens, delimiters[0]);
23 while (tokens.length && tokens[0] !== delimiters[1]) {
24 result.push(consumeElement(tokens));
25 maybeConsumeOp(tokens, ',');
28 consumeOp(tokens, delimiters[1]);
32 function consumeArray(tokens, hasDelimiters){
33 return consumeList(tokens, ['[', ']'], hasDelimiters);
35 function consumeTuple(tokens, hasDelimiters){
36 return consumeList(tokens, ['(', ')'], hasDelimiters);
38 function consumeFields(tokens, hasDelimiters){
41 consumeOp(tokens, '{');
44 while (tokens.length && (!hasDelimiters || tokens[0] !== '}')) {
46 consumeOp(tokens, ':');
47 result[key] = consumeElement(tokens);
48 maybeConsumeOp(tokens, ',');
51 consumeOp(tokens, '}');
55 function consumeElement(tokens){
58 return consumeArray(tokens, true);
60 return consumeTuple(tokens, true);
62 return consumeFields(tokens, true);
64 return tokens.shift();
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;
81 finalResult = consumeElement(tokens);
83 if (tokens.length && origTokens.length) {
84 throw new Error("Unable to parse " + JSON.stringify(origTokens) + " of type " + JSON.stringify(types) + ".");
89 special = /\[\]\(\)}{:,/.source;
90 tokenRegex = RegExp('("(?:[^"]|\\\\")*")|(\'(?:[^\']|\\\\\')*\')|(#.*#)|(/(?:\\\\/|[^/])*/[gimy]*)|([' + special + '])|([^\\s' + special + ']+)|\\s*');
91 module.exports = function(string, types){
93 tokens = reject(function(it){
94 return !it || /^\s+$/.test(it);
95 }, string.split(tokenRegex));
96 node = consumeTopLevel(tokens, types);
98 throw new Error("Error parsing '" + string + "'.");