jashkenas--coffeescript/lib/coffee-script/lexer.js

713 lines
24 KiB
JavaScript
Raw Normal View History

// Generated by CoffeeScript 1.2.1-pre
2010-07-25 07:15:12 +00:00
(function() {
2012-03-01 04:46:03 +00:00
var BOOL, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_ALIAS_MAP, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, HEREDOC, HEREDOC_ILLEGAL, HEREDOC_INDENT, HEREGEX, HEREGEX_OMIT, IDENTIFIER, INDEXABLE, INVERSES, JSTOKEN, JS_FORBIDDEN, JS_KEYWORDS, LINE_BREAK, LINE_CONTINUER, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NOT_REGEX, NOT_SPACED_REGEX, NUMBER, OPERATOR, REGEX, RELATION, RESERVED, Rewriter, SHIFT, SIMPLESTR, STRICT_PROSCRIBED, TRAILING_SPACES, UNARY, WHITESPACE, compact, count, key, last, starts, _ref, _ref1,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
2011-09-16 23:26:04 +00:00
_ref = require('./rewriter'), Rewriter = _ref.Rewriter, INVERSES = _ref.INVERSES;
2012-03-01 04:46:03 +00:00
_ref1 = require('./helpers'), count = _ref1.count, starts = _ref1.starts, compact = _ref1.compact, last = _ref1.last;
2010-12-23 18:50:52 +00:00
exports.Lexer = Lexer = (function() {
Lexer.name = 'Lexer';
2011-12-24 11:46:09 +00:00
function Lexer() {}
2010-11-05 04:04:52 +00:00
Lexer.prototype.tokenize = function(code, opts) {
2011-09-16 23:26:04 +00:00
var i, tag;
if (opts == null) opts = {};
if (WHITESPACE.test(code)) code = "\n" + code;
2010-10-03 23:22:42 +00:00
code = code.replace(/\r/g, '').replace(TRAILING_SPACES, '');
this.code = code;
2010-11-05 04:04:52 +00:00
this.line = opts.line || 0;
this.indent = 0;
this.indebt = 0;
this.outdebt = 0;
this.indents = [];
2011-09-16 23:26:04 +00:00
this.ends = [];
this.tokens = [];
i = 0;
while (this.chunk = code.slice(i)) {
i += this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
}
this.closeIndentation();
2011-09-18 01:39:24 +00:00
if (tag = this.ends.pop()) this.error("missing " + tag);
if (opts.rewrite === false) return this.tokens;
return (new Rewriter).rewrite(this.tokens);
};
Lexer.prototype.identifierToken = function() {
2012-03-01 04:46:03 +00:00
var colon, forcedIdentifier, id, input, match, prev, tag, _ref2, _ref3;
if (!(match = IDENTIFIER.exec(this.chunk))) return 0;
input = match[0], id = match[1], colon = match[2];
2010-11-28 23:33:43 +00:00
if (id === 'own' && this.tag() === 'FOR') {
this.token('OWN', id);
return id.length;
}
2012-03-01 04:46:03 +00:00
forcedIdentifier = colon || (prev = last(this.tokens)) && (((_ref2 = prev[0]) === '.' || _ref2 === '?.' || _ref2 === '::') || !prev.spaced && prev[0] === '@');
tag = 'IDENTIFIER';
2011-06-19 08:49:41 +00:00
if (!forcedIdentifier && (__indexOf.call(JS_KEYWORDS, id) >= 0 || __indexOf.call(COFFEE_KEYWORDS, id) >= 0)) {
tag = id.toUpperCase();
2012-03-01 04:46:03 +00:00
if (tag === 'WHEN' && (_ref3 = this.tag(), __indexOf.call(LINE_BREAK, _ref3) >= 0)) {
2010-09-23 05:14:18 +00:00
tag = 'LEADING_WHEN';
} else if (tag === 'FOR') {
this.seenFor = true;
} else if (tag === 'UNLESS') {
tag = 'IF';
} else if (__indexOf.call(UNARY, tag) >= 0) {
tag = 'UNARY';
} else if (__indexOf.call(RELATION, tag) >= 0) {
if (tag !== 'INSTANCEOF' && this.seenFor) {
tag = 'FOR' + tag;
this.seenFor = false;
} else {
tag = 'RELATION';
if (this.value() === '!') {
this.tokens.pop();
id = '!' + id;
}
}
2010-09-23 05:14:18 +00:00
}
}
if (__indexOf.call(JS_FORBIDDEN, id) >= 0) {
if (forcedIdentifier) {
tag = 'IDENTIFIER';
id = new String(id);
id.reserved = true;
} else if (__indexOf.call(RESERVED, id) >= 0) {
2011-11-25 13:49:51 +00:00
this.error("reserved word \"" + id + "\"");
}
}
2010-10-07 11:05:22 +00:00
if (!forcedIdentifier) {
if (__indexOf.call(COFFEE_ALIASES, id) >= 0) id = COFFEE_ALIAS_MAP[id];
2010-12-23 18:50:52 +00:00
tag = (function() {
2010-11-05 04:04:52 +00:00
switch (id) {
case '!':
return 'UNARY';
case '==':
case '!=':
return 'COMPARE';
case '&&':
case '||':
return 'LOGIC';
case 'true':
case 'false':
case 'null':
case 'undefined':
return 'BOOL';
case 'break':
case 'continue':
return 'STATEMENT';
2010-11-05 04:04:52 +00:00
default:
return tag;
}
2010-12-23 18:50:52 +00:00
})();
2010-03-22 01:46:06 +00:00
}
this.token(tag, id);
if (colon) this.token(':', ':');
return input.length;
};
Lexer.prototype.numberToken = function() {
var binaryLiteral, lexedLength, match, number, octalLiteral;
if (!(match = NUMBER.exec(this.chunk))) return 0;
number = match[0];
if (/^0[BOX]/.test(number)) {
this.error("radix prefix '" + number + "' must be lowercase");
} else if (/E/.test(number) && !/^0x/.test(number)) {
this.error("exponential notation '" + number + "' must be indicated with a lowercase 'e'");
} else if (/^0\d*[89]/.test(number)) {
this.error("decimal literal '" + number + "' must not be prefixed with '0'");
} else if (/^0\d+/.test(number)) {
this.error("octal literal '" + number + "' must be prefixed with '0o'");
}
lexedLength = number.length;
if (octalLiteral = /^0o([0-7]+)/.exec(number)) {
number = '0x' + (parseInt(octalLiteral[1], 8)).toString(16);
}
if (binaryLiteral = /^0b([01]+)/.exec(number)) {
number = '0x' + (parseInt(binaryLiteral[1], 2)).toString(16);
2011-10-24 18:51:41 +00:00
}
this.token('NUMBER', number);
2011-10-24 18:51:41 +00:00
return lexedLength;
};
Lexer.prototype.stringToken = function() {
var match, octalEsc, string;
2010-09-24 13:38:28 +00:00
switch (this.chunk.charAt(0)) {
case "'":
if (!(match = SIMPLESTR.exec(this.chunk))) return 0;
this.token('STRING', (string = match[0]).replace(MULTILINER, '\\\n'));
2010-09-24 13:38:28 +00:00
break;
case '"':
if (!(string = this.balancedString(this.chunk, '"'))) return 0;
if (0 < string.indexOf('#{', 1)) {
this.interpolateString(string.slice(1, -1));
2010-10-03 23:22:42 +00:00
} else {
this.token('STRING', this.escapeLines(string));
}
2010-09-24 13:38:28 +00:00
break;
default:
return 0;
}
if (octalEsc = /^(?:\\.|[^\\])*\\[0-7]/.test(string)) {
this.error("octal escape sequences " + string + " are not allowed");
}
2010-09-23 05:14:18 +00:00
this.line += count(string, '\n');
return string.length;
};
Lexer.prototype.heredocToken = function() {
2010-09-23 05:14:18 +00:00
var doc, heredoc, match, quote;
if (!(match = HEREDOC.exec(this.chunk))) return 0;
2010-09-23 05:14:18 +00:00
heredoc = match[0];
quote = heredoc.charAt(0);
doc = this.sanitizeHeredoc(match[2], {
quote: quote,
indent: null
});
if (quote === '"' && 0 <= doc.indexOf('#{')) {
this.interpolateString(doc, {
heredoc: true
});
} else {
this.token('STRING', this.makeString(doc, quote, true));
}
2010-09-23 05:14:18 +00:00
this.line += count(heredoc, '\n');
return heredoc.length;
};
Lexer.prototype.commentToken = function() {
var comment, here, match;
if (!(match = this.chunk.match(COMMENT))) return 0;
comment = match[0], here = match[1];
2010-09-23 05:14:18 +00:00
if (here) {
this.token('HERECOMMENT', this.sanitizeHeredoc(here, {
herecomment: true,
indent: Array(this.indent + 1).join(' ')
}));
}
2011-01-19 04:10:09 +00:00
this.line += count(comment, '\n');
return comment.length;
};
Lexer.prototype.jsToken = function() {
var match, script;
if (!(this.chunk.charAt(0) === '`' && (match = JSTOKEN.exec(this.chunk)))) {
return 0;
}
this.token('JS', (script = match[0]).slice(1, -1));
return script.length;
};
Lexer.prototype.regexToken = function() {
2012-03-01 04:46:03 +00:00
var flags, length, match, prev, regex, _ref2, _ref3;
if (this.chunk.charAt(0) !== '/') return 0;
2010-10-03 23:22:42 +00:00
if (match = HEREGEX.exec(this.chunk)) {
length = this.heregexToken(match);
this.line += count(match[0], '\n');
return length;
}
prev = last(this.tokens);
2012-03-01 04:46:03 +00:00
if (prev && (_ref2 = prev[0], __indexOf.call((prev.spaced ? NOT_REGEX : NOT_SPACED_REGEX), _ref2) >= 0)) {
return 0;
}
if (!(match = REGEX.exec(this.chunk))) return 0;
2012-03-01 04:46:03 +00:00
_ref3 = match, match = _ref3[0], regex = _ref3[1], flags = _ref3[2];
if (regex.slice(0, 2) === '/*') {
this.error('regular expressions cannot begin with `*`');
}
if (regex === '//') regex = '/(?:)/';
this.token('REGEX', "" + regex + flags);
return match.length;
};
2010-10-03 23:22:42 +00:00
Lexer.prototype.heregexToken = function(match) {
2012-03-01 04:46:03 +00:00
var body, flags, heregex, re, tag, tokens, value, _i, _len, _ref2, _ref3, _ref4, _ref5;
heregex = match[0], body = match[1], flags = match[2];
if (0 > body.indexOf('#{')) {
re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/');
if (re.match(/^\*/)) {
this.error('regular expressions cannot begin with `*`');
}
this.token('REGEX', "/" + (re || '(?:)') + "/" + flags);
return heregex.length;
}
2010-10-03 23:22:42 +00:00
this.token('IDENTIFIER', 'RegExp');
this.tokens.push(['CALL_START', '(']);
tokens = [];
2012-03-01 04:46:03 +00:00
_ref2 = this.interpolateString(body, {
regex: true
2010-11-20 21:25:22 +00:00
});
2012-03-01 04:46:03 +00:00
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
_ref3 = _ref2[_i], tag = _ref3[0], value = _ref3[1];
if (tag === 'TOKENS') {
tokens.push.apply(tokens, value);
} else {
if (!(value = value.replace(HEREGEX_OMIT, ''))) continue;
value = value.replace(/\\/g, '\\\\');
tokens.push(['STRING', this.makeString(value, '"', true)]);
}
tokens.push(['+', '+']);
}
tokens.pop();
2012-03-01 04:46:03 +00:00
if (((_ref4 = tokens[0]) != null ? _ref4[0] : void 0) !== 'STRING') {
this.tokens.push(['STRING', '""'], ['+', '+']);
}
2012-03-01 04:46:03 +00:00
(_ref5 = this.tokens).push.apply(_ref5, tokens);
if (flags) this.tokens.push([',', ','], ['STRING', '"' + flags + '"']);
this.token(')', ')');
return heregex.length;
};
Lexer.prototype.lineToken = function() {
2010-10-25 18:56:02 +00:00
var diff, indent, match, noNewlines, prev, size;
if (!(match = MULTI_DENT.exec(this.chunk))) return 0;
indent = match[0];
2010-09-23 05:14:18 +00:00
this.line += count(indent, '\n');
this.seenFor = false;
2010-09-28 12:52:51 +00:00
prev = last(this.tokens, 1);
2010-09-23 05:14:18 +00:00
size = indent.length - 1 - indent.lastIndexOf('\n');
2010-10-25 18:56:02 +00:00
noNewlines = this.unfinished();
if (size - this.indebt === this.indent) {
if (noNewlines) {
this.suppressNewlines();
} else {
this.newlineToken();
}
return indent.length;
}
if (size > this.indent) {
if (noNewlines) {
this.indebt = size - this.indent;
this.suppressNewlines();
return indent.length;
}
diff = size - this.indent + this.outdebt;
this.token('INDENT', diff);
this.indents.push(diff);
2011-09-16 23:26:04 +00:00
this.ends.push('OUTDENT');
this.outdebt = this.indebt = 0;
} else {
this.indebt = 0;
this.outdentToken(this.indent - size, noNewlines);
}
this.indent = size;
return indent.length;
};
2011-09-16 23:26:04 +00:00
Lexer.prototype.outdentToken = function(moveOut, noNewlines) {
var dent, len;
while (moveOut > 0) {
len = this.indents.length - 1;
if (this.indents[len] === void 0) {
moveOut = 0;
} else if (this.indents[len] === this.outdebt) {
moveOut -= this.outdebt;
this.outdebt = 0;
} else if (this.indents[len] < this.outdebt) {
this.outdebt -= this.indents[len];
moveOut -= this.indents[len];
} else {
dent = this.indents.pop() - this.outdebt;
moveOut -= dent;
this.outdebt = 0;
2011-09-16 23:26:04 +00:00
this.pair('OUTDENT');
this.token('OUTDENT', dent);
}
}
if (dent) this.outdebt -= moveOut;
while (this.value() === ';') {
this.tokens.pop();
}
if (!(this.tag() === 'TERMINATOR' || noNewlines)) {
this.token('TERMINATOR', '\n');
}
return this;
};
Lexer.prototype.whitespaceToken = function() {
2010-10-21 01:37:58 +00:00
var match, nline, prev;
if (!((match = WHITESPACE.exec(this.chunk)) || (nline = this.chunk.charAt(0) === '\n'))) {
return 0;
}
2010-09-28 12:52:51 +00:00
prev = last(this.tokens);
if (prev) prev[match ? 'spaced' : 'newLine'] = true;
if (match) {
return match[0].length;
} else {
return 0;
}
};
Lexer.prototype.newlineToken = function() {
while (this.value() === ';') {
this.tokens.pop();
}
if (this.tag() !== 'TERMINATOR') this.token('TERMINATOR', '\n');
return this;
};
Lexer.prototype.suppressNewlines = function() {
if (this.value() === '\\') this.tokens.pop();
return this;
};
Lexer.prototype.literalToken = function() {
2012-03-01 04:46:03 +00:00
var match, prev, tag, value, _ref2, _ref3, _ref4, _ref5;
2010-10-05 14:31:48 +00:00
if (match = OPERATOR.exec(this.chunk)) {
value = match[0];
if (CODE.test(value)) this.tagParameters();
2010-09-23 05:14:18 +00:00
} else {
value = this.chunk.charAt(0);
}
tag = value;
prev = last(this.tokens);
if (value === '=' && prev) {
2012-03-01 04:46:03 +00:00
if (!prev[1].reserved && (_ref2 = prev[1], __indexOf.call(JS_FORBIDDEN, _ref2) >= 0)) {
this.error("reserved word \"" + (this.value()) + "\" can't be assigned");
}
2012-03-01 04:46:03 +00:00
if ((_ref3 = prev[1]) === '||' || _ref3 === '&&') {
2010-10-05 14:31:48 +00:00
prev[0] = 'COMPOUND_ASSIGN';
prev[1] += '=';
return value.length;
2010-07-25 05:36:50 +00:00
}
2010-07-25 05:23:37 +00:00
}
if (value === ';') {
2011-09-21 04:21:46 +00:00
this.seenFor = false;
tag = 'TERMINATOR';
} else if (__indexOf.call(MATH, value) >= 0) {
tag = 'MATH';
} else if (__indexOf.call(COMPARE, value) >= 0) {
tag = 'COMPARE';
} else if (__indexOf.call(COMPOUND_ASSIGN, value) >= 0) {
tag = 'COMPOUND_ASSIGN';
} else if (__indexOf.call(UNARY, value) >= 0) {
tag = 'UNARY';
} else if (__indexOf.call(SHIFT, value) >= 0) {
tag = 'SHIFT';
} else if (__indexOf.call(LOGIC, value) >= 0 || value === '?' && (prev != null ? prev.spaced : void 0)) {
tag = 'LOGIC';
} else if (prev && !prev.spaced) {
2012-03-01 04:46:03 +00:00
if (value === '(' && (_ref4 = prev[0], __indexOf.call(CALLABLE, _ref4) >= 0)) {
if (prev[0] === '?') prev[0] = 'FUNC_EXIST';
tag = 'CALL_START';
2012-03-01 04:46:03 +00:00
} else if (value === '[' && (_ref5 = prev[0], __indexOf.call(INDEXABLE, _ref5) >= 0)) {
tag = 'INDEX_START';
switch (prev[0]) {
2010-09-23 05:14:18 +00:00
case '?':
2010-10-05 14:31:48 +00:00
prev[0] = 'INDEX_SOAK';
}
}
}
2011-09-16 23:26:04 +00:00
switch (value) {
case '(':
case '{':
case '[':
this.ends.push(INVERSES[value]);
break;
case ')':
case '}':
case ']':
this.pair(value);
}
this.token(tag, value);
return value.length;
};
Lexer.prototype.sanitizeHeredoc = function(doc, options) {
2012-03-01 04:46:03 +00:00
var attempt, herecomment, indent, match, _ref2;
indent = options.indent, herecomment = options.herecomment;
2011-01-19 04:10:09 +00:00
if (herecomment) {
if (HEREDOC_ILLEGAL.test(doc)) {
2011-09-18 01:39:24 +00:00
this.error("block comment cannot contain \"*/\", starting");
}
if (doc.indexOf('\n') <= 0) return doc;
2011-01-19 04:10:09 +00:00
} else {
while (match = HEREDOC_INDENT.exec(doc)) {
attempt = match[1];
2012-03-01 04:46:03 +00:00
if (indent === null || (0 < (_ref2 = attempt.length) && _ref2 < indent.length)) {
indent = attempt;
}
}
}
if (indent) doc = doc.replace(RegExp("\\n" + indent, "g"), '\n');
if (!herecomment) doc = doc.replace(/^\n/, '');
return doc;
};
Lexer.prototype.tagParameters = function() {
2010-10-26 04:09:46 +00:00
var i, stack, tok, tokens;
if (this.tag() !== ')') return this;
2010-10-26 04:09:46 +00:00
stack = [];
tokens = this.tokens;
i = tokens.length;
tokens[--i][0] = 'PARAM_END';
while (tok = tokens[--i]) {
switch (tok[0]) {
2010-09-22 03:58:05 +00:00
case ')':
2010-10-26 04:09:46 +00:00
stack.push(tok);
2010-09-22 03:58:05 +00:00
break;
case '(':
case 'CALL_START':
2010-10-26 04:09:46 +00:00
if (stack.length) {
stack.pop();
} else if (tok[0] === '(') {
2010-10-26 04:09:46 +00:00
tok[0] = 'PARAM_START';
return this;
} else {
return this;
2010-10-26 04:09:46 +00:00
}
}
}
return this;
};
Lexer.prototype.closeIndentation = function() {
return this.outdentToken(this.indent);
};
2010-12-18 19:48:44 +00:00
Lexer.prototype.balancedString = function(str, end) {
2012-03-01 04:46:03 +00:00
var continueCount, i, letter, match, prev, stack, _i, _ref2;
continueCount = 0;
2010-12-18 18:20:14 +00:00
stack = [end];
2012-03-01 04:46:03 +00:00
for (i = _i = 1, _ref2 = str.length; 1 <= _ref2 ? _i < _ref2 : _i > _ref2; i = 1 <= _ref2 ? ++_i : --_i) {
if (continueCount) {
--continueCount;
continue;
}
2010-12-18 18:20:14 +00:00
switch (letter = str.charAt(i)) {
case '\\':
++continueCount;
continue;
2010-12-18 18:20:14 +00:00
case end:
stack.pop();
2011-12-24 11:04:05 +00:00
if (!stack.length) return str.slice(0, i + 1 || 9e9);
2010-12-18 18:20:14 +00:00
end = stack[stack.length - 1];
continue;
}
if (end === '}' && (letter === '"' || letter === "'")) {
2010-12-18 18:20:14 +00:00
stack.push(end = letter);
} else if (end === '}' && letter === '/' && (match = HEREGEX.exec(str.slice(i)) || REGEX.exec(str.slice(i)))) {
continueCount += match[0].length - 1;
2010-12-18 18:20:14 +00:00
} else if (end === '}' && letter === '{') {
stack.push(end = '}');
} else if (end === '"' && prev === '#' && letter === '{') {
stack.push(end = '}');
}
2010-12-18 18:20:14 +00:00
prev = letter;
}
2011-09-18 01:39:24 +00:00
return this.error("missing " + (stack.pop()) + ", starting");
};
Lexer.prototype.interpolateString = function(str, options) {
2012-03-01 04:46:03 +00:00
var expr, heredoc, i, inner, interpolated, len, letter, nested, pi, regex, tag, tokens, value, _i, _len, _ref2, _ref3, _ref4;
if (options == null) options = {};
heredoc = options.heredoc, regex = options.regex;
tokens = [];
pi = 0;
i = -1;
2010-10-05 04:11:28 +00:00
while (letter = str.charAt(i += 1)) {
if (letter === '\\') {
i += 1;
2010-10-03 23:22:42 +00:00
continue;
}
if (!(letter === '#' && str.charAt(i + 1) === '{' && (expr = this.balancedString(str.slice(i + 1), '}')))) {
continue;
}
if (pi < i) tokens.push(['NEOSTRING', str.slice(pi, i)]);
inner = expr.slice(1, -1);
2010-10-03 23:22:42 +00:00
if (inner.length) {
nested = new Lexer().tokenize(inner, {
line: this.line,
rewrite: false
2010-10-03 23:22:42 +00:00
});
nested.pop();
2012-03-01 04:46:03 +00:00
if (((_ref2 = nested[0]) != null ? _ref2[0] : void 0) === 'TERMINATOR') {
nested.shift();
}
2011-01-15 15:57:50 +00:00
if (len = nested.length) {
if (len > 1) {
nested.unshift(['(', '(', this.line]);
nested.push([')', ')', this.line]);
2011-01-15 15:57:50 +00:00
}
tokens.push(['TOKENS', nested]);
}
}
2010-10-03 23:22:42 +00:00
i += expr.length;
pi = i + 1;
}
if ((i > pi && pi < str.length)) tokens.push(['NEOSTRING', str.slice(pi)]);
if (regex) return tokens;
if (!tokens.length) return this.token('STRING', '""');
if (tokens[0][0] !== 'NEOSTRING') tokens.unshift(['', '']);
if (interpolated = tokens.length > 1) this.token('(', '(');
for (i = _i = 0, _len = tokens.length; _i < _len; i = ++_i) {
2012-03-01 04:46:03 +00:00
_ref3 = tokens[i], tag = _ref3[0], value = _ref3[1];
if (i) this.token('+', '+');
if (tag === 'TOKENS') {
2012-03-01 04:46:03 +00:00
(_ref4 = this.tokens).push.apply(_ref4, value);
} else {
this.token('STRING', this.makeString(value, '"', heredoc));
2010-04-04 06:59:44 +00:00
}
}
if (interpolated) this.token(')', ')');
return tokens;
};
2011-09-16 23:26:04 +00:00
Lexer.prototype.pair = function(tag) {
var size, wanted;
if (tag !== (wanted = last(this.ends))) {
2011-09-18 01:39:24 +00:00
if ('OUTDENT' !== wanted) this.error("unmatched " + tag);
2011-09-16 23:26:04 +00:00
this.indent -= size = last(this.indents);
this.outdentToken(size, true);
return this.pair(tag);
}
return this.ends.pop();
};
Lexer.prototype.token = function(tag, value) {
return this.tokens.push([tag, value, this.line]);
};
2010-10-05 14:31:48 +00:00
Lexer.prototype.tag = function(index, tag) {
var tok;
2010-11-05 04:04:52 +00:00
return (tok = last(this.tokens, index)) && (tag ? tok[0] = tag : tok[0]);
};
Lexer.prototype.value = function(index, val) {
var tok;
2010-11-05 04:04:52 +00:00
return (tok = last(this.tokens, index)) && (val ? tok[1] = val : tok[1]);
};
Lexer.prototype.unfinished = function() {
2012-03-01 04:46:03 +00:00
var _ref2;
return LINE_CONTINUER.test(this.chunk) || ((_ref2 = this.tag()) === '\\' || _ref2 === '.' || _ref2 === '?.' || _ref2 === 'UNARY' || _ref2 === 'MATH' || _ref2 === '+' || _ref2 === '-' || _ref2 === 'SHIFT' || _ref2 === 'RELATION' || _ref2 === 'COMPARE' || _ref2 === 'LOGIC' || _ref2 === 'THROW' || _ref2 === 'EXTENDS');
};
Lexer.prototype.escapeLines = function(str, heredoc) {
return str.replace(MULTILINER, heredoc ? '\\n' : '');
};
Lexer.prototype.makeString = function(body, quote, heredoc) {
if (!body) return quote + quote;
body = body.replace(/\\([\s\S])/g, function(match, contents) {
if (contents === '\n' || contents === quote) {
return contents;
} else {
return match;
}
});
body = body.replace(RegExp("" + quote, "g"), '\\$&');
return quote + this.escapeLines(body, heredoc) + quote;
};
2011-09-18 01:39:24 +00:00
Lexer.prototype.error = function(message) {
2011-09-16 23:26:04 +00:00
throw SyntaxError("" + message + " on line " + (this.line + 1));
};
return Lexer;
2011-12-14 15:39:20 +00:00
2010-12-23 18:50:52 +00:00
})();
JS_KEYWORDS = ['true', 'false', 'null', 'this', 'new', 'delete', 'typeof', 'in', 'instanceof', 'return', 'throw', 'break', 'continue', 'debugger', 'if', 'else', 'switch', 'for', 'while', 'do', 'try', 'catch', 'finally', 'class', 'extends', 'super'];
COFFEE_KEYWORDS = ['undefined', 'then', 'unless', 'until', 'loop', 'of', 'by', 'when'];
2011-04-30 14:35:56 +00:00
COFFEE_ALIAS_MAP = {
and: '&&',
or: '||',
is: '==',
isnt: '!=',
not: '!',
2010-10-23 18:24:30 +00:00
yes: 'true',
no: 'false',
on: 'true',
off: 'false'
2011-04-30 14:35:56 +00:00
};
2011-04-30 14:35:56 +00:00
COFFEE_ALIASES = (function() {
var _results;
_results = [];
for (key in COFFEE_ALIAS_MAP) {
_results.push(key);
}
return _results;
})();
2011-04-30 14:35:56 +00:00
COFFEE_KEYWORDS = COFFEE_KEYWORDS.concat(COFFEE_ALIASES);
RESERVED = ['case', 'default', 'function', 'var', 'void', 'with', 'const', 'let', 'enum', 'export', 'import', 'native', '__hasProp', '__extends', '__slice', '__bind', '__indexOf', 'implements', 'interface', 'let', 'package', 'private', 'protected', 'public', 'static', 'yield'];
STRICT_PROSCRIBED = ['arguments', 'eval'];
JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED).concat(STRICT_PROSCRIBED);
exports.RESERVED = RESERVED.concat(JS_KEYWORDS).concat(COFFEE_KEYWORDS).concat(STRICT_PROSCRIBED);
exports.STRICT_PROSCRIBED = STRICT_PROSCRIBED;
2010-12-29 01:42:20 +00:00
IDENTIFIER = /^([$A-Za-z_\x7f-\uffff][$\w\x7f-\uffff]*)([^\n\S]*:(?!:))?/;
NUMBER = /^0b[01]+|^0o[0-7]+|^0x[\da-f]+|^\d*\.?\d+(?:e[+-]?\d+)?/i;
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[^\n\S]*)?\1/;
2010-11-20 21:25:22 +00:00
OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/;
WHITESPACE = /^[^\n\S]+/;
2010-12-16 03:59:28 +00:00
COMMENT = /^###([^#][\s\S]*?)(?:###[^\n\S]*|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/;
2010-09-23 05:14:18 +00:00
CODE = /^[-=]>/;
MULTI_DENT = /^(?:\n[^\n\S]*)+/;
2010-09-24 13:38:28 +00:00
SIMPLESTR = /^'[^\\']*(?:\\.[^\\']*)*'/;
2010-09-24 13:38:28 +00:00
JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/;
REGEX = /^(\/(?![\s=])[^[\/\n\\]*(?:(?:\\[\s\S]|\[[^\]\n\\]*(?:\\[\s\S][^\]\n\\]*)*])[^[\/\n\\]*)*\/)([imgy]{0,4})(?!\w)/;
2010-11-11 03:41:14 +00:00
HEREGEX = /^\/{3}([\s\S]+?)\/{3}([imgy]{0,4})(?!\w)/;
2010-10-03 23:22:42 +00:00
HEREGEX_OMIT = /\s+(?:#.*)?/g;
MULTILINER = /\n/g;
HEREDOC_INDENT = /\n+([^\n\S]*)/g;
2011-01-19 04:10:09 +00:00
HEREDOC_ILLEGAL = /\*\//;
LINE_CONTINUER = /^\s*(?:,|\??\.(?![.\d])|::)/;
2010-10-03 23:22:42 +00:00
TRAILING_SPACES = /\s+$/;
COMPOUND_ASSIGN = ['-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?=', '<<=', '>>=', '>>>=', '&=', '^=', '|='];
UNARY = ['!', '~', 'NEW', 'TYPEOF', 'DELETE', 'DO'];
2010-10-23 18:24:30 +00:00
LOGIC = ['&&', '||', '&', '|', '^'];
SHIFT = ['<<', '>>', '>>>'];
2010-10-23 18:24:30 +00:00
COMPARE = ['==', '!=', '<', '>', '<=', '>='];
MATH = ['*', '/', '%'];
RELATION = ['IN', 'OF', 'INSTANCEOF'];
BOOL = ['TRUE', 'FALSE', 'NULL', 'UNDEFINED'];
2010-10-11 08:05:50 +00:00
NOT_REGEX = ['NUMBER', 'REGEX', 'BOOL', '++', '--', ']'];
NOT_SPACED_REGEX = NOT_REGEX.concat(')', '}', 'THIS', 'IDENTIFIER', 'STRING');
CALLABLE = ['IDENTIFIER', 'STRING', 'REGEX', ')', ']', '}', '?', '::', '@', 'THIS', 'SUPER'];
INDEXABLE = CALLABLE.concat('NUMBER', 'BOOL');
LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR'];
2011-12-14 15:39:20 +00:00
}).call(this);