1 // Generated by LiveScript 1.6.0
3 var identifierRegex, tokenRegex;
4 identifierRegex = /[\$\w]+/;
9 throw new Error('Unexpected end of input.');
13 function consumeIdent(tokens){
16 if (!identifierRegex.test(token)) {
17 throw new Error("Expected text, got '" + token + "' instead.");
19 return tokens.shift();
21 function consumeOp(tokens, op){
25 throw new Error("Expected '" + op + "', got '" + token + "' instead.");
27 return tokens.shift();
29 function maybeConsumeOp(tokens, op){
33 return tokens.shift();
38 function consumeArray(tokens){
40 consumeOp(tokens, '[');
41 if (peek(tokens) === ']') {
42 throw new Error("Must specify type of Array - eg. [Type], got [] instead.");
44 types = consumeTypes(tokens);
45 consumeOp(tokens, ']');
51 function consumeTuple(tokens){
54 consumeOp(tokens, '(');
55 if (peek(tokens) === ')') {
56 throw new Error("Tuple must be of at least length 1 - eg. (Type), got () instead.");
59 components.push(consumeTypes(tokens));
60 maybeConsumeOp(tokens, ',');
61 if (')' === peek(tokens)) {
65 consumeOp(tokens, ')');
71 function consumeFields(tokens){
72 var fields, subset, ref$, key, types;
74 consumeOp(tokens, '{');
77 if (maybeConsumeOp(tokens, '...')) {
81 ref$ = consumeField(tokens), key = ref$[0], types = ref$[1];
83 maybeConsumeOp(tokens, ',');
84 if ('}' === peek(tokens)) {
88 consumeOp(tokens, '}');
95 function consumeField(tokens){
97 key = consumeIdent(tokens);
98 consumeOp(tokens, ':');
99 types = consumeTypes(tokens);
102 function maybeConsumeStructure(tokens){
105 return consumeArray(tokens);
107 return consumeTuple(tokens);
109 return consumeFields(tokens);
112 function consumeType(tokens){
113 var token, wildcard, type, structure;
114 token = peek(tokens);
115 wildcard = token === '*';
116 if (wildcard || identifierRegex.test(token)) {
118 ? consumeOp(tokens, '*')
119 : consumeIdent(tokens);
120 structure = maybeConsumeStructure(tokens);
122 return structure.type = type, structure;
129 structure = maybeConsumeStructure(tokens);
131 throw new Error("Unexpected character: " + token);
136 function consumeTypes(tokens){
137 var lookahead, types, typesSoFar, typeObj, type, structure;
138 if ('::' === peek(tokens)) {
139 throw new Error("No comment before comment separator '::' found.");
141 lookahead = tokens[1];
142 if (lookahead != null && lookahead === '::') {
148 if ('Maybe' === peek(tokens)) {
163 typeObj = consumeType(tokens), type = typeObj.type, structure = typeObj.structure;
164 if (!typesSoFar[type]) {
167 if (structure == null) {
168 typesSoFar[type] = true;
170 if (!maybeConsumeOp(tokens, '|')) {
176 tokenRegex = RegExp('\\.\\.\\.|::|->|' + identifierRegex.source + '|\\S', 'g');
177 module.exports = function(input){
180 throw new Error('No type specified.');
182 tokens = input.match(tokenRegex) || [];
183 if (in$('->', tokens)) {
184 throw new Error("Function types are not supported.\ To validate that something is a function, you may use 'Function'.");
187 return consumeTypes(tokens);
190 throw new Error(e.message + " - Remaining tokens: " + JSON.stringify(tokens) + " - Initial input: '" + input + "'");
194 var i = -1, l = xs.length >>> 0;
195 while (++i < l) if (x === xs[i]) return true;