@exercism/codemirror-lang-assembly 0.0.0-stage → 1.0.1

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