UNPKG

8.89 kBJavaScriptView Raw
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11})(function(CodeMirror) {
12"use strict";
13
14CodeMirror.defineMode("gas", function(_config, parserConfig) {
15 'use strict';
16
17 // If an architecture is specified, its initialization function may
18 // populate this array with custom parsing functions which will be
19 // tried in the event that the standard functions do not find a match.
20 var custom = [];
21
22 // The symbol used to start a line comment changes based on the target
23 // architecture.
24 // If no architecture is pased in "parserConfig" then only multiline
25 // comments will have syntax support.
26 var lineCommentStartSymbol = "";
27
28 // These directives are architecture independent.
29 // Machine specific directives should go in their respective
30 // architecture initialization function.
31 // Reference:
32 // http://sourceware.org/binutils/docs/as/Pseudo-Ops.html#Pseudo-Ops
33 var directives = {
34 ".abort" : "builtin",
35 ".align" : "builtin",
36 ".altmacro" : "builtin",
37 ".ascii" : "builtin",
38 ".asciz" : "builtin",
39 ".balign" : "builtin",
40 ".balignw" : "builtin",
41 ".balignl" : "builtin",
42 ".bundle_align_mode" : "builtin",
43 ".bundle_lock" : "builtin",
44 ".bundle_unlock" : "builtin",
45 ".byte" : "builtin",
46 ".cfi_startproc" : "builtin",
47 ".comm" : "builtin",
48 ".data" : "builtin",
49 ".def" : "builtin",
50 ".desc" : "builtin",
51 ".dim" : "builtin",
52 ".double" : "builtin",
53 ".eject" : "builtin",
54 ".else" : "builtin",
55 ".elseif" : "builtin",
56 ".end" : "builtin",
57 ".endef" : "builtin",
58 ".endfunc" : "builtin",
59 ".endif" : "builtin",
60 ".equ" : "builtin",
61 ".equiv" : "builtin",
62 ".eqv" : "builtin",
63 ".err" : "builtin",
64 ".error" : "builtin",
65 ".exitm" : "builtin",
66 ".extern" : "builtin",
67 ".fail" : "builtin",
68 ".file" : "builtin",
69 ".fill" : "builtin",
70 ".float" : "builtin",
71 ".func" : "builtin",
72 ".global" : "builtin",
73 ".gnu_attribute" : "builtin",
74 ".hidden" : "builtin",
75 ".hword" : "builtin",
76 ".ident" : "builtin",
77 ".if" : "builtin",
78 ".incbin" : "builtin",
79 ".include" : "builtin",
80 ".int" : "builtin",
81 ".internal" : "builtin",
82 ".irp" : "builtin",
83 ".irpc" : "builtin",
84 ".lcomm" : "builtin",
85 ".lflags" : "builtin",
86 ".line" : "builtin",
87 ".linkonce" : "builtin",
88 ".list" : "builtin",
89 ".ln" : "builtin",
90 ".loc" : "builtin",
91 ".loc_mark_labels" : "builtin",
92 ".local" : "builtin",
93 ".long" : "builtin",
94 ".macro" : "builtin",
95 ".mri" : "builtin",
96 ".noaltmacro" : "builtin",
97 ".nolist" : "builtin",
98 ".octa" : "builtin",
99 ".offset" : "builtin",
100 ".org" : "builtin",
101 ".p2align" : "builtin",
102 ".popsection" : "builtin",
103 ".previous" : "builtin",
104 ".print" : "builtin",
105 ".protected" : "builtin",
106 ".psize" : "builtin",
107 ".purgem" : "builtin",
108 ".pushsection" : "builtin",
109 ".quad" : "builtin",
110 ".reloc" : "builtin",
111 ".rept" : "builtin",
112 ".sbttl" : "builtin",
113 ".scl" : "builtin",
114 ".section" : "builtin",
115 ".set" : "builtin",
116 ".short" : "builtin",
117 ".single" : "builtin",
118 ".size" : "builtin",
119 ".skip" : "builtin",
120 ".sleb128" : "builtin",
121 ".space" : "builtin",
122 ".stab" : "builtin",
123 ".string" : "builtin",
124 ".struct" : "builtin",
125 ".subsection" : "builtin",
126 ".symver" : "builtin",
127 ".tag" : "builtin",
128 ".text" : "builtin",
129 ".title" : "builtin",
130 ".type" : "builtin",
131 ".uleb128" : "builtin",
132 ".val" : "builtin",
133 ".version" : "builtin",
134 ".vtable_entry" : "builtin",
135 ".vtable_inherit" : "builtin",
136 ".warning" : "builtin",
137 ".weak" : "builtin",
138 ".weakref" : "builtin",
139 ".word" : "builtin"
140 };
141
142 var registers = {};
143
144 function x86(_parserConfig) {
145 lineCommentStartSymbol = "#";
146
147 registers.ax = "variable";
148 registers.eax = "variable-2";
149 registers.rax = "variable-3";
150
151 registers.bx = "variable";
152 registers.ebx = "variable-2";
153 registers.rbx = "variable-3";
154
155 registers.cx = "variable";
156 registers.ecx = "variable-2";
157 registers.rcx = "variable-3";
158
159 registers.dx = "variable";
160 registers.edx = "variable-2";
161 registers.rdx = "variable-3";
162
163 registers.si = "variable";
164 registers.esi = "variable-2";
165 registers.rsi = "variable-3";
166
167 registers.di = "variable";
168 registers.edi = "variable-2";
169 registers.rdi = "variable-3";
170
171 registers.sp = "variable";
172 registers.esp = "variable-2";
173 registers.rsp = "variable-3";
174
175 registers.bp = "variable";
176 registers.ebp = "variable-2";
177 registers.rbp = "variable-3";
178
179 registers.ip = "variable";
180 registers.eip = "variable-2";
181 registers.rip = "variable-3";
182
183 registers.cs = "keyword";
184 registers.ds = "keyword";
185 registers.ss = "keyword";
186 registers.es = "keyword";
187 registers.fs = "keyword";
188 registers.gs = "keyword";
189 }
190
191 function armv6(_parserConfig) {
192 // Reference:
193 // http://infocenter.arm.com/help/topic/com.arm.doc.qrc0001l/QRC0001_UAL.pdf
194 // http://infocenter.arm.com/help/topic/com.arm.doc.ddi0301h/DDI0301H_arm1176jzfs_r0p7_trm.pdf
195 lineCommentStartSymbol = "@";
196 directives.syntax = "builtin";
197
198 registers.r0 = "variable";
199 registers.r1 = "variable";
200 registers.r2 = "variable";
201 registers.r3 = "variable";
202 registers.r4 = "variable";
203 registers.r5 = "variable";
204 registers.r6 = "variable";
205 registers.r7 = "variable";
206 registers.r8 = "variable";
207 registers.r9 = "variable";
208 registers.r10 = "variable";
209 registers.r11 = "variable";
210 registers.r12 = "variable";
211
212 registers.sp = "variable-2";
213 registers.lr = "variable-2";
214 registers.pc = "variable-2";
215 registers.r13 = registers.sp;
216 registers.r14 = registers.lr;
217 registers.r15 = registers.pc;
218
219 custom.push(function(ch, stream) {
220 if (ch === '#') {
221 stream.eatWhile(/\w/);
222 return "number";
223 }
224 });
225 }
226
227 var arch = (parserConfig.architecture || "x86").toLowerCase();
228 if (arch === "x86") {
229 x86(parserConfig);
230 } else if (arch === "arm" || arch === "armv6") {
231 armv6(parserConfig);
232 }
233
234 function nextUntilUnescaped(stream, end) {
235 var escaped = false, next;
236 while ((next = stream.next()) != null) {
237 if (next === end && !escaped) {
238 return false;
239 }
240 escaped = !escaped && next === "\\";
241 }
242 return escaped;
243 }
244
245 function clikeComment(stream, state) {
246 var maybeEnd = false, ch;
247 while ((ch = stream.next()) != null) {
248 if (ch === "/" && maybeEnd) {
249 state.tokenize = null;
250 break;
251 }
252 maybeEnd = (ch === "*");
253 }
254 return "comment";
255 }
256
257 return {
258 startState: function() {
259 return {
260 tokenize: null
261 };
262 },
263
264 token: function(stream, state) {
265 if (state.tokenize) {
266 return state.tokenize(stream, state);
267 }
268
269 if (stream.eatSpace()) {
270 return null;
271 }
272
273 var style, cur, ch = stream.next();
274
275 if (ch === "/") {
276 if (stream.eat("*")) {
277 state.tokenize = clikeComment;
278 return clikeComment(stream, state);
279 }
280 }
281
282 if (ch === lineCommentStartSymbol) {
283 stream.skipToEnd();
284 return "comment";
285 }
286
287 if (ch === '"') {
288 nextUntilUnescaped(stream, '"');
289 return "string";
290 }
291
292 if (ch === '.') {
293 stream.eatWhile(/\w/);
294 cur = stream.current().toLowerCase();
295 style = directives[cur];
296 return style || null;
297 }
298
299 if (ch === '=') {
300 stream.eatWhile(/\w/);
301 return "tag";
302 }
303
304 if (ch === '{') {
305 return "braket";
306 }
307
308 if (ch === '}') {
309 return "braket";
310 }
311
312 if (/\d/.test(ch)) {
313 if (ch === "0" && stream.eat("x")) {
314 stream.eatWhile(/[0-9a-fA-F]/);
315 return "number";
316 }
317 stream.eatWhile(/\d/);
318 return "number";
319 }
320
321 if (/\w/.test(ch)) {
322 stream.eatWhile(/\w/);
323 if (stream.eat(":")) {
324 return 'tag';
325 }
326 cur = stream.current().toLowerCase();
327 style = registers[cur];
328 return style || null;
329 }
330
331 for (var i = 0; i < custom.length; i++) {
332 style = custom[i](ch, stream, state);
333 if (style) {
334 return style;
335 }
336 }
337 },
338
339 lineComment: lineCommentStartSymbol,
340 blockCommentStart: "/*",
341 blockCommentEnd: "*/"
342 };
343});
344
345});