m68k-parser
v1.0.0
Published
Motorola 68000 assembly parser
Maintainers
Readme
M68k Parser
A robust TypeScript parser for Motorola 68000 assembly language that generates an Abstract Syntax Tree (AST). Supports parsing individual lines or entire assembly files with comprehensive error reporting.
Features
- Parse M68k assembly code into a structured AST
- Support for all standard M68k instructions and directives
- Handles multiple addressing modes (immediate, indirect, indexed, PC-relative, etc.)
- Expression parsing with operators and symbols
- Register lists and FPU support
- Macro parameter support
- Resilient error handling - continues parsing and reports all errors
- Full TypeScript type definitions
- Both ESM and CommonJS support
- Command-line interface for quick parsing
Installation
npm install m68k-parserUsage
As a Library
Parse a single line:
import { parseLine } from 'm68k-parser';
const result = parseLine('label: move.w #1,d0 ; comment');
console.log(result.value);
// {
// label: { type: 'label', label: 'label', scope: 'global', ... },
// mnemonic: { type: 'instruction', instruction: 'move', ... },
// qualifier: { type: 'size', size: 'w', ... },
// operands: [...],
// comment: { type: 'comment', content: 'comment', ... }
// }
console.log(result.errors); // Array of parse errors, if anyParse an entire file:
import { parseFile } from 'm68k-parser';
const source = `
move.w #$1234,d0
bra.s loop
loop:
add.l d0,d1
rts
`;
const result = parseFile(source);
console.log(result.lines); // Array of ParsedLine objects
console.log(result.errors); // Array of all parse errorsCommand Line Interface
Parse a file and output the AST as JSON:
# Parse from a file
m68k-parser input.asm
# Parse from stdin
cat input.asm | m68k-parser
echo "move.w #1,d0" | m68k-parser -API Reference
parseLine(source: string): ParserResult<ParsedLine>
Parses a single line of M68k assembly code.
Returns: ParserResult<ParsedLine>
value: The parsed line structureerrors: Array of parse errors encountered
parseFile(source: string): ParsedFile
Parses an entire M68k assembly file.
Returns: ParsedFile
lines: Array of parsed lineserrors: Array of all parse errors from the file
Type Exports
All AST node types are exported for TypeScript users:
import type {
ParsedLine,
ParsedFile,
InstructionNode,
DirectiveNode,
OperandNode,
ExpressionNode,
// ... and many more
} from 'm68k-parser';Supported Features
Instructions
All standard M68k instructions including:
- Data movement:
move,movea,movem,lea, etc. - Arithmetic:
add,sub,mul,div, etc. - Logic:
and,or,eor,not, etc. - Shifts/rotates:
lsl,asr,rol,ror, etc. - Branches:
bra,beq,bne,jmp,jsr, etc. - FPU instructions (68881/68882)
Directives
Common assembler directives:
dc,ds,dcb- data definitionequ,set- symbol definitionsection,org- program organizationinclude,incbin- file inclusionmacro,endm- macro definition- And many more
Addressing Modes
- Immediate:
#100,#$FF - Data/Address registers:
d0-d7,a0-a7 - Register indirect:
(a0),(a0)+,-(a0) - Displacement:
10(a0),offset(a0) - Indexed:
10(a0,d1.w),(a0,d1.l*4) - Absolute:
$1000.w,label - PC-relative:
label(pc),10(pc,d0) - Memory indirect (68020+):
([bd,An,Rn],od) - Bitfields (68020+):
{offset:width}
Expressions
Full expression support with:
- Binary operators:
+,-,*,/,&,|,^,<<,>>, etc. - Unary operators:
-,~,! - Grouping with parentheses
- Numeric literals: decimal, hex (
$FF), binary (%1010), octal (@77) - Symbol references
- Current address (
*)
Example Output
Input:
start: move.w #$1234,d0Output:
{
"label": {
"type": "label",
"scope": "global",
"label": "start",
"loc": { "start": 0, "end": 5 }
},
"mnemonic": {
"type": "instruction",
"instruction": "move",
"loc": { "start": 8, "end": 12 }
},
"qualifier": {
"type": "size",
"size": "w",
"loc": { "start": 13, "end": 14 }
},
"operands": [
{
"type": "immediate",
"value": {
"type": "numeric-literal",
"format": "hex",
"raw": "$1234",
"value": 4660
}
},
{
"type": "data-register",
"register": "d0"
}
]
}Error Handling
The parser is resilient and will attempt to parse as much as possible, collecting errors along the way:
const result = parseFile('invalid syntax here\nmove.w d0,d1');
result.errors.forEach(error => {
console.log(`Line ${error.line}: ${error.message}`);
});
// Still provides parsed output for valid linesLicense
MIT - See LICENSE file for details
Author
Graham Bates
Repository
https://github.com/grahambates/m68k-parser
