Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapted CodeMirror5 style for mode addon #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/awsm_theme_black.min.css">
<link rel="stylesheet" href="../../lib/codemirror.css">
<link rel="stylesheet" href="./codewars.css">
<script src="../../lib/codemirror.js"></script>
<script src="../../addon/mode/simple.js"></script>
<script src="./riscv.js"></script>
<style>
.CodeMirror { margin: auto; }
.text-center { text-align: center; }
Expand Down Expand Up @@ -34,14 +39,7 @@ <h1>RISC-V mode for CodeMirror</h1>
</main>

<script type="module">
import CodeMirror from "codemirror";
import "codemirror/addon/mode/simple.js";
import "codemirror/lib/codemirror.css";
import "./codewars.css";

import { defineMode } from "./riscv.js";
defineMode(CodeMirror);
const editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
mode: "riscv",
lineNumbers: true,
theme: "codewars",
Expand Down
43 changes: 28 additions & 15 deletions riscv.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"), require("../../addon/mode/simple"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror", "../../addon/mode/simple"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";

// prettier-ignore
const directives = [
// Architecture independent directives.
// http://sourceware.org/binutils/docs/as/Pseudo-Ops.html#Pseudo-Ops
"abort", "align", "altmacro", "ascii", "asciz",
"balign", "balignw", "balignl",
"bundle_align_mode", "bundle_lock", "bundle_unlock",
"byte", "cfi_startproc", "comm",
"byte", "cfi_endproc", "cfi_startproc", "cfi_undefined", "comm",
"data", "def", "desc", "dim", "double",
"eject", "else", "elseif", "end", "endef", "endfunc", "endif",
"equ", "equiv", "eqv", "err",
Expand All @@ -30,9 +40,9 @@ const directives = [
];
const directivesPattern = new RegExp("\\.(?:" + directives.join("|") + ")");

const registers = /x(?:[0-9]|[1-2][0-9]|31)|pc/;
const registers = /\b(?:x(?:[0-9]|[1-2][0-9]|30|31)|pc)\b/;
const registerAbiNames =
/zero|ra|[fsgt]p|t[0-6]|f?s(?:[0-9]|1[01])|f?a[0-7]|ft(?:[0-9]|1[01])/;
/\b(?:zero|ra|[fsgt]p|t[0-6]|f?s(?:[0-9]|1[01])|f?a[0-7]|ft(?:[0-9]|1[01]))\b/;

// https://github.com/riscv-non-isa/riscv-asm-manual/blob/master/riscv-asm.md#assembler-relocation-functions
// prettier-ignore
Expand All @@ -42,7 +52,7 @@ const relocationFunctions = [
"tls_ie_pcrel_hi", "tls_gd_pcrel_hi", "got_pcrel_hi",
];
const relocationFunctionPatterns = new RegExp(
"%(?:" + relocationFunctions.join("|") + ")\\([^)]+\\)"
"%(?:" + relocationFunctions.join("|") + ")(?=\\((?:[^)]+)\\))"
);

const instructions = (xs) =>
Expand All @@ -55,6 +65,7 @@ const pseudos = instructions([
"j", "jr", "la", "lla", "lga", "li",
"beqz", "bnez", "bgez", "blez", "bgtz", "bltz",
"bgt", "bgtu", "ble", "bleu",
"csrr", "csrw",
"seqz", "snez", "sgtz", "sltz",
"call", "tail", "fence",
"mv", "neg", "negw", "not", "nop", "ret",
Expand All @@ -67,7 +78,7 @@ const pseudos = instructions([
const baseI = instructions([
// RV32I/RV64I
"add", "addi", "and", "andi", "auipc", "beq", "bge", "bgeu", "blt", "bltu", "bne",
"csrrc", "csrrci", "csrrs", "csrrsi", "csrrw", "csrrwi", "ebreak", "ecall", "fence",
"csrrc", "csrrci", "csrrs", "csrrsi", "csrrw", "csrrwi", "csrwi", "ebreak", "ecall", "fence",
"fence.i", "jal", "jalr", "lb", "lbu", "lh", "lhu", "lui", "lw", "mret", "or", "ori",
"sb", "sfence.vma", "sh", "sll", "slli", "slt", "slti", "sltiu", "sltu", "sra", "srai",
"sret", "srl", "srli", "sub", "sw", "uret", "wfi", "xor", "xori",
Expand Down Expand Up @@ -122,22 +133,21 @@ const extC = instructions([
"c.slli", "c.srai", "c.srli", "c.sub", "c.subw", "c.sw", "c.swsp", "c.xor",
]);

export const defineMode = (CodeMirror) => {

CodeMirror.defineSimpleMode("riscv", {
meta: {
lineComment: "#",
},

start: [
{ regex: /#.*/, token: "comment" },
{ regex: /\/\*.*\*\//, token: "comment" },

// Labels
{ regex: /\w+:/, token: "tag" },
{ regex: /[1-9]\d*:/, token: "tag" },
{ regex: /\b(?:\w+:)/, token: "tag" },
{ regex: /\b(?:[1-9]\d*:)/, token: "tag" },
// Reference to local label
{ regex: /[1-9]\d*[bf]/, token: "variable-2" },
{ regex: /\b(?:[1-9]\d*[bf])\b/, token: "variable-2" },

// Integer literal
{ regex: /-?(?:0|[1-9]\d*|0x[0-9A-Fa-f]+)\b/, token: "number" },
{ regex: /\b(?:-|(?<![a-zA-Z0-9_]))(?:0|[1-9]\d*|0x[0-9A-Fa-f]+)\b/, token: "variable-2" },
// String literal
{ regex: /"(?:[^\\]|\\.)*?(?:"|$)/, token: "string" },

Expand All @@ -155,9 +165,12 @@ export const defineMode = (CodeMirror) => {

// Registers
{ regex: registers, token: "variable" },
{ regex: registerAbiNames, token: "variable-2" },
{ regex: registerAbiNames, token: "variable" },
],
meta: {
lineComment: "#",
}
});

CodeMirror.defineMIME("text/x-riscv", "riscv");
};
});