-
Notifications
You must be signed in to change notification settings - Fork 0
/
mumps-language-token.js
167 lines (147 loc) · 5.29 KB
/
mumps-language-token.js
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
let vscode = require('vscode');
let Position = vscode.Position;
let Range = vscode.Range;
let lookupLabelReference = require('./mumps-label-lookup').lookupLabelReference;
let createDefinitionForLabelReference = require('./mumps-label-lookup').createDefinitionForLabelReference;
const definitionsArray = require('./language-definitions.json');
const definitions = {};
exports.definitions = definitions;
class MumpsToken {
constructor(document, position) {
this.document = document;
this.position = position;
this.range = document.getWordRangeAtPosition(position);
if (!this.range) {
return;
}
this.word = document.getText(this.range);
if (!this.word) {
return;
}
this.surroundWord = getWordWithSurrounds(document, this.range) || this.word;
if (this.isIntrinsic) {
this.word = '$' + this.word;
}
}
get mayBeCommand() {
if (!this.surroundWord) {
return false;
}
var lastChar = this.surroundWord.charAt(this.surroundWord.length - 1);
return isWhitespace(this.surroundWord.charAt(0)) &&
(lastChar === ':' ||
isWhitespace(lastChar) ||
this.surroundWord.length === this.word.length + 1); // end-of-line
}
get isIntrinsic() {
if (!this.surroundWord) {
return false;
}
return this.surroundWord.charAt(0) === '$' && this.surroundWord.charAt(1) !== '$';
}
get isFunctionCall() {
if (!this.surroundWord) {
return false;
}
return this.surroundWord.charAt(this.surroundWord.length - 1) === '(';
}
get isLabelReference() {
if (this._isLabelReference === undefined) {
let line = this.document.lineAt(this.range.start);
let word = this.isIntrinsic ? this.word.substring(1) : this.word;
let regex = new RegExp('(([ \t](D|DO|G|GOTO)[ \t]+)|\\$\\$)([%\\^\\+A-Z0-9]*' + word + '[%\\^\\+A-Z0-9]*)', 'i');
let match = regex.exec(line.text);
this._isLabelReference = match !== null;
if (match) {
let fullLabel = match[4];
let partsRegex = /([%A-Z][%A-Z0-9]*)?(\+\d+)?(\^[%A-Z][%A-Z0-9]*)?/gi;
let parts = partsRegex.exec(fullLabel);
this.label = parts[1];
this.labelOffset = Number(withoutFirstCharacter(parts[2]));
this.labelProgram = withoutFirstCharacter(parts[3]);
this.referredToLabel = lookupLabelReference(this);
}
}
return this._isLabelReference;
}
get definition() {
if (this._definition === undefined) {
this._definition = false;
let matches = definitions[this.word.toUpperCase()];
if (matches) {
for (var definition of matches) {
if (this.isFunctionCall && definition.type !== 'function') {
continue;
}
this._definition = definition;
break;
}
} else if (this.isLabelReference) {
this._definition = createDefinitionForLabelReference(this.referredToLabel);
}
if (this._definition && this._definition.type === 'function') {
this._definition.functionSignature = formatFunctionSignature(this._definition);
}
}
return this._definition;
}
}
exports.MumpsToken = MumpsToken;
function getWordWithSurrounds(document, range) {
if (range.start.character <= 0) {
return;
}
let start = new Position(range.start.line, range.start.character - 1);
let end = new Position(range.end.line, range.end.character + 1);
let surroundWord = document.getText(new Range(start, end));
// check for two dollar signs
if (surroundWord.charAt(0) === '$') {
start = new Position(start.line, start.character - 1);
let extendedWord = document.getText(new Range(start, end));
if (extendedWord.charAt(0) === '$') {
surroundWord = extendedWord;
}
}
return surroundWord;
}
function isWhitespace(char) {
return /\s+/.test(char);
}
function withoutFirstCharacter(string) {
return string ? string.substring(1) : string;
}
function formatFunctionSignature(definition) {
let signature = definition.name + '(';
if (definition.parameters) {
for (var i = 0; i < definition.parameters.length; i++) {
signature += formatParameter(definition.parameters[i], i === 0);
}
}
signature += ')';
if (definition.returns) {
signature += ':' + definition.returns.type;
}
return signature;
}
function formatParameter(parameter, first) {
let s = (first ? '' : ',');
s += parameter.name;
s += ':' + parameter.type;
if (parameter.optional) {
s = '[' + s + ']';
}
return s;
}
function addDefinition(name, definition) {
if (!definitions[name]) {
definitions[name] = [definition];
} else {
definitions[name].push(definition);
}
}
for (var definition of definitionsArray) {
addDefinition(definition.name, definition);
if (definition.abbreviation) {
addDefinition(definition.abbreviation, definition);
}
}