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