Skip to content

Commit 2f922a6

Browse files
committed
Refactor formatter to handle #if-else directives
1 parent e0288d6 commit 2f922a6

File tree

4 files changed

+367
-73
lines changed

4 files changed

+367
-73
lines changed

server/src/antlr/vbafmt.g4

Lines changed: 123 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ classHeaderBlock
2828
;
2929

3030
basicStatement
31-
: ws? ((ambiguousComponent | keywordComponent | flowCharacter) ws?)+ endOfStatement
31+
: ws? ((ambiguousComponent | keywordComponent | flowCharacter) ws*)+ endOfStatement
3232
;
3333

3434
blankLine
@@ -38,23 +38,51 @@ blankLine
3838
documentElement
3939
: comment
4040
| classHeader
41-
| doBlock
42-
| forBlock
4341
| endStatement
44-
| enumBlock
4542
| labelStatement
46-
| methodDeclaration
4743
| attributeStatement
4844
| ifElseStatement
49-
| ifElseBlock
50-
| selectCaseBlock
51-
| whileBlock
52-
| withBlock
45+
| preIfElseBlock
5346
| onErrorResumeNextStatement
47+
| indentAfterElement
48+
| outdentBeforeElement
49+
| outdentOnIndentAfterElement
50+
| caseDefaultStatement
51+
| caseStatement
5452
| basicStatement
5553
| blankLine
5654
;
5755

56+
indentAfterElement
57+
: doBlockOpen
58+
| enumBlockOpen
59+
| forBlockOpen
60+
| ifBlockOpen
61+
| methodSignature
62+
| selectCaseOpen
63+
| typeBlockOpen
64+
| withBlockOpen
65+
| whileBlockOpen
66+
;
67+
68+
outdentOnIndentAfterElement
69+
: elseIfBlockOpen
70+
| ifBlockDefault
71+
;
72+
73+
outdentBeforeElement
74+
: doBlockClose
75+
| enumBlockClose
76+
| forBlockClose
77+
| ifBlockOpen
78+
| ifBlockClose
79+
| methodClose
80+
| selectCaseClose
81+
| typeBlockClose
82+
| withBlockClose
83+
| whileBlockClose
84+
;
85+
5886
attributeStatement
5987
: ws? ATTRIBUTE (ANYCHARS | ws | continuation | STRINGLITERAL)* endOfStatement
6088
;
@@ -81,12 +109,26 @@ enumBlockClose
81109
: ws? END ws ENUM
82110
;
83111

84-
enumBlock
85-
: enumBlockOpen
86-
block?
87-
enumBlockClose
112+
// enumBlock
113+
// : enumBlockOpen
114+
// block?
115+
// enumBlockClose
116+
// ;
117+
118+
typeBlockOpen
119+
: ws? (visibility ws)? TYPE ws ambiguousComponent endOfStatement
88120
;
89121

122+
typeBlockClose
123+
: ws? END ws TYPE
124+
;
125+
126+
// typeBlock
127+
// : typeBlockOpen
128+
// block?
129+
// typeBlockClose
130+
// ;
131+
90132
labelStatement
91133
: ws? ambiguousComponent COLON
92134
;
@@ -115,14 +157,18 @@ withBlockClose
115157
: ws? END ws WITH endOfStatement
116158
;
117159

118-
withBlock
119-
: withBlockOpen
120-
block?
121-
withBlockClose
122-
;
160+
// withBlock
161+
// : withBlockOpen
162+
// block?
163+
// withBlockClose
164+
// ;
123165

124166
block
125-
: documentElement+
167+
: (documentElement endOfStatement?)+
168+
;
169+
170+
preBlock
171+
: (documentElement endOfStatement? | preIfElseBlock)+
126172
;
127173

128174
methodDeclaration
@@ -144,11 +190,11 @@ doBlockClose
144190
: ws? LOOP (ws (WHILE | UNTIL) ws expression+)? endOfStatement
145191
;
146192

147-
doBlock
148-
: doBlockOpen
149-
block?
150-
doBlockClose
151-
;
193+
// doBlock
194+
// : doBlockOpen
195+
// block?
196+
// doBlockClose
197+
// ;
152198

153199
onErrorResumeNextStatement
154200
: ws? ON ws ERROR ws RESUME ws NEXT endOfStatement
@@ -205,6 +251,29 @@ ifElseBlock
205251
ifBlockClose
206252
;
207253

