Consume whitespace before, rather than after, reading a token
Simplifies several things
This commit is contained in:
parent
bc48c02a18
commit
141905f9fd
27
acorn.js
27
acorn.js
@ -38,7 +38,6 @@
|
|||||||
exports.parse = function(input, options) {
|
exports.parse = function(input, options) {
|
||||||
var p = new Parser(options, input);
|
var p = new Parser(options, input);
|
||||||
var startPos = p.options.locations ? [p.pos, p.curPosition()] : p.pos;
|
var startPos = p.options.locations ? [p.pos, p.curPosition()] : p.pos;
|
||||||
p.skipSpace();
|
|
||||||
p.readToken();
|
p.readToken();
|
||||||
return p.parseTopLevel(p.options.program || p.startNodeAt(startPos));
|
return p.parseTopLevel(p.options.program || p.startNodeAt(startPos));
|
||||||
};
|
};
|
||||||
@ -126,7 +125,6 @@
|
|||||||
|
|
||||||
exports.parseExpressionAt = function(input, pos, options) {
|
exports.parseExpressionAt = function(input, pos, options) {
|
||||||
var p = new Parser(options, input, pos);
|
var p = new Parser(options, input, pos);
|
||||||
p.skipSpace();
|
|
||||||
p.readToken();
|
p.readToken();
|
||||||
return p.parseExpression();
|
return p.parseExpression();
|
||||||
};
|
};
|
||||||
@ -174,7 +172,6 @@
|
|||||||
|
|
||||||
exports.tokenize = function(input, options) {
|
exports.tokenize = function(input, options) {
|
||||||
var p = new Parser(options, input);
|
var p = new Parser(options, input);
|
||||||
p.skipSpace();
|
|
||||||
|
|
||||||
function getToken() {
|
function getToken() {
|
||||||
p.lastTokEnd = p.end;
|
p.lastTokEnd = p.end;
|
||||||
@ -196,7 +193,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.exprAllowed = !!exprAllowed;
|
p.exprAllowed = !!exprAllowed;
|
||||||
p.skipSpace();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// If we're in an ES6 environment, make this an iterator.
|
// If we're in an ES6 environment, make this an iterator.
|
||||||
@ -641,7 +637,6 @@
|
|||||||
--this.curLine;
|
--this.curLine;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.skipSpace();
|
|
||||||
this.readToken();
|
this.readToken();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -653,12 +648,14 @@
|
|||||||
// properties.
|
// properties.
|
||||||
|
|
||||||
pp.readToken = function() {
|
pp.readToken = function() {
|
||||||
|
var inTemplate = this.curTokContext() === q_tmpl;
|
||||||
|
if (!inTemplate) this.skipSpace();
|
||||||
|
|
||||||
this.start = this.pos;
|
this.start = this.pos;
|
||||||
if (this.options.locations) this.startLoc = this.curPosition();
|
if (this.options.locations) this.startLoc = this.curPosition();
|
||||||
if (this.pos >= this.input.length) return this.finishToken(_eof);
|
if (this.pos >= this.input.length) return this.finishToken(_eof);
|
||||||
|
|
||||||
if (this.curTokContext() === q_tmpl)
|
if (inTemplate) return this.readTmplToken();
|
||||||
return this.readTmplToken();
|
|
||||||
|
|
||||||
var code = this.fullCharCodeAtPos();
|
var code = this.fullCharCodeAtPos();
|
||||||
|
|
||||||
@ -784,19 +781,17 @@
|
|||||||
pp.finishToken = function(type, val) {
|
pp.finishToken = function(type, val) {
|
||||||
this.end = this.pos;
|
this.end = this.pos;
|
||||||
if (this.options.locations) this.endLoc = this.curPosition();
|
if (this.options.locations) this.endLoc = this.curPosition();
|
||||||
var prevType = this.type, preserveSpace = false;
|
var prevType = this.type;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.value = val;
|
this.value = val;
|
||||||
|
|
||||||
// Update context info
|
// Update context info
|
||||||
if (type === _parenR || type === _braceR) {
|
if (type === _parenR || type === _braceR) {
|
||||||
var out = this.context.pop();
|
var out = this.context.pop();
|
||||||
if (out === b_tmpl) {
|
if (out === b_stat && this.curTokContext() === f_expr) {
|
||||||
preserveSpace = true;
|
|
||||||
} else if (out === b_stat && this.curTokContext() === f_expr) {
|
|
||||||
this.context.pop();
|
this.context.pop();
|
||||||
this.exprAllowed = false;
|
this.exprAllowed = false;
|
||||||
} else {
|
} else if (out !== b_tmpl) {
|
||||||
this.exprAllowed = !(out && out.isExpr);
|
this.exprAllowed = !(out && out.isExpr);
|
||||||
}
|
}
|
||||||
} else if (type === _braceL) {
|
} else if (type === _braceL) {
|
||||||
@ -819,18 +814,14 @@
|
|||||||
}
|
}
|
||||||
this.exprAllowed = false;
|
this.exprAllowed = false;
|
||||||
} else if (type === _backQuote) {
|
} else if (type === _backQuote) {
|
||||||
if (this.curTokContext() === q_tmpl) {
|
if (this.curTokContext() === q_tmpl)
|
||||||
this.context.pop();
|
this.context.pop();
|
||||||
} else {
|
else
|
||||||
this.context.push(q_tmpl);
|
this.context.push(q_tmpl);
|
||||||
preserveSpace = true;
|
|
||||||
}
|
|
||||||
this.exprAllowed = false;
|
this.exprAllowed = false;
|
||||||
} else {
|
} else {
|
||||||
this.exprAllowed = type.beforeExpr;
|
this.exprAllowed = type.beforeExpr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!preserveSpace) this.skipSpace();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// ### Token reading
|
// ### Token reading
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user