Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions client/src/syntaxes/vba.tmLanguage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ repository:
- include: "#methodSignature"
- include: "#continuations" # Consume continuations so they "continue" other matches.
- include: "#enum"
- include: "#struct"
- include: "#syntaxLines" # Split document lines into syntax lines.

continuations:
Expand All @@ -40,16 +41,18 @@ repository:

syntaxLines:
name: meta.syntax-lines.vba
match: ((?:[^\n":]|"(?:\\.|[^\n"\\])*")+|"(?:\\.|[^\n"\\])*")
match: ((?:[^\n"':]|"(?:\\.|[^\n"\\])*")+|"(?:\\.|[^\n"\\])*")(?:('.*)*)?
captures:
1: # Split line
patterns:
- include: "#main"
2: # Comments
patterns:
- include: "#comments"

main:
patterns:
- include: "#moduleHeader"
- include: "#struct"
- include: "#declareFunctionSignature"
- include: "#methodSignature"
- include: "#variableDeclarations"
Expand Down Expand Up @@ -148,12 +151,11 @@ repository:
repository:
flowDecision:
name: keyword.control.flow.decision.vba
match: '(?i)(^|\s+)(#if|then|#elseif|#else|#end if|select case|case|switch|end select)\b'
# match: '(?i)(^|\s+)([#]?if|then|[#]?elseif|[#]?else|[#]?end if|select case|case|switch|end select)\b'
match: (?i)(^|\s+)(#if|then|#elseif|[#]?else|#end if|select case|case|switch|end select)\b

flowLoop:
name: keyword.control.flow.loop.vba
match: "(?i)\\b(do|exit\\s+do|while|until|loop|for|each|in|to|exit\\s+for|next|with)\\b"
match: (?i)\b(do|exit\s+do|while|until|loop|for|each|in|to|exit\s+for|next|with)\b

forEachLoop:
name: meta.flow.foreach.vba
Expand All @@ -170,7 +172,7 @@ repository:

inlineIfElse:
name: meta.flow.inline-if-else.vba
match: (?i)\b(if)\s+(.*?)\s+(then)\s+(.*)\s+(else)\s+(.*)
match: (?i)\s*((?:else)?if)\s+(.*?)\s+(then)\s+(.*)\s+(else)\s+([^'\n]*)
captures:
1:
name: keyword.control.flow.decision.vba
Expand All @@ -193,7 +195,7 @@ repository:

inlineIf:
name: meta.flow.inline-if.vba
match: (?i)\b(if)\s+(.*?)\s+(then)
match: (?i)\s*((?:else)?if)\s+(.*?)\s+(then)
captures:
1:
name: keyword.control.flow.decision.vba
Expand Down Expand Up @@ -417,13 +419,13 @@ repository:
# The sub-pattern consumes the \n if preceded by line continuation.
# Capturing it there prevents the end pattern being matched.
name: comment.block.vba
begin: (?i)'.*\s_\s*
begin: (?i)\s*'.*\s_\s*
end: \n
patterns:
- include: "#lineContinuation"
apostropheComments:
name: comment.line.apostrophe.vba
match: (?i)'.*
name: comment.line.apostropheXX.vba
match: (?i)\s*'.*
remarkComments:
name: comment.line.remark.vba
match: (?i)(?<=^|:)\s*Rem\b.*
Expand Down Expand Up @@ -459,7 +461,7 @@ repository:

enum:
name: meta.enum.declaration.vba
begin: (?i)^\s*((?:(?:Public|Private)\s+)?\s*Enum)\s+([a-z][a-z0-9_]+)(\s+(?:'|Rem).*)
begin: (?i)^\s*((?:(?:Public|Private)\s+)?\s*Enum)\s+([a-z][a-z0-9_]+)(\s+(?:'|Rem).*)?
beginCaptures:
1:
name: storage.type.enum.vba
Expand All @@ -471,13 +473,17 @@ repository:
patterns:
- include: "#comment"
- include: "#enumMember"
end: (?i)^\s*End\s+Enum\b
- include: "#language"
end: (?i)^\s*(End\s+Enum)(\s+'.*)?
endCaptures:
0:
1:
name: storage.type.enum.vba
2:
patterns:
- include: "#comments"

enumMember:
match: (?i)^\s*([a-z][a-z0-9_]*)(?:\s+(=)\s+([^\n']*)\s+)?('.*)?$
match: (?i)^\s*([a-z][a-z0-9_]*)(?:\s+(=)\s+([^\n']*))?(\s+(?:'|Rem).*)?
captures:
1:
name: constant.numeric.enum.vba
Expand All @@ -493,34 +499,42 @@ repository:

struct:
name: meta.struct.declaration.vba
begin: "(?i)^\\s*((?:(?:Public|Private) )?\\s*Type)\\s+([a-z][a-z0-9_]*)"
begin: (?i)^\s*((?:(?:Public|Private)\s+)?Type)\s+([a-z][a-z0-9_]*)?(\s+(?:'|Rem).*)?
beginCaptures:
1:
1: # Type declaration
name: storage.type.struct.vba
2:
2: # Type name
name: entity.name.type.struct.vba
3: # Comments?
patterns:
- include: "#comments"
patterns:
- include: "#comment"
- include: "#structProperty"
- include: "#language"

end: "(?i)^\\s*End\\s+Type\\b"
end: (?i)^\s*(End\s+Type)(\s+'.*)?
endCaptures:
0:
1:
name: storage.type.struct.vba
2:
patterns:
- include: "#comments"

structProperty:
match: "(?i)^\\s*([a-z][a-z0-9_]*)(\\(.*\\))?(\\s+As\\s+[a-z][a-z0-9_]*)?"
match: (?i)^\s*([a-z][a-z0-9_]*)(\(.*\))?(\s+As\s+[a-z][a-z0-9_]*)?(\s+(?:'|Rem).*)?
captures:
1:
# Property
1: # Property
name: variable.other.readwrite.vba
2:
# Array bounds?
2: # Array bounds?
patterns:
- include: "#language"
3:
# As Type
3: # As Type?
patterns:
- include: "#types"
4: # Comments?
patterns:
- include: "#comments"

declareFunctionSignature:
name: source.declare.signature.vba
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"icon": "images/vba-lsp-icon.png",
"author": "SSlinky",
"license": "MIT",
"version": "1.3.0",
"version": "1.3.2",
"repository": {
"type": "git",
"url": "https://github.com/SSlinky/VBA-LanguageServer"
Expand Down
47 changes: 31 additions & 16 deletions server/src/antlr/vba.g4
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ moduleDeclarationsElement:
comment
| declareStmt
| implementsStmt
| moduleOption
| unknownLine;
| moduleOption;

macroStmt: macroConstStmt | macroIfThenElseStmt;

Expand Down Expand Up @@ -461,7 +460,19 @@ stopStmt: STOP;
timeStmt: TIME WS? EQ WS? valueStmt;

typeStmt:
(visibility WS)? TYPE WS ambiguousIdentifier endOfStatement typeStmt_Element* END_TYPE;
(visibility WS)? TYPE WS ambiguousIdentifier endOfStatement (typeStmt_Element|macroTypeIfThenElseStmt)* END_TYPE;

macroTypeIfThenElseStmt:
macroTypeIfBlockStmt macroTypeElseIfBlockStmt* macroTypeElseBlockStmt? MACRO_END_IF endOfStatement;

macroTypeIfBlockStmt:
MACRO_IF WS? ifConditionStmt WS THEN endOfStatement typeStmt_Element*;

macroTypeElseIfBlockStmt:
MACRO_ELSEIF WS? ifConditionStmt WS THEN endOfStatement typeStmt_Element*;

macroTypeElseBlockStmt:
MACRO_ELSE endOfStatement typeStmt_Element*;

typeStmt_Element:
ambiguousIdentifier (WS? LPAREN (WS? subscripts)? WS? RPAREN)? (
Expand Down Expand Up @@ -489,28 +500,32 @@ valueStmt:
| implicitCallStmt_InStmt WS? ASSIGN WS? valueStmt # vsAssign
| valueStmt WS? IS WS? valueStmt # vsIs
| valueStmt WS? LIKE WS? valueStmt # vsLike
| valueStmt WS? GEQ WS? valueStmt # vsGeq
| valueStmt WS? LEQ WS? valueStmt # vsLeq
| valueStmt WS? GT WS? valueStmt # vsGt
| valueStmt WS? LT WS? valueStmt # vsLt
| valueStmt WS? NEQ WS? valueStmt # vsNeq
| valueStmt WS? EQ WS? valueStmt # vsEq
| valueStmt WS? POW WS? valueStmt # vsPow
| valueStmt WS? operatorsStmt WS? valueStmt # vsOperator
| MINUS WS? valueStmt # vsNegation
| PLUS WS? valueStmt # vsPlus
| valueStmt WS? DIV WS? valueStmt # vsDiv
| valueStmt WS? MULT WS? valueStmt # vsMult
| valueStmt WS? MOD WS? valueStmt # vsMod
| valueStmt WS? PLUS WS? valueStmt # vsAdd
| valueStmt WS? MINUS WS? valueStmt # vsMinus
| valueStmt WS? AMPERSAND WS? valueStmt # vsAmp
| valueStmt WS? IMP WS? valueStmt # vsImp
| valueStmt WS? EQV WS? valueStmt # vsEqv
| valueStmt WS? XOR WS? valueStmt # vsXor
| valueStmt WS? OR WS? valueStmt # vsOr
| valueStmt WS? AND WS? valueStmt # vsAnd
| NOT WS? valueStmt # vsNot;

operatorsStmt:
(GEQ
| LEQ
| LT
| NEQ
| EQ
| POW
| DIV
| MULT
| MOD
| PLUS
| MINUS
| AMPERSAND
)+;

variableStmt: (DIM | STATIC | visibility) WS (WITHEVENTS WS)? variableListStmt;

variableListStmt:
Expand Down Expand Up @@ -1086,7 +1101,7 @@ R_SQUARE_BRACKET: ']';
STRINGLITERAL: '"' (~["\r\n] | '""')* '"';
OCTLITERAL: '&O' [0-7]+ '&'?;
HEXLITERAL: '&H' [0-9A-F]+ '&'?;
SHORTLITERAL: (PLUS | MINUS)? DIGIT+ ('#' | '&' | '@')?;
SHORTLITERAL: (PLUS | MINUS)? DIGIT+ ('#' | '&' | '@' | '^')?;
INTEGERLITERAL: SHORTLITERAL (E SHORTLITERAL)?;
DOUBLELITERAL: (PLUS | MINUS)? DIGIT* '.' DIGIT+ (E SHORTLITERAL)?;
DATELITERAL: '#' DATEORTIME '#';
Expand Down
26 changes: 25 additions & 1 deletion server/src/capabilities/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import { TextDocumentClientCapabilities } from 'vscode-languageserver';
import { CodeDescription, Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, DiagnosticTag, Range, TextDocumentClientCapabilities } from 'vscode-languageserver';


function hasDiagnosticRelatedInformationCapability(x: TextDocumentClientCapabilities) {
return !!(x && x.publishDiagnostics && x.publishDiagnostics.relatedInformation);
}

abstract class BaseDiagnostic implements Diagnostic {
range: Range;
severity?: DiagnosticSeverity | undefined;
code?: string | number | undefined;
codeDescription?: CodeDescription | undefined;
source?: string | undefined;
abstract message: string;
tags?: DiagnosticTag[] | undefined;
relatedInformation?: DiagnosticRelatedInformation[] | undefined;
data?: unknown;

constructor(range: Range) {
this.range = range;
}
}


export class MultipleOperatorsDiagnostic extends BaseDiagnostic {
message = "Unexpected duplicate operator";
constructor(range: Range) {
super(range);
}
}
24 changes: 22 additions & 2 deletions server/src/project/document.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CancellationToken, Diagnostic, LSPErrorCodes, ResponseError, SemanticTokens, SymbolInformation, SymbolKind } from 'vscode-languageserver';
import { CancellationToken, Diagnostic, LSPErrorCodes, PublishDiagnosticsParams, ResponseError, SemanticTokens, SymbolInformation, SymbolKind } from 'vscode-languageserver';
import { Workspace } from './workspace';
import { FoldableElement } from './elements/special';
import { BaseSyntaxElement, HasAttribute, HasSemanticToken, HasSymbolInformation } from './elements/base';
import { BaseSyntaxElement, HasAttribute, HasDiagnosticCapability, HasSemanticToken, HasSymbolInformation } from './elements/base';
import { Range, TextDocument } from 'vscode-languageserver-textdocument';
import { SyntaxParser } from './parser/vbaSyntaxParser';
import { FoldingRange } from '../capabilities/folding';
Expand All @@ -17,6 +17,7 @@ export abstract class BaseProjectDocument {
protected _unhandledNamedElements: [] = [];
protected _publicScopeDeclarations: Map<string, any> = new Map();
protected _documentScopeDeclarations: Map<string, Map<string, any>> = new Map();
protected _hasDiagnosticElements: HasDiagnosticCapability[] = [];

protected _diagnostics: Diagnostic[] = [];
protected _elementParents: BaseSyntaxElement[] = [];
Expand Down Expand Up @@ -77,6 +78,10 @@ export abstract class BaseProjectDocument {
if (await (new SyntaxParser()).parseAsync(this, token)) {
this.isBusy = false;
}
this._hasDiagnosticElements.forEach(element => {
element.evaluateDiagnostics;
this._diagnostics.concat(element.diagnostics);
});
};

registerNamedElementDeclaration(element: any) {
Expand All @@ -88,6 +93,11 @@ export abstract class BaseProjectDocument {
throw new Error("Not implemented");
}

registerDiagnosticElement(element: HasDiagnosticCapability) {
console.log("Registering diagnostic element");
this._hasDiagnosticElements.push(element);
}

/**
* Pushes an element to the attribute elements stack.
* Be careful to pair a register action with an appropriate deregister.
Expand Down Expand Up @@ -171,6 +181,16 @@ export abstract class BaseProjectDocument {
this.workspace.connection.console.info('Processing request for Folding Range');
return this._foldableElements;
}

getDiagnostics(): PublishDiagnosticsParams {
this._hasDiagnosticElements.forEach(e =>
e.evaluateDiagnostics()
);
return {
uri: this.textDocument.uri,
diagnostics: this._hasDiagnosticElements
.map((e) => e.diagnostics).flat(1) };
}
}


Expand Down
9 changes: 7 additions & 2 deletions server/src/project/elements/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ParserRuleContext } from 'antlr4ts';
import { Range, SemanticTokenModifiers, SemanticTokenTypes, SymbolInformation, SymbolKind } from 'vscode-languageserver';
import { Diagnostic, Range, SemanticTokenModifiers, SemanticTokenTypes, SymbolInformation, SymbolKind } from 'vscode-languageserver';
import { Position, TextDocument } from 'vscode-languageserver-textdocument';
import { FoldingRangeKind } from '../../capabilities/folding';
import { IdentifierElement } from './memory';
Expand All @@ -21,6 +21,11 @@ interface SyntaxElement extends ContextOptionalSyntaxElement {
context: ParserRuleContext;
}

export interface HasDiagnosticCapability {
diagnostics: Diagnostic[];
evaluateDiagnostics(): void;
}

export interface HasAttribute {
processAttribute(context: AttributeStmtContext): void;
}
Expand Down Expand Up @@ -90,7 +95,7 @@ export abstract class BaseSyntaxElement implements ContextOptionalSyntaxElement
const stopIndex = this.context.stop?.stopIndex ?? startIndex;
return Range.create(
this.document.positionAt(startIndex),
this.document.positionAt(stopIndex)
this.document.positionAt(stopIndex + 1)
);
}
}
Expand Down
Loading