Skip to content

Commit

Permalink
Extract AA comma when parsing
Browse files Browse the repository at this point in the history
Added a commaToken to allow AST operations with commas
  • Loading branch information
elsassph committed Sep 26, 2021
1 parent 2bfe703 commit 62d9235
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ export class AAMemberExpression extends Expression {
}

public range: Range;
public commaToken?: Token;

transpile(state: BrsTranspileState) {
//TODO move the logic from AALiteralExpression loop into this function
Expand Down
21 changes: 15 additions & 6 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2317,30 +2317,38 @@ export class Parser {
}

if (!this.match(TokenKind.RightCurlyBrace)) {
let lastAAMember: AAMemberExpression;
if (this.check(TokenKind.Comment)) {
lastAAMember = null;
members.push(new CommentStatement([this.advance()]));
} else {
let k = key();
let expr = this.expression();
members.push(new AAMemberExpression(
lastAAMember = new AAMemberExpression(
k.keyToken,
k.colonToken,
expr
));
);
members.push(lastAAMember);
}

while (this.matchAny(TokenKind.Comma, TokenKind.Newline, TokenKind.Colon, TokenKind.Comment)) {
// collect comma at end of expression
if (lastAAMember && this.checkPrevious(TokenKind.Comma)) {
lastAAMember.commaToken = this.previous();
}

//check for comment at the end of the current line
if (this.check(TokenKind.Comment) || this.checkPrevious(TokenKind.Comment)) {
let token = this.checkPrevious(TokenKind.Comment) ? this.previous() : this.advance();
members.push(new CommentStatement([token]));
} else {
while (this.matchAny(TokenKind.Newline, TokenKind.Colon)) {
this.consumeStatementSeparators(true);

}
//check for a comment on its own line
if (this.check(TokenKind.Comment) || this.checkPrevious(TokenKind.Comment)) {
let token = this.checkPrevious(TokenKind.Comment) ? this.previous() : this.advance();
lastAAMember = null;
members.push(new CommentStatement([token]));
continue;
}
Expand All @@ -2350,11 +2358,12 @@ export class Parser {
}
let k = key();
let expr = this.expression();
members.push(new AAMemberExpression(
lastAAMember = new AAMemberExpression(
k.keyToken,
k.colonToken,
expr
));
);
members.push(lastAAMember);
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/parser/tests/expression/AssociativeArrayLiterals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Parser } from '../../Parser';
import { TokenKind } from '../../../lexer';
import { EOF, identifier, token } from '../Parser.spec';
import { Range } from 'vscode-languageserver';
import type { AssignmentStatement } from '../../Statement';
import type { AALiteralExpression } from '../../Expression';
import { isCommentStatement } from '../../../astUtils';

describe('parser associative array literals', () => {
describe('empty associative arrays', () => {
Expand Down Expand Up @@ -178,6 +181,30 @@ describe('parser associative array literals', () => {
expect(statements).to.be.length.greaterThan(0);
});

it('captures commas', () => {
let { statements } = Parser.parse(`
_ = {
p1: 1,
p2: 2, 'comment
p3: 3
p4: 4
'comment
p5: 5,
}
`);
const commas = ((statements[0] as AssignmentStatement).value as AALiteralExpression).elements
.map(s => !isCommentStatement(s) && !!s.commaToken);
expect(commas).to.deep.equal([
true, // p1
true, // p2
false, // comment
false, // p3
false, // p4
false, // comment
true // p5
]);
});

it('location tracking', () => {
/**
* 0 0 0 1
Expand Down

0 comments on commit 62d9235

Please sign in to comment.