Skip to content

Commit 9a11194

Browse files
committed
6502/Z80 parsing & mbo style
- Opcodes and keywords are highlighted differently - Refactor, cleanup, and add several missing features for 6502/Z80 parsing and syntax highlighting - Treat `=` like `EQU`
1 parent e0b0622 commit 9a11194

7 files changed

Lines changed: 243 additions & 135 deletions

File tree

src/parser/lang-6502.grammar

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,37 @@ Statement {
1212
HexDirective |
1313
MacroDef |
1414
MacEnd |
15-
ControlOp |
16-
ErrorOp
15+
ControlOp
1716
}
1817

19-
Label { Identifier ":" | Identifier }
18+
Label { (Identifier | LocalIdentifier) Colon | Identifier | LocalIdentifier }
2019

2120
Instruction {
2221
Opcode Operand?
2322
}
2423

25-
Register {
26-
@specialize<Identifier, "a" | "x" | "y" | "A" | "X" | "Y">
27-
}
24+
@external specialize {Identifier} registerSpecializer from "../../src/parser/tokens-6502" { Register }
25+
@external specialize {Identifier} onOffSpecializer from "../../src/parser/tokens-6502" { OnOff }
26+
@external specialize {Identifier} hexOpSpecializer from "../../src/parser/tokens-6502" { HexOp }
27+
@external specialize {Identifier} opcodeSpecializer from "../../src/parser/tokens-6502" { Opcode }
2828

2929
Directive {
30-
PseudoOp (Expression)*
31-
}
32-
33-
PseudoOp {
34-
@specialize<Identifier,
35-
"org" | "equ" | "end" | ".end" |
36-
"ORG" | "EQU" | "END" | ".END" |
37-
"ds" | "ds.b" | "ds.w" | "dc" | "dc.b" | "dc.w" | "seg" | "seg.u" |
38-
"DS" | "DS.B" | "DS.W" | "DC" | "DC.B" | "DC.W" | "SEG" | "SEG.U" |
39-
"byte" | "word" | ".byte" | ".word" |
40-
"BYTE" | "WORD" | ".BYTE" | ".WORD" |
41-
"subroutine" | "SUBROUTINE" |
42-
"processor" | "PROCESSOR" |
43-
"echo" | "repeat" | "repend" | "set" |
44-
"ECHO" | "REPEAT" | "REPEND" | "SET"
45-
>
30+
PseudoOp (Expression)* |
31+
Equals (Expression)*
4632
}
4733

48-
HexOp { @specialize<Identifier, "hex" | "HEX"> }
34+
@external specialize {Identifier} pseudoOpSpecializer from "../../src/parser/tokens-6502" { PseudoOp }
35+
@external specialize {Identifier} localIdentifierSpecializer from "../../src/parser/tokens-6502" { LocalIdentifier }
4936

5037
HexDirective {
5138
HexOp HexByte*
5239
}
5340

5441
@external tokens hexTokenizer from "../../src/parser/tokens-6502" { HexByte }
5542

56-
Mac { @specialize<Identifier, "mac"> }
57-
MacEnd { @specialize<Identifier, "endm"> }
43+
@external specialize {Identifier} macSpecializer from "../../src/parser/tokens-6502" { Mac, MacEnd }
5844

59-
ControlOp { @specialize<Identifier, "if" | "else" | "endif"> }
60-
ErrorOp { @specialize<Identifier, "err"> }
45+
@external specialize {Identifier} controlOpSpecializer from "../../src/parser/tokens-6502" { ControlOp }
6146

