@@ -4,65 +4,25 @@ import { vbaLexer } from '../../antlr/out/vbaLexer';
44import { ClassModuleContext , ConstItemContext , EnumDeclarationContext , IgnoredAttrContext , ProceduralModuleContext , ProcedureDeclarationContext , UdtDeclarationContext , WhileStatementContext , vbaParser } from '../../antlr/out/vbaParser' ;
55import { vbaListener } from '../../antlr/out/vbaListener' ;
66
7- import { VbaClassDocument , VbaModuleDocument } from '../document' ;
8- import { sleep } from '../../utils/helpers' ;
7+ import { DocumentSettings , VbaClassDocument , VbaModuleDocument } from '../document' ;
98import { CancellationToken } from 'vscode-languageserver' ;
109import { CharStream , CommonTokenStream , DefaultErrorStrategy , ErrorNode , ParseTreeWalker , Parser , RecognitionException } from 'antlr4ng' ;
1110import { ClassElement , IgnoredAttributeElement , ModuleElement } from '../elements/module' ;
1211import { ConstDeclarationElement , DeclarationElement , EnumDeclarationElement , TypeDeclarationElement } from '../elements/memory' ;
1312import { WhileLoopElement } from '../elements/flow' ;
1413
1514export class SyntaxParser {
16- private static _lockIdentifier = 0 ;
17-
18- private static _acquireLock ( ) : number {
19- this . _lockIdentifier += 1 ;
20- return this . _lockIdentifier ;
21- }
22-
23- private static _hasLock ( lockIdentifier : number ) : boolean {
24- return this . _lockIdentifier === lockIdentifier ;
25- }
26-
27- private static _releaseLock ( ) : void {
28- this . _lockIdentifier = 0 ;
29- }
30-
3115 async parseAsync ( document : VbaClassDocument | VbaModuleDocument , token : CancellationToken ) : Promise < boolean > {
32- // token.onCancellationRequested(e => {
33- // throw new Error("No");
34- // });
35-
36- // Refuse to do anything that seems like too much work.
37- if ( document . textDocument . lineCount > 2000 ) {
38- // TODO: Make this an option that people can increase or decrease.
39- console . log ( `Document oversize: ${ document . textDocument . lineCount } lines.` ) ;
40- console . warn ( `Syntax parsing has been disabled to prevent crashing.` ) ;
41- return false ;
42- }
43-
44- // Wait a few seconds to see if any other input has ocurred.
45- const lock = SyntaxParser . _acquireLock ( ) ;
46- await sleep ( 1000 ) ;
47- if ( ! SyntaxParser . _hasLock ( lock ) ) {
48- console . info ( 'Newer lock detected. Cancelling parse.' ) ;
49- return false ;
50- }
51- SyntaxParser . _releaseLock ( ) ;
52-
53- // Parse the document.
54- this . parse ( document ) ;
55- return true ;
56- }
57-
58- parse ( document : VbaClassDocument | VbaModuleDocument ) {
59- console . info ( 'Parsing the document.' ) ;
16+ console . debug ( `Parse requested: ${ document . textDocument . version } ` ) ;
6017 const listener = new VbaListener ( document ) ;
18+ await listener . ensureHasSettings ( ) ;
6119 const parser = this . createParser ( document . textDocument ) ;
6220 ParseTreeWalker . DEFAULT . walk (
6321 listener ,
64- parser . startRule ( )
22+ parser . startRule ( ) ,
23+ token
6524 ) ;
25+ return true ;
6626 }
6727
6828 private createParser ( doc : TextDocument ) : VbaParser {
@@ -87,13 +47,18 @@ class VbaParser extends vbaParser {
8747
8848class VbaListener extends vbaListener {
8949 document : VbaClassDocument | VbaModuleDocument ;
50+ protected _documentSettings ?: DocumentSettings ;
9051 protected _isAfterMethodDeclaration = false ;
9152
9253 constructor ( document : VbaClassDocument | VbaModuleDocument ) {
9354 super ( ) ;
9455 this . document = document ;
9556 }
9657
58+ async ensureHasSettings ( ) {
59+ this . _documentSettings = await this . document . getDocumentConfiguration ( ) ;
60+ }
61+
9762 enterEnumDeclaration = ( ctx : EnumDeclarationContext ) => {
9863 const element = new EnumDeclarationElement ( ctx , this . document . textDocument , this . _isAfterMethodDeclaration ) ;
9964 this . document . registerFoldableElement ( element )
@@ -113,7 +78,7 @@ class VbaListener extends vbaListener {
11378 } ;
11479
11580 enterClassModule = ( ctx : ClassModuleContext ) => {
116- const element = new ClassElement ( ctx , this . document . textDocument ) ;
81+ const element = new ClassElement ( ctx , this . document . textDocument , this . _documentSettings ?? { doWarnOptionExplicitMissing : true } ) ;
11782 this . document . registerSymbolInformation ( element )
11883 . registerDiagnosticElement ( element )
11984 . registerScopedElement ( element ) ;
@@ -135,7 +100,7 @@ class VbaListener extends vbaListener {
135100 } ;
136101
137102 enterProceduralModule = ( ctx : ProceduralModuleContext ) => {
138- const element = new ModuleElement ( ctx , this . document . textDocument ) ;
103+ const element = new ModuleElement ( ctx , this . document . textDocument , this . _documentSettings ?? { doWarnOptionExplicitMissing : true } ) ;
139104 this . document . registerSymbolInformation ( element )
140105 . registerDiagnosticElement ( element )
141106 . registerScopedElement ( element ) ;
0 commit comments