-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
321 lines (287 loc) · 10.9 KB
/
extension.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
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
const vscode = require("vscode");
const AssemblyLinter = require('./src/AssemblyLinter');
const fs = require("fs");
const path = require("path");
const { Location, Position, Range } = require('vscode');
function checkIfLineIsInLanguageRegex(languageDict, document, position) {
const line = document.lineAt(position);
const text = line.text;
for (const patternName in languageDict) {
let regexCheck = text.match(languageDict[patternName].regexParsed);
if (regexCheck) {
return true;
}
}
return false;
}
function checkForInLanguageRegexJson(binLanguageDict, assemblyLanguageDict, document, position, simpleCheck = false) {
const line = document.lineAt(position);
const text = line.text;
if(simpleCheck)
console.log(text, position.e)
if(!simpleCheck) {
const firstSemiColon = text.indexOf(";");
if (firstSemiColon !== -1 && firstSemiColon <= position.character) {
return undefined;
}
if (text[position.e] == " ") return null;
}
let markdownString = new vscode.MarkdownString();
markdownString.isTrusted = true;
for (const patternName in binLanguageDict) {
// if the following match is 16 then there's no spaces in the binary number. assume binary.
let binaryNumberReg = text.match(/^ *([10]*)/)
let binaryNumberReg4Group = text.match(/^ *([10]{4} [10]{4} [10]{4} [10]{4})/)
if(binaryNumberReg4Group || (binaryNumberReg && binaryNumberReg[1].length == 16)) {
if(patternName != "constant.numeric.binary.lcc") continue;
}
let regexCheck = text.replace(/ /g, "").match(binLanguageDict[patternName].regexParsed);
if (regexCheck) {
if(simpleCheck){
console.log("valid")
return true;
}
let matchObj = binLanguageDict[patternName];
let header = "";
if (matchObj.descriptive_name) {
if (matchObj.Mnemonic) {
header = `${matchObj.Mnemonic}(${matchObj.descriptive_name}): `;
} else {
header = `${matchObj.descriptive_name}: `;
}
}
let description = matchObj.description ? matchObj.description + "\n" : "";
let binaryFormat = matchObj.binary_format
? `Binary format: ${matchObj.binary_format}\n`
: "";
let flags = `Flags set: ${
matchObj.flags_set ? matchObj.flags_set : "None"
}`;
let explaination = matchObj.explaination
? `\n${matchObj.explaination}\n`
: "";
let whatIsCalculated =
matchObj.binary_format.indexOf("offset") > -1 ? "Offset" : "Value";
let calculatedOffset = "";
if (regexCheck && regexCheck[1]) {
let decimalNumber = binaryStringToDecimal(regexCheck[1]);
let lineText = "";
if (whatIsCalculated === "Offset") {
let validLines = 0;
let i = 0;
for (i = position.line; i < document.lineCount; i++) {
if (
checkForInLanguageRegexJson(
binLanguageDict,
assemblyLanguageDict,
document,
new vscode.Position(i, 0),
true
)
) {
validLines++;
if (validLines == decimalNumber + 2) {
break;
}
}
}
let vscodeLineNumber =i;
if (vscodeLineNumber >= document.lineCount) {
// invalid line number
lineText = `Offset pointing to vscode line ${vscodeLineNumber + 1} is out of bounds\n`;
} else {
let line = document.lineAt(vscodeLineNumber);
lineText = `Offset pointing to vscode line ${vscodeLineNumber + 1}: \n${line.text.trim()}\n`;
}
}
// if the decimal number is a printable character, convert it to a char
let decimalToChar =
decimalNumber >= 32 && decimalNumber <= 126
? String.fromCharCode(decimalNumber)
: null;
let decimalToCharStr = decimalToChar ? ` = '${decimalToChar}' =` : "";
calculatedOffset = `${whatIsCalculated}: ${regexCheck[1].trim()} = ${decimalNumber}${decimalToCharStr} 0x${decimalNumber.toString(
16
)}\n${lineText}`;
}
markdownString.appendMarkdown(`\`\`\`lcc\n${header}${description}${binaryFormat}${calculatedOffset}${flags}${explaination}\n\`\`\``);
return markdownString;
//return `${header}${description}${binaryFormat}${calculatedOffset}${flags}${explaination}`;
}
}
for (const patternName in assemblyLanguageDict) {
let regexCheck = text.match(assemblyLanguageDict[patternName].regexParsed);
if (regexCheck) {
if(simpleCheck){
console.log("valid")
return true;
}
let matchObj = assemblyLanguageDict[patternName];
markdownString.appendMarkdown(`### ${matchObj.descriptive_name}\n\n`);
markdownString.appendMarkdown(` \n${matchObj.explaination}`);
markdownString.appendMarkdown(` \n**Binary info:**`);
markdownString.appendCodeblock(`${matchObj.binary_format}\n${matchObj.description}`, "javascript");
markdownString.appendMarkdown(` \nflags set: ${matchObj.flags_set}`);
return markdownString;
}
}
return undefined;
}
function binaryStringToDecimal(binaryString) {
let binaryNumber = binaryString.replace(/\s/g, "");
let isNegative = binaryNumber[0] === "1";
let decimalNumber;
if (isNegative) {
// Flip the bits
binaryNumber = binaryNumber
.split("")
.map((bit) => (bit === "1" ? "0" : "1"))
.join("");
// Add 1
decimalNumber = parseInt(binaryNumber, 2) + 1;
// Make the number negative
decimalNumber = -decimalNumber;
} else {
decimalNumber = parseInt(binaryNumber, 2);
}
return decimalNumber;
}
function loadRegexJson(languageDict, context, jsonFilePathStr, isBinaryData = true) {
const jsonFilePath = path.join(context.extensionPath, jsonFilePathStr);
// Load the JSON file
const fullRegexData = JSON.parse(fs.readFileSync(jsonFilePath, "utf8"));
if(!isBinaryData){
const assemblyRegex = fullRegexData["repository"]["assembly"]["patterns"];
for (const pattern of assemblyRegex) {
// iterate over array
languageDict[pattern.name] = pattern;
languageDict[pattern.name].regexParsed = new RegExp(
languageDict[pattern.name].match
); // cache the regex
languageDict[pattern.name].isAssembly = true;
}
} else {
const binaryRegex = fullRegexData["repository"]["binary"]["patterns"];
for (const pattern of binaryRegex) {
// iterate over array
languageDict[pattern.name] = pattern;
let firstSpace = false;
let str = languageDict[pattern.name].match; // ignoreSpacesInBinaryRegexString
str = str.replace(/ /g, function (match) {
if (!firstSpace) {
firstSpace = true;
return match;
} else {
return "";
}
});
languageDict[pattern.name].regexParsed = new RegExp(
str //languageDict[pattern.name].match
); // cache the regex
languageDict[pattern.name].isAssembly = false;
}
}
}
function activate(context) {
const binLanguageDict = {};
const assemblyLanguageDict = {};
loadRegexJson(binLanguageDict, context, path.join('syntaxes', 'lcc.tmLanguage.json'));
loadRegexJson(assemblyLanguageDict, context, path.join('syntaxes', 'lcc.tmLanguage.json'), false);
const linter = new AssemblyLinter();
// Lint all open assembly documents
vscode.workspace.textDocuments.forEach(linter.lintDocument, linter);
// Lint assembly documents when they are opened or changed
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(linter.lintDocument, linter));
context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(event => linter.lintDocument(event.document)));
// Dispose of the linter when the extension is deactivated
context.subscriptions.push({
dispose: () => linter.dispose()
});
context.subscriptions.push(
vscode.languages.registerHoverProvider("lcc", {
provideHover(document, position, token) {
let markdown = undefined;
// quite possible the dumbest way to do this
if(path.extname(document.uri.fsPath) == '.a') {
markdown = checkForInLanguageRegexJson(
{},
assemblyLanguageDict,
document,
position
);
} else if(path.extname(document.uri.fsPath) == '.bin') {
markdown = checkForInLanguageRegexJson(
binLanguageDict,
{},
document,
position
);
} else {
markdown = checkForInLanguageRegexJson(
binLanguageDict,
assemblyLanguageDict,
document,
position
);
}
if (markdown) {
return new vscode.Hover(
markdown//new vscode.MarkdownString("```lcc\n" + info + "\n```")
);
}
},
})
);
// add the ability to go to the label definition
context.subscriptions.push(vscode.languages.registerDefinitionProvider({ scheme: 'file', language: 'lcc' }, new LabelDefinitionProvider()));
// add the ability to toggle the linter warnings/errors
context.subscriptions.push(vscode.commands.registerCommand('lcc.toggleErrorLinting', () => {
linter.toggleErrorUnderlining()
linter.clearDiagnostics();
// delay the linting so that the underlining can be toggled
setTimeout(() => {
vscode.workspace.textDocuments.forEach(linter.lintDocument, linter);
}, 200);
}));
context.subscriptions.push(vscode.commands.registerCommand('lcc.toggleWarningLinting', () => {
linter.toggleWarningUnderlining()
linter.clearDiagnostics();
setTimeout(() => {
vscode.workspace.textDocuments.forEach(linter.lintDocument, linter);
}, 200);
}));
context.subscriptions.push(vscode.commands.registerCommand('lcc.toggleInformationLinting', () => {
linter.toggleInformationUnderlining()
linter.clearDiagnostics();
setTimeout(() => {
vscode.workspace.textDocuments.forEach(linter.lintDocument, linter);
}, 200);
}));
}
// class that enables the ability to go to the label definition
class LabelDefinitionProvider {
provideDefinition(document, position, token) {
const wordRange = document.getWordRangeAtPosition(position);
const word = document.getText(wordRange);
const text = document.getText();
const lines = text.split('\n');
const regex = /^([a-zA-Z_$@][a-zA-Z0-9_$@]*):?[\\s]*|^[\s]+([a-zA-Z_$@][a-zA-Z0-9_$@]*):[\s]*/;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const match = line.match(regex);
if (match && (match[1] === word || match[2] === word)) {
const start = new Position(i, 0);
const end = new Position(i, line.length);
const range = new Range(start, end);
return new Location(document.uri, range);
}
}
return null;
}
}
// This method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate,
};