mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-19 03:44:23 -05:00
Merge branch 'lexer-refactor' of http://github.com/satyr/coffee-script
This commit is contained in:
466
lib/lexer.js
466
lib/lexer.js
@@ -1,5 +1,5 @@
|
||||
(function() {
|
||||
var ASSIGNED, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, CONVERSIONS, HEREDOC, HEREDOC_INDENT, IDENTIFIER, JS_CLEANER, JS_FORBIDDEN, JS_KEYWORDS, LAST_DENT, LAST_DENTS, 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, UNARY, WHITESPACE, _ref, compact, count, include, starts;
|
||||
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;
|
||||
var __slice = Array.prototype.slice;
|
||||
_ref = require('./rewriter');
|
||||
Rewriter = _ref.Rewriter;
|
||||
@@ -12,7 +12,7 @@
|
||||
Lexer = function() {};
|
||||
Lexer.prototype.tokenize = function(code, options) {
|
||||
var o;
|
||||
code = code.replace(/(\r|\s+$)/g, '');
|
||||
code = code.replace(/\r/g, '').replace(/\s+$/, '');
|
||||
o = options || {};
|
||||
this.code = code;
|
||||
this.i = 0;
|
||||
@@ -22,9 +22,8 @@
|
||||
this.outdebt = 0;
|
||||
this.indents = [];
|
||||
this.tokens = [];
|
||||
while (this.i < this.code.length) {
|
||||
this.chunk = this.code.slice(this.i);
|
||||
this.extractNextToken();
|
||||
while ((this.chunk = code.slice(this.i))) {
|
||||
this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
|
||||
}
|
||||
this.closeIndentation();
|
||||
if (o.rewrite === false) {
|
||||
@@ -32,62 +31,33 @@
|
||||
}
|
||||
return (new Rewriter()).rewrite(this.tokens);
|
||||
};
|
||||
Lexer.prototype.extractNextToken = function() {
|
||||
if (this.identifierToken()) {
|
||||
return null;
|
||||
}
|
||||
if (this.commentToken()) {
|
||||
return null;
|
||||
}
|
||||
if (this.whitespaceToken()) {
|
||||
return null;
|
||||
}
|
||||
if (this.lineToken()) {
|
||||
return null;
|
||||
}
|
||||
if (this.heredocToken()) {
|
||||
return null;
|
||||
}
|
||||
if (this.stringToken()) {
|
||||
return null;
|
||||
}
|
||||
if (this.numberToken()) {
|
||||
return null;
|
||||
}
|
||||
if (this.regexToken()) {
|
||||
return null;
|
||||
}
|
||||
if (this.jsToken()) {
|
||||
return null;
|
||||
}
|
||||
return this.literalToken();
|
||||
};
|
||||
Lexer.prototype.identifierToken = function() {
|
||||
var close_index, forcedIdentifier, id, tag;
|
||||
if (!(id = this.match(IDENTIFIER, 1))) {
|
||||
var closeIndex, forcedIdentifier, id, match, tag;
|
||||
if (!(match = IDENTIFIER.exec(this.chunk))) {
|
||||
return false;
|
||||
}
|
||||
id = match[0];
|
||||
this.i += id.length;
|
||||
forcedIdentifier = this.tagAccessor() || this.match(ASSIGNED, 1);
|
||||
tag = 'IDENTIFIER';
|
||||
if (include(JS_KEYWORDS, id) || (!forcedIdentifier && include(COFFEE_KEYWORDS, id))) {
|
||||
tag = id.toUpperCase();
|
||||
}
|
||||
if (tag === 'WHEN' && include(LINE_BREAK, this.tag())) {
|
||||
tag = 'LEADING_WHEN';
|
||||
}
|
||||
if (id === 'all' && this.tag() === 'FOR') {
|
||||
tag = 'ALL';
|
||||
this.token('ALL', id);
|
||||
return true;
|
||||
}
|
||||
if (include(UNARY, tag)) {
|
||||
tag = 'UNARY';
|
||||
forcedIdentifier = this.tagAccessor() || ASSIGNED.test(this.chunk);
|
||||
tag = 'IDENTIFIER';
|
||||
if (include(JS_KEYWORDS, id) || !forcedIdentifier && include(COFFEE_KEYWORDS, id)) {
|
||||
tag = id.toUpperCase();
|
||||
if (tag === 'WHEN' && include(LINE_BREAK, this.tag())) {
|
||||
tag = 'LEADING_WHEN';
|
||||
} else if (include(UNARY, tag)) {
|
||||
tag = 'UNARY';
|
||||
}
|
||||
}
|
||||
if (include(JS_FORBIDDEN, id)) {
|
||||
if (forcedIdentifier) {
|
||||
tag = 'STRING';
|
||||
id = ("\"" + (id) + "\"");
|
||||
if (forcedIdentifier === 'accessor') {
|
||||
close_index = true;
|
||||
closeIndex = true;
|
||||
if (this.tag() !== '@') {
|
||||
this.tokens.pop();
|
||||
}
|
||||
@@ -101,25 +71,25 @@
|
||||
if (include(COFFEE_ALIASES, id)) {
|
||||
tag = (id = CONVERSIONS[id]);
|
||||
}
|
||||
if (include(LOGIC, id)) {
|
||||
tag = 'LOGIC';
|
||||
}
|
||||
if (id === '!') {
|
||||
tag = 'UNARY';
|
||||
} else if (include(LOGIC, id)) {
|
||||
tag = 'LOGIC';
|
||||
}
|
||||
}
|
||||
this.token(tag, id);
|
||||
if (close_index) {
|
||||
if (closeIndex) {
|
||||
this.token(']', ']');
|
||||
}
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.numberToken = function() {
|
||||
var number;
|
||||
if (!(number = this.match(NUMBER, 1))) {
|
||||
var match, number;
|
||||
if (!(match = NUMBER.exec(this.chunk))) {
|
||||
return false;
|
||||
}
|
||||
if (this.tag() === '.' && starts(number, '.')) {
|
||||
number = match[0];
|
||||
if (this.tag() === '.' && number.charAt(0) === '.') {
|
||||
return false;
|
||||
}
|
||||
this.i += number.length;
|
||||
@@ -127,43 +97,57 @@
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.stringToken = function() {
|
||||
var string;
|
||||
if (!(starts(this.chunk, '"') || starts(this.chunk, "'"))) {
|
||||
return false;
|
||||
var match, string;
|
||||
switch (this.chunk.charAt(0)) {
|
||||
case "'":
|
||||
if (!(match = SIMPLESTR.exec(this.chunk))) {
|
||||
return false;
|
||||
}
|
||||
this.token('STRING', (string = match[0]).replace(MULTILINER, '\\\n'));
|
||||
break;
|
||||
case '"':
|
||||
if (!(string = this.balancedToken(['"', '"'], ['#{', '}']))) {
|
||||
return false;
|
||||
}
|
||||
this.interpolateString(string);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
if (!(string = this.balancedToken(['"', '"'], ['#{', '}']) || this.balancedToken(["'", "'"]))) {
|
||||
return false;
|
||||
}
|
||||
this.interpolateString(string.replace(/\n/g, '\\\n'));
|
||||
this.line += count(string, "\n");
|
||||
this.line += count(string, '\n');
|
||||
this.i += string.length;
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.heredocToken = function() {
|
||||
var doc, match, quote;
|
||||
var doc, heredoc, match, quote;
|
||||
if (!(match = this.chunk.match(HEREDOC))) {
|
||||
return false;
|
||||
}
|
||||
quote = match[1].substr(0, 1);
|
||||
doc = this.sanitizeHeredoc(match[2] || match[4] || '', {
|
||||
quote: quote
|
||||
heredoc = match[0];
|
||||
quote = heredoc.charAt(0);
|
||||
doc = this.sanitizeHeredoc(match[2], {
|
||||
quote: quote,
|
||||
indent: null
|
||||
});
|
||||
this.interpolateString(quote + doc + quote, {
|
||||
heredoc: true
|
||||
});
|
||||
this.line += count(match[1], "\n");
|
||||
this.i += match[1].length;
|
||||
this.line += count(heredoc, '\n');
|
||||
this.i += heredoc.length;
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.commentToken = function() {
|
||||
var match;
|
||||
var _ref2, comment, here, match;
|
||||
if (!(match = this.chunk.match(COMMENT))) {
|
||||
return false;
|
||||
}
|
||||
this.line += count(match[1], "\n");
|
||||
this.i += match[1].length;
|
||||
if (match[2]) {
|
||||
this.token('HERECOMMENT', this.sanitizeHeredoc(match[2], {
|
||||
_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, {
|
||||
herecomment: true,
|
||||
indent: Array(this.indent + 1).join(' ')
|
||||
}));
|
||||
@@ -172,14 +156,11 @@
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.jsToken = function() {
|
||||
var script;
|
||||
if (!(starts(this.chunk, '`'))) {
|
||||
var match, script;
|
||||
if (!(this.chunk.charAt(0) === '`' && (match = JSTOKEN.exec(this.chunk)))) {
|
||||
return false;
|
||||
}
|
||||
if (!(script = this.balancedToken(['`', '`']))) {
|
||||
return false;
|
||||
}
|
||||
this.token('JS', script.replace(JS_CLEANER, ''));
|
||||
this.token('JS', (script = match[0]).slice(1, -1));
|
||||
this.i += script.length;
|
||||
return true;
|
||||
};
|
||||
@@ -197,29 +178,25 @@
|
||||
if (!(regex = this.balancedToken(['/', '/']))) {
|
||||
return false;
|
||||
}
|
||||
if (!(end = this.chunk.substr(regex.length).match(REGEX_END))) {
|
||||
if (!(end = this.chunk.slice(regex.length).match(REGEX_END))) {
|
||||
return false;
|
||||
}
|
||||
if (end[2]) {
|
||||
regex += (flags = end[2]);
|
||||
}
|
||||
if (regex.match(REGEX_INTERPOLATION)) {
|
||||
str = regex.substring(1).split('/')[0];
|
||||
str = str.replace(REGEX_ESCAPE, function(escaped) {
|
||||
return '\\' + escaped;
|
||||
});
|
||||
this.tokens = this.tokens.concat([['(', '('], ['NEW', 'new'], ['IDENTIFIER', 'RegExp'], ['CALL_START', '(']]);
|
||||
flags = end[0];
|
||||
if (REGEX_INTERPOLATION.test(regex)) {
|
||||
str = regex.slice(1, -1);
|
||||
str = str.replace(REGEX_ESCAPE, '\\$&');
|
||||
this.tokens.push(['(', '('], ['NEW', 'new'], ['IDENTIFIER', 'RegExp'], ['CALL_START', '(']);
|
||||
this.interpolateString("\"" + (str) + "\"", {
|
||||
escapeQuotes: true
|
||||
});
|
||||
if (flags) {
|
||||
this.tokens.splice(this.tokens.length, 0, [',', ','], ['STRING', ("\"" + (flags) + "\"")]);
|
||||
this.tokens.push([',', ','], ['STRING', ("\"" + (flags) + "\"")]);
|
||||
}
|
||||
this.tokens.splice(this.tokens.length, 0, [')', ')'], [')', ')']);
|
||||
this.tokens.push([')', ')'], [')', ')']);
|
||||
} else {
|
||||
this.token('REGEX', regex);
|
||||
this.token('REGEX', regex + flags);
|
||||
}
|
||||
this.i += regex.length;
|
||||
this.i += regex.length + flags.length;
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.balancedToken = function() {
|
||||
@@ -228,16 +205,17 @@
|
||||
return this.balancedString(this.chunk, delimited);
|
||||
};
|
||||
Lexer.prototype.lineToken = function() {
|
||||
var diff, indent, nextCharacter, noNewlines, prev, size;
|
||||
if (!(indent = this.match(MULTI_DENT, 1))) {
|
||||
var diff, indent, match, nextCharacter, noNewlines, prev, size;
|
||||
if (!(match = MULTI_DENT.exec(this.chunk))) {
|
||||
return false;
|
||||
}
|
||||
this.line += count(indent, "\n");
|
||||
indent = match[0];
|
||||
this.line += count(indent, '\n');
|
||||
this.i += indent.length;
|
||||
prev = this.prev(2);
|
||||
size = indent.match(LAST_DENTS).reverse()[0].match(LAST_DENT)[1].length;
|
||||
nextCharacter = this.match(NEXT_CHARACTER, 1);
|
||||
noNewlines = nextCharacter === '.' || nextCharacter === ',' || this.unfinished();
|
||||
size = indent.length - 1 - indent.lastIndexOf('\n');
|
||||
nextCharacter = NEXT_CHARACTER.exec(this.chunk)[1];
|
||||
noNewlines = (('.' === nextCharacter || ',' === nextCharacter)) || this.unfinished();
|
||||
if (size - this.indebt === this.indent) {
|
||||
if (noNewlines) {
|
||||
return this.suppressNewlines();
|
||||
@@ -283,43 +261,46 @@
|
||||
this.outdebt -= moveOut;
|
||||
}
|
||||
if (!(this.tag() === 'TERMINATOR' || noNewlines)) {
|
||||
this.token('TERMINATOR', "\n");
|
||||
this.token('TERMINATOR', '\n');
|
||||
}
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.whitespaceToken = function() {
|
||||
var prev, space;
|
||||
if (!(space = this.match(WHITESPACE, 1))) {
|
||||
var match, prev;
|
||||
if (!(match = WHITESPACE.exec(this.chunk))) {
|
||||
return false;
|
||||
}
|
||||
prev = this.prev();
|
||||
if (prev) {
|
||||
prev.spaced = true;
|
||||
}
|
||||
this.i += space.length;
|
||||
this.i += match[0].length;
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.newlineToken = function(newlines) {
|
||||
if (this.tag() !== 'TERMINATOR') {
|
||||
this.token('TERMINATOR', "\n");
|
||||
this.token('TERMINATOR', '\n');
|
||||
}
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.suppressNewlines = function() {
|
||||
if (this.value() === "\\") {
|
||||
if (this.value() === '\\') {
|
||||
this.tokens.pop();
|
||||
}
|
||||
return true;
|
||||
};
|
||||
Lexer.prototype.literalToken = function() {
|
||||
var _ref2, match, prev, space, spaced, tag, value;
|
||||
match = this.chunk.match(OPERATOR);
|
||||
value = match && match[1];
|
||||
space = match && match[2];
|
||||
if (value && value.match(CODE)) {
|
||||
this.tagParameters();
|
||||
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);
|
||||
}
|
||||
value || (value = this.chunk.substr(0, 1));
|
||||
this.i += value.length;
|
||||
spaced = (prev = this.prev()) && prev.spaced;
|
||||
tag = value;
|
||||
@@ -354,11 +335,13 @@
|
||||
tag = 'CALL_START';
|
||||
} else if (value === '[') {
|
||||
tag = 'INDEX_START';
|
||||
if (this.tag() === '?') {
|
||||
this.tag(1, 'INDEX_SOAK');
|
||||
}
|
||||
if (this.tag() === '::') {
|
||||
this.tag(1, 'INDEX_PROTO');
|
||||
switch (this.tag()) {
|
||||
case '?':
|
||||
this.tag(1, 'INDEX_SOAK');
|
||||
break;
|
||||
case '::':
|
||||
this.tag(1, 'INDEX_PROTO');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -373,7 +356,7 @@
|
||||
accessor = (function() {
|
||||
if (prev[1] === '::') {
|
||||
return this.tag(1, 'PROTOTYPE_ACCESS');
|
||||
} else if (prev[1] === '.' && !(this.value(2) === '.')) {
|
||||
} else if (prev[1] === '.' && this.value(2) !== '.') {
|
||||
if (this.tag(2) === '?') {
|
||||
this.tag(1, 'SOAK_ACCESS');
|
||||
return this.tokens.splice(-2, 1);
|
||||
@@ -387,26 +370,32 @@
|
||||
return accessor ? 'accessor' : false;
|
||||
};
|
||||
Lexer.prototype.sanitizeHeredoc = function(doc, options) {
|
||||
var _ref2, attempt, indent, match;
|
||||
indent = options.indent;
|
||||
if (options.herecomment && !include(doc, '\n')) {
|
||||
var _ref2, attempt, herecomment, indent, match;
|
||||
_ref2 = options;
|
||||
indent = _ref2.indent;
|
||||
herecomment = _ref2.herecomment;
|
||||
if (herecomment && !include(doc, '\n')) {
|
||||
return doc;
|
||||
}
|
||||
if (!(options.herecomment)) {
|
||||
while ((match = HEREDOC_INDENT.exec(doc)) !== null) {
|
||||
attempt = (typeof (_ref2 = match[2]) !== "undefined" && _ref2 !== null) ? match[2] : match[3];
|
||||
if (!(typeof indent !== "undefined" && indent !== null) || (0 < attempt.length) && (attempt.length < indent.length)) {
|
||||
if (!(herecomment)) {
|
||||
while ((match = HEREDOC_INDENT.exec(doc))) {
|
||||
attempt = match[1];
|
||||
if (indent === null || (0 < attempt.length) && (attempt.length < indent.length)) {
|
||||
indent = attempt;
|
||||
}
|
||||
}
|
||||
}
|
||||
indent || (indent = '');
|
||||
doc = doc.replace(new RegExp("^" + indent, 'gm'), '');
|
||||
if (options.herecomment) {
|
||||
if (indent) {
|
||||
doc = doc.replace(new RegExp("\\n" + (indent), "g"), '\n');
|
||||
}
|
||||
if (herecomment) {
|
||||
return doc;
|
||||
}
|
||||
doc = doc.replace(/^\n/, '');
|
||||
return doc.replace(MULTILINER, "\\n").replace(new RegExp(options.quote, 'g'), "\\" + (options.quote));
|
||||
doc = doc.replace(/^\n/, '').replace(new RegExp("" + (options.quote), "g"), '\\$&');
|
||||
if (options.quote === "'") {
|
||||
doc = this.oldline(doc, true);
|
||||
}
|
||||
return doc;
|
||||
};
|
||||
Lexer.prototype.tagParameters = function() {
|
||||
var i, tok;
|
||||
@@ -444,13 +433,14 @@
|
||||
throw new Error("SyntaxError: Reserved word \"" + (this.value()) + "\" on line " + (this.line + 1) + " can't be assigned");
|
||||
};
|
||||
Lexer.prototype.balancedString = function(str, delimited, options) {
|
||||
var _i, _len, _ref2, _ref3, close, i, levels, open, pair, slash;
|
||||
var _i, _len, _ref2, _ref3, close, i, levels, open, pair, slash, slen;
|
||||
options || (options = {});
|
||||
slash = delimited[0][0] === '/';
|
||||
levels = [];
|
||||
i = 0;
|
||||
while (i < str.length) {
|
||||
if (levels.length && starts(str, '\\', i)) {
|
||||
slen = str.length;
|
||||
while (i < slen) {
|
||||
if (levels.length && str.charAt(i) === '\\') {
|
||||
i += 1;
|
||||
} else {
|
||||
_ref2 = delimited;
|
||||
@@ -473,7 +463,7 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!levels.length || slash && starts(str, '\n', i)) {
|
||||
if (!levels.length || slash && str.charAt(i) === '\n') {
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
@@ -484,85 +474,87 @@
|
||||
}
|
||||
throw new Error("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
|
||||
}
|
||||
return !i ? false : str.substring(0, i);
|
||||
return !i ? false : str.slice(0, i);
|
||||
};
|
||||
Lexer.prototype.interpolateString = function(str, options) {
|
||||
var _len, _ref2, _ref3, escaped, expr, i, idx, inner, interpolated, lexer, nested, pi, quote, tag, tok, token, tokens, value;
|
||||
var _len, _ref2, _ref3, end, escaped, expr, i, idx, inner, interpolated, lexer, nested, pi, push, quote, s, tag, tok, token, tokens, value;
|
||||
options || (options = {});
|
||||
if (str.length < 3 || !starts(str, '"')) {
|
||||
quote = str.charAt(0);
|
||||
if (quote !== '"' || str.length < 3) {
|
||||
return this.token('STRING', str);
|
||||
} else {
|
||||
lexer = new Lexer();
|
||||
tokens = [];
|
||||
quote = str.substring(0, 1);
|
||||
_ref2 = [1, 1];
|
||||
i = _ref2[0];
|
||||
pi = _ref2[1];
|
||||
while (i < str.length - 1) {
|
||||
if (starts(str, '\\', i)) {
|
||||
i += 1;
|
||||
} else if (expr = this.balancedString(str.substring(i), [['#{', '}']])) {
|
||||
if (pi < i) {
|
||||
tokens.push(['STRING', quote + str.substring(pi, i) + quote]);
|
||||
}
|
||||
inner = expr.substring(2, expr.length - 1);
|
||||
if (inner.length) {
|
||||
if (options.heredoc) {
|
||||
inner = inner.replace(new RegExp('\\\\' + quote, 'g'), quote);
|
||||
}
|
||||
nested = lexer.tokenize("(" + (inner) + ")", {
|
||||
line: this.line
|
||||
});
|
||||
_ref2 = nested;
|
||||
for (idx = 0, _len = _ref2.length; idx < _len; idx++) {
|
||||
tok = _ref2[idx];
|
||||
if (tok[0] === 'CALL_END') {
|
||||
(tok[0] = ')');
|
||||
}
|
||||
}
|
||||
nested.pop();
|
||||
tokens.push(['TOKENS', nested]);
|
||||
} else {
|
||||
tokens.push(['STRING', quote + quote]);
|
||||
}
|
||||
i += expr.length - 1;
|
||||
pi = i + 1;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
if (pi < i && pi < str.length - 1) {
|
||||
tokens.push(['STRING', quote + str.substring(pi, i) + quote]);
|
||||
}
|
||||
if (tokens[0][0] !== 'STRING') {
|
||||
tokens.unshift(['STRING', '""']);
|
||||
}
|
||||
interpolated = tokens.length > 1;
|
||||
if (interpolated) {
|
||||
this.token('(', '(');
|
||||
}
|
||||
_ref2 = tokens;
|
||||
for (i = 0, _len = _ref2.length; i < _len; i++) {
|
||||
token = _ref2[i];
|
||||
_ref3 = token;
|
||||
tag = _ref3[0];
|
||||
value = _ref3[1];
|
||||
if (tag === 'TOKENS') {
|
||||
this.tokens = this.tokens.concat(value);
|
||||
} else if (tag === 'STRING' && options.escapeQuotes) {
|
||||
escaped = value.substring(1, value.length - 1).replace(/"/g, '\\"');
|
||||
this.token(tag, "\"" + (escaped) + "\"");
|
||||
} else {
|
||||
this.token(tag, value);
|
||||
}
|
||||
if (i < tokens.length - 1) {
|
||||
this.token('+', '+');
|
||||
}
|
||||
}
|
||||
if (interpolated) {
|
||||
this.token(')', ')');
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
lexer = new Lexer();
|
||||
tokens = [];
|
||||
i = (pi = 1);
|
||||
end = str.length - 1;
|
||||
while (i < end) {
|
||||
if (str.charAt(i) === '\\') {
|
||||
i += 1;
|
||||
} else if (expr = this.balancedString(str.slice(i), [['#{', '}']])) {
|
||||
if (pi < i) {
|
||||
s = quote + this.oldline(str.slice(pi, i), options.heredoc) + quote;
|
||||
tokens.push(['STRING', s]);
|
||||
}
|
||||
inner = expr.slice(2, -1).replace(/^\s+/, '');
|
||||
if (inner.length) {
|
||||
if (options.heredoc) {
|
||||
inner = inner.replace(RegExp('\\\\' + quote, 'g'), quote);
|
||||
}
|
||||
nested = lexer.tokenize("(" + (inner) + ")", {
|
||||
line: this.line
|
||||
});
|
||||
_ref2 = nested;
|
||||
for (idx = 0, _len = _ref2.length; idx < _len; idx++) {
|
||||
tok = _ref2[idx];
|
||||
if (tok[0] === 'CALL_END') {
|
||||
(tok[0] = ')');
|
||||
}
|
||||
}
|
||||
nested.pop();
|
||||
tokens.push(['TOKENS', nested]);
|
||||
} else {
|
||||
tokens.push(['STRING', quote + quote]);
|
||||
}
|
||||
i += expr.length - 1;
|
||||
pi = i + 1;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
if ((i > pi) && (pi < str.length - 1)) {
|
||||
s = str.slice(pi, i).replace(MULTILINER, options.heredoc ? '\\n' : '');
|
||||
tokens.push(['STRING', quote + s + quote]);
|
||||
}
|
||||
if (tokens[0][0] !== 'STRING') {
|
||||
tokens.unshift(['STRING', '""']);
|
||||
}
|
||||
interpolated = tokens.length > 1;
|
||||
if (interpolated) {
|
||||
this.token('(', '(');
|
||||
}
|
||||
_ref2 = tokens;
|
||||
push = _ref2.push;
|
||||
_ref2 = tokens;
|
||||
for (i = 0, _len = _ref2.length; i < _len; i++) {
|
||||
token = _ref2[i];
|
||||
_ref3 = token;
|
||||
tag = _ref3[0];
|
||||
value = _ref3[1];
|
||||
if (tag === 'TOKENS') {
|
||||
push.apply(this.tokens, value);
|
||||
} else if (tag === 'STRING' && options.escapeQuotes) {
|
||||
escaped = value.slice(1, -1).replace(/"/g, '\\"');
|
||||
this.token(tag, "\"" + (escaped) + "\"");
|
||||
} else {
|
||||
this.token(tag, value);
|
||||
}
|
||||
if (i < tokens.length - 1) {
|
||||
this.token('+', '+');
|
||||
}
|
||||
}
|
||||
if (interpolated) {
|
||||
this.token(')', ')');
|
||||
}
|
||||
return tokens;
|
||||
};
|
||||
Lexer.prototype.token = function(tag, value) {
|
||||
return this.tokens.push([tag, value, this.line]);
|
||||
@@ -592,43 +584,41 @@
|
||||
};
|
||||
Lexer.prototype.match = function(regex, index) {
|
||||
var m;
|
||||
if (!(m = this.chunk.match(regex))) {
|
||||
return false;
|
||||
}
|
||||
return m ? m[index] : false;
|
||||
return (m = this.chunk.match(regex)) ? m[index || 0] : false;
|
||||
};
|
||||
Lexer.prototype.unfinished = function() {
|
||||
var prev;
|
||||
prev = this.prev(2);
|
||||
return this.value() && this.value().match && this.value().match(NO_NEWLINE) && prev && (prev[0] !== '.') && !this.value().match(CODE) && !this.chunk.match(ASSIGNED);
|
||||
var prev, value;
|
||||
return (prev = this.prev(2)) && prev[0] !== '.' && (value = this.value()) && NO_NEWLINE.test(value) && !CODE.test(value) && !ASSIGNED.test(this.chunk);
|
||||
};
|
||||
Lexer.prototype.oldline = function(str, heredoc) {
|
||||
return str.replace(MULTILINER, heredoc ? '\\n' : '');
|
||||
};
|
||||
return Lexer;
|
||||
})();
|
||||
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"];
|
||||
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'];
|
||||
JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED);
|
||||
IDENTIFIER = /^([a-zA-Z\$_](\w|\$)*)/;
|
||||
NUMBER = /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b/i;
|
||||
HEREDOC = /^("{6}|'{6}|"{3}([\s\S]*?)\n?([ \t]*)"{3}|'{3}([\s\S]*?)\n?([ \t]*)'{3})/;
|
||||
OPERATOR = /^(-[\-=>]?|\+[+=]?|[*&|\/%=<>^:!?]+)([ \t]*)/;
|
||||
WHITESPACE = /^([ \t]+)/;
|
||||
COMMENT = /^(###([^#][\s\S]*?)(###[ \t]*\n|(###)?$)|(\s*#(?!##[^#])[^\n]*)+)/;
|
||||
CODE = /^((-|=)>)/;
|
||||
MULTI_DENT = /^((\n([ \t]*))+)(\.)?/;
|
||||
LAST_DENTS = /\n([ \t]*)/g;
|
||||
LAST_DENT = /\n([ \t]*)/;
|
||||
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*#(?!##[^#]).*)+/;
|
||||
CODE = /^[-=]>/;
|
||||
MULTI_DENT = /^(?:\n[ \t]*)+/;
|
||||
SIMPLESTR = /^'[^\\']*(?:\\.[^\\']*)*'/;
|
||||
JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/;
|
||||
REGEX_START = /^\/([^\/])/;
|
||||
REGEX_INTERPOLATION = /([^\\]#\{.*[^\\]\})/;
|
||||
REGEX_END = /^(([imgy]{1,4})\b|\W|$)/;
|
||||
REGEX_ESCAPE = /\\[^\$]/g;
|
||||
JS_CLEANER = /(^`|`$)/g;
|
||||
REGEX_INTERPOLATION = /[^\\]#\{.*[^\\]\}/;
|
||||
REGEX_END = /^[imgy]{0,4}(?![a-zA-Z])/;
|
||||
REGEX_ESCAPE = /\\[^#]/g;
|
||||
MULTILINER = /\n/g;
|
||||
NO_NEWLINE = /^([+\*&|\/\-%=<>!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)$/;
|
||||
HEREDOC_INDENT = /(\n+([ \t]*)|^([ \t]+))/g;
|
||||
ASSIGNED = /^\s*(([a-zA-Z\$_@]\w*|["'][^\r\n]+?["']|\d+)[ \t]*?[:=][^:=])/;
|
||||
NEXT_CHARACTER = /^\s*(\S)/;
|
||||
NO_NEWLINE = /^(?:[-+*&|\/%=<>!.\\][<>=&|]*|and|or|is(?:nt)?|not|delete|typeof|instanceof)$/;
|
||||
HEREDOC_INDENT = /\n+([ \t]*)/g;
|
||||
ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/;
|
||||
NEXT_CHARACTER = /^\s*(\S?)/;
|
||||
COMPOUND_ASSIGN = ['-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?=', '<<=', '>>=', '>>>=', '&=', '^=', '|='];
|
||||
UNARY = ['UMINUS', 'UPLUS', '!', '!!', '~', 'TYPEOF', 'DELETE'];
|
||||
LOGIC = ['&', '|', '^', '&&', '||'];
|
||||
|
||||
Reference in New Issue
Block a user