aboutsummaryrefslogtreecommitdiffstats
path: root/atk16_syntax
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2024-02-21 12:07:07 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2024-02-21 12:07:07 +0200
commit927c018f12e73c254cec32c0f60e1c01b9fc0319 (patch)
tree21fbb975e3567cb0d9ab3fb34519c50325571d6c /atk16_syntax
parenta0b4a1a6107e4dc905b21858d8ac76c47293cfb4 (diff)
Refactor names
Diffstat (limited to 'atk16_syntax')
-rw-r--r--atk16_syntax/README.md9
-rw-r--r--atk16_syntax/atk16-syntax-0.0.1.vsixbin0 -> 4351 bytes
-rw-r--r--atk16_syntax/extension.js196
-rw-r--r--atk16_syntax/language-configuration.json41
-rw-r--r--atk16_syntax/package.json48
-rw-r--r--atk16_syntax/syntaxes/atk16.tmLanguage.json44
6 files changed, 338 insertions, 0 deletions
diff --git a/atk16_syntax/README.md b/atk16_syntax/README.md
new file mode 100644
index 0000000..da81144
--- /dev/null
+++ b/atk16_syntax/README.md
@@ -0,0 +1,9 @@
+# VSCode syntax highlighting extension for ATK16 assembly
+
+## Installation
+
+In `atk16-syntax-highlighting` directory, run:
+
+ npx @vscode/vsce package
+
+Then go to VSCode > Command Palette > Extensions: Install from VSIX and select the generated `.vsix` file.
diff --git a/atk16_syntax/atk16-syntax-0.0.1.vsix b/atk16_syntax/atk16-syntax-0.0.1.vsix
new file mode 100644
index 0000000..253d326
--- /dev/null
+++ b/atk16_syntax/atk16-syntax-0.0.1.vsix
Binary files differ
diff --git a/atk16_syntax/extension.js b/atk16_syntax/extension.js
new file mode 100644
index 0000000..26b4d9d
--- /dev/null
+++ b/atk16_syntax/extension.js
@@ -0,0 +1,196 @@
+const vscode = require('vscode');
+const path = require('path');
+
+class ATK16HoverProvider {
+ async provideHover(document, position, token) {
+ const range = document.getWordRangeAtPosition(position, /[\w-/.]+/);
+ const labelText = document.getText(range);
+ const subroutineDefinition = await findSubroutineInDocument(document, labelText);
+ if (subroutineDefinition) {
+ const hoverText = new vscode.MarkdownString(subroutineDefinition);
+ hoverText.isTrusted = true;
+ return new vscode.Hover(hoverText);
+ }
+
+ // Search for labels in included files
+ for (let i = 0; i < document.lineCount; i++) {
+ const line = document.lineAt(i);
+ const includeMatch = line.text.match(/@include\s+(\S+)/);
+ if (includeMatch) {
+ const includedFilePath = path.join(
+ path.dirname(document.fileName),
+ `${includeMatch[1]}.atk16`
+ );
+
+ if (await vscode.workspace.fs.stat(vscode.Uri.file(includedFilePath))) {
+ const includedDocument = await vscode.workspace.openTextDocument(
+ includedFilePath
+ );
+ const subroutineDefinition = await findSubroutineInDocument(includedDocument, labelText);
+ if (subroutineDefinition) {
+ const hoverText = new vscode.MarkdownString(subroutineDefinition);
+ hoverText.isTrusted = true;
+ return new vscode.Hover(hoverText);
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+}
+
+class ATK16DefinitionProvider {
+ async provideDefinition(document, position, token) {
+ const range = document.getWordRangeAtPosition(position, /[\w-/.]+/);
+ const text = document.getText(range);
+
+ const line = document.lineAt(position);
+ const includeMatch = line.text.match(/@include\s+(\S+)/);
+ const useMatch = line.text.match(/@use\s+(\S+):/);
+
+ if (includeMatch && includeMatch[1] === text) {
+ const includedFilePath = path.join(
+ path.dirname(document.fileName),
+ `${includeMatch[1]}.atk16`
+ );
+
+ try {
+ await vscode.workspace.fs.stat(vscode.Uri.file(includedFilePath));
+ return new vscode.Location(
+ vscode.Uri.file(includedFilePath),
+ new vscode.Position(0, 0)
+ );
+ } catch (err) {
+ vscode.window.showErrorMessage(
+ `ATK16: File not found: ${includedFilePath}`
+ );
+ }
+ } else if (useMatch && useMatch[1] === text) {
+ const includedFilePath = path.join(
+ path.dirname(document.fileName),
+ `${useMatch[1]}.py`
+ );
+
+ try {
+ await vscode.workspace.fs.stat(vscode.Uri.file(includedFilePath));
+ return new vscode.Location(
+ vscode.Uri.file(includedFilePath),
+ new vscode.Position(0, 0)
+ );
+ } catch (err) {
+ vscode.window.showErrorMessage(
+ `ATK16: File not found: ${includedFilePath}`
+ );
+ }
+ } else {
+ const labelText = document.getText(range);
+ let definitionLocation = await findDefinitionInDocument(document, labelText);
+ if (definitionLocation) {
+ return definitionLocation;
+ }
+
+ // Search for labels in included files
+ for (let i = 0; i < document.lineCount; i++) {
+ const line = document.lineAt(i);
+ const includeMatch = line.text.match(/@include\s+(\S+)/);
+ if (includeMatch) {
+ const includedFilePath = path.join(
+ path.dirname(document.fileName),
+ `${includeMatch[1]}.atk16`
+ );
+
+ if (await vscode.workspace.fs.stat(vscode.Uri.file(includedFilePath))) {
+ const includedDocument = await vscode.workspace.openTextDocument(
+ includedFilePath
+ );
+ definitionLocation = await findDefinitionInDocument(includedDocument, labelText);
+ if (definitionLocation) {
+ return definitionLocation;
+ }
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+}
+
+async function findDefinitionInDocument(document, labelText) {
+ for (let i = 0; i < document.lineCount; i++) {
+ const line = document.lineAt(i);
+ if (line.text.includes(`@label ${labelText}`) || line.text.includes(`@let ${labelText}`)) {
+ return new vscode.Location(document.uri, line.range.start);
+ }
+ }
+ return null;
+}
+
+async function findSubroutineInDocument(document, labelText) {
+ let name = null;
+ const params = [];
+ const returns = [];
+ let extra = "";
+ for (let i = 0; i < document.lineCount; i++) {
+ const line = document.lineAt(i);
+ if (line.text.includes(`@label ${labelText}`)) {
+ for (let j = i + 1; j < document.lineCount; j++) {
+ const docLine = document.lineAt(j);
+ const docCommentMatch = docLine.text.match(/;;;\s+(.*)/);
+ if (docCommentMatch) {
+ const lineText = docCommentMatch[1];
+ const parts = lineText.trim().split(/\s+/g);
+ if (lineText.startsWith('subroutine')) {
+ name = parts[parts.length - 1].trim();
+ } else if (lineText.startsWith('param')) {
+ const [_kw, reg, ...desc] = parts;
+ params.push([reg.trim(), desc.filter(a => a).join(" ")])
+ } else if (lineText.startsWith('return')) {
+ const [_kw, reg, ...desc] = parts;
+ returns.push([reg.trim(), desc.filter(a => a).join(" ")])
+ } else {
+ extra += `${lineText}\n`;
+ }
+ } else {
+ break;
+ }
+ }
+ }
+ }
+
+ if (name === null) return null;
+
+ let definition = `### ${name}\n_Subroutine_\n\n`;
+ definition += extra;
+ if (params.length > 0) {
+ definition += "\n\n|Reg|Parameter|\n";
+ definition += "|:---|:---|\n";
+ }
+ for (const [reg, desc] of params) {
+ definition += `|${reg}|${desc}|\n`;
+ }
+
+ if (returns.length > 0) {
+ definition += "\n|Reg|Return|\n";
+ definition += "|:---|:---|\n";
+ }
+ for (const [reg, desc] of returns) {
+ definition += `|${reg}|${desc}|\n`;
+ }
+ return definition;
+}
+
+function activate(context) {
+ context.subscriptions.push(
+ vscode.languages.registerDefinitionProvider('atk16', new ATK16DefinitionProvider()),
+ vscode.languages.registerHoverProvider('atk16', new ATK16HoverProvider())
+ );
+}
+
+function deactivate() {}
+
+module.exports = {
+ activate,
+ deactivate,
+}; \ No newline at end of file
diff --git a/atk16_syntax/language-configuration.json b/atk16_syntax/language-configuration.json
new file mode 100644
index 0000000..c186b1b
--- /dev/null
+++ b/atk16_syntax/language-configuration.json
@@ -0,0 +1,41 @@
+{
+ "comments": {
+ "lineComment": ";"
+ },
+ "brackets": [
+ [
+ "{",
+ "}"
+ ],
+ [
+ "[",
+ "]"
+ ],
+ [
+ "(",
+ ")"
+ ]
+ ],
+ "autoClosingPairs": [
+ [
+ "{",
+ "}"
+ ],
+ [
+ "[",
+ "]"
+ ],
+ [
+ "(",
+ ")"
+ ],
+ [
+ "\"",
+ "\""
+ ],
+ [
+ "'",
+ "'"
+ ]
+ ]
+} \ No newline at end of file
diff --git a/atk16_syntax/package.json b/atk16_syntax/package.json
new file mode 100644
index 0000000..6a64b6c
--- /dev/null
+++ b/atk16_syntax/package.json
@@ -0,0 +1,48 @@
+{
+ "name": "atk16-syntax",
+ "displayName": "ATK16 Syntax",
+ "description": "Syntax highlighting and support for ATK16 assembly language",
+ "version": "0.0.1",
+ "publisher": "JanTuomi",
+ "license": "MIT",
+ "repository": "",
+ "engines": {
+ "vscode": "^1.60.0"
+ },
+ "categories": ["Programming Languages"],
+ "activationEvents": [
+ "onLanguage:atk16"
+ ],
+ "main": "./extension.js",
+ "contributes": {
+ "languages": [
+ {
+ "id": "atk16",
+ "aliases": ["ATK16 Assembly", "atk16"],
+ "extensions": [".atk16"],
+ "configuration": "./language-configuration.json"
+ }
+ ],
+ "grammars": [
+ {
+ "language": "atk16",
+ "scopeName": "source.atk16",
+ "path": "./syntaxes/atk16.tmLanguage.json"
+ }
+ ],
+ "configurationDefaults": {
+ "editor.tokenColorCustomizations": {
+ "[Default Dark+]": {
+ "textMateRules": [
+ {
+ "scope": "comment.documentation.atk16",
+ "settings": {
+ "fontStyle": "italic"
+ }
+ }
+ ]
+ }
+ }
+ }
+ }
+}
diff --git a/atk16_syntax/syntaxes/atk16.tmLanguage.json b/atk16_syntax/syntaxes/atk16.tmLanguage.json
new file mode 100644
index 0000000..6dcf0ea
--- /dev/null
+++ b/atk16_syntax/syntaxes/atk16.tmLanguage.json
@@ -0,0 +1,44 @@
+{
+ "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
+ "name": "ATK16",
+ "patterns": [
+ {
+ "name": "comment.documentation.atk16",
+ "match": ";;;.*"
+ },
+ {
+ "name": "comment.line.semicolon.atk16",
+ "match": ";.*"
+ },
+ {
+ "name": "keyword.control.directive.atk16",
+ "match": "^@\\w+"
+ },
+ {
+ "name": "variable.other.register.atk16",
+ "match": "\\bR[A-H]\\b"
+ },
+ {
+ "name": "constant.numeric.hex.atk16",
+ "match": "\\b0x[0-9a-fA-F]+\\b"
+ },
+ {
+ "name": "constant.numeric.binary.atk16",
+ "match": "\\b0b[01]+\\b"
+ },
+ {
+ "name": "constant.numeric.decimal.atk16",
+ "match": "\\b[0-9]+\\b"
+ },
+ {
+ "name": "keyword.control.symbol.atk16",
+ "match": "^\\s+[a-zA-Z_-]+"
+ },
+ {
+ "name": "constant.language.flag.atk16",
+ "match": "\\b(carry|overflow|zero|sign)\\b"
+ }
+ ],
+ "repository": {},
+ "scopeName": "source.atk16"
+}