1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2026-03-12 04:41:54 +00:00

lang-6502 stream parser

This commit is contained in:
Fred Sauer
2026-02-14 23:06:58 -08:00
parent 0f91ac7ab7
commit 7c072e0cfb
2 changed files with 96 additions and 95 deletions

View File

@@ -1,95 +0,0 @@
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
// 6502 DASM syntax
CodeMirror.defineMode('6502', function(_config, parserConfig) {
var keywords1, keywords2;
var directives_list = [
'processor',
'byte','word','long',
'include','seg','dc','ds','dv','hex','err','org','rorg','echo','rend',
'align','subroutine','equ','eqm','set','mac','endm','mexit','ifconst',
'ifnconst','if','else','endif','eif','repeat','repend'];
var directives = new Map();
directives_list.forEach(function(s) { directives.set(s, 'keyword'); });
var opcodes = /^[a-z][a-z][a-z]\b/i;
var numbers = /^([\da-f]+h|[0-7]+o|[01]+b|\d+d?)\b/i;
return {
startState: function() {
return {
context: 0
};
},
token: function(stream, state) {
if (!stream.column()) {
state.context = 0;
if (stream.eatWhile(/[\w.]/))
return 'tag';
}
if (stream.eatSpace())
return null;
var w;
if (stream.eatWhile(/\w/)) {
w = stream.current();
var cur = w.toLowerCase();
var style = directives.get(cur);
if (style)
return style;
if (opcodes.test(w)) {
state.context = 4;
return 'keyword';
} else if (state.context == 4 && numbers.test(w)) {
return 'number';
} else if (stream.match(numbers)) {
return 'number';
} else {
return null;
}
} else if (stream.eat(';')) {
stream.skipToEnd();
return 'comment';
} else if (stream.eat('"')) {
while (w = stream.next()) {
if (w == '"')
break;
if (w == '\\')
stream.next();
}
return 'string';
} else if (stream.eat('\'')) {
if (stream.match(/\\?.'/))
return 'number';
} else if (stream.eat('$') || stream.eat('#')) {
if (stream.eatWhile(/[^;]/i))
return 'number';
} else if (stream.eat('%')) {
if (stream.eatWhile(/[01]/))
return 'number';
} else {
stream.next();
}
return null;
}
};
});
CodeMirror.defineMIME("text/x-6502", "6502");
});

96
src/parser/lang-6502.ts Normal file
View File

@@ -0,0 +1,96 @@
// CodeMirror 6 language support for 6502 assembly
// Migrated from CodeMirror 5 mode
// Original copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: https://codemirror.net/5/LICENSE
import { StreamLanguage, StreamParser } from "@codemirror/language";
import { LanguageSupport } from "@codemirror/language";
// TODO: Migrate to CodeMirror 6 Lezer parser.
const asm6502Parser: StreamParser<{ context: number }> = {
startState() {
return {
context: 0
};
},
token(stream, state) {
// Labels at start of line
if (!stream.column()) {
state.context = 0;
if (stream.eatWhile(/[\w.]/))
return 'labelName';
}
if (stream.eatSpace())
return null;
var w;
if (stream.eatWhile(/\w/)) {
w = stream.current();
var cur = w.toLowerCase();
// Check for directives
var style = directives.get(cur);
if (style)
return style;
// Check for opcodes (3-letter mnemonics)
if (opcodes.test(w)) {
state.context = 4;
return 'keyword';
} else if (state.context == 4 && numbers.test(w)) {
return 'number';
} else if (stream.match(numbers)) {
return 'number';
} else {
return null;
}
} else if (stream.eat(';')) {
stream.skipToEnd();
return 'comment';
} else if (stream.eat('"')) {
while (w = stream.next()) {
if (w == '"')
break;
if (w == '\\')
stream.next();
}
return 'string';
} else if (stream.eat('\'')) {
if (stream.match(/\\?.'/) || stream.match(/\\?.'/))
return 'number';
} else if (stream.eat('$') || stream.eat('#')) {
if (stream.eatWhile(/[^;]/i))
return 'number';
} else if (stream.eat('%')) {
if (stream.eatWhile(/[01]/))
return 'number';
} else {
stream.next();
}
return null;
}
};
// Directive keywords
const directives_list = [
'processor',
'byte', 'word', 'long',
'include', 'seg', 'dc', 'ds', 'dv', 'hex', 'err', 'org', 'rorg', 'echo', 'rend',
'align', 'subroutine', 'equ', 'eqm', 'set', 'mac', 'endm', 'mexit', 'ifconst',
'ifnconst', 'if', 'else', 'endif', 'eif', 'repeat', 'repend'
];
const directives = new Map<string, string>();
directives_list.forEach(function (s) { directives.set(s, 'keyword'); });
const opcodes = /^[a-z][a-z][a-z]\b/i;
const numbers = /^([\da-f]+h|[0-7]+o|[01]+b|\d+d?)\b/i;
/**
* Language support for 6502 assembly language
*/
export function asm6502(): LanguageSupport {
return new LanguageSupport(StreamLanguage.define(asm6502Parser));
}