NASM 0.91
[platform/upstream/nasm.git] / misc / nasm.sl
1 % This file defines a NASM editor mode for the JED editor.
2 % JED's home page is http://space.mit.edu/~davis/jed.html.
3 %
4 % To install, copy this file into your JED_LIBRARY directory
5 % (/usr/local/jed/lib or C:\JED\LIB or whatever), then add the
6 % following lines to your .jedrc or jed.rc file:
7 %   autoload("nasm_mode", "nasm");
8 %   add_mode_for_extension("nasm", "asm");
9 % (you can of course replace "asm" with whatever file extension
10 % you like to use for your NASM source files).
11
12 variable Nasm_Instruction_Indent = 10;
13 variable Nasm_Comment_Column = 33;
14 variable Nasm_Comment_Space = 1;
15
16 variable nasm_kw_2 = strcat("ahalaxbhblbpbtbxchclcscxdbdddhdidldqdsdtdwdxes",
17                             "fsgsinjajbjcjejgjljojpjsjzorsispssto");
18 variable nasm_kw_3 = strncat("a16a32aaaaadaamaasadcaddandbsfbsrbtcbtrbtscbw",
19                              "cdqclccldclicmccmpcr0cr2cr3cr4cwddaadasdecdiv",
20                              "dr0dr1dr2dr3dr6dr7eaxebpebxecxediedxequesiesp",
21                              "farfldfsthltincintjaejbejgejlejmpjnajnbjncjne",
22                              "jngjnljnojnpjnsjnzjpejpolarldslealeslfslgslsl",
23                              "lssltrmm0mm1mm2mm3mm4mm5mm6mm7movmulnegnopnot",
24                              "o16o32outpopporrclrcrrepretrolrorrsmsalsarsbb",
25                              "segshlshrst0st1st2st3st4st5st6st7stcstdstistr",
26                              "subtr3tr4tr5tr6tr7wrtxor", 9);
27 variable nasm_kw_4 = strncat("arplbytecallcltscwdeemmsfabsfaddfbldfchsfcom",
28                              "fcosfdivfenifildfistfld1fldzfmulfnopfsinfstp",
29                              "fsubftstfxamfxchidivimulinsbinsdinswint3into",
30                              "invdiretjcxzjnaejnbejngejnlelahflgdtlidtlldt",
31                              "lmswlocklongloopmovdmovqnearpandpopapopfpush",
32                              "pxorreperepzresbresdreswretfretnsahfsetasetb",
33                              "setcsetesetgsetlsetosetpsetssetzsgdtshldshrd",
34                              "sidtsldtsmswtestverrverwwaitwordxaddxchg", 8);
35 variable nasm_kw_5 = strncat("boundbswapcmpsbcmpsdcmpswcpuiddwordenterf2xm1",
36                              "faddpfbstpfclexfcompfdisifdivpfdivrffreefiadd",
37                              "ficomfidivfimulfinitfistpfisubfldcwfldpifmulp",
38                              "fpremfptanfsavefsqrtfstcwfstswfsubpfsubrfucom",
39                              "fyl2xiretdiretwjecxzleavelodsblodsdlodswloope",
40                              "loopzmovsbmovsdmovswmovsxmovzxoutsboutsdoutsw",
41                              "paddbpadddpaddwpandnpopadpopawpopfdpopfwpslld",
42                              "psllqpsllwpsradpsrawpsrldpsrlqpsrlwpsubbpsubd",
43                              "psubwpushapushfqwordrdmsrrdtscrepnerepnzscasb",
44                              "scasdscaswsetaesetbesetgesetlesetnasetnbsetnc",
45                              "setnesetngsetnlsetnosetnpsetnssetnzsetpesetpo",
46                              "shortstosbstosdstoswtimestwordwrmsrxlatb", 12);
47 variable nasm_kw_6 = strncat("fcomppfdivrpficompfidivrfisubrfldenvfldl2e",
48                              "fldl2tfldlg2fldln2fpatanfprem1frstorfscale",
49                              "fsetpmfstenvfsubrpfucompinvlpgloopneloopnz",
50                              "paddsbpaddswpmulhwpmullwpsubsbpsubswpushad",
51                              "pushawpushfdpushfwsetnaesetnbesetngesetnle",
52                              "wbinvd", 6);
53 variable nasm_kw_7 = strncat("cmpxchgfdecstpfincstpfrndintfsincosfucompp",
54                              "fxtractfyl2xp1paddusbpadduswpcmpeqbpcmpeqd",
55                              "pcmpeqwpcmpgtbpcmpgtdpcmpgtwpmaddwdpsubusb",
56                              "psubusw", 4);
57 variable nasm_kw_8 = "packssdwpacksswbpackuswb";
58 variable nasm_kw_9 = strcat("cmpxchg8bpunpckhbwpunpckhdqpunpckhwdpunpcklbw",
59                             "punpckldqpunpcklwd");
60
61 define nasm_is_kw {
62     variable word;
63     variable len;
64     variable list, min, max, pos, cmp;
65
66     word = strlow(());
67     len = strlen(word);
68
69     switch (len)
70     { case 0: return 1; }
71     { case 2: list = nasm_kw_2; }
72     { case 3: list = nasm_kw_3; }
73     { case 4: list = nasm_kw_4; }
74     { case 5: list = nasm_kw_5; }
75     { case 6: list = nasm_kw_6; }
76     { case 7: list = nasm_kw_7; }
77     { case 8: list = nasm_kw_8; }
78     { case 9: list = nasm_kw_9; }
79     { pop(); return 0; }
80
81     min = -1;
82     max = strlen(list) / len;
83     while (max - min >= 2) {
84         pos = (max + min) / 2;
85         cmp = strcmp(word, substr(list, pos * len + 1, len));
86         if (cmp == 0)
87             return 1;                  % it's a keyword
88         else if (cmp < 0)
89             max = pos;                 % bottom half
90         else if (cmp > 0)
91             min = pos;                 % top half
92     }
93     return 0;
94 }
95
96 define nasm_indent_line() {
97     variable word, len, e;
98
99     e = eolp();
100
101     push_spot();
102     EXIT_BLOCK {
103         pop_spot();
104         if (what_column() <= Nasm_Instruction_Indent)
105             skip_white();
106     }
107
108     bol_skip_white();
109
110     if (orelse
111        {looking_at_char(';')}
112        {looking_at_char('#')}
113        {looking_at_char('[')}) {
114         bol_trim();
115         pop_spot();
116         EXIT_BLOCK {
117         }
118         return;
119     }
120
121     push_mark();
122     skip_chars("0-9a-zA-Z_.");
123     word = bufsubstr();
124
125     if (nasm_is_kw(word)) {
126         bol_trim();
127         whitespace(Nasm_Instruction_Indent);
128     } else {
129         push_spot();
130         bol_trim();
131         pop_spot();
132         len = strlen(word);
133         if (looking_at_char(':')) {
134             go_right_1();
135             len++;
136         }
137         trim();
138         if (e or not(eolp())) {
139             if (len >= Nasm_Instruction_Indent) {
140                 pop();
141                 whitespace(1);
142             } else
143                 whitespace(Nasm_Instruction_Indent - len);
144             if (e) {
145                 pop_spot();
146                 eol();
147                 push_spot();
148             }
149         }
150     }
151 }
152
153 define nasm_newline_indent {
154     push_spot();
155     bol_skip_white();
156     if (eolp())
157         trim();
158     pop_spot();
159     newline();
160     nasm_indent_line();
161 }
162
163 define nasm_bol_self_ins {
164     push_spot();
165     bskip_white();
166     bolp();
167     pop_spot();
168
169     call("self_insert_cmd");
170
171     % Grotty: force immediate update of the syntax highlighting.
172     insert_char('.');
173     deln(left(1));
174
175     if (())
176         nasm_indent_line();
177 }
178
179 define nasm_self_ins_ind {
180     call("self_insert_cmd");
181
182     % Grotty: force immediate update of the syntax highlighting.
183     insert_char('.');
184     deln(left(1));
185
186     nasm_indent_line();
187 }
188
189 define nasm_insert_comment {
190     variable spc;
191
192     bol_skip_white();
193     if (looking_at_char(';')) {
194         bol_trim();
195         go_right(1);
196         skip_white();
197         return;
198     } else if (eolp()) {
199         bol_trim();
200         insert("; ");
201         return;
202     }
203
204     forever {
205         skip_chars("^;\n'\"");
206         if (looking_at_char('\'')) {
207             go_right_1();
208             skip_chars("^'\n");
209             !if (eolp())
210                 go_right_1();
211         } else if (looking_at_char('\"')) {
212             go_right_1();
213             skip_chars("^\"\n");
214             !if (eolp())
215                 go_right_1();
216         } else if (looking_at_char(';')) {
217             !if (bolp()) {
218                 go_left_1();
219                 trim();
220                 !if (looking_at_char(';'))
221                     go_right_1();
222             }
223             break;
224         } else {
225             break;
226         }
227     }
228     spc = Nasm_Comment_Column - what_column();
229     if (spc < Nasm_Comment_Space)
230         spc = Nasm_Comment_Space;
231     whitespace(spc);
232     if (eolp()) {
233         insert("; ");
234     } else {
235         go_right_1();
236         skip_white();
237     }
238 }
239
240 $1 = "NASM";
241 create_syntax_table($1);
242
243 define_syntax (";", "", '%', $1);
244 define_syntax ("([", ")]", '(', $1);
245 define_syntax ('"', '"', $1);
246 define_syntax ('\'', '\'', $1);
247 define_syntax ("0-9a-zA-Z_.@#", 'w', $1);
248 define_syntax ("-+0-9a-fA-F.xXL", '0', $1);
249 define_syntax (",:", ',', $1);
250 define_syntax ('#', '#', $1);
251 define_syntax ("|^&<>+-*/%~", '+', $1);
252
253 set_syntax_flags($1,1);
254
255 #ifdef HAS_DFA_SYNTAX
256
257 enable_highlight_cache("nasm.dfa", $1);
258 define_highlight_rule(";.*$", "comment", $1);
259 define_highlight_rule("[A-Za-z_\\.\\?][A-Za-z0-9_\\.\\?\\$#@~]*",
260                       "Knormal", $1);
261 define_highlight_rule("$([A-Za-z_\\.\\?][A-Za-z0-9_\\.\\?\\$#@~]*)?",
262                       "normal", $1);
263 define_highlight_rule("[0-9]+(\\.[0-9]*)?([Ee][\\+\\-]?[0-9]*)?",
264                       "number", $1);
265 define_highlight_rule("[0-9]+[QqBb]", "number", $1);
266 define_highlight_rule("(0x|\\$[0-9A-Fa-f])[0-9A-Fa-f]*", "number", $1);
267 define_highlight_rule("[0-9A-Fa-f]+[Hh]", "number", $1);
268 define_highlight_rule("\"[^\"]*\"", "string", $1);
269 define_highlight_rule("\"[^\"]*$", "string", $1);
270 define_highlight_rule("'[^']*'", "string", $1);
271 define_highlight_rule("'[^']*$", "string", $1);
272 define_highlight_rule("[\\(\\)\\[\\],:]*", "delimiter", $1);
273 define_highlight_rule("[\\|\\^&<>\\+\\-\\*/%~]*", "operator", $1);
274 define_highlight_rule("^[ \t]*#", "PQpreprocess", $1);
275 define_highlight_rule("@[0-9A-Za-z_\\.]*", "keyword1", $1);
276 define_highlight_rule("[ \t]*", "normal", $1);
277 define_highlight_rule(".", "normal", $1);
278 build_highlight_table($1);
279
280 #endif
281
282 define_keywords_n($1, nasm_kw_2, 2, 0);
283 define_keywords_n($1, nasm_kw_3, 3, 0);
284 define_keywords_n($1, nasm_kw_4, 4, 0);
285 define_keywords_n($1, nasm_kw_5, 5, 0);
286 define_keywords_n($1, nasm_kw_6, 6, 0);
287 define_keywords_n($1, nasm_kw_7, 7, 0);
288 define_keywords_n($1, nasm_kw_8, 8, 0);
289 define_keywords_n($1, nasm_kw_9, 9, 0);
290
291 !if (keymap_p ($1)) make_keymap ($1);
292 definekey("nasm_bol_self_ins", ";", $1);
293 definekey("nasm_bol_self_ins", "#", $1);
294 definekey("nasm_bol_self_ins", "[", $1);
295 definekey("nasm_self_ins_ind", ":", $1);
296 definekey("nasm_insert_comment", "^[;", $1);
297
298 define nasm_mode {
299     set_mode("NASM", 4);
300     use_keymap ("NASM");
301     use_syntax_table ("NASM");
302     set_buffer_hook ("indent_hook", "nasm_indent_line");
303     set_buffer_hook ("newline_indent_hook", "nasm_newline_indent");
304     runhooks("nasm_mode_hook");
305 }