1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module.exports = grammar({
name: 'asm',
extras: $ => [
/ |\t|\r/,
$.line_comment,
$.block_comment,
],
conflicts: $ => [
[
$._expr,
$._tc_expr,
],
],
rules: {
program: $ => sep(repeat1('\n'), $._item),
_item: $ =>
choice(
$.meta,
$.label,
$.const,
$.instruction,
),
meta: $ =>
seq(
field('kind', $.meta_ident),
optional(choice(
$.ident,
seq($.int, repeat(seq(',', $.int))),
seq($.float, repeat(seq(',', $.float))),
seq($.string, repeat(seq(',', $.string))),
)),
),
label: $ =>
choice(
seq(
choice($.meta_ident, alias($.word, $.ident), alias($._ident, $.ident)),
':',
optional(seq('(', $.ident, ')')),
),
seq(
'label',
field('name', $.word),
),
),
const: $ => seq('const', field('name', $.word), field('value', $._tc_expr)),
instruction: $ => seq(field('kind', $.word), choice(sep(',', $._expr), repeat($._tc_expr))),
_expr: $ => choice($.ptr, $.ident, $.int, $.string, $.float),
ptr: $ =>
choice(
seq(
optional(seq(choice('byte', 'word', 'dword', 'qword'), 'ptr')),
'[',
$.reg,
optional(seq(choice('+', '-'), choice($.int, $.ident))),
']',
),
seq(
optional($.int),
'(',
$.reg,
')',
),
seq(
'*',
'rel',
'[',
$.int,
']',
),
// Aarch64
seq(
'[',
$.reg,
optional(seq(',', $.int)),
']',
optional('!'),
),
),
// Turing Complete
_tc_expr: $ =>
choice(
$.ident,
$.int,
$.string,
$.tc_infix,
),
tc_infix: $ =>
choice(
...[
['+', 0],
['-', 0],
['*', 1],
['/', 1],
['%', 1],
['|', 2],
['^', 3],
['&', 4],
].map(([op, p]) =>
prec.left(
p,
seq(field('lhs', $._tc_expr), field('op', op), field('rhs', $._tc_expr)),
)
),
),
int: $ => {
const _int = /-?([0-9][0-9_]*|(0x|\$)[0-9A-Fa-f][0-9A-Fa-f_]*|0b[01][01_]*)/
return choice(
seq('#', token.immediate(_int)),
_int,
)
},
float: $ => /-?[0-9][0-9_]*\.([0-9][0-9_]*)?/,
string: $ => /"[^"]*"/,
word: $ => /[a-zA-Z0-9_]+/,
_reg: $ => /%?[a-z0-9]+/,
address: $ => /\$[a-zA-Z0-9_]+/, // GAS x86 address
reg: $ => choice($._reg, $.word, $.address),
meta_ident: $ => /\.[a-z_]+/,
_ident: $ => /[a-zA-Z_0-9.]+/,
ident: $ => choice($._ident, $.meta_ident, $.reg),
line_comment: $ =>
choice(
seq('#', token.immediate(/.*/)),
/(\/\/|;).*/,
),
block_comment: $ =>
token(seq(
'/*',
/[^*]*\*+([^/*][^*]*\*+)*/,
'/',
)),
},
})
function sep(separator, rule) {
return optional(seq(rule, repeat(seq(separator, rule)), optional(separator)))
}