6247
MacroDef {
6348
Mac Identifier
@@ -67,25 +52,6 @@ CurrentAddress {
6752
@specialize<Identifier, ".">
6853
}
6954

70-
Opcode {
71-
@specialize<Identifier,
72-
"adc" | "and" | "asl" | "bcc" | "bcs" | "beq" | "bit" | "bmi" |
73-
"bne" | "bpl" | "brk" | "bvc" | "bvs" | "clc" | "cld" | "cli" |
74-
"clv" | "cmp" | "cpx" | "cpy" | "dec" | "dex" | "dey" | "eor" |
75-
"inc" | "inx" | "iny" | "jmp" | "jsr" | "lda" | "ldx" | "ldy" |
76-
"lsr" | "nop" | "ora" | "pha" | "php" | "pla" | "plp" | "rol" |
77-
"ror" | "rti" | "rts" | "sbc" | "sec" | "sed" | "sei" | "sta" |
78-
"stx" | "sty" | "tax" | "tay" | "tsx" | "txa" | "txs" | "tya" |
79-
"ADC" | "AND" | "ASL" | "BCC" | "BCS" | "BEQ" | "BIT" | "BMI" |
80-
"BNE" | "BPL" | "BRK" | "BVC" | "BVS" | "CLC" | "CLD" | "CLI" |
81-
"CLV" | "CMP" | "CPX" | "CPY" | "DEC" | "DEX" | "DEY" | "EOR" |
82-
"INC" | "INX" | "INY" | "JMP" | "JSR" | "LDA" | "LDX" | "LDY" |
83-
"LSR" | "NOP" | "ORA" | "PHA" | "PHP" | "PLA" | "PLP" | "ROL" |
84-
"ROR" | "RTI" | "RTS" | "SBC" | "SEC" | "SED" | "SEI" | "STA" |
85-
"STX" | "STY" | "TAX" | "TAY" | "TSX" | "TXA" | "TXS" | "TYA"
86-
>
87-
}
88-
8955
Expression {
9056
Expression !logic LogicOp Expression |
9157
Expression !bit BitOp Expression |
@@ -108,13 +74,15 @@ UnaryGt { gt !un }
10874
Value {
10975
Number |
11076
Identifier |
77+
LocalIdentifier |
78+
OnOff |
11179
CurrentAddress |
11280
String |
11381
Char
11482
}
11583

11684
Operand {
117-
"#" Expression |
85+
ImmediatePrefix Expression |
11886
"(" Expression Comma Register ")" |
11987
Expression (Comma Register)? |
12088
Register
@@ -139,7 +107,8 @@ Operand {
139107
eol { $[\n\r]+ }
140108

141109
Comma { "," }
142-
"#"
110+
Colon { ":" }
111+
ImmediatePrefix { "#" }
143112
"(" ")"
144113

145114
ArithOp { "*" | "/" }
@@ -153,6 +122,7 @@ Operand {
153122
LogicOp { "&&" | "||" }
154123
Not { "!" }
155124

125+
Equals { "=" }
156126
CompareOp { "==" | "!=" | "<=" | ">=" }
157127
lt { "<" }
158128
gt { ">" }

src/parser/lang-6502.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,27 @@ export const Lezer6502: LRLanguage = LRLanguage.define({
1313
}),
1414
styleTags({
1515
Identifier: t.variableName,
16+
LocalIdentifier: t.local(t.variableName),
1617
CurrentAddress: t.self,
17-
PseudoOp: t.definition(t.variableName),
18-
Opcode: t.keyword,
18+
// t.constant()
19+
// t.function()
20+
// t.standard()
21+
// t.local()
22+
23+
// t.literal
24+
// t.modifier
25+
// t.quote
26+
// t.processingInstruction
27+
PseudoOp: t.keyword,
28+
Equals: t.keyword,
29+
Opcode: t.standard(t.keyword),
1930
Label: t.labelName,
2031
String: t.string,
21-
Char: t.number,
32+
Char: t.character,
2233
Number: t.number,
23-
Register: t.typeName,
24-
Comment: t.lineComment,
34+
Register: t.standard(t.modifier),
35+
OnOff: t.bool,
36+
Comment: t.comment,
2537
ArithOp: t.arithmeticOperator,
2638
Plus: t.arithmeticOperator,
2739
Minus: t.arithmeticOperator,
@@ -35,14 +47,15 @@ export const Lezer6502: LRLanguage = LRLanguage.define({
3547
BinaryGt: t.compareOperator,
3648
UnaryLt: t.arithmeticOperator,
3749
UnaryGt: t.arithmeticOperator,
38-
HexOp: t.definition(t.variableName),
50+
HexOp: t.keyword,
3951
HexByte: t.number,
4052
Mac: t.definitionKeyword,
4153
MacEnd: t.definitionKeyword,
4254
"MacroDef/Identifier": t.macroName,
4355
ControlOp: t.controlKeyword,
44-
ErrorOp: t.keyword,
56+
ImmediatePrefix: t.constant(t.modifier),
4557
Comma: t.separator,
58+
Colon: t.separator,
4659
"( )": t.paren
4760
})
4861
]

src/parser/lang-z80.grammar

Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ Line {
88

99
Statement {
1010
Instruction |
11-
Directive
11+
Directive |
12+
MacroDef |
13+
MacEnd |
14+
ControlOp
1215
}
1316

14-
Label { Identifier ":" | Identifier }
17+
Label { Identifier Colon Colon? | Identifier }
1518

1619
Instruction {
1720
Opcode Operand?
@@ -21,66 +24,16 @@ Directive {
2124
PseudoOp (Expression)*
2225
}
2326

24-
PseudoOp {
25-
@specialize<Identifier,
26-
"defb" | "defw" | "defm" |
27-
"DEFB" | "DEFW" | "DEFM" |
28-
"org" | "equ" | "end" | "public" |
29-
"ORG" | "EQU" | "END" | "PUBLIC"
30-
>
27+
MacroDef {
28+
Mac Identifier
3129
}
3230

33-
Condition {
34-
@specialize<Identifier,
35-
"nz" | "z" | "nc" | "po" | "pe" | "p" | "m" |
36-
"NZ" | "Z" | "NC" | "PO" | "PE" | "P" | "M"
37-
>
38-
}
39-
40-
Register {
41-
@specialize<Identifier,
42-
"a" | "b" | "c" | "d" | "e" | "h" | "l" | "i" | "r" | "af" | "bc" | "de" | "hl" | "ix" | "iy" | "sp" | "pc" | "psw" |
43-
"A" | "B" | "C" | "D" | "E" | "H" | "L" | "I" | "R" | "AF" | "BC" | "DE" | "HL" | "IX" | "IY" | "SP" | "PC" | "PSW"
44-
>
45-
}
46-
47-
Opcode {
48-
@specialize<Identifier,
49-
// Z80 Instructions
50-
"ld" | "push" | "pop" | "inc" | "dec" | "add" | "adc" | "sub" | "sbc" | "and" | "or" | "xor" |
51-
"cp" | "ret" | "jp" | "jr" | "call" | "rst" | "nop" | "halt" | "di" | "ei" |
52-
"im" | "ex" | "exx" | "neg" | "cpl" | "ccf" | "scf" | "rlca" | "rla" | "rrca" | "rra" |
53-
"rlc" | "rl" | "rrc" | "rr" | "sla" | "sra" | "srl" | "bit" | "set" | "res" |
54-
"out" | "in" | "djnz" | "rld" | "rrd" | "ldi" | "ldir" | "ldd" | "lddr" | "cpi" | "cpir" | "cpd" | "cpdr" |
55-
"ini" | "inir" | "ind" | "indr" | "outi" | "otir" | "outd" | "otdr" |
56-
57-
"LD" | "PUSH" | "POP" | "INC" | "DEC" | "ADD" | "ADC" | "SUB" | "SBC" | "AND" | "OR" | "XOR" |
58-
"CP" | "RET" | "JP" | "JR" | "CALL" | "RST" | "NOP" | "HALT" | "DI" | "EI" |
59-
"IM" | "EX" | "EXX" | "NEG" | "CPL" | "CCF" | "SCF" | "RLCA" | "RLA" | "RRCA" | "RRA" |
60-
"RLC" | "RL" | "RRC" | "RR" | "SLA" | "SRA" | "SRL" | "BIT" | "SET" | "RES" |
61-
"OUT" | "IN" | "DJNZ" | "RLD" | "RRD" | "LDI" | "LDIR" | "LDD" | "LDDR" | "CPI" | "CPIR" | "CPD" | "CPDR" |
62-
"INI" | "INIR" | "IND" | "INDR" | "OUTI" | "OTIR" | "OUTD" | "OTDR" |
63-
64-
// 8080 Instructions
65-
"mov" | "mvi" | "lxi" | "lda" | "sta" | "lhld" | "shld" | "ldax" | "stax" |
66-
"adi" | "aci" | "sui" | "sbi" | "sbb" | "ana" | "ani" | "xra" | "xri" | "ora" | "ori" | "cmp" |
67-
"inr" | "dcr" | "inx" | "dcx" | "dad" |
68-
"daa" | "cma" | "stc" | "cmc" | "ral" | "rar" |
69-
"jmp" | "jnz" | "jz" | "jnc" | "jc" | "jpo" | "jpe" | "jm" |
70-
"cnz" | "cz" | "cnc" | "cc" | "cpo" | "cpe" | "cm" |
71-
"rnz" | "rz" | "rnc" | "rc" | "rpo" | "rpe" | "rp" | "rm" |
72-
"pchl" | "sphl" | "xthl" | "xchg" | "hlt" |
73-
74-
"MOV" | "MVI" | "LXI" | "LDA" | "STA" | "LHLD" | "SHLD" | "LDAX" | "STAX" |
75-
"ADI" | "ACI" | "SUI" | "SBI" | "SBB" | "ANA" | "ANI" | "XRA" | "XRI" | "ORA" | "ORI" | "CMP" |
76-
"INR" | "DCR" | "INX" | "DCX" | "DAD" |
77-
"DAA" | "CMA" | "STC" | "CMC" | "RAL" | "RAR" |
78-
"JMP" | "JNZ" | "JZ" | "JNC" | "JC" | "JPO" | "JPE" | "JM" |
79-
"CNZ" | "CZ" | "CNC" | "CC" | "CPO" | "CPE" | "CM" |
80-
"RNZ" | "RZ" | "RNC" | "RC" | "RPO" | "RPE" | "RP" | "RM" |
81-
"PCHL" | "SPHL" | "XTHL" | "XCHG" | "HLT"
82-
>
83-
}
31+
@external specialize {Identifier} pseudoOpSpecializer from "../../src/parser/tokens-z80" { PseudoOp }
32+
@external specialize {Identifier} macSpecializer from "../../src/parser/tokens-z80" { Mac, MacEnd }
33+
@external specialize {Identifier} controlOpSpecializer from "../../src/parser/tokens-z80" { ControlOp }
34+
@external specialize {Identifier} opcodeSpecializer from "../../src/parser/tokens-z80" { Opcode }
35+
@external specialize {Identifier} registerSpecializer from "../../src/parser/tokens-z80" { Register }
36+
@external specialize {Identifier} conditionSpecializer from "../../src/parser/tokens-z80" { Condition }
8437

8538
Expression {
8639
Expression !logic LogicOp Expression |
@@ -115,12 +68,12 @@ Operand {
11568
}
11669

11770
@tokens {
118-
Identifier { $[a-zA-Z_] $[a-zA-Z0-9_]* }
71+
Identifier { $[a-zA-Z_.?] $[a-zA-Z0-9_.?]* }
11972

120-
Hex { ("0x" | "$") $[0-9a-fA-F]+ | $[0-9] $[0-9a-fA-F]* "h" }
121-
Bin { "%" $[01]+ | $[01]+ "b" }
122-
Oct { "0o" $[0-7]+ | $[0-7]+ "o" }
123-
Dec { $[0-9]+ }
73+
Hex { ("0x" | "0X" | "$") $[0-9a-fA-F]+ | $[0-9] $[0-9a-fA-F]* $[hH] }
74+
Bin { $[01]+ $[bB] }
75+
Oct { ("0o" | "0O") $[0-7]+ | $[0-7]+ $[oOqQ] }
76+
Dec { $[0-9]+ $[dD]? }
12477

12578
Number { Hex | Bin | Oct | Dec }
12679

@@ -134,7 +87,7 @@ Operand {
13487
eol { $[\n\r]+ }
13588

13689
Comma { "," }
137-
":"
90+
Colon { ":" }
13891
"#"
13992
"(" ")"
14093

src/parser/lang-z80.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ export const LezerZ80: LRLanguage = LRLanguage.define({
77
props: [
88
styleTags({
99
Identifier: t.variableName,
10-
PseudoOp: t.definition(t.variableName),
11-
Opcode: t.keyword,
12-
Register: t.typeName,
13-
Condition: t.className,
10+
PseudoOp: t.keyword,
11+
Opcode: t.standard(t.keyword),
12+
Register: t.standard(t.modifier),
13+
Condition: t.constant(t.modifier),
1414
Label: t.labelName,
1515
String: t.string,
16-
Char: t.number,
16+
Char: t.character,
1717
Number: t.number,
18-
Comment: t.lineComment,
18+
Comment: t.comment,
1919
ArithOp: t.arithmeticOperator,
2020
Plus: t.arithmeticOperator,
2121
Minus: t.arithmeticOperator,
@@ -29,7 +29,12 @@ export const LezerZ80: LRLanguage = LRLanguage.define({
2929
BinaryGt: t.compareOperator,
3030
UnaryLt: t.arithmeticOperator,
3131
UnaryGt: t.arithmeticOperator,
32+
Mac: t.definitionKeyword,
33+
MacEnd: t.definitionKeyword,
34+
"MacroDef/Identifier": t.macroName,
35+
ControlOp: t.controlKeyword,
3236
Comma: t.separator,
37+
Colon: t.separator,
3338
"( )": t.paren
3439
})
3540
]

0 commit comments

Comments
 (0)