Skip to content

Commit

Permalink
Refactored statement separators processing and cleaned error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe Elsass committed Nov 27, 2020
1 parent 0d6925e commit 1861f54
Show file tree
Hide file tree
Showing 15 changed files with 711 additions and 656 deletions.
50 changes: 25 additions & 25 deletions src/DiagnosticMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ export let DiagnosticMessages = {
code: 1038,
severity: DiagnosticSeverity.Error
}),
expectedNewlineOrColonAfterCallableSignature: (callableType: string) => ({
message: `Expected newline or ':' after ${callableType} signature`,
expectedNewlineOrColon: () => ({
message: `Expected newline or ':' at the end of a statement`,
code: 1039,
severity: DiagnosticSeverity.Error
}),
Expand Down Expand Up @@ -248,13 +248,13 @@ export let DiagnosticMessages = {
severity: DiagnosticSeverity.Error
};
},
expectedNewlineOrColonAfterAssignment: () => ({
message: `Expected newline or ':' after assignment`,
expectedInlineIfStatement: () => ({
message: `If/else statement within an inline if should be also inline`,
code: 1047,
severity: DiagnosticSeverity.Error
}),
expectedNewlineAfterWhileCondition: () => ({
message: `Expected newline after while condition`,
expectedFinalNewline: () => ({
message: `Expected newline at the end of an inline if statement`,
code: 1048,
severity: DiagnosticSeverity.Error
}),
Expand All @@ -263,8 +263,8 @@ export let DiagnosticMessages = {
code: 1049,
severity: DiagnosticSeverity.Error
}),
expectedNewlineAfterExitWhile: () => ({
message: `Expected newline after 'exit while'`,
__unused1050: () => ({
message: ``,
code: 1050,
severity: DiagnosticSeverity.Error
}),
Expand All @@ -283,8 +283,8 @@ export let DiagnosticMessages = {
code: 1053,
severity: DiagnosticSeverity.Error
}),
expectedNewlineAfterExitFor: () => ({
message: `Expected newline after 'exit for'`,
unexpectedColonBeforeIfStatement: () => ({
message: `Colon before 'if' statement is not allowed`,
code: 1054,
severity: DiagnosticSeverity.Error
}),
Expand All @@ -308,8 +308,8 @@ export let DiagnosticMessages = {
code: 1058,
severity: DiagnosticSeverity.Error
}),
expectedColonToPreceedEndIf: () => ({
message: `Expected ':' to preceed 'end if'`,
__unused1059: () => ({
message: ``,
code: 1059,
severity: DiagnosticSeverity.Error
}),
Expand Down Expand Up @@ -338,8 +338,8 @@ export let DiagnosticMessages = {
code: 1064,
severity: DiagnosticSeverity.Error
}),
expectedNewlineOrColonAfterExpressionStatement: () => ({
message: `Expected newline or ':' after expression statement`,
__unused1065: () => ({
message: ``,
code: 1065,
severity: DiagnosticSeverity.Error
}),
Expand All @@ -348,18 +348,18 @@ export let DiagnosticMessages = {
code: 1066,
severity: DiagnosticSeverity.Error
}),
expectedNewlineOrColonAfterIndexedSetStatement: () => ({
message: `Expected newline or ':' after indexed set statement`,
__unused1067: () => ({
message: ``,
code: 1067,
severity: DiagnosticSeverity.Error
}),
expectedNewlineOrColonAfterDottedSetStatement: () => ({
message: `Expected newline or ':' after dotted set statement`,
__unused1068: () => ({
message: ``,
code: 1068,
severity: DiagnosticSeverity.Error
}),
expectedNewlineOrColonAfterPrintedValues: () => ({
message: `Expected newline or ':' after printed values`,
__unused1069: () => ({
message: ``,
code: 1069,
severity: DiagnosticSeverity.Error
}),
Expand Down Expand Up @@ -531,8 +531,8 @@ export let DiagnosticMessages = {
code: 1102,
severity: DiagnosticSeverity.Error
}),
autoImportComponentScriptCollision: () => ({
message: `Component script auto-import found '.bs' and '.brs' files with the same name and will import only the '.bs' file`,
__unused1103: () => ({
message: ``,
code: 1103,
severity: DiagnosticSeverity.Warning
}),
Expand All @@ -546,7 +546,7 @@ export let DiagnosticMessages = {
code: 1105,
severity: DiagnosticSeverity.Error
}),
__unused: () => ({
__unused1106: () => ({
message: ``,
code: 1106,
severity: DiagnosticSeverity.Error
Expand All @@ -561,8 +561,8 @@ export let DiagnosticMessages = {
code: 1108,
severity: DiagnosticSeverity.Error
}),
callfuncExpressionMustHaveAtLeastOneArgument: () => ({
message: `A callfunc expression must have at least one argument`,
__unused1109: () => ({
message: ``,
code: 1109,
severity: DiagnosticSeverity.Error
}),
Expand Down
11 changes: 3 additions & 8 deletions src/astUtils/reflection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable no-multi-spaces */
import { Position, Range } from 'vscode-languageserver';
import { expect } from 'chai';
import { PrintStatement, Block, Body, AssignmentStatement, CommentStatement, ExitForStatement, ExitWhileStatement, ExpressionStatement, FunctionStatement, IfStatement, IncrementStatement, GotoStatement, LabelStatement, ReturnStatement, EndStatement, StopStatement, ForStatement, ForEachStatement, WhileStatement, DottedSetStatement, IndexedSetStatement, LibraryStatement, NamespaceStatement, ImportStatement, ClassStatement, EmptyStatement, ElseIfStatement } from '../parser/Statement';
import { PrintStatement, Block, Body, AssignmentStatement, CommentStatement, ExitForStatement, ExitWhileStatement, ExpressionStatement, FunctionStatement, IfStatement, IncrementStatement, GotoStatement, LabelStatement, ReturnStatement, EndStatement, StopStatement, ForStatement, ForEachStatement, WhileStatement, DottedSetStatement, IndexedSetStatement, LibraryStatement, NamespaceStatement, ImportStatement, ClassStatement, EmptyStatement } from '../parser/Statement';
import { FunctionExpression, NamespacedVariableNameExpression, BinaryExpression, CallExpression, DottedGetExpression, IndexedGetExpression, GroupingExpression, LiteralExpression, EscapedCharCodeLiteralExpression, ArrayLiteralExpression, AALiteralExpression, UnaryExpression, VariableExpression, SourceLiteralExpression, NewExpression, CallfuncExpression, TemplateStringQuasiExpression, XmlAttributeGetExpression, TemplateStringExpression, TaggedTemplateStringExpression, AnnotationExpression } from '../parser/Expression';
import type { Token } from '../lexer';
import { TokenKind } from '../lexer';
import { BrsString } from '../brsTypes';
import { isPrintStatement, isIfStatement, isBody, isAssignmentStatement, isBlock, isExpressionStatement, isCommentStatement, isExitForStatement, isExitWhileStatement, isFunctionStatement, isIncrementStatement, isGotoStatement, isLabelStatement, isReturnStatement, isEndStatement, isStopStatement, isForStatement, isForEachStatement, isWhileStatement, isDottedSetStatement, isIndexedSetStatement, isLibraryStatement, isNamespaceStatement, isImportStatement, isExpression, isBinaryExpression, isCallExpression, isFunctionExpression, isNamespacedVariableNameExpression, isDottedGetExpression, isXmlAttributeGetExpression, isIndexedGetExpression, isGroupingExpression, isLiteralExpression, isEscapedCharCodeLiteralExpression, isArrayLiteralExpression, isAALiteralExpression, isUnaryExpression, isVariableExpression, isSourceLiteralExpression, isNewExpression, isCallfuncExpression, isTemplateStringQuasiExpression, isTemplateStringExpression, isTaggedTemplateStringExpression, isBrsFile, isXmlFile, isClassStatement, isStatement, isAnnotationExpression, isElseIfStatement } from './reflection';
import { isPrintStatement, isIfStatement, isBody, isAssignmentStatement, isBlock, isExpressionStatement, isCommentStatement, isExitForStatement, isExitWhileStatement, isFunctionStatement, isIncrementStatement, isGotoStatement, isLabelStatement, isReturnStatement, isEndStatement, isStopStatement, isForStatement, isForEachStatement, isWhileStatement, isDottedSetStatement, isIndexedSetStatement, isLibraryStatement, isNamespaceStatement, isImportStatement, isExpression, isBinaryExpression, isCallExpression, isFunctionExpression, isNamespacedVariableNameExpression, isDottedGetExpression, isXmlAttributeGetExpression, isIndexedGetExpression, isGroupingExpression, isLiteralExpression, isEscapedCharCodeLiteralExpression, isArrayLiteralExpression, isAALiteralExpression, isUnaryExpression, isVariableExpression, isSourceLiteralExpression, isNewExpression, isCallfuncExpression, isTemplateStringQuasiExpression, isTemplateStringExpression, isTaggedTemplateStringExpression, isBrsFile, isXmlFile, isClassStatement, isStatement, isAnnotationExpression } from './reflection';
import { createRange, createToken, createStringLiteral, createIdentifier } from './creators';
import { Program } from '../Program';
import { BrsFile } from '../files/BrsFile';
Expand Down Expand Up @@ -39,8 +39,7 @@ describe('reflection', () => {
const exitFor = new ExitForStatement({ exitFor: token });
const exitWhile = new ExitWhileStatement({ exitWhile: token });
const funs = new FunctionStatement(ident, new FunctionExpression([], undefined, block, token, token, token, token), undefined);
const ifs = new IfStatement({ if: token }, expr, block, []);
const elseifs = new ElseIfStatement({ elseIfToken: token }, expr, block);
const ifs = new IfStatement({ if: token }, expr, block);
const increment = new IncrementStatement(expr, token);
const print = new PrintStatement({ print: token }, []);
const gotos = new GotoStatement({ goto: token, label: token });
Expand Down Expand Up @@ -106,10 +105,6 @@ describe('reflection', () => {
expect(isIfStatement(ifs)).to.be.true;
expect(isIfStatement(body)).to.be.false;
});
it('isElseIfStatement', () => {
expect(isElseIfStatement(elseifs)).to.be.true;
expect(isElseIfStatement(body)).to.be.false;
});
it('isIncrementStatement', () => {
expect(isIncrementStatement(increment)).to.be.true;
expect(isIncrementStatement(body)).to.be.false;
Expand Down
5 changes: 1 addition & 4 deletions src/astUtils/reflection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Body, AssignmentStatement, Block, ExpressionStatement, CommentStatement, ExitForStatement, ExitWhileStatement, FunctionStatement, IfStatement, ElseIfStatement, IncrementStatement, PrintStatement, GotoStatement, LabelStatement, ReturnStatement, EndStatement, StopStatement, ForStatement, ForEachStatement, WhileStatement, DottedSetStatement, IndexedSetStatement, LibraryStatement, NamespaceStatement, ImportStatement, ClassFieldStatement, ClassMethodStatement, ClassStatement, Statement } from '../parser/Statement';
import type { Body, AssignmentStatement, Block, ExpressionStatement, CommentStatement, ExitForStatement, ExitWhileStatement, FunctionStatement, IfStatement, IncrementStatement, PrintStatement, GotoStatement, LabelStatement, ReturnStatement, EndStatement, StopStatement, ForStatement, ForEachStatement, WhileStatement, DottedSetStatement, IndexedSetStatement, LibraryStatement, NamespaceStatement, ImportStatement, ClassFieldStatement, ClassMethodStatement, ClassStatement, Statement } from '../parser/Statement';
import type { LiteralExpression, Expression, BinaryExpression, CallExpression, FunctionExpression, NamespacedVariableNameExpression, DottedGetExpression, XmlAttributeGetExpression, IndexedGetExpression, GroupingExpression, EscapedCharCodeLiteralExpression, ArrayLiteralExpression, AALiteralExpression, UnaryExpression, VariableExpression, SourceLiteralExpression, NewExpression, CallfuncExpression, TemplateStringQuasiExpression, TemplateStringExpression, TaggedTemplateStringExpression, AnnotationExpression } from '../parser/Expression';
import type { BrsString, BrsInvalid, BrsBoolean, RoString, RoArray, RoAssociativeArray, RoSGNode, FunctionParameterExpression } from '../brsTypes';
import { ValueKind } from '../brsTypes';
Expand Down Expand Up @@ -60,9 +60,6 @@ export function isFunctionStatement(element: Statement | Expression | undefined)
export function isIfStatement(element: Statement | Expression | undefined): element is IfStatement {
return element?.constructor?.name === 'IfStatement';
}
export function isElseIfStatement(element: Statement | Expression | undefined): element is ElseIfStatement {
return element?.constructor?.name === 'ElseIfStatement';
}
export function isIncrementStatement(element: Statement | Expression | undefined): element is IncrementStatement {
return element?.constructor?.name === 'IncrementStatement';
}
Expand Down
22 changes: 11 additions & 11 deletions src/astUtils/visitors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ describe('astUtils visitors', () => {
'IfStatement:1', // if a = 1
'Block:2', // then block
'PrintStatement:3', // print 3
'ElseIfStatement:2', // elseif statement
'IfStatement:2', // elseif statement
'Block:3', // elseif block
'PrintStatement:4', // print 4
'Block:2', // else block
'PrintStatement:3', // print 5
'Block:3', // else block
'PrintStatement:4', // print 5
'WhileStatement:1', // while a <> invalid
'Block:2', // while block
'PrintStatement:3', // print 6
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('astUtils visitors', () => {
'IfStatement', // if a = 1
'Block', // then block
'PrintStatement', // print 3
'ElseIfStatement', // elseif statement
'IfStatement', // elseif statement
'Block', // elseif block
'PrintStatement', // print 4
'Block', // else block
Expand Down Expand Up @@ -196,7 +196,7 @@ describe('astUtils visitors', () => {
'IfStatement', // if a = 1
'Block', // then block
'PrintStatement', // print 3
'ElseIfStatement', // elseif statement
'IfStatement', // elseif statement
'Block', // elseif block
'PrintStatement' // print 4
]);
Expand Down Expand Up @@ -305,10 +305,10 @@ describe('astUtils visitors', () => {
'IfStatement:1:BinaryExpression', // if <j > 0>
'IfStatement:1:VariableExpression', // if <j> > 0
'IfStatement:1:LiteralExpression', // if j > <0>
'ElseIfStatement:2:BinaryExpression', // else if <j < -10>
'ElseIfStatement:2:VariableExpression', // else if <j> < -10
'ElseIfStatement:2:UnaryExpression', // else if j < <-10>
'ElseIfStatement:2:LiteralExpression', // else if j < -<10>
'IfStatement:2:BinaryExpression', // else if <j < -10>
'IfStatement:2:VariableExpression', // else if <j> < -10
'IfStatement:2:UnaryExpression', // else if j < <-10>
'IfStatement:2:LiteralExpression', // else if j < -<10>
'ReturnStatement:1:LiteralExpression' // return <invalid>
]);
});
Expand Down Expand Up @@ -378,7 +378,7 @@ describe('astUtils visitors', () => {
'PrintStatement',
'LiteralExpression',
//else if
'ElseIfStatement',
'IfStatement',
'LiteralExpression',
'Block',
'PrintStatement',
Expand Down Expand Up @@ -753,7 +753,7 @@ describe('astUtils visitors', () => {
'Block',
'ReturnStatement',
//else if
'ElseIfStatement',
'IfStatement',
'BinaryExpression',
'VariableExpression',
'LiteralExpression',
Expand Down
29 changes: 8 additions & 21 deletions src/lexer/Lexer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,15 @@ describe('lexer', () => {
expect(tokens[5].text.charCodeAt(0), 'should contain \\r\\n').to.eql(10);
});

it('correctly identifies the elseif token', () => {
it('correctly splits the elseif token', () => {
let { tokens } = Lexer.scan('else if elseif else if');
expect(tokens.map(t => t.kind)).to.deep.equal([
TokenKind.ElseIf,
TokenKind.ElseIf,
TokenKind.ElseIf,
TokenKind.Else,
TokenKind.If,
TokenKind.Else,
TokenKind.If,
TokenKind.Else,
TokenKind.If,
TokenKind.Eof
]);
});
Expand Down Expand Up @@ -859,9 +862,8 @@ describe('lexer', () => {
});

it('matches multi-word keywords', () => {
let { tokens } = Lexer.scan('else if end if end while End Sub end Function Exit wHILe');
let { tokens } = Lexer.scan('end if end while End Sub end Function Exit wHILe');
expect(tokens.map(w => w.kind)).to.deep.equal([
TokenKind.ElseIf,
TokenKind.EndIf,
TokenKind.EndWhile,
TokenKind.EndSub,
Expand Down Expand Up @@ -1067,21 +1069,6 @@ describe('lexer', () => {
TokenKind.Eof
]);
});
it('supports various spacing between else if', () => {
let { tokens } = Lexer.scan(
'else if else if else if else\tif else\t if else \tif else \t if'
);
expect(tokens.map(t => t.kind)).to.deep.equal([
TokenKind.ElseIf,
TokenKind.ElseIf,
TokenKind.ElseIf,
TokenKind.ElseIf,
TokenKind.ElseIf,
TokenKind.ElseIf,
TokenKind.ElseIf,
TokenKind.Eof
]);
});
});

it('detects rem when used as keyword', () => {
Expand Down
Loading

0 comments on commit 1861f54

Please sign in to comment.