]> git.lizzy.rs Git - rust.git/blob - src/grammar/lexer.l
Auto merge of #49335 - GuillaumeGomez:remove-unneeded-trait-implementations-title...
[rust.git] / src / grammar / lexer.l
1 %{
2 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
3 // file at the top-level directory of this distribution and at
4 // http://rust-lang.org/COPYRIGHT.
5 //
6 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9 // option. This file may not be copied, modified, or distributed
10 // except according to those terms.
11
12 #include <stdio.h>
13 #include <ctype.h>
14
15 static int num_hashes;
16 static int end_hashes;
17 static int saw_non_hash;
18
19 %}
20
21 %option stack
22 %option yylineno
23
24 %x str
25 %x rawstr
26 %x rawstr_esc_begin
27 %x rawstr_esc_body
28 %x rawstr_esc_end
29 %x byte
30 %x bytestr
31 %x rawbytestr
32 %x rawbytestr_nohash
33 %x pound
34 %x shebang_or_attr
35 %x ltorchar
36 %x linecomment
37 %x doc_line
38 %x blockcomment
39 %x doc_block
40 %x suffix
41
42 ident [a-zA-Z\x80-\xff_][a-zA-Z0-9\x80-\xff_]*
43
44 %%
45
46 <suffix>{ident}            { BEGIN(INITIAL); }
47 <suffix>(.|\n)  { yyless(0); BEGIN(INITIAL); }
48
49 [ \n\t\r]             { }
50
51 \xef\xbb\xbf {
52   // UTF-8 byte order mark (BOM), ignore if in line 1, error otherwise
53   if (yyget_lineno() != 1) {
54     return -1;
55   }
56 }
57
58 \/\/(\/|\!)           { BEGIN(doc_line); yymore(); }
59 <doc_line>\n          { BEGIN(INITIAL);
60                         yyleng--;
61                         yytext[yyleng] = 0;
62                         return ((yytext[2] == '!') ? INNER_DOC_COMMENT : OUTER_DOC_COMMENT);
63                       }
64 <doc_line>[^\n]*      { yymore(); }
65
66 \/\/|\/\/\/\/         { BEGIN(linecomment); }
67 <linecomment>\n       { BEGIN(INITIAL); }
68 <linecomment>[^\n]*   { }
69
70 \/\*(\*|\!)[^*]       { yy_push_state(INITIAL); yy_push_state(doc_block); yymore(); }
71 <doc_block>\/\*       { yy_push_state(doc_block); yymore(); }
72 <doc_block>\*\/       {
73     yy_pop_state();
74     if (yy_top_state() == doc_block) {
75         yymore();
76     } else {
77         return ((yytext[2] == '!') ? INNER_DOC_COMMENT : OUTER_DOC_COMMENT);
78     }
79 }
80 <doc_block>(.|\n)     { yymore(); }
81
82 \/\*                  { yy_push_state(blockcomment); }
83 <blockcomment>\/\*    { yy_push_state(blockcomment); }
84 <blockcomment>\*\/    { yy_pop_state(); }
85 <blockcomment>(.|\n)   { }
86
87 _        { return UNDERSCORE; }
88 abstract { return ABSTRACT; }
89 alignof  { return ALIGNOF; }
90 as       { return AS; }
91 become   { return BECOME; }
92 box      { return BOX; }
93 break    { return BREAK; }
94 catch    { return CATCH; }
95 const    { return CONST; }
96 continue { return CONTINUE; }
97 crate    { return CRATE; }
98 default  { return DEFAULT; }
99 do       { return DO; }
100 else     { return ELSE; }
101 enum     { return ENUM; }
102 extern   { return EXTERN; }
103 false    { return FALSE; }
104 final    { return FINAL; }
105 fn       { return FN; }
106 for      { return FOR; }
107 if       { return IF; }
108 impl     { return IMPL; }
109 in       { return IN; }
110 let      { return LET; }
111 loop     { return LOOP; }
112 macro    { return MACRO; }
113 match    { return MATCH; }
114 mod      { return MOD; }
115 move     { return MOVE; }
116 mut      { return MUT; }
117 offsetof { return OFFSETOF; }
118 override { return OVERRIDE; }
119 priv     { return PRIV; }
120 proc     { return PROC; }
121 pure     { return PURE; }
122 pub      { return PUB; }
123 ref      { return REF; }
124 return   { return RETURN; }
125 self     { return SELF; }
126 sizeof   { return SIZEOF; }
127 static   { return STATIC; }
128 struct   { return STRUCT; }
129 super    { return SUPER; }
130 trait    { return TRAIT; }
131 true     { return TRUE; }
132 type     { return TYPE; }
133 typeof   { return TYPEOF; }
134 union    { return UNION; }
135 unsafe   { return UNSAFE; }
136 unsized  { return UNSIZED; }
137 use      { return USE; }
138 virtual  { return VIRTUAL; }
139 where    { return WHERE; }
140 while    { return WHILE; }
141 yield    { return YIELD; }
142
143 {ident}  { return IDENT; }
144
145 0x[0-9a-fA-F_]+                                    { BEGIN(suffix); return LIT_INTEGER; }
146 0o[0-7_]+                                          { BEGIN(suffix); return LIT_INTEGER; }
147 0b[01_]+                                           { BEGIN(suffix); return LIT_INTEGER; }
148 [0-9][0-9_]*                                       { BEGIN(suffix); return LIT_INTEGER; }
149 [0-9][0-9_]*\.(\.|[a-zA-Z])    { yyless(yyleng - 2); BEGIN(suffix); return LIT_INTEGER; }
150
151 [0-9][0-9_]*\.[0-9_]*([eE][-\+]?[0-9_]+)?          { BEGIN(suffix); return LIT_FLOAT; }
152 [0-9][0-9_]*(\.[0-9_]*)?[eE][-\+]?[0-9_]+          { BEGIN(suffix); return LIT_FLOAT; }
153
154 ;      { return ';'; }
155 ,      { return ','; }
156 \.\.\. { return DOTDOTDOT; }
157 \.\.   { return DOTDOT; }
158 \.     { return '.'; }
159 \(     { return '('; }
160 \)     { return ')'; }
161 \{     { return '{'; }
162 \}     { return '}'; }
163 \[     { return '['; }
164 \]     { return ']'; }
165 @      { return '@'; }
166 #      { BEGIN(pound); yymore(); }
167 <pound>\! { BEGIN(shebang_or_attr); yymore(); }
168 <shebang_or_attr>\[ {
169   BEGIN(INITIAL);
170   yyless(2);
171   return SHEBANG;
172 }
173 <shebang_or_attr>[^\[\n]*\n {
174   // Since the \n was eaten as part of the token, yylineno will have
175   // been incremented to the value 2 if the shebang was on the first
176   // line. This yyless undoes that, setting yylineno back to 1.
177   yyless(yyleng - 1);
178   if (yyget_lineno() == 1) {
179     BEGIN(INITIAL);
180     return SHEBANG_LINE;
181   } else {
182     BEGIN(INITIAL);
183     yyless(2);
184     return SHEBANG;
185   }
186 }
187 <pound>. { BEGIN(INITIAL); yyless(1); return '#'; }
188
189 \~     { return '~'; }
190 ::     { return MOD_SEP; }
191 :      { return ':'; }
192 \$     { return '$'; }
193 \?     { return '?'; }
194
195 ==    { return EQEQ; }
196 =>    { return FAT_ARROW; }
197 =     { return '='; }
198 \!=   { return NE; }
199 \!    { return '!'; }
200 \<=   { return LE; }
201 \<\<  { return SHL; }
202 \<\<= { return SHLEQ; }
203 \<    { return '<'; }
204 \>=   { return GE; }
205 \>\>  { return SHR; }
206 \>\>= { return SHREQ; }
207 \>    { return '>'; }
208
209 \x27                                      { BEGIN(ltorchar); yymore(); }
210 <ltorchar>static                          { BEGIN(INITIAL); return STATIC_LIFETIME; }
211 <ltorchar>{ident}                         { BEGIN(INITIAL); return LIFETIME; }
212 <ltorchar>\\[nrt\\\x27\x220]\x27          { BEGIN(suffix); return LIT_CHAR; }
213 <ltorchar>\\x[0-9a-fA-F]{2}\x27           { BEGIN(suffix); return LIT_CHAR; }
214 <ltorchar>\\u\{([0-9a-fA-F]_*){1,6}\}\x27 { BEGIN(suffix); return LIT_CHAR; }
215 <ltorchar>.\x27                           { BEGIN(suffix); return LIT_CHAR; }
216 <ltorchar>[\x80-\xff]{2,4}\x27            { BEGIN(suffix); return LIT_CHAR; }
217 <ltorchar><<EOF>>                         { BEGIN(INITIAL); return -1; }
218
219 b\x22              { BEGIN(bytestr); yymore(); }
220 <bytestr>\x22      { BEGIN(suffix); return LIT_BYTE_STR; }
221
222 <bytestr><<EOF>>                     { return -1; }
223 <bytestr>\\[n\nrt\\\x27\x220]        { yymore(); }
224 <bytestr>\\x[0-9a-fA-F]{2}           { yymore(); }
225 <bytestr>\\u\{([0-9a-fA-F]_*){1,6}\} { yymore(); }
226 <bytestr>\\[^n\nrt\\\x27\x220]       { return -1; }
227 <bytestr>(.|\n)                      { yymore(); }
228
229 br\x22                      { BEGIN(rawbytestr_nohash); yymore(); }
230 <rawbytestr_nohash>\x22     { BEGIN(suffix); return LIT_BYTE_STR_RAW; }
231 <rawbytestr_nohash>(.|\n)   { yymore(); }
232 <rawbytestr_nohash><<EOF>>  { return -1; }
233
234 br/# {
235     BEGIN(rawbytestr);
236     yymore();
237     num_hashes = 0;
238     saw_non_hash = 0;
239     end_hashes = 0;
240 }
241 <rawbytestr># {
242     if (!saw_non_hash) {
243         num_hashes++;
244     } else if (end_hashes != 0) {
245         end_hashes++;
246         if (end_hashes == num_hashes) {
247             BEGIN(INITIAL);
248             return LIT_BYTE_STR_RAW;
249         }
250     }
251     yymore();
252 }
253 <rawbytestr>\x22# {
254     end_hashes = 1;
255     if (end_hashes == num_hashes) {
256         BEGIN(INITIAL);
257         return LIT_BYTE_STR_RAW;
258     }
259     yymore();
260 }
261 <rawbytestr>(.|\n) {
262     if (!saw_non_hash) {
263         saw_non_hash = 1;
264     }
265     if (end_hashes != 0) {
266         end_hashes = 0;
267     }
268     yymore();
269 }
270 <rawbytestr><<EOF>> { return -1; }
271
272 b\x27                           { BEGIN(byte); yymore(); }
273 <byte>\\[nrt\\\x27\x220]\x27    { BEGIN(INITIAL); return LIT_BYTE; }
274 <byte>\\x[0-9a-fA-F]{2}\x27     { BEGIN(INITIAL); return LIT_BYTE; }
275 <byte>\\u([0-9a-fA-F]_*){4}\x27 { BEGIN(INITIAL); return LIT_BYTE; }
276 <byte>\\U([0-9a-fA-F]_*){8}\x27 { BEGIN(INITIAL); return LIT_BYTE; }
277 <byte>.\x27                     { BEGIN(INITIAL); return LIT_BYTE; }
278 <byte><<EOF>>                   { BEGIN(INITIAL); return -1; }
279
280 r\x22           { BEGIN(rawstr); yymore(); }
281 <rawstr>\x22    { BEGIN(suffix); return LIT_STR_RAW; }
282 <rawstr>(.|\n)  { yymore(); }
283 <rawstr><<EOF>> { return -1; }
284
285 r/#             {
286     BEGIN(rawstr_esc_begin);
287     yymore();
288     num_hashes = 0;
289     saw_non_hash = 0;
290     end_hashes = 0;
291 }
292
293 <rawstr_esc_begin># {
294     num_hashes++;
295     yymore();
296 }
297 <rawstr_esc_begin>\x22 {
298     BEGIN(rawstr_esc_body);
299     yymore();
300 }
301 <rawstr_esc_begin>(.|\n) { return -1; }
302
303 <rawstr_esc_body>\x22/# {
304   BEGIN(rawstr_esc_end);
305   yymore();
306  }
307 <rawstr_esc_body>(.|\n) {
308   yymore();
309  }
310
311 <rawstr_esc_end># {
312   end_hashes++;
313   if (end_hashes == num_hashes) {
314     BEGIN(INITIAL);
315     return LIT_STR_RAW;
316   }
317   yymore();
318  }
319 <rawstr_esc_end>[^#] {
320   end_hashes = 0;
321   BEGIN(rawstr_esc_body);
322   yymore();
323  }
324
325 <rawstr_esc_begin,rawstr_esc_body,rawstr_esc_end><<EOF>> { return -1; }
326
327 \x22                     { BEGIN(str); yymore(); }
328 <str>\x22                { BEGIN(suffix); return LIT_STR; }
329
330 <str><<EOF>>                     { return -1; }
331 <str>\\[n\nr\rt\\\x27\x220]      { yymore(); }
332 <str>\\x[0-9a-fA-F]{2}           { yymore(); }
333 <str>\\u\{([0-9a-fA-F]_*){1,6}\} { yymore(); }
334 <str>\\[^n\nrt\\\x27\x220]       { return -1; }
335 <str>(.|\n)                      { yymore(); }
336
337 \<-  { return LARROW; }
338 -\>  { return RARROW; }
339 -    { return '-'; }
340 -=   { return MINUSEQ; }
341 &&   { return ANDAND; }
342 &    { return '&'; }
343 &=   { return ANDEQ; }
344 \|\| { return OROR; }
345 \|   { return '|'; }
346 \|=  { return OREQ; }
347 \+   { return '+'; }
348 \+=  { return PLUSEQ; }
349 \*   { return '*'; }
350 \*=  { return STAREQ; }
351 \/   { return '/'; }
352 \/=  { return SLASHEQ; }
353 \^   { return '^'; }
354 \^=  { return CARETEQ; }
355 %    { return '%'; }
356 %=   { return PERCENTEQ; }
357
358 <<EOF>> { return 0; }
359
360 %%