Skip to content

Commit

Permalink
fix(parse): Include surrounding whitespace in Block nodes(#257)
Browse files Browse the repository at this point in the history
Updates `Stmt.Block` locations to include surrounding whitespace, and
renamed `startingLocation` to `location`. 

fixes #159
  • Loading branch information
TwitchBronBron authored and sjbarag committed Sep 20, 2019
1 parent 46f7759 commit 4ecfacb
Show file tree
Hide file tree
Showing 19 changed files with 1,213 additions and 317 deletions.
13 changes: 12 additions & 1 deletion src/parser/Parser.ts
Expand Up @@ -1198,7 +1198,18 @@ export class Parser {
// TODO: Figure out how to handle unterminated blocks well
}

return new Stmt.Block(statements, startingToken.location);
//the block's location starts at the end of the preceeding token, and stops at the beginning of the `end` token
const preceedingToken = tokens[tokens.indexOf(startingToken) - 1];
const postceedingToken = tokens[current];
const location: Location = {
file: startingToken.location.file,
start: {
line: preceedingToken.location.end.line,
column: preceedingToken.location.end.column,
},
end: postceedingToken.location.start,
};
return new Stmt.Block(statements, location);
}

function expression(): Expression {
Expand Down
17 changes: 1 addition & 16 deletions src/parser/Statement.ts
Expand Up @@ -60,26 +60,11 @@ export class Assignment implements Statement {
}

export class Block implements Statement {
constructor(
readonly statements: ReadonlyArray<Statement>,
readonly startingLocation: Location
) {}
constructor(readonly statements: ReadonlyArray<Statement>, readonly location: Location) {}

accept<R>(visitor: Visitor<R>): BrsType {
return visitor.visitBlock(this);
}

get location() {
let end = this.statements.length
? this.statements[this.statements.length - 1].location.end
: this.startingLocation.start;

return {
file: this.startingLocation.file,
start: this.startingLocation.start,
end: end,
};
}
}

export class Expression implements Statement {
Expand Down
9 changes: 6 additions & 3 deletions test/parser/controlFlow/__snapshots__/For.test.js.snap
Expand Up @@ -4,11 +4,12 @@ exports[`parser for loops accepts a 'step' clause 1`] = `
Array [
For {
"body": Block {
"startingLocation": Object {
"location": Object {
"end": Object {
"column": -9,
"line": -9,
},
"file": undefined,
"start": Object {
"column": -9,
"line": -9,
Expand Down Expand Up @@ -174,11 +175,12 @@ exports[`parser for loops allows 'next' to terminate loop 1`] = `
Array [
For {
"body": Block {
"startingLocation": Object {
"location": Object {
"end": Object {
"column": -9,
"line": -9,
},
"file": undefined,
"start": Object {
"column": -9,
"line": -9,
Expand Down Expand Up @@ -329,11 +331,12 @@ exports[`parser for loops defaults a missing 'step' clause to '1' 1`] = `
Array [
For {
"body": Block {
"startingLocation": Object {
"location": Object {
"end": Object {
"column": -9,
"line": -9,
},
"file": undefined,
"start": Object {
"column": -9,
"line": -9,
Expand Down
6 changes: 4 additions & 2 deletions test/parser/controlFlow/__snapshots__/ForEach.test.js.snap
Expand Up @@ -4,11 +4,12 @@ exports[`parser foreach loops allows 'next' to terminate loop 1`] = `
Array [
ForEach {
"body": Block {
"startingLocation": Object {
"location": Object {
"end": Object {
"column": -9,
"line": -9,
},
"file": undefined,
"start": Object {
"column": -9,
"line": -9,
Expand Down Expand Up @@ -108,11 +109,12 @@ exports[`parser foreach loops requires a name and target 1`] = `
Array [
ForEach {
"body": Block {
"startingLocation": Object {
"location": Object {
"end": Object {
"column": -9,
"line": -9,
},
"file": undefined,
"start": Object {
"column": -9,
"line": -9,
Expand Down

0 comments on commit 4ecfacb

Please sign in to comment.