@exercism/codemirror-lang-assembly 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +56 -0
- package/LICENSE +21 -0
- package/README.md +332 -2
- package/dist/index.cjs +1938 -0
- package/dist/index.d.cts +595 -0
- package/dist/index.d.ts +595 -0
- package/dist/index.js +1914 -0
- package/package.json +62 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
## 1.0.0
|
|
2
|
+
|
|
3
|
+
This is Exercism's first release of the package, published as
|
|
4
|
+
`@exercism/codemirror-lang-assembly`. It adds `x86`, `arm64` and `mips` dialects
|
|
5
|
+
of the grammar, each with completion, folding and indentation for its
|
|
6
|
+
instruction set.
|
|
7
|
+
|
|
8
|
+
## 0.2.0 (2026-08-13)
|
|
9
|
+
|
|
10
|
+
Two things, and between them they were most of what this grammar could not read.
|
|
11
|
+
|
|
12
|
+
**A brace may hold lines.** bass and armips write `scope AA {` and `if cond {`
|
|
13
|
+
with the statements underneath and the closing brace on a line of its own, where
|
|
14
|
+
every other assembler's brace holds operands: a register list to ARM, a mask to
|
|
15
|
+
AVX. The two are the same character and only what follows says which, so a brace
|
|
16
|
+
that ends its own line opens the block and one with operands behind it is the
|
|
17
|
+
list it always was. That decision sits in the tokenizer, which meant taking the
|
|
18
|
+
brace out of the built-in token set: Lezer's own tokenizer runs first and
|
|
19
|
+
settles what it can, which is why the star and the slash were already left out.
|
|
20
|
+
|
|
21
|
+
**A DOS file ends at a Ctrl-Z.** The bytes behind it are not part of the file,
|
|
22
|
+
which every assembler of that era knew and which the published source of MS-DOS
|
|
23
|
+
still relies on. 154 files in the corpus carry one.
|
|
24
|
+
|
|
25
|
+
Over 11155 published files of assembly, 88.61% parsed with no error node before
|
|
26
|
+
this release and 95.19% do. Error nodes fall from 29349 to 11327.
|
|
27
|
+
|
|
28
|
+
What is left is a parenthesised group written across several lines, which the
|
|
29
|
+
RISC-V test macros do; Merlin's `]name`, the Apple II assembler's local label;
|
|
30
|
+
and objdump listings with the C source interleaved, which is a corpus problem
|
|
31
|
+
and not a grammar one.
|
|
32
|
+
|
|
33
|
+
## 0.1.1 (2026-08-06)
|
|
34
|
+
|
|
35
|
+
The repository link, which npm renders as a link and a browser cannot follow
|
|
36
|
+
with `git+` in front of it.
|
|
37
|
+
|
|
38
|
+
## 0.1.0 (2026-07-31)
|
|
39
|
+
|
|
40
|
+
### First release
|
|
41
|
+
|
|
42
|
+
Assembly language support: a Lezer grammar covering the shape every assembler
|
|
43
|
+
shares, plus highlighting, folding and completion for directives, operand widths
|
|
44
|
+
and registers.
|
|
45
|
+
|
|
46
|
+
CodeMirror 6 has never shipped assembly, and no Lezer grammar for it existed
|
|
47
|
+
anywhere, so there was nothing to be compatible with. What an editor could
|
|
48
|
+
offer until now was the CodeMirror 5 stream mode for the GNU assembler, which
|
|
49
|
+
knows AT&T syntax and nothing else.
|
|
50
|
+
|
|
51
|
+
The grammar was developed against the 1966 assembly snippets of the
|
|
52
|
+
language-guessing corpus behind codincod.com, which carry 8080, Z80, NASM,
|
|
53
|
+
FASM, MASM, gas in both syntaxes, 68000, ARM, 6502, MIPS, PDP-8 and the IBM 360
|
|
54
|
+
assemblers. 97.81% of them parse with no error node, and 100% of their
|
|
55
|
+
non-whitespace characters come back with a highlight tag, against the 30.39%
|
|
56
|
+
the stream mode tagged.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CodinCod
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,333 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @exercism/codemirror-lang-assembly [](https://www.npmjs.org/package/@exercism/codemirror-lang-assembly)
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
## This fork
|
|
4
|
+
|
|
5
|
+
This is Exercism's fork of
|
|
6
|
+
[`@codincod/codemirror-lang-assembly`](https://codeberg.org/reeven/codemirror-lang-assembly)
|
|
7
|
+
by CodinCod, released under the MIT license. Exercism uses it in the x86-64
|
|
8
|
+
Assembly, ARM64 Assembly and MIPS Assembly editors on [exercism.org](https://exercism.org). Our changes are on
|
|
9
|
+
the `main` branch, and are published as `@exercism/codemirror-lang-assembly`.
|
|
10
|
+
|
|
11
|
+
The shared grammar still reads every assembler. The fork adds dialects to it.
|
|
12
|
+
A dialect gives the tokenizer one instruction set's registers and directives
|
|
13
|
+
in place of the shared lists. The first dialect is `x86`, which reads NASM as
|
|
14
|
+
written for x86-64:
|
|
15
|
+
|
|
16
|
+
- `x` and `a` are names, because x86 has no registers with those names.
|
|
17
|
+
- `.loop` is always a local label, and `loop .zero` is an instruction.
|
|
18
|
+
- `mov qword [x], 1` is an instruction. The shared grammar also reads this
|
|
19
|
+
correctly.
|
|
20
|
+
- `%1` is a macro parameter (`MacroParameter`), and `%%done:` is a label.
|
|
21
|
+
- `rep movsb` and `lock xadd` are one mnemonic each.
|
|
22
|
+
- `db` after `times` or `at` is a directive.
|
|
23
|
+
|
|
24
|
+
The second dialect is `arm64`, which reads the GNU assembler as written for
|
|
25
|
+
AArch64:
|
|
26
|
+
|
|
27
|
+
- The registers are `x0` to `x30`, `w0` to `w30`, `sp`, `xzr`, `lr`, the
|
|
28
|
+
floating point and vector registers, and the SVE registers. A vector
|
|
29
|
+
register keeps its arrangement, so `v0.16b` is one register. `a` and `x` are
|
|
30
|
+
names.
|
|
31
|
+
- Every directive starts with a dot, and `.loop` without a colon is a label.
|
|
32
|
+
- `count .req x19` names `count`.
|
|
33
|
+
- `lsl`, `uxtw` and the relocations in `:lo12:msg` are operand keywords.
|
|
34
|
+
- A condition code such as `eq` is a keyword when it ends the line, as in
|
|
35
|
+
`csel x0, x1, x2, eq`. Anywhere else it is a name, so `hi` can still be an
|
|
36
|
+
alias in `ldr x0, [hi]`.
|
|
37
|
+
- `1b` and `1f` refer to the numeric label `1:`.
|
|
38
|
+
|
|
39
|
+
The third dialect is `mips`, which reads MIPS32 as MARS assembles it. MARS
|
|
40
|
+
is the simulator that runs the MIPS track's tests.
|
|
41
|
+
|
|
42
|
+
- Every register has a `$` in front: `$t0`, `$a0`, `$zero`, `$31` and
|
|
43
|
+
`$f12`. `$a0` is a register, never a hexadecimal number, and a word without
|
|
44
|
+
a `$`, such as `sp`, is a name.
|
|
45
|
+
- `#` always opens a comment, so `#note` with no space after it is one too.
|
|
46
|
+
- `%count` is a macro parameter, as in `.macro incr(%count)`, and
|
|
47
|
+
`.end_macro` closes the macro.
|
|
48
|
+
|
|
49
|
+
In the shared grammar and every dialect, `#:lo12:msg`, `#~32` and a gas
|
|
50
|
+
macro's `#\offset` are immediates, and `.copy_\name:` is one label.
|
|
51
|
+
|
|
52
|
+
The package has four entry points:
|
|
53
|
+
|
|
54
|
+
- `assembly()` is upstream's support for every assembler.
|
|
55
|
+
- `x86_64Assembly()` is the support for x86-64 NASM, as used on Exercism's
|
|
56
|
+
x86-64 Assembly track. It offers:
|
|
57
|
+
- Completion of the x86-64 mnemonics where a statement starts, and of NASM's
|
|
58
|
+
directives, the preprocessor words after `%`, and the section names after
|
|
59
|
+
`section`.
|
|
60
|
+
- Completion of the registers and size keywords in an operand.
|
|
61
|
+
- Snippets for `section .text` with a global function, `section .data`,
|
|
62
|
+
`section .bss`, a function with a prologue and epilogue, a loop, a compare
|
|
63
|
+
and jump, `%macro` and `struc`.
|
|
64
|
+
- The names declared in the file: labels, `equ` constants, `%define` and
|
|
65
|
+
`%assign` names, macros, `extern` names and structure fields such as
|
|
66
|
+
`point.x`. A local label such as `.loop` is offered only after the global
|
|
67
|
+
label it belongs to, and a macro's `%%done` label only inside the macro.
|
|
68
|
+
- Folding of `%macro`, `%rep`, `%if`, `struc` and `istruc` blocks, and of a
|
|
69
|
+
function's body under its label.
|
|
70
|
+
- Indentation with labels at the margin and instructions one unit in.
|
|
71
|
+
|
|
72
|
+
- `arm64Assembly()` is the support for AArch64 in GNU assembler syntax, as
|
|
73
|
+
used on Exercism's ARM64 Assembly track. It offers:
|
|
74
|
+
- Completion of the AArch64 mnemonics where a statement starts, including
|
|
75
|
+
`b.eq` and the other conditional branches, and of the GNU assembler's
|
|
76
|
+
directives and the section names after `.section`.
|
|
77
|
+
- Completion of the registers, shifts, extensions and condition codes in an
|
|
78
|
+
operand, and of the relocations between colons, as in `:lo12:`.
|
|
79
|
+
- Snippets for `.text` with a global function, `.data`, `.bss`, a function
|
|
80
|
+
with an `stp` and `ldp` prologue and epilogue, a loop, a compare and
|
|
81
|
+
branch, `cbz`, loading an address with `adrp`, and `.macro`.
|
|
82
|
+
- The names declared in the file: labels, `.equ` and `.set` constants,
|
|
83
|
+
`.req` aliases and macros. A numeric label `1:` is offered as `1b` below
|
|
84
|
+
it and as `1f` above it. After `#`, only the constants are offered, and
|
|
85
|
+
after a backslash inside a macro, its parameters.
|
|
86
|
+
- Folding of `.macro`, `.rept`, `.irp` and `.if` blocks, and of a function's
|
|
87
|
+
body under its label.
|
|
88
|
+
- Indentation with labels at the margin and instructions one unit in, and
|
|
89
|
+
`//` for toggling comments.
|
|
90
|
+
|
|
91
|
+
- `mipsAssembly()` is the support for MIPS32 as MARS assembles it, as used
|
|
92
|
+
on Exercism's MIPS Assembly track. It offers:
|
|
93
|
+
- Completion of the MIPS32 instructions and the MARS pseudo-instructions,
|
|
94
|
+
such as `li`, `la`, `move` and `blt`, where a statement starts, and of
|
|
95
|
+
MARS's directives.
|
|
96
|
+
- Completion of the registers in an operand, by name and by number.
|
|
97
|
+
- Snippets for `.text` with a global function, `.data` with a string, a
|
|
98
|
+
function that saves `$ra` and `$s0` on the stack, a loop, an if and else,
|
|
99
|
+
the syscalls that print a string, print an integer, read an integer and
|
|
100
|
+
exit, and `.macro`.
|
|
101
|
+
- The names declared in the file: labels, `.eqv` constants and macros.
|
|
102
|
+
After `%` inside a macro, its parameters are offered.
|
|
103
|
+
- Folding of `.macro` blocks, and of the lines under a label.
|
|
104
|
+
- Indentation with labels at the margin and instructions one unit in, and
|
|
105
|
+
`#` for toggling comments.
|
|
106
|
+
|
|
107
|
+
Nothing is offered inside comments and strings, or where a new name is being
|
|
108
|
+
written after `%define`, `%macro`, `.equ`, `.eqv` or `.macro`.
|
|
109
|
+
|
|
110
|
+
The grammar reads `;` as a comment in every dialect. The GNU assembler for
|
|
111
|
+
AArch64 reads it as a statement separator, but none of the track's solutions
|
|
112
|
+
use it that way.
|
|
113
|
+
|
|
114
|
+
### Adding another instruction set
|
|
115
|
+
|
|
116
|
+
The line structure lives in `src/assembly.grammar` and `src/tokens.ts`, and
|
|
117
|
+
knows no instruction set. Each instruction set has its own files, such as these
|
|
118
|
+
for x86-64 (ARM64 and MIPS have the same three, named `arm64-*` and `mips-*`):
|
|
119
|
+
|
|
120
|
+
- `src/x86-64-vocabulary.ts` lists the registers, directives and size keywords
|
|
121
|
+
that the tokenizer reads, as a `Vocabulary`.
|
|
122
|
+
- `src/x86-64-mnemonics.ts` lists the mnemonics offered by completion.
|
|
123
|
+
- `src/x86-64.ts` builds the language, the completion and the snippets.
|
|
124
|
+
|
|
125
|
+
To add another one, such as RISC-V:
|
|
126
|
+
|
|
127
|
+
1. Name a dialect in `@dialects` in the grammar, and declare its
|
|
128
|
+
`Dialect_<name>` term in `src/parser.terms.d.ts`.
|
|
129
|
+
2. Write its vocabulary, and list it in `src/dialects.ts`. `src/vocabulary.ts`
|
|
130
|
+
describes each field. The optional fields cover a register suffix such as
|
|
131
|
+
ARM64's `.16b`, keywords that only count at the end of a line, gas's
|
|
132
|
+
`1b` and `1f`, registers written with a `$`, `#` as a comment and
|
|
133
|
+
`%name` macro parameters.
|
|
134
|
+
3. Build its entry point from `assemblyCompletionSource` (in `src/complete.ts`)
|
|
135
|
+
and `assemblyLayout` (in `src/layout.ts`). Both take the instruction set's
|
|
136
|
+
own words. For a GNU assembler dialect, pass `labels: "gas"` to both, and
|
|
137
|
+
the comment markers to the layout as `lineComments`.
|
|
138
|
+
4. Export it from `src/index.ts`, and add a test file in `test/`.
|
|
139
|
+
|
|
140
|
+
## Install
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
npm install @exercism/codemirror-lang-assembly
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Each Exercism track calls its own entry point:
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
import {EditorView, basicSetup} from "codemirror"
|
|
150
|
+
import {x86_64Assembly} from "@exercism/codemirror-lang-assembly"
|
|
151
|
+
// or {arm64Assembly} or {mipsAssembly}
|
|
152
|
+
|
|
153
|
+
new EditorView({
|
|
154
|
+
parent: document.body,
|
|
155
|
+
doc: `section .text\nglobal add\nadd:\n mov rax, rdi\n add rax, rsi\n ret\n`,
|
|
156
|
+
extensions: [basicSetup, x86_64Assembly()]
|
|
157
|
+
})
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Upstream README
|
|
161
|
+
|
|
162
|
+
[ [**CHANGELOG**](https://codeberg.org/reeven/codemirror-lang-assembly/src/branch/master/CHANGELOG.md) ]
|
|
163
|
+
|
|
164
|
+
This package implements assembly language support for the
|
|
165
|
+
[CodeMirror](https://codemirror.net/) code editor, using a
|
|
166
|
+
[Lezer](https://lezer.codemirror.net/) grammar written for this package.
|
|
167
|
+
|
|
168
|
+
It is not written for one assembler. It reads 8080, Z80, NASM, FASM, MASM, the
|
|
169
|
+
GNU assembler in both its syntaxes, 68000, ARM and AArch64, 6502, MIPS, RISC-V,
|
|
170
|
+
PDP-8 and the IBM 360 assemblers, because it is written for the shape all of
|
|
171
|
+
them share rather than the vocabulary none of them do.
|
|
172
|
+
|
|
173
|
+
CodeMirror 6 has never shipped assembly, and no Lezer grammar for it existed
|
|
174
|
+
anywhere before this one. What a CodeMirror 6 editor could offer until now was
|
|
175
|
+
the CodeMirror 5 stream mode for the GNU assembler, which knows AT&T syntax and
|
|
176
|
+
nothing else.
|
|
177
|
+
|
|
178
|
+
Written in part for [CodinCod](https://codincod.com/), a competitive coding
|
|
179
|
+
platform, where it colours the editor people solve puzzles in.
|
|
180
|
+
|
|
181
|
+
This code is released under an
|
|
182
|
+
[MIT license](https://codeberg.org/reeven/codemirror-lang-assembly/src/branch/master/LICENSE).
|
|
183
|
+
|
|
184
|
+
## Usage
|
|
185
|
+
|
|
186
|
+
```javascript
|
|
187
|
+
import {EditorView, basicSetup} from "codemirror"
|
|
188
|
+
import {assembly} from "@codincod/codemirror-lang-assembly"
|
|
189
|
+
|
|
190
|
+
const view = new EditorView({
|
|
191
|
+
parent: document.body,
|
|
192
|
+
doc: ` global main
|
|
193
|
+
section .text
|
|
194
|
+
main:
|
|
195
|
+
push rbp
|
|
196
|
+
mov rbp, rsp
|
|
197
|
+
xor eax, eax
|
|
198
|
+
lea rdi, [fmt]
|
|
199
|
+
call printf@PLT ; say it
|
|
200
|
+
leave
|
|
201
|
+
ret
|
|
202
|
+
|
|
203
|
+
section .data
|
|
204
|
+
fmt db "Hello, World!", 10, 0
|
|
205
|
+
`,
|
|
206
|
+
extensions: [basicSetup, assembly()]
|
|
207
|
+
})
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Coverage
|
|
211
|
+
|
|
212
|
+
Measured against the 1966 assembly snippets of the language-guessing corpus
|
|
213
|
+
behind CodinCod, which are drawn from Rosetta Code and carry every assembler
|
|
214
|
+
listed above: **97.81% parse with no error node, and 100% of their
|
|
215
|
+
non-whitespace characters come back with a highlight tag**. The stream mode this
|
|
216
|
+
replaces there tagged 30.39% of the same corpus.
|
|
217
|
+
|
|
218
|
+
The tag matters as much as the tree. A grammar that parses a file and leaves
|
|
219
|
+
half of it black has failed at the only job a highlighter has, and one that
|
|
220
|
+
colours everything by reading the file as one long string has failed at the
|
|
221
|
+
other. Both numbers are in `test/corpus.ts`, which takes the path to your own
|
|
222
|
+
directory of assembly files.
|
|
223
|
+
|
|
224
|
+
Of the files that do not parse cleanly, most are IBM 360 listings, where the
|
|
225
|
+
commentary on a line is written in the operand field with nothing in front of it
|
|
226
|
+
and only the column it starts in says where the operands stopped. A few are not
|
|
227
|
+
assembly at all: the corpus files them under assembly because a Rosetta Code
|
|
228
|
+
page did.
|
|
229
|
+
|
|
230
|
+
## The shape every assembler agrees on
|
|
231
|
+
|
|
232
|
+
label: mnemonic operand, operand ; comment
|
|
233
|
+
|
|
234
|
+
That is the whole grammar. Everything an assembler argues about sits inside
|
|
235
|
+
those four slots, and a grammar written for one assembler is wrong about the
|
|
236
|
+
next one, so this one is written for none of them and reads the slots.
|
|
237
|
+
|
|
238
|
+
The tree says less than a language grammar's would. There is no addressing-mode
|
|
239
|
+
node, because two assemblers spell the same mode differently and a third gives
|
|
240
|
+
that spelling to another mode. An operand is a run of atoms, and what the atoms
|
|
241
|
+
mean is the assembler's business. What the tree does say is which slot every
|
|
242
|
+
character stands in, which is what a highlighter needs and what an assembler
|
|
243
|
+
programmer reads.
|
|
244
|
+
|
|
245
|
+
## What a tokenizer has to settle
|
|
246
|
+
|
|
247
|
+
Four things cannot be written down as a grammar, and they are the whole of
|
|
248
|
+
`tokens.ts`.
|
|
249
|
+
|
|
250
|
+
**The first word of a line is not the first word of a statement.** `main:` is a
|
|
251
|
+
label, `mov` is a mnemonic and `.text` is a directive, and all three are a word
|
|
252
|
+
at the start of a line. A colon settles it when there is one. When there is
|
|
253
|
+
none, the word behind it does: `bdos equ 5h` declares `bdos`, because `equ` can
|
|
254
|
+
only follow a name, and `push a` does not, because `a` cannot. That one
|
|
255
|
+
lookahead is what reads FASM's `message db 'x'` and MASM's `msg BYTE "x"` and
|
|
256
|
+
the CP/M assembler's `wboot equ 0` without being told which assembler wrote
|
|
257
|
+
them. The size words are the exception it has to know about, because
|
|
258
|
+
`mov byte ptr [x], 1` writes one of them where a declaration would: the `ptr`
|
|
259
|
+
behind it says the line is an instruction after all.
|
|
260
|
+
|
|
261
|
+
**A word means one thing in a statement and another in an operand.** `byte` is a
|
|
262
|
+
directive that reserves one and a size that qualifies one. `a` is a
|
|
263
|
+
mnemonic-shaped word and it is the accumulator. The parser knows which slot it
|
|
264
|
+
is standing in, so the tokenizer asks it rather than guessing, and hands back a
|
|
265
|
+
different token for each. That is also what separates `%macro`, which opens a
|
|
266
|
+
statement, from `%eax`, which is a register.
|
|
267
|
+
|
|
268
|
+
**A sigil is not a sigil.** `$` is hexadecimal to a 6502 assembler, an immediate
|
|
269
|
+
to gas, a register prefix to a MIPS one and the current address to NASM. `%` is
|
|
270
|
+
a register, a binary literal, a remainder or a preprocessor word. `#` prefixes
|
|
271
|
+
an immediate or opens a comment. The character behind the sigil and the slot it
|
|
272
|
+
stands in decide which, and no token rule can see either. Hexadecimal wins the
|
|
273
|
+
one real tie: `$a0` is a MIPS register and a 6502 address, and an assembler that
|
|
274
|
+
prefixes its addresses writes far more of them than one that prefixes its
|
|
275
|
+
registers writes registers.
|
|
276
|
+
|
|
277
|
+
**A comment character is also an operator.** `#` opens a comment when a space
|
|
278
|
+
follows and prefixes an immediate when a digit does; `@` opens one when a space
|
|
279
|
+
follows and joins a symbol to its relocation when it does not. Both of those are
|
|
280
|
+
decided by the character behind them. `*` and `/` are decided by where they
|
|
281
|
+
stand: each opens a comment in column one, to the 68000 assemblers and to the
|
|
282
|
+
PDP-8's, and multiplies or divides everywhere else.
|
|
283
|
+
|
|
284
|
+
Lezer's own tokenizer runs before an external one and settles a token wherever
|
|
285
|
+
it can, so `*`, `/` and `%` are missing from the operator token rule on purpose.
|
|
286
|
+
Left there, they would never reach the tokenizer that has to decide what they
|
|
287
|
+
are.
|
|
288
|
+
|
|
289
|
+
## What the tree does not say
|
|
290
|
+
|
|
291
|
+
**A register is a list, and the list is shared.** A register is spelt like any
|
|
292
|
+
other word, and only the assembler reading it knows that `d0` names one and
|
|
293
|
+
`msg` does not, so this package carries the registers of every instruction set
|
|
294
|
+
it has been measured against and strips the prefix before the lookup. `%eax`,
|
|
295
|
+
`$t0` and `eax` all land on the same entry, which is what lets one grammar read
|
|
296
|
+
an assembler that insists on a prefix and one that forbids it. The cost is a
|
|
297
|
+
label named `a` or `x` read as a register, which is the trade every assembly
|
|
298
|
+
highlighter makes.
|
|
299
|
+
|
|
300
|
+
**A mnemonic is not in a list.** There is no opcode table here and there will
|
|
301
|
+
not be one. A mnemonic is the first word of a statement; that is what makes it a
|
|
302
|
+
mnemonic, and it is why an instruction set nobody has heard of reads as well as
|
|
303
|
+
x86 does.
|
|
304
|
+
|
|
305
|
+
**A bracket does not span a line.** Assembly has no expression that runs past
|
|
306
|
+
the newline, so a `(` left open ends at the line rather than swallowing the rest
|
|
307
|
+
of the file. The same goes for a quote: an unterminated literal ends where the
|
|
308
|
+
line does, because a lone apostrophe standing for a character is ordinary in
|
|
309
|
+
half these assemblers.
|
|
310
|
+
|
|
311
|
+
**A column is not counted.** The IBM assemblers put the commentary of a line in
|
|
312
|
+
the operand field with no marker in front of it, and only the column it starts
|
|
313
|
+
in says the operands have stopped. Reading that would mean counting columns,
|
|
314
|
+
which would be wrong for every other assembler on the list, so those comments
|
|
315
|
+
are read as operands.
|
|
316
|
+
|
|
317
|
+
## Prior art
|
|
318
|
+
|
|
319
|
+
There was no Lezer grammar to be compatible with, so this one is not compatible
|
|
320
|
+
with anything. Two things were read while writing it:
|
|
321
|
+
[`@defasm/codemirror`](https://github.com/NewDefectus/defasm), which highlights
|
|
322
|
+
x86-64 in gas syntax by assembling it, and
|
|
323
|
+
[`tree-sitter-asm`](https://github.com/RubixDev/tree-sitter-asm), whose generic
|
|
324
|
+
grammar makes the same bet this one does: that the shape is worth more than the
|
|
325
|
+
vocabulary.
|
|
326
|
+
|
|
327
|
+
## Building and testing
|
|
328
|
+
|
|
329
|
+
```
|
|
330
|
+
npm install # installs the dependencies and builds the package into dist/
|
|
331
|
+
npm run build # regenerates the parser, then rebuilds the package
|
|
332
|
+
npm test # builds the package, then runs the tests
|
|
333
|
+
```
|