-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.js
169 lines (148 loc) · 3.96 KB
/
grammar.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* @file A syntax parser for the MIPS Instruction Set Architecture.
* @author Oskar Meyenburg <oskar.meyenburg@gmail.com>
* @license MIT
*/
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = grammar({
name: "mips",
// The default ignores whitespace.
// Overwrite the extras default to manage
// whitespace and new lines explicitly.
extras: $ => [
/ |\t/,
],
rules: {
program: $ => seq(
repeat($._statement),
optional(choice(
$.directive,
$.instruction,
$.macro,
$._label
)),
optional($.comment)
),
// Statement
_statement: $ => prec(1, choice(
";",
"\n",
seq(
choice(
$.directive,
$.instruction,
$.macro
),
choice(";", seq(optional($.comment), "\n"))
),
$._label,
seq($.comment, "\n")
)),
// Line comment
// Starts with a hash symbol
// Optionally prefixed with whitespace
comment: $ => /#.*/,
// A directive consists of a name beginning with a dot,
// optionally followed by more arguments
directive: $ => seq($.meta, optional( seq(
/[ \t]+/,
optional($.attributes) // Allow trailing space without attributes
))),
meta: $ => /[.][a-z_]+/,
_attrsep: $ => token(choice(",", "(", ")")),
attributes: $ => seq($._attribute, repeat(choice(
seq(" ", optional($._attrsep), $._attribute),
seq("\t", optional($._attrsep), $._attribute),
seq($._attrsep, $._attribute),
$._attrsep,
" ", // Allow trailing space after attributes
"\t"
))),
_attribute: $ => choice(
$._number,
$.string,
$.attribute
),
// Decreased priority in favor of number and string
attribute: $ => token(prec(-1, /[^\s,)(]+/)),
// Macros
macro: $ => /[a-zA-Z_]+\([^#]*\)/,
// Labels
_label: $ => seq(
$.label,
repeat(choice(" ", "\t")) // Allow trailing space
),
label: $ => /[a-zA-Z_][a-zA-Z0-9_]*:/,
// Instructions
instruction: $ => choice(
seq($.opcode),
seq(
$.opcode,
/,| |\t/,
optional($.operands) // Allow trailing space without operands
)
),
opcode: $ => /[a-z][a-z0-9.]*/,
operands: $ => seq(
$._operand,
repeat(choice(
seq(" ", optional(","), $._operand),
seq("\t",optional(","), $._operand),
seq(",", $._operand)
)),
optional(choice(" ", "\t")) // Allow trailing space with operands
),
_operand: $ => choice(
$.register,
$.macro_variable,
$._number,
$.address
),
// Match any macro variable
// The starting symbol depends on the assembler in use
// Examples: %value \\value
macro_variable: $ => /[%\\][0-9a-zA-Z_:$%\\]+/,
// Match any number
_number: $ => choice($.char, $.octal, $.decimal, $.hexadecimal, $.float),
// Match any address
// Examples: main, main($s4), value+4($s1), ($v1), -0x10($a0)
address: $ => choice(
$._identifier,
seq(
optional(choice(
$._identifier,
seq(
optional(seq($._identifier, "+")),
choice($._char, $._octal, $._decimal, $._hexadecimal)
)
)),
'(',
$._register,
')'
)
),
// Primitives
_char: $ => /'[^']'/,
_string: $ => /"[^"]*"/,
_octal: $ => /-?0[0-7]*/,
_decimal: $ => /-?\d+/, // Would match octal and decimal
_hexadecimal: $ => /-?0[xX][0-9a-fA-F]+/,
_float: $ => choice( // Would match decimal and float
seq(
choice(/-?\d+\.?\d*/, /-?\d*\.\d+/),
optional(/[eE]-?\d+/)
),
/-?\d+[eE]-?\d+/
),
_register: $ => /[$][0-9a-z]+/,
_identifier: $ => /[a-zA-Z_][a-zA-Z0-9_]*/,
char: $ => $._char,
string: $ => $._string,
octal: $ => $._octal,
decimal: $ => $._decimal,
hexadecimal: $ => $._hexadecimal,
float: $ => $._float,
register: $ => $._register
}
})