254+
preIfBlockOpen
255+
: ws? PREIF ws expression+ THEN endOfStatement
256+
;
257+
258+
preElseIfBlockOpen
259+
: ws? PREELSEIF ws expression+ THEN endOfStatement
260+
;
261+
262+
preIfBlockDefault
263+
: ws? PREELSE endOfStatement
264+
;
265+
266+
preIfBlockClose
267+
: ws? PREEND ws IF endOfStatement
268+
;
269+
270+
preIfElseBlock
271+
: preIfBlockOpen preBlock?
272+
(preElseIfBlockOpen preBlock?)*
273+
(preIfBlockDefault preBlock?)?
274+
preIfBlockClose
275+
;
276+
208277
ifElseStatement
209278
: ws? IF ws expression+ THEN ws expression+ (ELSE ws expression+)? endOfStatement
210279
;
@@ -229,23 +298,23 @@ caseBlock
229298
: ((caseStatement | caseDefaultStatement) block)+
230299
;
231300

232-
selectCaseBlock
233-
: selectCaseOpen
234-
documentElement*
235-
caseBlock?
236-
selectCaseClose
237-
;
301+
// selectCaseBlock
302+
// : selectCaseOpen
303+
// documentElement*
304+
// caseBlock?
305+
// selectCaseClose
306+
// ;
238307

239308
comment
240309
: ws? (COMMENT | REMCOMMENT)
241310
;
242311

243312
colonEnding
244-
: COLON ws? NEWLINE?
313+
: COLON ws? lineEnding?
245314
;
246315

247316
lineEnding
248-
: ws? NEWLINE
317+
: (ws? NEWLINE)+
249318
| endOfFile
250319
;
251320

@@ -273,13 +342,15 @@ keywordComponent
273342
| GLOBAL
274343
| IF
275344
| IS
345+
| NEXT
276346
| ON
277347
| PRIVATE
278348
| PROPERTY
279349
| PUBLIC
280350
| RESUME
281351
| SUB
282352
| THEN
353+
| TYPE
283354
;
284355

285356
flowCharacter
@@ -382,6 +453,10 @@ RESUME
382453
: 'RESUME'
383454
;
384455
456+
PREIF
457+
: '#IF'
458+
;
459+
385460
IF
386461
: 'IF'
387462
;
@@ -390,10 +465,18 @@ IS
390465
: 'IS'
391466
;
392467
468+
PREELSE
469+
: '#ELSE'
470+
;
471+
393472
ELSE
394473
: 'ELSE'
395474
;
396475
476+
PREELSEIF
477+
: '#ELSEIF'
478+
;
479+
397480
ELSEIF
398481
: 'ELSEIF'
399482
;
@@ -402,6 +485,10 @@ THEN
402485
: 'THEN'
403486
;
404487
488+
PREEND
489+
: '#END'
490+
;
491+
405492
END
406493
: 'END'
407494
;
@@ -454,6 +541,10 @@ SUB
454541
: 'SUB'
455542
;
456543

544+
TYPE
545+
: 'TYPE'
546+
;
547+
457548
UNDERSCORE
458549
: '_'
459550
;

server/src/project/document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Range, TextDocument } from 'vscode-languageserver-textdocument';
33
import { CancellationToken, Diagnostic, DocumentDiagnosticReport, DocumentDiagnosticReportKind, SymbolInformation, SymbolKind } from 'vscode-languageserver';
44

55
// Antlr
6-
import { ParseCancellationException, ParserRuleContext } from 'antlr4ng';
6+
import { ParserRuleContext } from 'antlr4ng';
77

88
// Project
99
import { Workspace } from './workspace';

server/src/project/parser/vbaAntlr.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export class VbaFmtParser extends vbafmtParser {
8080

8181
static create(document: string): VbaFmtParser {
8282
const lexer = VbaFmtLexer.create(document);
83-
const parser = new VbaFmtParser(new CommonTokenStream(lexer));
83+
const tokens = new CommonTokenStream(lexer);
84+
const parser = new VbaFmtParser(tokens);
8485
parser.removeErrorListeners();
8586
parser.errorHandler = new VbaErrorHandler();
8687
return parser;

0 commit comments

Comments
 (0)