Skip to content

Commit 6211cfa

Browse files
committed
Parse unexpected line endings
1 parent fdee7ca commit 6211cfa

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

server/src/antlr/vba.g4

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ functionType: AS wsc typeExpression wsc? arrayDesignator?;
464464
arrayDesignator: '(' wsc? ')';
465465

466466
// 5.3.1.5 Parameter Lists
467-
procedureParameters: '(' wsc? parameterList? wsc? ')';
467+
procedureParameters: '(' wsc? parameterList? wscu? ')';
468468
propertyParameters: '(' wsc? (parameterList wsc? ',' wsc?)? valueParam wsc? ')';
469469
validParameterList
470470
: (positionalParameters wsc? ',' wsc? optionalParameters)
@@ -1198,6 +1198,14 @@ wsc: (WS | LINE_CONTINUATION)+;
11981198
endOfLine
11991199
: wsc? (NEWLINE | commentBody | remStatement) wsc?
12001200
;
1201+
unexpectedEndOfLine
1202+
: wsc? (NEWLINE | commentBody | remStatement) wsc?
1203+
;
1204+
// White space, continuation, unexpected ending.
1205+
// TODO: Replace as many wsc with wscu and test.
1206+
wscu
1207+
: (wsc | unexpectedEndOfLine)+
1208+
;
12011209
// We usually don't care if a line of code begins with whitespace, and the parser rules are
12021210
// cleaner if we lump that in with the EOL or EOS "token". However, for those cases where
12031211
// something MUST occur on the start of a line, use endOfLineNoWs.

server/src/capabilities/diagnostics.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ export class ShadowDeclarationDiagnostic extends BaseDiagnostic {
7575
}
7676

7777

78+
// test
79+
export class UnexpectedLineEndingDiagnostic extends BaseDiagnostic {
80+
message = "Unexpected line ending.";
81+
severity = DiagnosticSeverity.Error;
82+
constructor(range: Range) {
83+
super(range);
84+
}
85+
}
86+
87+
7888
export class IgnoredAttributeDiagnostic extends BaseDiagnostic {
7989
severity = DiagnosticSeverity.Warning;
8090
constructor(range: Range, attributeName: string) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Core
2+
import { TextDocument } from 'vscode-languageserver-textdocument';
3+
4+
// Antlr
5+
import { UnexpectedEndOfLineContext } from '../../antlr/out/vbaParser';
6+
7+
// Project
8+
import { DiagnosticCapability } from '../../capabilities/capabilities';
9+
import { BaseContextSyntaxElement, HasDiagnosticCapability } from './base';
10+
import { UnexpectedLineEndingDiagnostic } from '../../capabilities/diagnostics';
11+
12+
13+
export class UnexpectedEndOfLineElement extends BaseContextSyntaxElement<UnexpectedEndOfLineContext> implements HasDiagnosticCapability {
14+
diagnosticCapability: DiagnosticCapability;
15+
16+
constructor(context: UnexpectedEndOfLineContext, document: TextDocument) {
17+
super(context, document);
18+
this.diagnosticCapability = new DiagnosticCapability(this, () => {
19+
this.diagnosticCapability.diagnostics.push(new UnexpectedLineEndingDiagnostic(this.context.range));
20+
return this.diagnosticCapability.diagnostics;
21+
});
22+
}
23+
}

server/src/project/parser/vbaListener.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
SubroutineDeclarationContext,
2525
TypeSuffixContext,
2626
UdtDeclarationContext,
27+
UnexpectedEndOfLineContext,
2728
WhileStatementContext
2829
} from '../../antlr/out/vbaParser';
2930

@@ -34,6 +35,7 @@ import { ClassElement, ModuleElement, ModuleIgnoredAttributeElement } from '../e
3435
import { DocumentSettings, VbaClassDocument, VbaModuleDocument } from '../document';
3536
import { FunctionDeclarationElement, PropertyGetDeclarationElement, PropertyLetDeclarationElement, PropertySetDeclarationElement, SubDeclarationElement } from '../elements/procedure';
3637
import { DeclarationStatementElement, EnumDeclarationElement, TypeDeclarationElement, TypeSuffixElement } from '../elements/typing';
38+
import { UnexpectedEndOfLineElement } from '../elements/utils';
3739

3840

3941
class CommonParserCapability {
@@ -171,6 +173,11 @@ export class VbaListener extends vbaListener {
171173
element.declarations.forEach(x => this.document.registerElement(x));
172174
}
173175

176+
enterUnexpectedEndOfLine = (ctx: UnexpectedEndOfLineContext) => {
177+
const element = new UnexpectedEndOfLineElement(ctx, this.document.textDocument);
178+
this.document.registerElement(element);
179+
}
180+
174181
enterWhileStatement = (ctx: WhileStatementContext) => {
175182
const element = new WhileLoopElement(ctx, this.document.textDocument)
176183
this.document.registerDiagnosticElement(element);

0 commit comments

Comments
 (0)