Debian Codegraph @

codemirror-legacy-modes/mode/z80.js

raw
1 function mkZ80(ez80) {
2 var keywords1, keywords2;
3 if (ez80) {
4 keywords1 = /^(exx?|(ld|cp)([di]r?)?|[lp]ea|pop|push|ad[cd]|cpl|daa|dec|inc|neg|sbc|sub|and|bit|[cs]cf|x?or|res|set|r[lr]c?a?|r[lr]d|s[lr]a|srl|djnz|nop|[de]i|halt|im|in([di]mr?|ir?|irx|2r?)|ot(dmr?|[id]rx|imr?)|out(0?|[di]r?|[di]2r?)|tst(io)?|slp)(\.([sl]?i)?[sl])?\b/i;
5 keywords2 = /^(((call|j[pr]|rst|ret[in]?)(\.([sl]?i)?[sl])?)|(rs|st)mix)\b/i;
6 } else {
7 keywords1 = /^(exx?|(ld|cp|in)([di]r?)?|pop|push|ad[cd]|cpl|daa|dec|inc|neg|sbc|sub|and|bit|[cs]cf|x?or|res|set|r[lr]c?a?|r[lr]d|s[lr]a|srl|djnz|nop|rst|[de]i|halt|im|ot[di]r|out[di]?)\b/i;
8 keywords2 = /^(call|j[pr]|ret[in]?|b_?(call|jump))\b/i;
9 }
10
11 var variables1 = /^(af?|bc?|c|de?|e|hl?|l|i[xy]?|r|sp)\b/i;
12 var variables2 = /^(n?[zc]|p[oe]?|m)\b/i;
13 var errors = /^([hl][xy]|i[xy][hl]|slia|sll)\b/i;
14 var numbers = /^([\da-f]+h|[0-7]+o|[01]+b|\d+d?)\b/i;
15
16 return {
17 name: "z80",
18 startState: function() {
19 return {
20 context: 0
21 };
22 },
23 token: function(stream, state) {
24 if (!stream.column())
25 state.context = 0;
26
27 if (stream.eatSpace())
28 return null;
29
30 var w;
31
32 if (stream.eatWhile(/\w/)) {
33 if (ez80 && stream.eat('.')) {
34 stream.eatWhile(/\w/);
35 }
36 w = stream.current();
37
38 if (stream.indentation()) {
39 if ((state.context == 1 || state.context == 4) && variables1.test(w)) {
40 state.context = 4;
41 return 'variable';
42 }
43
44 if (state.context == 2 && variables2.test(w)) {
45 state.context = 4;
46 return 'variableName.special';
47 }
48
49 if (keywords1.test(w)) {
50 state.context = 1;
51 return 'keyword';
52 } else if (keywords2.test(w)) {
53 state.context = 2;
54 return 'keyword';
55 } else if (state.context == 4 && numbers.test(w)) {
56 return 'number';
57 }
58
59 if (errors.test(w))
60 return 'error';
61 } else if (stream.match(numbers)) {
62 return 'number';
63 } else {
64 return null;
65 }
66 } else if (stream.eat(';')) {
67 stream.skipToEnd();
68 return 'comment';
69 } else if (stream.eat('"')) {
70 while (w = stream.next()) {
71 if (w == '"')
72 break;
73
74 if (w == '\\')
75 stream.next();
76 }
77 return 'string';
78 } else if (stream.eat('\'')) {
79 if (stream.match(/\\?.'/))
80 return 'number';
81 } else if (stream.eat('.') || stream.sol() && stream.eat('#')) {
82 state.context = 5;
83
84 if (stream.eatWhile(/\w/))
85 return 'def';
86 } else if (stream.eat('$')) {
87 if (stream.eatWhile(/[\da-f]/i))
88 return 'number';
89 } else if (stream.eat('%')) {
90 if (stream.eatWhile(/[01]/))
91 return 'number';
92 } else {
93 stream.next();
94 }
95 return null;
96 }
97 };
98 };
99
100 export const z80 = mkZ80(false)
101 export const ez80 = mkZ80(true)