Skip to content

Commit

Permalink
Eliminate nested block level tracking
Browse files Browse the repository at this point in the history
See wcjohnson/lightscript#33

# Conflicts:
#	src/plugins/lightscript.js
  • Loading branch information
wcjohnson committed Oct 7, 2017
1 parent e07daad commit f17bfad
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 13 deletions.
2 changes: 0 additions & 2 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,6 @@ pp.parseBlockBody = function (node, allowDirectives, topLevel, end) {

const oldInWhiteBlock = this.state.inWhiteBlock;
const oldWhiteBlockIndentLevel = this.state.whiteBlockIndentLevel;
this.state.nestedBlockLevel++;

let isEnd;
if (this.hasPlugin("lightscript") && typeof end === "number") {
Expand Down Expand Up @@ -648,7 +647,6 @@ pp.parseBlockBody = function (node, allowDirectives, topLevel, end) {
node.body.push(stmt);
}

this.state.nestedBlockLevel--;
if (this.hasPlugin("lightscript")) {
this.state.inWhiteBlock = oldInWhiteBlock;
this.state.whiteBlockIndentLevel = oldWhiteBlockIndentLevel;
Expand Down
14 changes: 8 additions & 6 deletions src/plugins/bangCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ export default function(parser) {
this.unexpected(null, "Whitespace required between `!` and first argument.");
}

// Read args
let first = true;
// Collect state
const oldBangUnwindLevel = this.state.bangUnwindLevel;
const oldBangBlockLevel = this.state.bangBlockLevel;
this.state.bangBlockLevel = this.state.nestedBlockLevel;
this.state.bangUnwindLevel = bangIndentLevel + 1;

const oldBangWhiteBlockLevel = this.state.bangWhiteBlockLevel;
this.state.bangWhiteBlockLevel = this.state.whiteBlockIndentLevel;

// Read args
let first = true;
while (true) {
// First argument on a different line from the `!` establishes indent level
if (this.state.curLine !== bangLine && argIndentLevel === null) {
Expand Down Expand Up @@ -83,7 +85,7 @@ export default function(parser) {
}

this.state.bangUnwindLevel = oldBangUnwindLevel;
this.state.bangBlockLevel = oldBangBlockLevel;
this.state.bangWhiteBlockLevel = oldBangWhiteBlockLevel;

node = this.finishNode(node, nodeType);

Expand All @@ -97,7 +99,7 @@ export default function(parser) {
// Subscripts to a bang call must appear at the arg indent level
pp.shouldUnwindBangSubscript = function() {
return this.isLineBreak() &&
(this.state.bangBlockLevel == this.state.nestedBlockLevel) &&
(this.state.bangWhiteBlockLevel === this.state.whiteBlockIndentLevel) &&
(this.state.indentLevel <= this.state.bangUnwindLevel);
};
}
4 changes: 0 additions & 4 deletions src/plugins/lightscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ pp.rethrowObjParseError = function(objParseResult, blockParseError) {
pp.parseInlineWhiteBlock = function(node) {
if (this.state.type.startsExpr) return this.parseMaybeAssign();
// oneline statement case
this.state.nestedBlockLevel++;
node.body = [this.parseStatement(true)];
this.state.nestedBlockLevel--;
node.directives = [];
this.addExtra(node, "curly", false);
return this.finishNode(node, "BlockStatement");
Expand Down Expand Up @@ -237,9 +235,7 @@ pp.parseWhiteBlock = function (isExpression?) {
if (objParseResult[0]) return objParseResult[0];
}
try {
this.state.nestedBlockLevel++;
const stmt = this.parseStatement(false);
this.state.nestedBlockLevel--;
return stmt;
} catch (err) {
this.rethrowObjParseError(objParseResult, err);
Expand Down
1 change: 0 additions & 1 deletion src/tokenizer/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default class State {

// for lightscript
this.indentLevel = 0;
this.nestedBlockLevel = 0;
this.inMatchCaseTest = false;

this.type = tt.eof;
Expand Down

0 comments on commit f17bfad

Please sign in to comment.