-
Notifications
You must be signed in to change notification settings - Fork 16
/
server.ts
1282 lines (1194 loc) · 40.2 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* --------------------------------------------------------------------------------------------
* Copyright (c) MV Extensions. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
"use strict";
import {
createConnection,
TextDocuments,
Diagnostic,
DiagnosticSeverity,
ProposedFeatures,
InitializeParams,
DidChangeConfigurationNotification,
CompletionItem,
CompletionItemKind,
TextDocumentPositionParams,
TextDocumentSyncKind,
InitializeResult,
Location,
Range,
SymbolInformation,
SymbolKind,
Hover,
MarkupContent,
MarkupKind
} from "vscode-languageserver/node";
import {
TextDocument
} from 'vscode-languageserver-textdocument';
import fs = require("fs");
import * as path from "path";
/* Initialize Variables */
// Create a connection for the server. The connection uses Node's IPC as a transport
let connection = createConnection(ProposedFeatures.all);
// The settings interface describe the server relevant settings part
interface Settings {
MVBasic: ExampleSettings;
}
// These are the example settings we defined in the client's package.json file
interface ExampleSettings {
maxNumberOfProblems: number;
useCamelCase: boolean;
userVariablesEnabled: boolean;
ignoreGotoScope: boolean;
customWords: string;
customWordPath: string;
customFunctionPath: string;
languageType: string;
trace: any; // expect trace.server is string enum 'off', 'messages', 'verbose'
}
// Describes a line inside a document
interface DocumentLine {
lineNumber: number;
lineOfCode: string;
}
let logLevel: number;
let Intellisense: CompletionItem[] = [];
// Label class represents instances of parsed labels in code, i.e. "LABEL.NAME:"
class Label {
LabelName: string;
LineNumber: number;
Level: number;
Referenced: boolean;
constructor(
LabelName: string,
LineNumber: number,
Level: number,
Referenced: boolean
) {
this.LabelName = LabelName;
this.Level = Level;
this.LineNumber = LineNumber;
this.Referenced = Referenced;
}
}
var LabelList: Label[] = [];
// Create a simple text document manager. The text document manager
// supports full document sync only
let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection);
// A document was opened
documents.onDidOpen(event => {
if (logLevel) {
connection.console.log(
`[Server(${process.pid})] Document opened: ${event.document.uri}`
);
}
});
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent(change => {
validateTextDocument(change.document);
});
/* Functions */
function convertRange(
document: TextDocument,
span: { start: number; length: number }
): Range {
const startPosition = document.positionAt(span.start);
const endPosition = document.positionAt(span.start + span.length);
return Range.create(startPosition, endPosition);
}
function getWord(line: string, wordCount: number): string {
if (line.length == 0 || wordCount < 1) {
return line;
}
let rWhitespace = new RegExp("\\s{2,}"); // 2 or more whitespace chars
line = line.replace(rWhitespace, " ").trim();
let words = line.split(" ");
if (wordCount > words.length) {
return "";
}
return words[wordCount - 1];
}
//Find variables in lineOfCode
function getVariableName(lineOfCode: any) {
let variableName: string[];
variableName = [];
let position;
let wordArray;
if (lineOfCode.length < 3) {return variableName;}
if (lineOfCode.includes(" case ") || lineOfCode.includes(" CASE ")) {return variableName;}
//get arguments from a CALL or SUBROUTINE as variables
if (lineOfCode.includes("call ") || lineOfCode.includes("CALL ") || lineOfCode.includes("subroutine ") || lineOfCode.includes("SUBROUTINE ")) {
let argumentString = lineOfCode.substring(lineOfCode.lastIndexOf("(") + 1, lineOfCode.lastIndexOf(")"));
if (argumentString!=null) {
position=lineOfCode.indexOf(",");
if (position > 0) {
var argumentList = argumentString.split("=");
for (var i = 0; i < argumentList.length; i++) {
variableName.push(argumentString[i].trim());
}
return variableName;
} else {
variableName.push(argumentString.trim());
return variableName;
}
}
}
//get variables from a READ
if (lineOfCode.includes(" from ") || lineOfCode.includes(" FROM ")) {
wordArray=lineOfCode.split(" ");
position=wordArray.indexOf("from");
if (position > -1) {
variableName.push(wordArray[position-1].trim());
return variableName;
}
position=wordArray.indexOf("FROM");
if (position > -1) {
variableName.push(wordArray[position-1].trim());
return variableName;
}
}
//get variables from a FOR TO
if ((lineOfCode.includes("for ") || lineOfCode.includes("FOR ")) && (lineOfCode.includes(" to ") || lineOfCode.includes("TO ")) && lineOfCode.includes("=")) {
wordArray=lineOfCode.split(" ");
position=wordArray.indexOf("for");
if (position > -1) {
let thisString=wordArray[position+1]
thisString=thisString.split("=");
thisString=thisString[0].trim()
if (thisString!=null) {
variableName.push(thisString);
return variableName;
}
}
position=wordArray.indexOf("FOR");
if (position > -1) {
let thisString=wordArray[position+1]
thisString=thisString.split("=");
thisString=thisString[0].trim()
if (thisString!=null) {
variableName.push(thisString);
return variableName;
}
}
}
//get variables from a EQUATE TO
if ((lineOfCode.includes("equate ") || lineOfCode.includes("EQUATE ")) && (lineOfCode.includes(" to ") || lineOfCode.includes("TO "))) {
wordArray=lineOfCode.split(" ");
position=wordArray.indexOf("equate");
if (position > -1) {
variableName.push(wordArray[position+1].trim());
return variableName;
}
position=wordArray.indexOf("EQUATE");
if (position > -1) {
variableName.push(wordArray[position+1].trim());
return variableName;
}
}
//get variables from a EQU TO
if ((lineOfCode.includes("equ ") || lineOfCode.includes("EQU ")) && (lineOfCode.includes(" to ") || lineOfCode.includes("TO "))) {
wordArray=lineOfCode.split(" ");
position=wordArray.indexOf("equ");
if (position > -1) {
variableName.push(wordArray[position+1].trim());
return variableName;
}
position=wordArray.indexOf("EQU");
if (position > -1) {
variableName.push(wordArray[position+1].trim());
return variableName;
}
}
//get variables from a OPEN
if (lineOfCode.includes("open ") || lineOfCode.includes("OPEN ")) {
wordArray=lineOfCode.split(" ");
position=wordArray.indexOf("to");
if (position > -1) {
variableName.push(wordArray[position+1].trim());
return variableName;
}
position=wordArray.indexOf("TO");
if (position > -1) {
variableName.push(wordArray[position+1].trim());
return variableName;
}
}
//Check after THEN
if (lineOfCode.includes(" then")) {
lineOfCode=lineOfCode.split(" then")[1];
}
if (lineOfCode.includes(" THEN")) {
lineOfCode=lineOfCode.split(" THEN")[1];
}
position=lineOfCode.indexOf("=");
if (position > 0) {
var variables = lineOfCode.split("=");
for (var i = 0; i < variables.length; i++) {
let thisString = variables[i].trim();;
let lastCharacter=thisString.substr(thisString.length - 1);
if (lastCharacter==":" || lastCharacter=="+" || lastCharacter=="-") { continue; }
if (thisString.includes("(")) { continue; }
if (thisString.includes("<")) { continue; }
if (thisString.includes("else") || thisString.includes("ELSE")) { continue; }
if (thisString.includes("then") || thisString.includes("THEN")) { continue; }
if (thisString.includes("if") || thisString.includes("IF")) { continue; }
variableName.push(thisString.trim());
return variableName;
}
}
return variableName;
}
function loadIntelliSense(): CompletionItem[] {
// Load new IntelliSense
Intellisense = [];
var filePath = __dirname;
// Use the Language Type setting to drive the language file used for Intellisense
// MW: Ugly but let's try it for now
switch (settings.MVBasic.languageType) {
case "jBASE":
filePath =
filePath +
path.join("../", "../", "../", "Syntaxes", "jBASELanguage.json");
break;
case "OpenQM":
filePath =
filePath +
path.join("../", "../", "../", "Syntaxes", "QMLanguage.json");
break;
case "UniVerse":
filePath =
filePath +
path.join("../", "../", "../", "Syntaxes", "UniVerseLanguage.json");
break;
case "MVON":
default:
filePath =
filePath +
path.join("../", "../", "../", "Syntaxes", "MvLanguage.json");
break;
}
filePath = path.normalize(filePath);
var languageDefinition = fs.readFileSync(filePath, "utf8");
var languageDefinitionList = JSON.parse(languageDefinition);
var keywords = languageDefinitionList.Language.Keywords;
for (let i = 0; i < keywords.length; i++) {
if (settings.MVBasic.useCamelCase === true) {
Intellisense.push({
data: Intellisense.length + 1,
label: keywords[i].key,
kind: keywords[i].icon,
detail: keywords[i].detail,
documentation: keywords[i].documentation
});
} else {
Intellisense.push({
data: Intellisense.length + 1,
label: keywords[i].key.toUpperCase(),
kind: keywords[i].icon,
detail: keywords[i].detail,
documentation: keywords[i].documentation
});
}
}
// Load CustomWord definition
// THIS REALLY OUGHT TO BE A JSON FILE INSTEAD OF A BESPOKE NAMED PAIR TEXT FILE
if (settings.MVBasic.customWordPath !== "") {
var contents = fs.readFileSync(settings.MVBasic.customWordPath, "utf8");
settings.MVBasic.customWords = "(";
var lines = contents.replace("\r", "").split("\n");
for (let i = 0; i < lines.length; i++) {
let parts = lines[i].split(":");
settings.MVBasic.customWords += parts[0].replace('"', "").replace('"', "") + "|";
}
settings.MVBasic.customWords = settings.MVBasic.customWords.substr(0, settings.MVBasic.customWords.length - 1) + ")";
var items = settings.MVBasic.customWords.split("|");
for (let i = 0; i < items.length; i++) {
Intellisense.push({
data: Intellisense.length + 1,
label: items[i],
kind: CompletionItemKind.Interface
});
}
}
// Load CustomFunction definition
if (settings.MVBasic.customFunctionPath !== "") {
var functionDefinition = fs.readFileSync(settings.MVBasic.customFunctionPath, "utf8");
var customFunctionList = JSON.parse(functionDefinition);
var functions = customFunctionList.Language.functions;
for (let i = 0; i < functions.length; i++) {
Intellisense.push({
data: Intellisense.length + 1,
label: functions[i].key,
insertText: functions[i].insertText,
kind: functions[i].kind,
detail: functions[i].detail,
documentation: functions[i].documentation
});
}
}
if (logLevel) {
connection.console.log(
`[Server(${process.pid})] Language definition loaded for ${settings.MVBasic.languageType}`
);
}
return Intellisense;
}
async function validateTextDocument(textDocument: TextDocument): Promise<void> {
let diagnostics: Diagnostic[] = [];
let originalLines = textDocument.getText().split(/\r?\n/g);
let lines: DocumentLine[] = [];
originalLines.forEach((lineOfCode, index) => lines.push({ lineNumber: index, lineOfCode }));
let problems = 0;
LabelList.length = 0;
let rBlockStart = new RegExp("(^| )(begin case$|(if|readnext|open|read|readv|readu|readt|locate|openseq|matread|create|readlist|openpath|find|findstr)\\s+?)", "i");
let rBlockAlways = new RegExp("(^| )(for |loop( |$))", "i");
let rBlockContinue = new RegExp(" (then|else|case|on error|locked)$", "i");
let rBlockEnd = new RegExp("(^| )(end|end case|next|next\\s+.+|repeat)$", "i");
let rStartFor = new RegExp("(^| )for\\s+[\\w.]+\\s*=", "i");
let rEndFor = new RegExp("(^| )next($|\\s+.+$)", "i");
let rStartLoop = new RegExp("(^| )loop( |$)\\s*?", "i");
let rEndLoop = new RegExp("(^| )repeat( |$)\\s*$", "i");
let rStartCase = new RegExp("(^| )begin case$", "i");
let rEndCase = new RegExp("^\\s*end case$", "i");
let rElseEnd = new RegExp("^\\s*end else$", "i");
let rLabel = new RegExp("^\\s*([\\w\\.\\$]+:(?!=)|[0-9\\.]+)", "i");
let rComment = new RegExp("^\\s*(\\*|!|REM\\s+?).*", "i"); // Start-of-line 0-or-more whitespace {* ! REM<space>} Anything
let tComment = new RegExp(";\\s*(\\*|!|REM\\s+?).*", "i"); // (something); {0-or-more whitespace} {* ! REM<space>} Anything
let lComment = new RegExp("^\\s*([\\w\\.]+:(?!=)|[0-9\\.]+)(\\s*(\\*|!|REM\\s+?).*)", "i");
let qStrings = new RegExp("'.*?'|\".*?\"|\\\\.*?\\\\", "g");
let rParenthesis = new RegExp("\\(.*\\)", "g");
let noCase = 0;
let noLoop = 0;
let noEndLoop = 0;
let noEndCase = 0;
// Remove all variables from Intellisense to start clean
if (settings.MVBasic.userVariablesEnabled === true) {
var i = Intellisense.length;
while (i--) {
if (Intellisense[i].kind === CompletionItemKind.Variable) {
Intellisense.splice(i, 1);
}
}
}
// first build a list of labels in the program and indentation levels, strip comments, break up ; delimited lines
let Level = 0;
var RowLevel: number[] = [lines.length];
let forNext = [] // FOR statements list
let forNextErr = [] // FOR NEXT errors list
for (var i = 0; i < lines.length && problems < settings.MVBasic.maxNumberOfProblems; i++) {
let line = lines[i];
// Begin cleanup lines[] array -- Remove and replace irrelevant code.
// replace comment line with blank line
line.lineOfCode = line.lineOfCode.replace(rComment, "");
// remove comments after label (no semi-colon)
if (lComment.test(line.lineOfCode)) {
let comment = lComment.exec(line.lineOfCode);
line.lineOfCode = comment![1];
}
// remove trailing comments with a semi-colon
line.lineOfCode = line.lineOfCode.replace(tComment, "");
// replace contents of parenthesis with spaces, maintaining original
// character positions for intellisense error highlighting.
let v = rParenthesis.exec(line.lineOfCode);
if (v !== null) {
let value = "(" + " ".repeat(v[0].length - 2) + ")";
line.lineOfCode = line.lineOfCode.replace(rParenthesis, value);
}
// replace contents of quoted strings with spaces, maintaining original
// character positions for intellisense error highlighting.
v = qStrings.exec(line.lineOfCode);
if (v !== null) {
let value = "'" + " ".repeat(v[0].length - 2) + "'";
line.lineOfCode = line.lineOfCode.replace(qStrings, value);
}
// Trim() trailing spaces
line.lineOfCode = line.lineOfCode.trimRight();
// Save cleaned line
lines[i] = line;
// End cleanup of lines[] array
/* Before we do anything else, split line into statements on semicolon
Should split lines like:
FOR F=1 TO 20;CRT "NEW=":F;NEXT F ;* COMMENT
Should not split lines such as:
LEASE.TYPE=OCONV(ID,"TLS.MASTER,LS.BILLING;X;38;38")
locate(acontinent,continents,1;position;’al’) then crt acontinent:’ is already there’
*/
if (line.lineOfCode.indexOf(";") > 0) {
let a = line.lineOfCode.split(";");
// Replace line i with the first statement
lines[i] = { lineNumber: line.lineNumber, lineOfCode: a[0].trimRight() };
line = lines[i];
// Insert new lines for each subsequent statement, but keep line.lineNumber the same
for (let j = 1; j < a.length; j++) {
lines.splice(i + j, 0, { lineNumber: line.lineNumber, lineOfCode: a[j].trimRight() });
}
}
//Check for variable names to add to Intellisens if it does not already exist
if (settings.MVBasic.userVariablesEnabled === true) {
let variableNames = getVariableName(line.lineOfCode);
if (variableNames != null) {
for (var vc = 0; vc < variableNames.length; vc++) {
let variableName = variableNames[vc];
if (variableName != '') {
var checkVariables = Intellisense.filter(IntellisenseFilter => IntellisenseFilter.label === variableName);
if (checkVariables.length < 1) {
Intellisense.push({
data: Intellisense.length + 1,
label: variableName,
kind: CompletionItemKind.Variable
});
}
}
}
}
}
// check opening and closing block FOR/NEXT - Track matches
// and build errors list (forNextErr[]).
let arrFor = rStartFor.exec(line.lineOfCode)
if (arrFor !== null) {
let sFor = arrFor[0].split("=");
let forvar = getWord(sFor[0], 2);
forNext.push({ forVar: forvar, forLine: i });
}
let arrNext = rEndFor.exec(line.lineOfCode)
if (arrNext !== null) {
let nextvar = getWord(arrNext[0], 2);
let pos = forNext.length - 1;
if (pos < 0) {
forNextErr.push({ errMsg: "Missing FOR statement - NEXT " + nextvar, errLine: i });
} else {
let o = forNext[pos];
if (nextvar != "" && o.forVar !== nextvar) {
forNextErr.push({ errMsg: "Missing NEXT statement - FOR " + o.forVar, errLine: o.forLine });
forNextErr.push({ errMsg: "Missing FOR statement - NEXT " + nextvar, errLine: i });
}
forNext.pop();
}
}
// Check for CASE/LOOP
if (rStartCase.test(line.lineOfCode)) {
noCase++;
}
if (rEndCase.test(line.lineOfCode)) {
noEndCase++;
}
if (rStartLoop.test(line.lineOfCode)) {
noLoop++;
}
if (rEndLoop.test(line.lineOfCode)) {
noEndLoop++;
}
// check block statements
if (rBlockStart.test(line.lineOfCode)) {
Level++;
if (rBlockContinue.test(line.lineOfCode) === false) {
// single line statement
Level--;
}
}
if (rBlockAlways.test(line.lineOfCode)) {
Level++;
}
if (rBlockEnd.test(line.lineOfCode)) {
Level--;
}
if (rElseEnd.test(line.lineOfCode)) {
// decrement 1 to cater for end else stements
Level--;
}
// 10 10: start: labels
if (rLabel.test(line.lineOfCode)) {
let label = "";
if (line !== null) {
let labels = rLabel.exec(line.lineOfCode.trim());
if (labels !== null) {
label = labels[0].trim().replace(":", "");
}
}
LabelList.push(new Label(label, line.lineNumber, Level, false));
}
RowLevel[i] = Level;
}
// Missing NEXT errors. More FOR statements than NEXT values matched.
forNext.forEach(function (o) {
let errorMsg = "Missing NEXT statement - FOR " + o.forVar;
let line = lines[o.forLine];
let diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Error,
range: {
start: { line: line.lineNumber, character: 0 },
end: { line: line.lineNumber, character: line.lineOfCode.length }
},
message: errorMsg,
source: "MV Basic"
};
if (hasDiagnosticRelatedInformationCapability) {
diagnostic.relatedInformation = [
{
location: {
uri: textDocument.uri,
range: Object.assign({}, diagnostic.range)
},
message: 'NEXT is a required closure for the FOR construct'
}
];
}
diagnostics.push(diagnostic);
});
// Missing END CASE statement
if (noCase != noEndCase) {
// find the innermost for
for (var i = 0; i < lines.length && problems < settings.MVBasic.maxNumberOfProblems; i++) {
let line = lines[i];
if (rStartCase.test(line.lineOfCode)) {
noCase--;
}
if (noCase == 0) {
let diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Error,
range: {
start: { line: line.lineNumber, character: 0 },
end: { line: line.lineNumber, character: line.lineOfCode.length }
},
message: `Missing END CASE statement`,
source: "MV Basic"
};
if (hasDiagnosticRelatedInformationCapability) {
diagnostic.relatedInformation = [
{
location: {
uri: textDocument.uri,
range: Object.assign({}, diagnostic.range)
},
message: 'END CASE is a required closure for the BEGIN CASE construct'
}
];
}
diagnostics.push(diagnostic);
}
}
}
// Missing REPEAT statement
if (noLoop != noEndLoop) {
// find the innermost for
for (var i = 0; i < lines.length && problems < settings.MVBasic.maxNumberOfProblems; i++) {
let line = lines[i];
if (rStartLoop.test(line.lineOfCode)) {
noLoop--;
}
if (noLoop == 0) {
let diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Error,
range: {
start: { line: line.lineNumber, character: 0 },
end: { line: line.lineNumber, character: line.lineOfCode.length }
},
message: `Missing REPEAT statement`,
source: "MV Basic"
};
if (hasDiagnosticRelatedInformationCapability) {
diagnostic.relatedInformation = [
{
location: {
uri: textDocument.uri,
range: Object.assign({}, diagnostic.range)
},
message: 'REPEAT is a required closure for the LOOP construct'
}
];
}
diagnostics.push(diagnostic);
}
}
}
// Missing END, END CASE or REPEAT statements
// if Level is != 0, we have mis matched code blocks
if (Level > 0) {
let lastLine = lines[lines.length - 1];
let diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Error,
range: {
start: { line: lastLine.lineNumber, character: 0 },
end: { line: lastLine.lineNumber, character: lastLine.lineOfCode.length }
},
message: `Missing END, END CASE or REPEAT statements`,
source: "MV Basic"
};
diagnostics.push(diagnostic);
}
// Missing GO, GO TO, GOTO, GOSUB
// regex to check for goto/gosub in a line
let rGoto = new RegExp("(^| )(go to|goto|go|gosub)(\\s+.*)", "i");
for (var i = 0; i < lines.length && problems < settings.MVBasic.maxNumberOfProblems; i++) {
let line = lines[i];
let labelName = "";
// check any gosubs or goto's to ensure label is present
let text = line.lineOfCode;
text.split(rGoto)
let arrLabels = rGoto.exec(text);
if (arrLabels == null) { continue }
text = arrLabels[3];
let labels = text.split(",");
for (let ndx = 0; ndx < labels.length; ndx++) {
const item = labels[ndx];
labelName = getWord(item, 1);
if (labelName.toLocaleLowerCase() == "to") {
labelName = getWord(item, 2);
}
let checkLabel = "";
let labelMatch = LabelList.find(label => label.LabelName === labelName);
if (labelMatch) {
// set the referened flag
labelMatch.Referenced = true;
if (
labelMatch.Level != RowLevel[i] &&
labelMatch.Level > 1 &&
settings.MVBasic.ignoreGotoScope === false
) {
// jumping into or out of a loop
let index = line.lineOfCode.indexOf(labelName);
let diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Warning,
range: {
start: { line: line.lineNumber, character: index },
end: { line: line.lineNumber, character: index + labelName.length }
},
message: `${labelName} goes out of scope. Invalid GOTO or GOSUB`,
source: "MV Basic"
};
if (hasDiagnosticRelatedInformationCapability) {
diagnostic.relatedInformation = [
{
location: {
uri: textDocument.uri,
range: Object.assign({}, diagnostic.range)
},
message: 'GOTO and GOSUB should not be used within loops'
}
];
}
diagnostics.push(diagnostic);
}
} else {
let index = line.lineOfCode.indexOf(labelName);
let diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Error,
range: {
start: { line: line.lineNumber, character: index },
end: { line: line.lineNumber, character: index + labelName.length }
},
message: `Label ${labelName} not found! - Invalid GOTO or GOSUB`,
source: "MV Basic"
};
if (hasDiagnosticRelatedInformationCapability) {
diagnostic.relatedInformation = [
{
location: {
uri: textDocument.uri,
range: Object.assign({}, diagnostic.range)
},
message: 'Labels must be defined in order for a GOTO or GOSUB to function'
}
];
}
diagnostics.push(diagnostic);
if (logLevel) {
connection.console.log(
`[Server(${process.pid})] CheckLabel: ${checkLabel} + MatchedLabel: ${labelMatch}`
);
}
}
}
}
// Missing Labels
LabelList.forEach(function (label) {
if (label.Referenced === false) {
let diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Warning,
range: {
start: { line: label.LineNumber, character: 0 },
end: { line: label.LineNumber, character: label.LabelName.length }
},
message: `Label ${label.LabelName} is not referenced`,
source: "MV Basic"
};
if (hasDiagnosticRelatedInformationCapability) {
diagnostic.relatedInformation = [
{
location: {
uri: textDocument.uri,
range: Object.assign({}, diagnostic.range)
},
message: 'Labels should only be defined when needed for a GOTO or GOSUB'
}
];
}
diagnostics.push(diagnostic);
}
});
// Send the computed diagnostics to VSCode.
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
}
/* Connection Events */
// Listen on the connection
connection.listen();
let hasConfigurationCapability: boolean = false;
let hasWorkspaceFolderCapability: boolean = false;
let hasDiagnosticRelatedInformationCapability: boolean = false;
connection.onInitialize(
(params: InitializeParams): InitializeResult => {
let capabilities = params.capabilities;
// Does the client support the `workspace/configuration` request?
// If not, we fall back using global settings.
hasConfigurationCapability = !!(
capabilities.workspace && !!capabilities.workspace.configuration
);
hasWorkspaceFolderCapability = !!(
capabilities.workspace && !!capabilities.workspace.workspaceFolders
);
hasDiagnosticRelatedInformationCapability = !!(
capabilities.textDocument &&
capabilities.textDocument.publishDiagnostics &&
capabilities.textDocument.publishDiagnostics.relatedInformation
);
if (logLevel) {
connection.console.log(
`[Server(${process.pid})] Started and initialize received`
);
}
const result: InitializeResult = {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Incremental,
// Tell the client that this server supports code completion.
completionProvider: {
resolveProvider: true
},
definitionProvider: true,
referencesProvider: true,
documentSymbolProvider: true,
hoverProvider: true
}
};
if (hasWorkspaceFolderCapability) {
result.capabilities.workspace = {
workspaceFolders: {
supported: true
}
};
}
return result;
}
);
connection.onInitialized(() => {
if (hasConfigurationCapability) {
// Register for all configuration changes.
connection.client.register(DidChangeConfigurationNotification.type, undefined);
}
if (hasWorkspaceFolderCapability) {
connection.workspace.onDidChangeWorkspaceFolders(_event => {
connection.console.log('Workspace folder change event received.');
});
}
});
// The settings have changed. Is send on server activation as well.
connection.onNotification("languagePath", (path: string) => {
path = path + "";
});
// The global settings, used when the `workspace/configuration` request is not supported by the client.
// Please note that this is not the case when using this server with the client provided in this example
// but could happen with other clients.
const defaultSettings: Settings = {
MVBasic: {
maxNumberOfProblems: 100,
useCamelCase: true,
userVariablesEnabled: false,
ignoreGotoScope: false,
customWords: "",
customWordPath: "",
customFunctionPath: "",
languageType: "MVON",
trace: "off"
}
};
let settings: Settings = defaultSettings;
connection.onDidChangeConfiguration(change => {
if (change.settings) {
settings = <Settings>change.settings;
const _logLevel = <string>(settings.MVBasic.trace && settings.MVBasic.trace.server);
switch (_logLevel) {
case 'messages': logLevel = 1; break;
case 'verbose': logLevel = 2; break;
default: logLevel = 0;
}
loadIntelliSense();
// Revalidate any open text documents
documents.all().forEach(validateTextDocument);
}
});
connection.onDidChangeWatchedFiles(_change => {
// Monitored files have change in VSCode
if (logLevel) {
connection.console.log("We received an file change event");
}
});
// This handler provides the initial list of the completion items.
connection.onCompletion(
(_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => {
// The pass parameter contains the position of the text document in
// which code complete got requested. For the example we ignore this
// info and always provide the same completion items.
// if the previous word is GOTO GOSUB or GO TO make a list of labels available
let lineNo = _textDocumentPosition.position.line;
let charCount = _textDocumentPosition.position.character;
let document = documents.get(_textDocumentPosition.textDocument.uri);
if (document) {
let lines = document.getText().split(/\r?\n/g);
let line = lines[lineNo].toLocaleLowerCase().replace("go to", "goto");
while (charCount > 0) {
charCount--;
let statement = "";
if (line[charCount] != " ") {
let wordStart = charCount;
while (charCount > 0) {
charCount--;
if (line[charCount] === " ") {
break;
}
}
if (charCount === 0) {
charCount--;
}
statement = line.substr(charCount + 1, wordStart - charCount);
if (
statement.toLocaleLowerCase() === "gosub" ||
statement.toLocaleLowerCase() === "go" ||
statement.toLocaleLowerCase() === "go to" ||
statement.toLocaleLowerCase() === "goto"
) {
for (let i = 0; i < LabelList.length; i++) {
if (!Intellisense.some(statementLabel => statementLabel['label'] === LabelList[i].LabelName)) {
Intellisense.push({
label: LabelList[i].LabelName,
kind: CompletionItemKind.Reference,
data: 999
});
}
}
}
}
}
}
return Intellisense;
}
);
// This handler resolve additional information for the item selected in the completion list.
connection.onCompletionResolve(
(item: CompletionItem): CompletionItem => {
if (Intellisense != undefined) {
for (let i = 0; i < Intellisense.length; i++) {
if (Intellisense[i].data == item.data) {
item.documentation = Intellisense[i].documentation;
item.detail = Intellisense[i].detail;
}
}
return item;
}
return item;
}
);
connection.onReferences(params => {
var locations: Location[] = [];
let uri = params.textDocument.uri;
let xpos = params.position.character;
let ypos = params.position.line;
let doc = documents.get(uri);
if (doc) {
let lines = doc.getText().split(/\r?\n/g);
let line = lines[ypos];
// scan back to the begining of the word
while (xpos > 0) {
let char = line.substr(xpos, 1);
if (" +-*/><".indexOf(char) != -1) {
break;
}
xpos--;
}
let labelStart = xpos;