2010-07-25 03:15:12 -04:00
|
|
|
(function() {
|
2010-09-24 09:38:28 -04:00
|
|
|
var ASSIGNED, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, CONVERSIONS, HEREDOC, HEREDOC_INDENT, IDENTIFIER, JSTOKEN, JS_FORBIDDEN, JS_KEYWORDS, LINE_BREAK, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NEXT_CHARACTER, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX_END, REGEX_ESCAPE, REGEX_INTERPOLATION, REGEX_START, RESERVED, Rewriter, SHIFT, SIMPLESTR, UNARY, WHITESPACE, _ref, compact, count, include, starts;
|
2010-03-30 19:17:40 -04:00
|
|
|
var __slice = Array.prototype.slice;
|
2010-09-21 05:19:49 -04:00
|
|
|
_ref = require('./rewriter');
|
|
|
|
Rewriter = _ref.Rewriter;
|
|
|
|
_ref = require('./helpers').helpers;
|
2010-09-20 22:13:08 -04:00
|
|
|
include = _ref.include;
|
|
|
|
count = _ref.count;
|
|
|
|
starts = _ref.starts;
|
|
|
|
compact = _ref.compact;
|
2010-02-27 19:40:53 -05:00
|
|
|
exports.Lexer = (function() {
|
2010-07-22 17:08:07 -04:00
|
|
|
Lexer = function() {};
|
2010-05-14 23:40:04 -04:00
|
|
|
Lexer.prototype.tokenize = function(code, options) {
|
2010-03-06 16:06:47 -05:00
|
|
|
var o;
|
2010-09-23 01:14:18 -04:00
|
|
|
code = code.replace(/\r/g, '').replace(/\s+$/, '');
|
2010-03-09 20:53:56 -05:00
|
|
|
o = options || {};
|
2010-03-08 23:07:26 -05:00
|
|
|
this.code = code;
|
2010-02-27 19:40:53 -05:00
|
|
|
this.i = 0;
|
2010-03-06 16:06:47 -05:00
|
|
|
this.line = o.line || 0;
|
2010-02-27 19:40:53 -05:00
|
|
|
this.indent = 0;
|
2010-09-08 22:46:13 -04:00
|
|
|
this.indebt = 0;
|
2010-06-01 23:32:46 -04:00
|
|
|
this.outdebt = 0;
|
2010-02-27 19:40:53 -05:00
|
|
|
this.indents = [];
|
|
|
|
this.tokens = [];
|
2010-09-23 01:14:18 -04:00
|
|
|
while ((this.chunk = code.slice(this.i))) {
|
2010-06-12 19:05:13 -04:00
|
|
|
this.extractNextToken();
|
2010-02-27 19:40:53 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
this.closeIndentation();
|
2010-04-11 09:26:21 -04:00
|
|
|
if (o.rewrite === false) {
|
|
|
|
return this.tokens;
|
|
|
|
}
|
2010-09-25 04:39:19 -04:00
|
|
|
return (new Rewriter).rewrite(this.tokens);
|
2010-02-27 19:40:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.extractNextToken = function() {
|
2010-09-23 01:14:18 -04:00
|
|
|
return this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
|
2010-02-27 19:40:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.identifierToken = function() {
|
2010-09-25 03:00:07 -04:00
|
|
|
var closeIndex, forcedIdentifier, id, tag;
|
2010-09-23 01:14:18 -04:00
|
|
|
if (!(id = this.match(IDENTIFIER))) {
|
2010-02-27 19:40:53 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-15 00:54:02 -04:00
|
|
|
this.i += id.length;
|
2010-09-25 03:00:07 -04:00
|
|
|
if (id === 'all' && this.tag() === 'FOR') {
|
|
|
|
this.token('ALL', id);
|
|
|
|
return true;
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
forcedIdentifier = this.tagAccessor() || this.match(ASSIGNED, 1);
|
2010-02-27 19:40:53 -05:00
|
|
|
tag = 'IDENTIFIER';
|
2010-09-23 01:14:18 -04:00
|
|
|
if (include(JS_KEYWORDS, id) || !forcedIdentifier && include(COFFEE_KEYWORDS, id)) {
|
2010-02-27 19:40:53 -05:00
|
|
|
tag = id.toUpperCase();
|
2010-09-23 01:14:18 -04:00
|
|
|
if (tag === 'WHEN' && include(LINE_BREAK, this.tag())) {
|
|
|
|
tag = 'LEADING_WHEN';
|
2010-09-25 03:00:07 -04:00
|
|
|
} else if (include(UNARY, tag)) {
|
|
|
|
tag = 'UNARY';
|
2010-09-23 01:14:18 -04:00
|
|
|
}
|
2010-07-15 21:18:35 -04:00
|
|
|
}
|
2010-09-25 03:00:07 -04:00
|
|
|
if (include(JS_FORBIDDEN, id)) {
|
2010-06-15 00:54:02 -04:00
|
|
|
if (forcedIdentifier) {
|
|
|
|
tag = 'STRING';
|
2010-08-07 08:02:16 -04:00
|
|
|
id = ("\"" + (id) + "\"");
|
2010-06-15 00:54:02 -04:00
|
|
|
if (forcedIdentifier === 'accessor') {
|
2010-09-25 03:00:07 -04:00
|
|
|
closeIndex = true;
|
2010-06-15 01:03:14 -04:00
|
|
|
if (this.tag() !== '@') {
|
|
|
|
this.tokens.pop();
|
|
|
|
}
|
2010-06-15 00:54:02 -04:00
|
|
|
this.token('INDEX_START', '[');
|
|
|
|
}
|
|
|
|
} else if (include(RESERVED, id)) {
|
|
|
|
this.identifierError(id);
|
|
|
|
}
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (!(forcedIdentifier)) {
|
2010-05-31 14:42:30 -04:00
|
|
|
if (include(COFFEE_ALIASES, id)) {
|
|
|
|
tag = (id = CONVERSIONS[id]);
|
2010-03-21 23:24:24 -04:00
|
|
|
}
|
2010-08-11 22:24:43 -04:00
|
|
|
if (id === '!') {
|
|
|
|
tag = 'UNARY';
|
2010-09-23 01:14:18 -04:00
|
|
|
} else if (include(LOGIC, id)) {
|
|
|
|
tag = 'LOGIC';
|
2010-08-11 22:24:43 -04:00
|
|
|
}
|
2010-03-21 21:46:06 -04:00
|
|
|
}
|
|
|
|
this.token(tag, id);
|
2010-09-25 03:00:07 -04:00
|
|
|
if (closeIndex) {
|
2010-06-15 00:54:02 -04:00
|
|
|
this.token(']', ']');
|
|
|
|
}
|
2010-02-27 19:40:53 -05:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.numberToken = function() {
|
2010-05-31 13:40:03 -04:00
|
|
|
var number;
|
2010-09-23 01:14:18 -04:00
|
|
|
if (!(number = this.match(NUMBER))) {
|
2010-02-27 19:40:53 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
if (this.tag() === '.' && number.charAt(0) === '.') {
|
2010-05-31 13:40:03 -04:00
|
|
|
return false;
|
2010-05-19 17:20:33 -04:00
|
|
|
}
|
2010-05-31 13:40:03 -04:00
|
|
|
this.i += number.length;
|
2010-05-19 17:20:33 -04:00
|
|
|
this.token('NUMBER', number);
|
2010-02-27 19:40:53 -05:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.stringToken = function() {
|
2010-09-24 09:38:28 -04:00
|
|
|
var string;
|
|
|
|
switch (this.chunk.charAt(0)) {
|
|
|
|
case "'":
|
|
|
|
if (!(string = this.match(SIMPLESTR))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.token('STRING', string.replace(MULTILINER, '\\\n'));
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
if (!(string = this.balancedToken(['"', '"'], ['#{', '}']))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.interpolateString(string.replace(MULTILINER, '\\\n'));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
2010-02-27 19:40:53 -05:00
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
this.line += count(string, '\n');
|
2010-02-27 19:40:53 -05:00
|
|
|
this.i += string.length;
|
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.heredocToken = function() {
|
2010-09-23 01:14:18 -04:00
|
|
|
var doc, heredoc, match, quote;
|
2010-03-23 00:18:50 -04:00
|
|
|
if (!(match = this.chunk.match(HEREDOC))) {
|
2010-02-27 19:40:53 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
heredoc = match[0];
|
|
|
|
quote = heredoc.charAt(0);
|
|
|
|
doc = this.sanitizeHeredoc(match[2], {
|
2010-05-12 20:56:44 -04:00
|
|
|
quote: quote
|
|
|
|
});
|
2010-08-07 08:02:16 -04:00
|
|
|
this.interpolateString(quote + doc + quote, {
|
2010-07-22 12:38:26 -04:00
|
|
|
heredoc: true
|
|
|
|
});
|
2010-09-23 01:14:18 -04:00
|
|
|
this.line += count(heredoc, '\n');
|
|
|
|
this.i += heredoc.length;
|
2010-02-27 19:40:53 -05:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.commentToken = function() {
|
2010-09-23 01:14:18 -04:00
|
|
|
var _ref2, comment, here, match;
|
2010-05-12 20:56:44 -04:00
|
|
|
if (!(match = this.chunk.match(COMMENT))) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
_ref2 = match;
|
|
|
|
comment = _ref2[0];
|
|
|
|
here = _ref2[1];
|
|
|
|
this.line += count(comment, '\n');
|
|
|
|
this.i += comment.length;
|
|
|
|
if (here) {
|
|
|
|
this.token('HERECOMMENT', this.sanitizeHeredoc(here, {
|
2010-08-23 21:00:47 -04:00
|
|
|
herecomment: true,
|
2010-09-21 23:17:43 -04:00
|
|
|
indent: Array(this.indent + 1).join(' ')
|
2010-08-23 20:27:34 -04:00
|
|
|
}));
|
2010-07-01 21:26:33 -04:00
|
|
|
this.token('TERMINATOR', '\n');
|
|
|
|
}
|
2010-05-12 20:56:44 -04:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.jsToken = function() {
|
2010-03-08 20:07:19 -05:00
|
|
|
var script;
|
2010-09-24 09:38:28 -04:00
|
|
|
if (!(this.chunk.charAt(0) === '`' && (script = this.match(JSTOKEN)))) {
|
2010-02-27 19:40:53 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
this.token('JS', script.slice(1, -1));
|
2010-02-27 19:40:53 -05:00
|
|
|
this.i += script.length;
|
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.regexToken = function() {
|
2010-09-20 22:13:08 -04:00
|
|
|
var _ref2, end, first, flags, regex, str;
|
2010-08-12 20:24:53 -04:00
|
|
|
if (!(first = this.chunk.match(REGEX_START))) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-20 22:13:08 -04:00
|
|
|
if (first[1] === ' ' && !('CALL_START' === (_ref2 = this.tag()) || '=' === _ref2)) {
|
2010-03-08 19:43:12 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-03-14 17:58:32 -04:00
|
|
|
if (include(NOT_REGEX, this.tag())) {
|
2010-03-08 13:05:02 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (!(regex = this.balancedToken(['/', '/']))) {
|
2010-02-27 19:40:53 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
if (!(end = this.chunk.slice(regex.length).match(REGEX_END))) {
|
2010-05-14 09:14:41 -04:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
flags = end[0];
|
|
|
|
if (REGEX_INTERPOLATION.test(regex)) {
|
|
|
|
str = regex.slice(1, -1);
|
|
|
|
str = str.replace(REGEX_ESCAPE, '\\$&');
|
2010-09-25 04:39:19 -04:00
|
|
|
this.tokens.push(['(', '('], ['IDENTIFIER', 'RegExp'], ['CALL_START', '(']);
|
2010-08-14 17:25:29 -04:00
|
|
|
this.interpolateString("\"" + (str) + "\"", {
|
2010-07-22 12:38:26 -04:00
|
|
|
escapeQuotes: true
|
|
|
|
});
|
2010-07-21 13:32:36 -04:00
|
|
|
if (flags) {
|
2010-09-23 01:14:18 -04:00
|
|
|
this.tokens.push([',', ','], ['STRING', ("\"" + (flags) + "\"")]);
|
2010-07-21 13:32:36 -04:00
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
this.tokens.push([')', ')'], [')', ')']);
|
2010-03-08 13:05:02 -05:00
|
|
|
} else {
|
2010-09-23 01:14:18 -04:00
|
|
|
this.token('REGEX', regex + flags);
|
2010-03-08 13:05:02 -05:00
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
this.i += regex.length + flags.length;
|
2010-02-27 19:40:53 -05:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.balancedToken = function() {
|
2010-03-07 07:56:27 -05:00
|
|
|
var delimited;
|
2010-07-25 03:15:12 -04:00
|
|
|
delimited = __slice.call(arguments, 0);
|
2010-06-28 00:19:58 -04:00
|
|
|
return this.balancedString(this.chunk, delimited);
|
2010-03-06 15:16:37 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.lineToken = function() {
|
|
|
|
var diff, indent, nextCharacter, noNewlines, prev, size;
|
2010-09-23 01:14:18 -04:00
|
|
|
if (!(indent = this.match(MULTI_DENT))) {
|
2010-02-27 19:40:53 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
this.line += count(indent, '\n');
|
2010-02-27 19:40:53 -05:00
|
|
|
this.i += indent.length;
|
|
|
|
prev = this.prev(2);
|
2010-09-23 01:14:18 -04:00
|
|
|
size = indent.length - 1 - indent.lastIndexOf('\n');
|
2010-06-27 12:59:54 -04:00
|
|
|
nextCharacter = this.match(NEXT_CHARACTER, 1);
|
2010-06-12 19:05:13 -04:00
|
|
|
noNewlines = nextCharacter === '.' || nextCharacter === ',' || this.unfinished();
|
2010-09-08 22:46:13 -04:00
|
|
|
if (size - this.indebt === this.indent) {
|
2010-06-12 19:05:13 -04:00
|
|
|
if (noNewlines) {
|
|
|
|
return this.suppressNewlines();
|
2010-02-28 12:49:37 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.newlineToken(indent);
|
2010-02-28 12:49:37 -05:00
|
|
|
} else if (size > this.indent) {
|
2010-06-12 19:05:13 -04:00
|
|
|
if (noNewlines) {
|
2010-09-08 22:46:13 -04:00
|
|
|
this.indebt = size - this.indent;
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.suppressNewlines();
|
2010-02-28 12:49:37 -05:00
|
|
|
}
|
2010-08-21 12:13:43 -04:00
|
|
|
diff = size - this.indent + this.outdebt;
|
2010-02-27 19:40:53 -05:00
|
|
|
this.token('INDENT', diff);
|
|
|
|
this.indents.push(diff);
|
2010-09-08 22:46:13 -04:00
|
|
|
this.outdebt = (this.indebt = 0);
|
2010-02-27 19:40:53 -05:00
|
|
|
} else {
|
2010-09-08 22:46:13 -04:00
|
|
|
this.indebt = 0;
|
2010-06-12 19:05:13 -04:00
|
|
|
this.outdentToken(this.indent - size, noNewlines);
|
2010-02-27 19:40:53 -05:00
|
|
|
}
|
|
|
|
this.indent = size;
|
|
|
|
return true;
|
|
|
|
};
|
2010-07-30 22:06:22 -04:00
|
|
|
Lexer.prototype.outdentToken = function(moveOut, noNewlines, close) {
|
|
|
|
var dent, len;
|
|
|
|
while (moveOut > 0) {
|
|
|
|
len = this.indents.length - 1;
|
|
|
|
if (this.indents[len] === undefined) {
|
|
|
|
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();
|
|
|
|
dent -= this.outdebt;
|
|
|
|
moveOut -= dent;
|
|
|
|
this.outdebt = 0;
|
|
|
|
this.token('OUTDENT', dent);
|
2010-06-01 23:32:46 -04:00
|
|
|
}
|
|
|
|
}
|
2010-07-30 22:06:22 -04:00
|
|
|
if (dent) {
|
|
|
|
this.outdebt -= moveOut;
|
2010-02-27 19:40:53 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (!(this.tag() === 'TERMINATOR' || noNewlines)) {
|
2010-09-23 01:14:18 -04:00
|
|
|
this.token('TERMINATOR', '\n');
|
2010-02-27 19:40:53 -05:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.whitespaceToken = function() {
|
2010-02-27 19:40:53 -05:00
|
|
|
var prev, space;
|
2010-09-23 01:14:18 -04:00
|
|
|
if (!(space = this.match(WHITESPACE))) {
|
2010-02-27 19:40:53 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
prev = this.prev();
|
|
|
|
if (prev) {
|
|
|
|
prev.spaced = true;
|
|
|
|
}
|
|
|
|
this.i += space.length;
|
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.newlineToken = function(newlines) {
|
2010-08-15 15:13:33 -04:00
|
|
|
if (this.tag() !== 'TERMINATOR') {
|
2010-09-23 01:14:18 -04:00
|
|
|
this.token('TERMINATOR', '\n');
|
2010-02-27 19:40:53 -05:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.suppressNewlines = function() {
|
2010-09-23 01:14:18 -04:00
|
|
|
if (this.value() === '\\') {
|
2010-02-27 19:40:53 -05:00
|
|
|
this.tokens.pop();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.literalToken = function() {
|
2010-09-20 22:13:08 -04:00
|
|
|
var _ref2, match, prev, space, spaced, tag, value;
|
2010-09-23 01:14:18 -04:00
|
|
|
if (match = this.chunk.match(OPERATOR)) {
|
|
|
|
_ref2 = match;
|
|
|
|
value = _ref2[0];
|
|
|
|
space = _ref2[1];
|
|
|
|
if (CODE.test(value)) {
|
|
|
|
this.tagParameters();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value = this.chunk.charAt(0);
|
2010-02-27 19:40:53 -05:00
|
|
|
}
|
2010-07-25 01:36:50 -04:00
|
|
|
this.i += value.length;
|
2010-08-25 14:31:56 -04:00
|
|
|
spaced = (prev = this.prev()) && prev.spaced;
|
2010-02-27 19:40:53 -05:00
|
|
|
tag = value;
|
2010-07-25 01:36:50 -04:00
|
|
|
if (value === '=') {
|
|
|
|
if (include(JS_FORBIDDEN, this.value())) {
|
|
|
|
this.assignmentError();
|
|
|
|
}
|
2010-09-20 22:13:08 -04:00
|
|
|
if (('or' === (_ref2 = this.value()) || 'and' === _ref2)) {
|
2010-08-25 14:31:56 -04:00
|
|
|
this.tokens.splice(this.tokens.length - 1, 1, ['COMPOUND_ASSIGN', CONVERSIONS[this.value()] + '=', prev[2]]);
|
2010-08-11 22:24:43 -04:00
|
|
|
return true;
|
2010-07-25 01:36:50 -04:00
|
|
|
}
|
2010-07-25 01:23:37 -04:00
|
|
|
}
|
|
|
|
if (value === ';') {
|
2010-02-27 19:40:53 -05:00
|
|
|
tag = 'TERMINATOR';
|
2010-08-14 16:33:20 -04:00
|
|
|
} else if (include(LOGIC, value)) {
|
2010-08-11 22:24:43 -04:00
|
|
|
tag = 'LOGIC';
|
|
|
|
} else if (include(MATH, value)) {
|
|
|
|
tag = 'MATH';
|
|
|
|
} else if (include(COMPARE, value)) {
|
|
|
|
tag = 'COMPARE';
|
|
|
|
} else if (include(COMPOUND_ASSIGN, value)) {
|
|
|
|
tag = 'COMPOUND_ASSIGN';
|
|
|
|
} else if (include(UNARY, value)) {
|
|
|
|
tag = 'UNARY';
|
|
|
|
} else if (include(SHIFT, value)) {
|
|
|
|
tag = 'SHIFT';
|
|
|
|
} else if (include(CALLABLE, this.tag()) && !spaced) {
|
2010-04-03 09:58:45 -04:00
|
|
|
if (value === '(') {
|
2010-08-25 14:31:56 -04:00
|
|
|
if (prev[0] === '?') {
|
|
|
|
prev[0] = 'FUNC_EXIST';
|
|
|
|
}
|
2010-02-27 19:40:53 -05:00
|
|
|
tag = 'CALL_START';
|
2010-05-31 22:32:43 -04:00
|
|
|
} else if (value === '[') {
|
2010-02-27 19:40:53 -05:00
|
|
|
tag = 'INDEX_START';
|
2010-09-23 01:14:18 -04:00
|
|
|
switch (this.tag()) {
|
|
|
|
case '?':
|
|
|
|
this.tag(1, 'INDEX_SOAK');
|
|
|
|
break;
|
|
|
|
case '::':
|
|
|
|
this.tag(1, 'INDEX_PROTO');
|
|
|
|
break;
|
2010-05-31 22:32:43 -04:00
|
|
|
}
|
2010-02-27 19:40:53 -05:00
|
|
|
}
|
|
|
|
}
|
2010-03-21 23:24:24 -04:00
|
|
|
this.token(tag, value);
|
2010-02-27 19:40:53 -05:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.tagAccessor = function() {
|
2010-06-15 00:54:02 -04:00
|
|
|
var accessor, prev;
|
2010-05-31 13:25:06 -04:00
|
|
|
if ((!(prev = this.prev())) || (prev && prev.spaced)) {
|
|
|
|
return false;
|
2010-02-28 13:34:52 -05:00
|
|
|
}
|
2010-06-15 00:54:02 -04:00
|
|
|
accessor = (function() {
|
|
|
|
if (prev[1] === '::') {
|
|
|
|
return this.tag(1, 'PROTOTYPE_ACCESS');
|
2010-09-23 01:14:18 -04:00
|
|
|
} else if (prev[1] === '.' && this.value(2) !== '.') {
|
2010-06-15 00:54:02 -04:00
|
|
|
if (this.tag(2) === '?') {
|
|
|
|
this.tag(1, 'SOAK_ACCESS');
|
|
|
|
return this.tokens.splice(-2, 1);
|
|
|
|
} else {
|
|
|
|
return this.tag(1, 'PROPERTY_ACCESS');
|
|
|
|
}
|
2010-02-28 13:34:52 -05:00
|
|
|
} else {
|
2010-06-15 00:54:02 -04:00
|
|
|
return prev[0] === '@';
|
2010-02-28 13:34:52 -05:00
|
|
|
}
|
2010-06-15 00:54:02 -04:00
|
|
|
}).call(this);
|
2010-07-06 23:04:35 -04:00
|
|
|
return accessor ? 'accessor' : false;
|
2010-02-28 13:34:52 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.sanitizeHeredoc = function(doc, options) {
|
2010-09-20 22:13:08 -04:00
|
|
|
var _ref2, attempt, indent, match;
|
2010-09-15 22:08:13 -04:00
|
|
|
indent = options.indent;
|
2010-08-23 21:00:47 -04:00
|
|
|
if (options.herecomment && !include(doc, '\n')) {
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
if (!(options.herecomment)) {
|
2010-09-23 01:14:18 -04:00
|
|
|
while ((match = HEREDOC_INDENT.exec(doc))) {
|
|
|
|
attempt = (typeof (_ref2 = match[1]) !== "undefined" && _ref2 !== null) ? match[1] : match[2];
|
2010-09-22 09:47:43 -04:00
|
|
|
if (!(typeof indent !== "undefined" && indent !== null) || (0 < attempt.length) && (attempt.length < indent.length)) {
|
2010-08-23 21:00:47 -04:00
|
|
|
indent = attempt;
|
|
|
|
}
|
2010-05-12 21:47:31 -04:00
|
|
|
}
|
|
|
|
}
|
2010-09-15 22:08:13 -04:00
|
|
|
indent || (indent = '');
|
2010-09-23 01:14:18 -04:00
|
|
|
doc = doc.replace(new RegExp('^' + indent, 'gm'), '');
|
2010-07-01 21:26:33 -04:00
|
|
|
if (options.herecomment) {
|
|
|
|
return doc;
|
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
return doc.replace(/^\n/, '').replace(MULTILINER, '\\n').replace(new RegExp(options.quote, 'g'), "\\" + (options.quote));
|
2010-02-28 13:34:52 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.tagParameters = function() {
|
2010-09-15 23:46:01 -04:00
|
|
|
var i, tok;
|
2010-02-28 20:44:33 -05:00
|
|
|
if (this.tag() !== ')') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
while (true) {
|
|
|
|
i += 1;
|
|
|
|
tok = this.prev(i);
|
|
|
|
if (!tok) {
|
|
|
|
return null;
|
|
|
|
}
|
2010-09-15 23:46:01 -04:00
|
|
|
switch (tok[0]) {
|
2010-09-21 23:58:05 -04:00
|
|
|
case 'IDENTIFIER':
|
|
|
|
tok[0] = 'PARAM';
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
tok[0] = 'PARAM_END';
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
case 'CALL_START':
|
|
|
|
return (tok[0] = 'PARAM_START');
|
2010-02-28 20:44:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.closeIndentation = function() {
|
|
|
|
return this.outdentToken(this.indent);
|
2010-02-28 20:44:33 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.identifierError = function(word) {
|
2010-08-14 17:25:29 -04:00
|
|
|
throw new Error("SyntaxError: Reserved word \"" + (word) + "\" on line " + (this.line + 1));
|
2010-02-28 13:34:52 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.assignmentError = function() {
|
2010-08-14 17:25:29 -04:00
|
|
|
throw new Error("SyntaxError: Reserved word \"" + (this.value()) + "\" on line " + (this.line + 1) + " can't be assigned");
|
2010-02-28 13:34:52 -05:00
|
|
|
};
|
2010-06-28 00:19:58 -04:00
|
|
|
Lexer.prototype.balancedString = function(str, delimited, options) {
|
2010-09-23 01:14:18 -04:00
|
|
|
var _i, _len, _ref2, _ref3, close, i, levels, open, pair, slash, slen;
|
2010-08-14 18:02:07 -04:00
|
|
|
options || (options = {});
|
2010-06-28 00:19:58 -04:00
|
|
|
slash = delimited[0][0] === '/';
|
|
|
|
levels = [];
|
|
|
|
i = 0;
|
2010-09-23 01:14:18 -04:00
|
|
|
slen = str.length;
|
|
|
|
while (i < slen) {
|
|
|
|
if (levels.length && str.charAt(i) === '\\') {
|
2010-06-28 00:19:58 -04:00
|
|
|
i += 1;
|
|
|
|
} else {
|
2010-09-20 22:13:08 -04:00
|
|
|
_ref2 = delimited;
|
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
|
|
|
pair = _ref2[_i];
|
|
|
|
_ref3 = pair;
|
|
|
|
open = _ref3[0];
|
|
|
|
close = _ref3[1];
|
2010-06-28 00:19:58 -04:00
|
|
|
if (levels.length && starts(str, close, i) && levels[levels.length - 1] === pair) {
|
|
|
|
levels.pop();
|
|
|
|
i += close.length - 1;
|
|
|
|
if (!(levels.length)) {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else if (starts(str, open, i)) {
|
|
|
|
levels.push(pair);
|
|
|
|
i += open.length - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
if (!levels.length || slash && str.charAt(i) === '\n') {
|
2010-06-28 00:19:58 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
if (levels.length) {
|
|
|
|
if (slash) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-14 17:25:29 -04:00
|
|
|
throw new Error("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
|
2010-06-28 00:19:58 -04:00
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
return !i ? false : str.slice(0, i);
|
2010-06-28 00:19:58 -04:00
|
|
|
};
|
2010-07-22 12:38:26 -04:00
|
|
|
Lexer.prototype.interpolateString = function(str, options) {
|
2010-09-23 01:14:18 -04:00
|
|
|
var _len, _ref2, _ref3, end, escaped, expr, i, idx, inner, interpolated, lexer, nested, pi, quote, tag, tok, token, tokens, value;
|
2010-08-14 18:02:07 -04:00
|
|
|
options || (options = {});
|
2010-09-23 01:14:18 -04:00
|
|
|
if (str.length < 3 || str.charAt(0) !== '"') {
|
2010-03-05 20:42:36 -05:00
|
|
|
return this.token('STRING', str);
|
2010-03-05 17:30:49 -05:00
|
|
|
} else {
|
2010-09-25 04:39:19 -04:00
|
|
|
lexer = new Lexer;
|
2010-03-05 17:30:49 -05:00
|
|
|
tokens = [];
|
2010-09-23 01:14:18 -04:00
|
|
|
quote = str.charAt(0);
|
2010-09-20 22:13:08 -04:00
|
|
|
_ref2 = [1, 1];
|
|
|
|
i = _ref2[0];
|
|
|
|
pi = _ref2[1];
|
2010-09-23 01:14:18 -04:00
|
|
|
end = str.length - 1;
|
|
|
|
while (i < end) {
|
|
|
|
if (str.charAt(i) === '\\') {
|
2010-03-07 07:56:27 -05:00
|
|
|
i += 1;
|
2010-09-23 01:14:18 -04:00
|
|
|
} else if (expr = this.balancedString(str.slice(i), [['#{', '}']])) {
|
2010-03-07 12:47:03 -05:00
|
|
|
if (pi < i) {
|
2010-09-23 01:14:18 -04:00
|
|
|
tokens.push(['STRING', quote + str.slice(pi, i) + quote]);
|
2010-03-07 12:47:03 -05:00
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
inner = expr.slice(2, -1);
|
2010-03-08 02:31:31 -05:00
|
|
|
if (inner.length) {
|
2010-07-22 12:38:26 -04:00
|
|
|
if (options.heredoc) {
|
2010-07-21 18:45:25 -04:00
|
|
|
inner = inner.replace(new RegExp('\\\\' + quote, 'g'), quote);
|
|
|
|
}
|
2010-08-14 17:25:29 -04:00
|
|
|
nested = lexer.tokenize("(" + (inner) + ")", {
|
2010-03-08 02:31:31 -05:00
|
|
|
line: this.line
|
|
|
|
});
|
2010-09-20 22:13:08 -04:00
|
|
|
_ref2 = nested;
|
|
|
|
for (idx = 0, _len = _ref2.length; idx < _len; idx++) {
|
|
|
|
tok = _ref2[idx];
|
2010-08-11 00:40:15 -04:00
|
|
|
if (tok[0] === 'CALL_END') {
|
|
|
|
(tok[0] = ')');
|
2010-08-14 11:42:19 -04:00
|
|
|
}
|
2010-04-11 05:22:54 -04:00
|
|
|
}
|
2010-03-08 02:31:31 -05:00
|
|
|
nested.pop();
|
|
|
|
tokens.push(['TOKENS', nested]);
|
|
|
|
} else {
|
2010-08-07 08:02:16 -04:00
|
|
|
tokens.push(['STRING', quote + quote]);
|
2010-03-08 02:31:31 -05:00
|
|
|
}
|
2010-03-07 12:47:03 -05:00
|
|
|
i += expr.length - 1;
|
|
|
|
pi = i + 1;
|
2010-03-05 17:30:49 -05:00
|
|
|
}
|
2010-03-07 07:56:27 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
2010-09-23 01:14:18 -04:00
|
|
|
if ((i > pi) && (pi < str.length - 1)) {
|
|
|
|
tokens.push(['STRING', quote + str.slice(pi, i) + quote]);
|
2010-03-05 17:30:49 -05:00
|
|
|
}
|
2010-08-15 15:13:33 -04:00
|
|
|
if (tokens[0][0] !== 'STRING') {
|
2010-03-23 00:18:50 -04:00
|
|
|
tokens.unshift(['STRING', '""']);
|
2010-03-08 13:05:02 -05:00
|
|
|
}
|
2010-04-04 02:59:44 -04:00
|
|
|
interpolated = tokens.length > 1;
|
|
|
|
if (interpolated) {
|
|
|
|
this.token('(', '(');
|
|
|
|
}
|
2010-09-20 22:13:08 -04:00
|
|
|
_ref2 = tokens;
|
|
|
|
for (i = 0, _len = _ref2.length; i < _len; i++) {
|
|
|
|
token = _ref2[i];
|
|
|
|
_ref3 = token;
|
|
|
|
tag = _ref3[0];
|
|
|
|
value = _ref3[1];
|
2010-03-08 21:55:06 -05:00
|
|
|
if (tag === 'TOKENS') {
|
|
|
|
this.tokens = this.tokens.concat(value);
|
2010-07-22 12:38:26 -04:00
|
|
|
} else if (tag === 'STRING' && options.escapeQuotes) {
|
2010-09-23 01:14:18 -04:00
|
|
|
escaped = value.slice(1, -1).replace(/"/g, '\\"');
|
2010-08-14 17:25:29 -04:00
|
|
|
this.token(tag, "\"" + (escaped) + "\"");
|
2010-03-08 21:55:06 -05:00
|
|
|
} else {
|
|
|
|
this.token(tag, value);
|
|
|
|
}
|
|
|
|
if (i < tokens.length - 1) {
|
|
|
|
this.token('+', '+');
|
2010-03-08 13:05:02 -05:00
|
|
|
}
|
2010-03-05 17:30:49 -05:00
|
|
|
}
|
2010-04-04 02:59:44 -04:00
|
|
|
if (interpolated) {
|
|
|
|
this.token(')', ')');
|
|
|
|
}
|
2010-03-08 13:05:02 -05:00
|
|
|
return tokens;
|
2010-03-05 17:30:49 -05:00
|
|
|
}
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Lexer.prototype.token = function(tag, value) {
|
2010-02-27 19:40:53 -05:00
|
|
|
return this.tokens.push([tag, value, this.line]);
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Lexer.prototype.tag = function(index, newTag) {
|
2010-02-27 19:40:53 -05:00
|
|
|
var tok;
|
2010-03-23 00:18:50 -04:00
|
|
|
if (!(tok = this.prev(index))) {
|
2010-01-30 15:56:40 -05:00
|
|
|
return null;
|
|
|
|
}
|
2010-08-14 17:52:37 -04:00
|
|
|
if (typeof newTag !== "undefined" && newTag !== null) {
|
2010-07-25 03:15:12 -04:00
|
|
|
return (tok[0] = newTag);
|
2010-02-27 19:40:53 -05:00
|
|
|
}
|
|
|
|
return tok[0];
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Lexer.prototype.value = function(index, val) {
|
2010-02-27 19:40:53 -05:00
|
|
|
var tok;
|
2010-03-23 00:18:50 -04:00
|
|
|
if (!(tok = this.prev(index))) {
|
2010-02-27 19:40:53 -05:00
|
|
|
return null;
|
|
|
|
}
|
2010-08-14 17:52:37 -04:00
|
|
|
if (typeof val !== "undefined" && val !== null) {
|
2010-07-25 03:15:12 -04:00
|
|
|
return (tok[1] = val);
|
2010-02-27 19:40:53 -05:00
|
|
|
}
|
|
|
|
return tok[1];
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Lexer.prototype.prev = function(index) {
|
2010-02-27 19:40:53 -05:00
|
|
|
return this.tokens[this.tokens.length - (index || 1)];
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Lexer.prototype.match = function(regex, index) {
|
2010-02-27 19:40:53 -05:00
|
|
|
var m;
|
2010-09-23 01:14:18 -04:00
|
|
|
return (m = this.chunk.match(regex)) ? m[index || 0] : false;
|
2010-02-27 19:40:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Lexer.prototype.unfinished = function() {
|
2010-09-23 01:14:18 -04:00
|
|
|
var prev, value;
|
2010-03-20 00:58:25 -04:00
|
|
|
prev = this.prev(2);
|
2010-09-23 01:14:18 -04:00
|
|
|
value = this.value();
|
|
|
|
return value && NO_NEWLINE.test(value) && prev && prev[0] !== '.' && !CODE.test(value) && !ASSIGNED.test(this.chunk);
|
2010-03-20 00:58:25 -04:00
|
|
|
};
|
2010-02-27 19:40:53 -05:00
|
|
|
return Lexer;
|
2010-06-26 13:36:31 -04:00
|
|
|
})();
|
2010-09-23 01:14:18 -04:00
|
|
|
JS_KEYWORDS = ['if', 'else', 'true', 'false', 'new', 'return', 'try', 'catch', 'finally', 'throw', 'break', 'continue', 'for', 'in', 'while', 'delete', 'instanceof', 'typeof', 'switch', 'super', 'extends', 'class', 'this', 'null', 'debugger'];
|
|
|
|
COFFEE_ALIASES = ['and', 'or', 'is', 'isnt', 'not'];
|
|
|
|
COFFEE_KEYWORDS = COFFEE_ALIASES.concat(['then', 'unless', 'until', 'loop', 'yes', 'no', 'on', 'off', 'of', 'by', 'where', 'when']);
|
|
|
|
RESERVED = ['case', 'default', 'do', 'function', 'var', 'void', 'with', 'const', 'let', 'enum', 'export', 'import', 'native', '__hasProp', '__extends', '__slice'];
|
2010-03-07 14:41:52 -05:00
|
|
|
JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED);
|
2010-09-23 01:14:18 -04:00
|
|
|
IDENTIFIER = /^[a-zA-Z_$][\w$]*/;
|
|
|
|
NUMBER = /^(?:0x[\da-f]+)|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i;
|
|
|
|
HEREDOC = /^("""|''')([\s\S]*?)\n?[ \t]*\1/;
|
|
|
|
OPERATOR = /^(?:-[-=>]?|\+[+=]?|[*&|\/%=<>^:!?]+)(?=([ \t]*))/;
|
|
|
|
WHITESPACE = /^[ \t]+/;
|
|
|
|
COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#])[^\n]*)+/;
|
|
|
|
CODE = /^[-=]>/;
|
|
|
|
MULTI_DENT = /^(?:\n[ \t]*)+/;
|
2010-09-24 09:38:28 -04:00
|
|
|
SIMPLESTR = /^'[^\\']*(?:\\.[^\\']*)*'/;
|
|
|
|
JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/;
|
2010-08-12 20:24:53 -04:00
|
|
|
REGEX_START = /^\/([^\/])/;
|
2010-09-23 01:14:18 -04:00
|
|
|
REGEX_INTERPOLATION = /[^\\]#\{.*[^\\]\}/;
|
|
|
|
REGEX_END = /^[imgy]{0,4}(?![a-zA-Z])/;
|
|
|
|
REGEX_ESCAPE = /\\[^#]/g;
|
2010-03-07 14:41:52 -05:00
|
|
|
MULTILINER = /\n/g;
|
2010-09-25 04:39:19 -04:00
|
|
|
NO_NEWLINE = /^(?:[-+*&|\/%=<>!.\\][<>=&|]*|and|or|is(?:nt)?|n(?:ot|ew)|delete|typeof|instanceof)$/;
|
2010-09-23 01:14:18 -04:00
|
|
|
HEREDOC_INDENT = /\n+([ \t]*)|^([ \t]+)/g;
|
|
|
|
ASSIGNED = /^\s*((?:[a-zA-Z$_@]\w*|["'][^\n]+?["']|\d+)[ \t]*?[:=][^:=])/;
|
2010-06-11 08:29:16 -04:00
|
|
|
NEXT_CHARACTER = /^\s*(\S)/;
|
2010-08-11 22:24:43 -04:00
|
|
|
COMPOUND_ASSIGN = ['-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?=', '<<=', '>>=', '>>>=', '&=', '^=', '|='];
|
2010-09-25 04:39:19 -04:00
|
|
|
UNARY = ['UMINUS', 'UPLUS', '!', '!!', '~', 'NEW', 'TYPEOF', 'DELETE'];
|
2010-08-11 22:24:43 -04:00
|
|
|
LOGIC = ['&', '|', '^', '&&', '||'];
|
|
|
|
SHIFT = ['<<', '>>', '>>>'];
|
|
|
|
COMPARE = ['<=', '<', '>', '>='];
|
|
|
|
MATH = ['*', '/', '%'];
|
2010-05-04 23:50:22 -04:00
|
|
|
NOT_REGEX = ['NUMBER', 'REGEX', '++', '--', 'FALSE', 'NULL', 'TRUE', ']'];
|
2010-05-31 22:32:43 -04:00
|
|
|
CALLABLE = ['IDENTIFIER', 'SUPER', ')', ']', '}', 'STRING', '@', 'THIS', '?', '::'];
|
2010-03-20 00:58:25 -04:00
|
|
|
LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR'];
|
2010-05-31 14:42:30 -04:00
|
|
|
CONVERSIONS = {
|
|
|
|
'and': '&&',
|
|
|
|
'or': '||',
|
|
|
|
'is': '==',
|
|
|
|
'isnt': '!=',
|
2010-07-25 01:36:50 -04:00
|
|
|
'not': '!',
|
|
|
|
'===': '=='
|
2010-05-31 14:42:30 -04:00
|
|
|
};
|
2010-09-21 03:53:58 -04:00
|
|
|
}).call(this);
|