added last to helpers

This commit is contained in:
satyr
2010-09-28 21:52:51 +09:00
parent c064c90ee9
commit b2313beaf4
7 changed files with 128 additions and 135 deletions

View File

@@ -1,5 +1,5 @@
(function() {
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 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, last, starts;
var __slice = Array.prototype.slice;
_ref = require('./rewriter');
Rewriter = _ref.Rewriter;
@@ -8,6 +8,7 @@
count = _ref.count;
starts = _ref.starts;
compact = _ref.compact;
last = _ref.last;
exports.Lexer = (function() {
Lexer = function() {};
Lexer.prototype.tokenize = function(code, options) {
@@ -212,7 +213,7 @@
indent = match[0];
this.line += count(indent, '\n');
this.i += indent.length;
prev = this.prev(2);
prev = last(this.tokens, 1);
size = indent.length - 1 - indent.lastIndexOf('\n');
nextCharacter = NEXT_CHARACTER.exec(this.chunk)[1];
noNewlines = (('.' === nextCharacter || ',' === nextCharacter)) || this.unfinished();
@@ -270,7 +271,7 @@
if (!(match = WHITESPACE.exec(this.chunk))) {
return false;
}
prev = this.prev();
prev = last(this.tokens);
if (prev) {
prev.spaced = true;
}
@@ -290,7 +291,7 @@
return true;
};
Lexer.prototype.literalToken = function() {
var _ref2, match, prev, space, spaced, tag, value;
var _ref2, match, prev, space, spaced, tag, val, value;
if (match = this.chunk.match(OPERATOR)) {
_ref2 = match;
value = _ref2[0];
@@ -302,14 +303,15 @@
value = this.chunk.charAt(0);
}
this.i += value.length;
spaced = (prev = this.prev()) && prev.spaced;
prev = last(this.tokens);
spaced = prev == null ? undefined : prev.spaced;
tag = value;
if (value === '=') {
if (include(JS_FORBIDDEN, this.value())) {
if (include(JS_FORBIDDEN, val = this.value())) {
this.assignmentError();
}
if (('or' === (_ref2 = this.value()) || 'and' === _ref2)) {
this.tokens.splice(this.tokens.length - 1, 1, ['COMPOUND_ASSIGN', CONVERSIONS[this.value()] + '=', prev[2]]);
if (('or' === val || 'and' === val)) {
this.tokens.splice(-1, 1, ['COMPOUND_ASSIGN', CONVERSIONS[val] + '=', prev[2]]);
return true;
}
}
@@ -337,10 +339,10 @@
tag = 'INDEX_START';
switch (this.tag()) {
case '?':
this.tag(1, 'INDEX_SOAK');
this.tag(0, 'INDEX_SOAK');
break;
case '::':
this.tag(1, 'INDEX_PROTO');
this.tag(0, 'INDEX_PROTO');
break;
}
}
@@ -350,18 +352,18 @@
};
Lexer.prototype.tagAccessor = function() {
var accessor, prev;
if ((!(prev = this.prev())) || (prev && prev.spaced)) {
if (!(prev = last(this.tokens)) || prev.spaced) {
return false;
}
accessor = (function() {
if (prev[1] === '::') {
return this.tag(1, 'PROTOTYPE_ACCESS');
} else if (prev[1] === '.' && this.value(2) !== '.') {
if (this.tag(2) === '?') {
this.tag(1, 'SOAK_ACCESS');
return this.tag(0, 'PROTOTYPE_ACCESS');
} else if (prev[1] === '.' && this.value(1) !== '.') {
if (this.tag(1) === '?') {
this.tag(0, 'SOAK_ACCESS');
return this.tokens.splice(-2, 1);
} else {
return this.tag(1, 'PROPERTY_ACCESS');
return this.tag(0, 'PROPERTY_ACCESS');
}
} else {
return prev[0] === '@';
@@ -402,11 +404,9 @@
if (this.tag() !== ')') {
return null;
}
i = 0;
i = this.tokens.length;
while (true) {
i += 1;
tok = this.prev(i);
if (!tok) {
if (!(tok = this.tokens[--i])) {
return null;
}
switch (tok[0]) {
@@ -449,7 +449,7 @@
_ref3 = pair;
open = _ref3[0];
close = _ref3[1];
if (levels.length && starts(str, close, i) && levels[levels.length - 1] === pair) {
if (levels.length && starts(str, close, i) && last(levels) === pair) {
levels.pop();
i += close.length - 1;
if (!(levels.length)) {
@@ -563,30 +563,21 @@
};
Lexer.prototype.tag = function(index, newTag) {
var tok;
if (!(tok = this.prev(index))) {
if (!(tok = last(this.tokens, index))) {
return null;
}
if (typeof newTag !== "undefined" && newTag !== null) {
return (tok[0] = newTag);
}
return tok[0];
return (tok[0] = (typeof newTag !== "undefined" && newTag !== null) ? newTag : tok[0]);
};
Lexer.prototype.value = function(index, val) {
var tok;
if (!(tok = this.prev(index))) {
if (!(tok = last(this.tokens, index))) {
return null;
}
if (typeof val !== "undefined" && val !== null) {
return (tok[1] = val);
}
return tok[1];
};
Lexer.prototype.prev = function(index) {
return this.tokens[this.tokens.length - (index || 1)];
return (tok[1] = (typeof val !== "undefined" && val !== null) ? val : tok[1]);
};
Lexer.prototype.unfinished = function() {
var prev, value;
return (prev = this.prev(2)) && prev[0] !== '.' && (value = this.value()) && NO_NEWLINE.test(value) && !CODE.test(value) && !ASSIGNED.test(this.chunk);
return (prev = last(this.tokens, 1)) && prev[0] !== '.' && (value = this.value()) && NO_NEWLINE.test(value) && !CODE.test(value) && !ASSIGNED.test(this.chunk);
};
Lexer.prototype.escapeLines = function(str, heredoc) {
return str.replace(MULTILINER, heredoc ? '\\n' : '');