blob: 80914151caa306fafce7786683e8d1d3085b826a [file] [log] [blame]
Alex Crichtona914a612018-04-04 07:48:44 -07001#![cfg_attr(not(procmacro2_semver_exempt), allow(dead_code))]
Alex Crichtonaf5bad42018-03-27 14:45:10 -07002
Alex Crichton44bffbc2017-05-19 17:51:59 -07003use std::borrow::Borrow;
4use std::cell::RefCell;
David Tolnay1ebe3972018-01-02 20:14:20 -08005#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -05006use std::cmp;
Alex Crichton44bffbc2017-05-19 17:51:59 -07007use std::collections::HashMap;
8use std::fmt;
9use std::iter;
Alex Crichton44bffbc2017-05-19 17:51:59 -070010use std::rc::Rc;
11use std::str::FromStr;
12use std::vec;
13
David Tolnayb28f38a2018-03-31 22:02:29 +020014use strnom::{block_comment, skip_whitespace, whitespace, word_break, Cursor, PResult};
David Tolnayb1032662017-05-31 15:52:28 -070015use unicode_xid::UnicodeXID;
Alex Crichton44bffbc2017-05-19 17:51:59 -070016
Alex Crichtonf3888432018-05-16 09:11:05 -070017use {Delimiter, Group, Punct, Spacing, TokenTree};
Alex Crichton44bffbc2017-05-19 17:51:59 -070018
David Tolnay034205f2018-04-22 16:45:28 -070019#[derive(Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -070020pub struct TokenStream {
21 inner: Vec<TokenTree>,
22}
23
24#[derive(Debug)]
25pub struct LexError;
26
27impl TokenStream {
David Tolnayc3bb4592018-05-28 20:09:44 -070028 pub fn new() -> TokenStream {
Alex Crichton44bffbc2017-05-19 17:51:59 -070029 TokenStream { inner: Vec::new() }
30 }
31
32 pub fn is_empty(&self) -> bool {
33 self.inner.len() == 0
34 }
35}
36
David Tolnay1ebe3972018-01-02 20:14:20 -080037#[cfg(procmacro2_semver_exempt)]
Nika Layzella9dbc182017-12-30 14:50:13 -050038fn get_cursor(src: &str) -> Cursor {
39 // Create a dummy file & add it to the codemap
40 CODEMAP.with(|cm| {
41 let mut cm = cm.borrow_mut();
42 let name = format!("<parsed string {}>", cm.files.len());
43 let span = cm.add_file(&name, src);
44 Cursor {
45 rest: src,
46 off: span.lo,
47 }
48 })
49}
50
David Tolnay1ebe3972018-01-02 20:14:20 -080051#[cfg(not(procmacro2_semver_exempt))]
Nika Layzella9dbc182017-12-30 14:50:13 -050052fn get_cursor(src: &str) -> Cursor {
David Tolnayb28f38a2018-03-31 22:02:29 +020053 Cursor { rest: src }
Nika Layzella9dbc182017-12-30 14:50:13 -050054}
55
Alex Crichton44bffbc2017-05-19 17:51:59 -070056impl FromStr for TokenStream {
57 type Err = LexError;
58
59 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Nika Layzellf8d5f212017-12-11 14:07:02 -050060 // Create a dummy file & add it to the codemap
Nika Layzella9dbc182017-12-30 14:50:13 -050061 let cursor = get_cursor(src);
Nika Layzellf8d5f212017-12-11 14:07:02 -050062
63 match token_stream(cursor) {
David Tolnay1218e122017-06-01 11:13:45 -070064 Ok((input, output)) => {
Alex Crichton44bffbc2017-05-19 17:51:59 -070065 if skip_whitespace(input).len() != 0 {
66 Err(LexError)
67 } else {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070068 Ok(output)
Alex Crichton44bffbc2017-05-19 17:51:59 -070069 }
70 }
David Tolnay1218e122017-06-01 11:13:45 -070071 Err(LexError) => Err(LexError),
Alex Crichton44bffbc2017-05-19 17:51:59 -070072 }
73 }
74}
75
76impl fmt::Display for TokenStream {
77 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78 let mut joint = false;
79 for (i, tt) in self.inner.iter().enumerate() {
80 if i != 0 && !joint {
81 write!(f, " ")?;
82 }
83 joint = false;
Alex Crichtonaf5bad42018-03-27 14:45:10 -070084 match *tt {
85 TokenTree::Group(ref tt) => {
86 let (start, end) = match tt.delimiter() {
Alex Crichton44bffbc2017-05-19 17:51:59 -070087 Delimiter::Parenthesis => ("(", ")"),
88 Delimiter::Brace => ("{", "}"),
89 Delimiter::Bracket => ("[", "]"),
90 Delimiter::None => ("", ""),
91 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -070092 if tt.stream().into_iter().next().is_none() {
Alex Crichton852d53d2017-05-19 19:25:08 -070093 write!(f, "{} {}", start, end)?
94 } else {
Alex Crichtonaf5bad42018-03-27 14:45:10 -070095 write!(f, "{} {} {}", start, tt.stream(), end)?
Alex Crichton852d53d2017-05-19 19:25:08 -070096 }
Alex Crichton44bffbc2017-05-19 17:51:59 -070097 }
Alex Crichtonf3888432018-05-16 09:11:05 -070098 TokenTree::Ident(ref tt) => write!(f, "{}", tt)?,
99 TokenTree::Punct(ref tt) => {
100 write!(f, "{}", tt.as_char())?;
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700101 match tt.spacing() {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700102 Spacing::Alone => {}
103 Spacing::Joint => joint = true,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700104 }
105 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700106 TokenTree::Literal(ref tt) => write!(f, "{}", tt)?,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700107 }
108 }
109
110 Ok(())
111 }
112}
113
David Tolnay034205f2018-04-22 16:45:28 -0700114impl fmt::Debug for TokenStream {
115 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116 f.write_str("TokenStream ")?;
117 f.debug_list().entries(self.clone()).finish()
118 }
119}
120
Alex Crichton0e8e7f42018-02-22 06:15:13 -0800121#[cfg(feature = "proc-macro")]
122impl From<::proc_macro::TokenStream> for TokenStream {
123 fn from(inner: ::proc_macro::TokenStream) -> TokenStream {
David Tolnayb28f38a2018-03-31 22:02:29 +0200124 inner
125 .to_string()
126 .parse()
127 .expect("compiler token stream parse failed")
Alex Crichton44bffbc2017-05-19 17:51:59 -0700128 }
129}
130
Alex Crichton0e8e7f42018-02-22 06:15:13 -0800131#[cfg(feature = "proc-macro")]
132impl From<TokenStream> for ::proc_macro::TokenStream {
133 fn from(inner: TokenStream) -> ::proc_macro::TokenStream {
David Tolnayb28f38a2018-03-31 22:02:29 +0200134 inner
135 .to_string()
136 .parse()
137 .expect("failed to parse to compiler tokens")
Alex Crichton44bffbc2017-05-19 17:51:59 -0700138 }
139}
140
Alex Crichton44bffbc2017-05-19 17:51:59 -0700141impl From<TokenTree> for TokenStream {
142 fn from(tree: TokenTree) -> TokenStream {
143 TokenStream { inner: vec![tree] }
144 }
145}
146
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700147impl iter::FromIterator<TokenTree> for TokenStream {
David Tolnayb28f38a2018-03-31 22:02:29 +0200148 fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700149 let mut v = Vec::new();
150
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700151 for token in streams.into_iter() {
152 v.push(token);
Alex Crichton44bffbc2017-05-19 17:51:59 -0700153 }
154
155 TokenStream { inner: v }
156 }
157}
158
Alex Crichtonf3888432018-05-16 09:11:05 -0700159impl Extend<TokenTree> for TokenStream {
160 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
161 self.inner.extend(streams);
162 }
163}
164
Alex Crichton1a7f7622017-07-05 17:47:15 -0700165pub type TokenTreeIter = vec::IntoIter<TokenTree>;
Alex Crichton44bffbc2017-05-19 17:51:59 -0700166
167impl IntoIterator for TokenStream {
168 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700169 type IntoIter = TokenTreeIter;
Alex Crichton44bffbc2017-05-19 17:51:59 -0700170
Alex Crichton1a7f7622017-07-05 17:47:15 -0700171 fn into_iter(self) -> TokenTreeIter {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700172 self.inner.into_iter()
173 }
174}
175
Nika Layzellb35a9a32017-12-30 14:34:35 -0500176#[derive(Clone, PartialEq, Eq, Debug)]
177pub struct FileName(String);
178
Alex Crichtonf3888432018-05-16 09:11:05 -0700179#[allow(dead_code)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700180pub fn file_name(s: String) -> FileName {
181 FileName(s)
182}
183
Nika Layzellb35a9a32017-12-30 14:34:35 -0500184impl fmt::Display for FileName {
185 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
186 self.0.fmt(f)
187 }
188}
189
Nika Layzellf8d5f212017-12-11 14:07:02 -0500190#[derive(Clone, PartialEq, Eq)]
191pub struct SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500192 name: FileName,
Nika Layzellf8d5f212017-12-11 14:07:02 -0500193}
194
195impl SourceFile {
196 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500197 pub fn path(&self) -> &FileName {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500198 &self.name
199 }
200
201 pub fn is_real(&self) -> bool {
202 // XXX(nika): Support real files in the future?
203 false
204 }
205}
206
Nika Layzellb35a9a32017-12-30 14:34:35 -0500207impl AsRef<FileName> for SourceFile {
208 fn as_ref(&self) -> &FileName {
209 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500210 }
211}
212
213impl fmt::Debug for SourceFile {
214 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
215 f.debug_struct("SourceFile")
Nika Layzellb35a9a32017-12-30 14:34:35 -0500216 .field("path", &self.path())
Nika Layzellf8d5f212017-12-11 14:07:02 -0500217 .field("is_real", &self.is_real())
218 .finish()
219 }
220}
221
222#[derive(Clone, Copy, Debug, PartialEq, Eq)]
223pub struct LineColumn {
224 pub line: usize,
225 pub column: usize,
226}
227
David Tolnay1ebe3972018-01-02 20:14:20 -0800228#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500229thread_local! {
230 static CODEMAP: RefCell<Codemap> = RefCell::new(Codemap {
231 // NOTE: We start with a single dummy file which all call_site() and
232 // def_site() spans reference.
233 files: vec![FileInfo {
234 name: "<unspecified>".to_owned(),
235 span: Span { lo: 0, hi: 0 },
236 lines: vec![0],
237 }],
238 });
239}
240
David Tolnay1ebe3972018-01-02 20:14:20 -0800241#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500242struct FileInfo {
243 name: String,
244 span: Span,
245 lines: Vec<usize>,
246}
247
David Tolnay1ebe3972018-01-02 20:14:20 -0800248#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500249impl FileInfo {
250 fn offset_line_column(&self, offset: usize) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200251 assert!(self.span_within(Span {
252 lo: offset as u32,
253 hi: offset as u32
254 }));
Nika Layzellf8d5f212017-12-11 14:07:02 -0500255 let offset = offset - self.span.lo as usize;
256 match self.lines.binary_search(&offset) {
257 Ok(found) => LineColumn {
258 line: found + 1,
David Tolnayb28f38a2018-03-31 22:02:29 +0200259 column: 0,
Nika Layzellf8d5f212017-12-11 14:07:02 -0500260 },
261 Err(idx) => LineColumn {
262 line: idx,
David Tolnayb28f38a2018-03-31 22:02:29 +0200263 column: offset - self.lines[idx - 1],
Nika Layzellf8d5f212017-12-11 14:07:02 -0500264 },
265 }
266 }
267
268 fn span_within(&self, span: Span) -> bool {
269 span.lo >= self.span.lo && span.hi <= self.span.hi
270 }
271}
272
Alex Crichtona914a612018-04-04 07:48:44 -0700273/// Computesthe offsets of each line in the given source string.
David Tolnay1ebe3972018-01-02 20:14:20 -0800274#[cfg(procmacro2_semver_exempt)]
Nika Layzella0a7c3d2017-12-30 14:52:39 -0500275fn lines_offsets(s: &str) -> Vec<usize> {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500276 let mut lines = vec![0];
277 let mut prev = 0;
Nika Layzella0a7c3d2017-12-30 14:52:39 -0500278 while let Some(len) = s[prev..].find('\n') {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500279 prev += len + 1;
280 lines.push(prev);
281 }
282 lines
283}
284
David Tolnay1ebe3972018-01-02 20:14:20 -0800285#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500286struct Codemap {
287 files: Vec<FileInfo>,
288}
289
David Tolnay1ebe3972018-01-02 20:14:20 -0800290#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500291impl Codemap {
292 fn next_start_pos(&self) -> u32 {
293 // Add 1 so there's always space between files.
294 //
295 // We'll always have at least 1 file, as we initialize our files list
296 // with a dummy file.
297 self.files.last().unwrap().span.hi + 1
298 }
299
300 fn add_file(&mut self, name: &str, src: &str) -> Span {
Nika Layzella0a7c3d2017-12-30 14:52:39 -0500301 let lines = lines_offsets(src);
Nika Layzellf8d5f212017-12-11 14:07:02 -0500302 let lo = self.next_start_pos();
303 // XXX(nika): Shouild we bother doing a checked cast or checked add here?
David Tolnayb28f38a2018-03-31 22:02:29 +0200304 let span = Span {
305 lo: lo,
306 hi: lo + (src.len() as u32),
307 };
Nika Layzellf8d5f212017-12-11 14:07:02 -0500308
309 self.files.push(FileInfo {
310 name: name.to_owned(),
311 span: span,
312 lines: lines,
313 });
314
315 span
316 }
317
318 fn fileinfo(&self, span: Span) -> &FileInfo {
319 for file in &self.files {
320 if file.span_within(span) {
321 return file;
322 }
323 }
324 panic!("Invalid span with no related FileInfo!");
325 }
326}
327
David Tolnay034205f2018-04-22 16:45:28 -0700328#[derive(Clone, Copy, PartialEq, Eq)]
David Tolnayddfca052017-12-31 10:41:24 -0500329pub struct Span {
David Tolnay1ebe3972018-01-02 20:14:20 -0800330 #[cfg(procmacro2_semver_exempt)]
David Tolnayddfca052017-12-31 10:41:24 -0500331 lo: u32,
David Tolnay1ebe3972018-01-02 20:14:20 -0800332 #[cfg(procmacro2_semver_exempt)]
David Tolnayddfca052017-12-31 10:41:24 -0500333 hi: u32,
334}
Alex Crichton44bffbc2017-05-19 17:51:59 -0700335
336impl Span {
David Tolnay1ebe3972018-01-02 20:14:20 -0800337 #[cfg(not(procmacro2_semver_exempt))]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700338 pub fn call_site() -> Span {
David Tolnay79105e52017-12-31 11:03:04 -0500339 Span {}
340 }
341
David Tolnay1ebe3972018-01-02 20:14:20 -0800342 #[cfg(procmacro2_semver_exempt)]
David Tolnay79105e52017-12-31 11:03:04 -0500343 pub fn call_site() -> Span {
344 Span { lo: 0, hi: 0 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700345 }
Alex Crichtone6085b72017-11-21 07:24:25 -0800346
347 pub fn def_site() -> Span {
David Tolnay79105e52017-12-31 11:03:04 -0500348 Span::call_site()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500349 }
350
David Tolnay4e8e3972018-01-05 18:10:22 -0800351 pub fn resolved_at(&self, _other: Span) -> Span {
352 // Stable spans consist only of line/column information, so
353 // `resolved_at` and `located_at` only select which span the
354 // caller wants line/column information from.
355 *self
356 }
357
358 pub fn located_at(&self, other: Span) -> Span {
359 other
360 }
361
David Tolnay1ebe3972018-01-02 20:14:20 -0800362 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500363 pub fn source_file(&self) -> SourceFile {
364 CODEMAP.with(|cm| {
365 let cm = cm.borrow();
366 let fi = cm.fileinfo(*self);
367 SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500368 name: FileName(fi.name.clone()),
Nika Layzellf8d5f212017-12-11 14:07:02 -0500369 }
370 })
371 }
372
David Tolnay1ebe3972018-01-02 20:14:20 -0800373 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500374 pub fn start(&self) -> LineColumn {
375 CODEMAP.with(|cm| {
376 let cm = cm.borrow();
377 let fi = cm.fileinfo(*self);
378 fi.offset_line_column(self.lo as usize)
379 })
380 }
381
David Tolnay1ebe3972018-01-02 20:14:20 -0800382 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500383 pub fn end(&self) -> LineColumn {
384 CODEMAP.with(|cm| {
385 let cm = cm.borrow();
386 let fi = cm.fileinfo(*self);
387 fi.offset_line_column(self.hi as usize)
388 })
389 }
390
David Tolnay1ebe3972018-01-02 20:14:20 -0800391 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500392 pub fn join(&self, other: Span) -> Option<Span> {
393 CODEMAP.with(|cm| {
394 let cm = cm.borrow();
395 // If `other` is not within the same FileInfo as us, return None.
396 if !cm.fileinfo(*self).span_within(other) {
397 return None;
398 }
399 Some(Span {
400 lo: cmp::min(self.lo, other.lo),
401 hi: cmp::max(self.hi, other.hi),
402 })
403 })
Alex Crichtone6085b72017-11-21 07:24:25 -0800404 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700405}
406
David Tolnay034205f2018-04-22 16:45:28 -0700407impl fmt::Debug for Span {
408 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
409 #[cfg(procmacro2_semver_exempt)]
410 return write!(f, "bytes({}..{})", self.lo, self.hi);
411
412 #[cfg(not(procmacro2_semver_exempt))]
413 write!(f, "Span")
414 }
415}
416
Alex Crichtonf3888432018-05-16 09:11:05 -0700417#[derive(Clone)]
418pub struct Ident {
David Tolnay041bcd42017-06-03 09:18:04 -0700419 intern: usize,
Alex Crichtonb2c94622018-04-04 07:36:41 -0700420 span: Span,
Alex Crichtonf3888432018-05-16 09:11:05 -0700421 raw: bool,
David Tolnay041bcd42017-06-03 09:18:04 -0700422}
Alex Crichton44bffbc2017-05-19 17:51:59 -0700423
424thread_local!(static SYMBOLS: RefCell<Interner> = RefCell::new(Interner::new()));
425
Alex Crichtonf3888432018-05-16 09:11:05 -0700426impl Ident {
427 fn _new(string: &str, raw: bool, span: Span) -> Ident {
David Tolnay489c6422018-04-07 08:37:28 -0700428 validate_term(string);
429
Alex Crichtonf3888432018-05-16 09:11:05 -0700430 Ident {
David Tolnay041bcd42017-06-03 09:18:04 -0700431 intern: SYMBOLS.with(|s| s.borrow_mut().intern(string)),
Alex Crichtona914a612018-04-04 07:48:44 -0700432 span: span,
Alex Crichtonf3888432018-05-16 09:11:05 -0700433 raw: raw,
David Tolnay041bcd42017-06-03 09:18:04 -0700434 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700435 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700436
Alex Crichtonf3888432018-05-16 09:11:05 -0700437 pub fn new(string: &str, span: Span) -> Ident {
438 Ident::_new(string, false, span)
439 }
440
441 pub fn new_raw(string: &str, span: Span) -> Ident {
442 Ident::_new(string, true, span)
443 }
444
David Tolnay10effeb2018-01-06 11:07:49 -0800445 pub fn as_str(&self) -> &str {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700446 SYMBOLS.with(|interner| {
447 let interner = interner.borrow();
David Tolnay041bcd42017-06-03 09:18:04 -0700448 let s = interner.get(self.intern);
David Tolnayb28f38a2018-03-31 22:02:29 +0200449 unsafe { &*(s as *const str) }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700450 })
451 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700452
453 pub fn span(&self) -> Span {
454 self.span
455 }
456
457 pub fn set_span(&mut self, span: Span) {
458 self.span = span;
459 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700460}
461
Nicholas Nethercotee3cb3532018-05-28 20:11:58 +1000462#[inline]
463fn is_ident_start(c: char) -> bool {
David Tolnay03b43da2018-06-02 15:25:57 -0700464 ('a' <= c && c <= 'z')
465 || ('A' <= c && c <= 'Z')
466 || c == '_'
467 || (c > '\x7f' && UnicodeXID::is_xid_start(c))
Nicholas Nethercotee3cb3532018-05-28 20:11:58 +1000468}
469
470#[inline]
471fn is_ident_continue(c: char) -> bool {
David Tolnay03b43da2018-06-02 15:25:57 -0700472 ('a' <= c && c <= 'z')
473 || ('A' <= c && c <= 'Z')
474 || c == '_'
475 || ('0' <= c && c <= '9')
476 || (c > '\x7f' && UnicodeXID::is_xid_continue(c))
Nicholas Nethercotee3cb3532018-05-28 20:11:58 +1000477}
478
David Tolnay489c6422018-04-07 08:37:28 -0700479fn validate_term(string: &str) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700480 let validate = string;
David Tolnay489c6422018-04-07 08:37:28 -0700481 if validate.is_empty() {
Alex Crichtonf3888432018-05-16 09:11:05 -0700482 panic!("Ident is not allowed to be empty; use Option<Ident>");
David Tolnay489c6422018-04-07 08:37:28 -0700483 }
484
485 if validate.bytes().all(|digit| digit >= b'0' && digit <= b'9') {
Alex Crichtonf3888432018-05-16 09:11:05 -0700486 panic!("Ident cannot be a number; use Literal instead");
David Tolnay489c6422018-04-07 08:37:28 -0700487 }
488
Nicholas Nethercotee3cb3532018-05-28 20:11:58 +1000489 fn ident_ok(string: &str) -> bool {
David Tolnay489c6422018-04-07 08:37:28 -0700490 let mut chars = string.chars();
491 let first = chars.next().unwrap();
Nicholas Nethercotee3cb3532018-05-28 20:11:58 +1000492 if !is_ident_start(first) {
David Tolnay489c6422018-04-07 08:37:28 -0700493 return false;
494 }
495 for ch in chars {
Nicholas Nethercotee3cb3532018-05-28 20:11:58 +1000496 if !is_ident_continue(ch) {
David Tolnay489c6422018-04-07 08:37:28 -0700497 return false;
498 }
499 }
500 true
501 }
502
Nicholas Nethercotee3cb3532018-05-28 20:11:58 +1000503 if !ident_ok(validate) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700504 panic!("{:?} is not a valid Ident", string);
David Tolnay489c6422018-04-07 08:37:28 -0700505 }
506}
507
Alex Crichtonf3888432018-05-16 09:11:05 -0700508impl fmt::Display for Ident {
David Tolnay8ad3e3e2017-06-03 16:45:00 -0700509 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonf3888432018-05-16 09:11:05 -0700510 if self.raw {
511 "r#".fmt(f)?;
512 }
513 self.as_str().fmt(f)
514 }
515}
516
517impl fmt::Debug for Ident {
David Tolnayd8fcdb82018-06-02 15:43:53 -0700518 // Ident(proc_macro), Ident(r#union)
519 #[cfg(not(procmacro2_semver_exempt))]
520 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
521 let mut debug = f.debug_tuple("Ident");
522 debug.field(&format_args!("{}", self));
523 debug.finish()
524 }
525
526 // Ident {
527 // sym: proc_macro,
528 // span: bytes(128..138)
529 // }
530 #[cfg(procmacro2_semver_exempt)]
Alex Crichtonf3888432018-05-16 09:11:05 -0700531 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
532 let mut debug = f.debug_struct("Ident");
David Tolnayd8fcdb82018-06-02 15:43:53 -0700533 debug.field("sym", &format_args!("{}", self));
David Tolnay034205f2018-04-22 16:45:28 -0700534 debug.field("span", &self.span);
535 debug.finish()
David Tolnay8ad3e3e2017-06-03 16:45:00 -0700536 }
537}
538
Alex Crichton44bffbc2017-05-19 17:51:59 -0700539struct Interner {
540 string_to_index: HashMap<MyRc, usize>,
541 index_to_string: Vec<Rc<String>>,
542}
543
544#[derive(Hash, Eq, PartialEq)]
545struct MyRc(Rc<String>);
546
547impl Borrow<str> for MyRc {
548 fn borrow(&self) -> &str {
549 &self.0
550 }
551}
552
553impl Interner {
554 fn new() -> Interner {
555 Interner {
556 string_to_index: HashMap::new(),
557 index_to_string: Vec::new(),
558 }
559 }
560
David Tolnayb28f38a2018-03-31 22:02:29 +0200561 fn intern(&mut self, s: &str) -> usize {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700562 if let Some(&idx) = self.string_to_index.get(s) {
David Tolnayb28f38a2018-03-31 22:02:29 +0200563 return idx;
Alex Crichton44bffbc2017-05-19 17:51:59 -0700564 }
565 let s = Rc::new(s.to_string());
566 self.index_to_string.push(s.clone());
David Tolnayb28f38a2018-03-31 22:02:29 +0200567 self.string_to_index
568 .insert(MyRc(s), self.index_to_string.len() - 1);
Alex Crichton44bffbc2017-05-19 17:51:59 -0700569 self.index_to_string.len() - 1
570 }
571
David Tolnayb28f38a2018-03-31 22:02:29 +0200572 fn get(&self, idx: usize) -> &str {
573 &self.index_to_string[idx]
574 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700575}
576
David Tolnay034205f2018-04-22 16:45:28 -0700577#[derive(Clone)]
Alex Crichtonb2c94622018-04-04 07:36:41 -0700578pub struct Literal {
579 text: String,
580 span: Span,
581}
Alex Crichton44bffbc2017-05-19 17:51:59 -0700582
Alex Crichtona914a612018-04-04 07:48:44 -0700583macro_rules! suffixed_numbers {
584 ($($name:ident => $kind:ident,)*) => ($(
585 pub fn $name(n: $kind) -> Literal {
586 Literal::_new(format!(concat!("{}", stringify!($kind)), n))
587 }
588 )*)
589}
590
591macro_rules! unsuffixed_numbers {
592 ($($name:ident => $kind:ident,)*) => ($(
593 pub fn $name(n: $kind) -> Literal {
594 Literal::_new(n.to_string())
595 }
596 )*)
597}
598
Alex Crichton852d53d2017-05-19 19:25:08 -0700599impl Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700600 fn _new(text: String) -> Literal {
601 Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700602 text: text,
Alex Crichtonb2c94622018-04-04 07:36:41 -0700603 span: Span::call_site(),
604 }
605 }
606
Alex Crichtona914a612018-04-04 07:48:44 -0700607 suffixed_numbers! {
608 u8_suffixed => u8,
609 u16_suffixed => u16,
610 u32_suffixed => u32,
611 u64_suffixed => u64,
612 usize_suffixed => usize,
613 i8_suffixed => i8,
614 i16_suffixed => i16,
615 i32_suffixed => i32,
616 i64_suffixed => i64,
617 isize_suffixed => isize,
618
619 f32_suffixed => f32,
620 f64_suffixed => f64,
621 }
622
623 unsuffixed_numbers! {
624 u8_unsuffixed => u8,
625 u16_unsuffixed => u16,
626 u32_unsuffixed => u32,
627 u64_unsuffixed => u64,
628 usize_unsuffixed => usize,
629 i8_unsuffixed => i8,
630 i16_unsuffixed => i16,
631 i32_unsuffixed => i32,
632 i64_unsuffixed => i64,
633 isize_unsuffixed => isize,
634 }
635
636 pub fn f32_unsuffixed(f: f32) -> Literal {
637 let mut s = f.to_string();
638 if !s.contains(".") {
639 s.push_str(".0");
Alex Crichton76a5cc82017-05-23 07:01:44 -0700640 }
Alex Crichtona914a612018-04-04 07:48:44 -0700641 Literal::_new(s)
642 }
643
644 pub fn f64_unsuffixed(f: f64) -> Literal {
645 let mut s = f.to_string();
646 if !s.contains(".") {
647 s.push_str(".0");
648 }
649 Literal::_new(s)
650 }
651
652 pub fn string(t: &str) -> Literal {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700653 let mut s = t
654 .chars()
Alex Crichtona914a612018-04-04 07:48:44 -0700655 .flat_map(|c| c.escape_default())
656 .collect::<String>();
657 s.push('"');
658 s.insert(0, '"');
659 Literal::_new(s)
660 }
661
662 pub fn character(t: char) -> Literal {
663 Literal::_new(format!("'{}'", t.escape_default().collect::<String>()))
Alex Crichton76a5cc82017-05-23 07:01:44 -0700664 }
665
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700666 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton852d53d2017-05-19 19:25:08 -0700667 let mut escaped = "b\"".to_string();
668 for b in bytes {
669 match *b {
670 b'\0' => escaped.push_str(r"\0"),
671 b'\t' => escaped.push_str(r"\t"),
672 b'\n' => escaped.push_str(r"\n"),
673 b'\r' => escaped.push_str(r"\r"),
674 b'"' => escaped.push_str("\\\""),
675 b'\\' => escaped.push_str("\\\\"),
David Tolnayb28f38a2018-03-31 22:02:29 +0200676 b'\x20'...b'\x7E' => escaped.push(*b as char),
Alex Crichton852d53d2017-05-19 19:25:08 -0700677 _ => escaped.push_str(&format!("\\x{:02X}", b)),
678 }
679 }
680 escaped.push('"');
Alex Crichtonb2c94622018-04-04 07:36:41 -0700681 Literal::_new(escaped)
Alex Crichton76a5cc82017-05-23 07:01:44 -0700682 }
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700683
Alex Crichtonb2c94622018-04-04 07:36:41 -0700684 pub fn span(&self) -> Span {
685 self.span
Alex Crichton31316622017-05-26 12:54:47 -0700686 }
687
Alex Crichtonb2c94622018-04-04 07:36:41 -0700688 pub fn set_span(&mut self, span: Span) {
689 self.span = span;
Alex Crichton31316622017-05-26 12:54:47 -0700690 }
Alex Crichton852d53d2017-05-19 19:25:08 -0700691}
692
Alex Crichton44bffbc2017-05-19 17:51:59 -0700693impl fmt::Display for Literal {
694 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700695 self.text.fmt(f)
Alex Crichton44bffbc2017-05-19 17:51:59 -0700696 }
697}
698
David Tolnay034205f2018-04-22 16:45:28 -0700699impl fmt::Debug for Literal {
700 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
701 let mut debug = fmt.debug_struct("Literal");
702 debug.field("lit", &format_args!("{}", self.text));
703 #[cfg(procmacro2_semver_exempt)]
704 debug.field("span", &self.span);
705 debug.finish()
706 }
707}
708
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700709fn token_stream(mut input: Cursor) -> PResult<TokenStream> {
Alex Crichton1eb96a02018-04-04 13:07:35 -0700710 let mut trees = Vec::new();
711 loop {
712 let input_no_ws = skip_whitespace(input);
713 if input_no_ws.rest.len() == 0 {
David Tolnay48ea5042018-04-23 19:17:35 -0700714 break;
Alex Crichton1eb96a02018-04-04 13:07:35 -0700715 }
716 if let Ok((a, tokens)) = doc_comment(input_no_ws) {
717 input = a;
718 trees.extend(tokens);
David Tolnay48ea5042018-04-23 19:17:35 -0700719 continue;
Alex Crichton1eb96a02018-04-04 13:07:35 -0700720 }
721
722 let (a, tt) = match token_tree(input_no_ws) {
723 Ok(p) => p,
724 Err(_) => break,
725 };
726 trees.push(tt);
727 input = a;
728 }
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700729 Ok((input, TokenStream { inner: trees }))
Alex Crichton1eb96a02018-04-04 13:07:35 -0700730}
Alex Crichton44bffbc2017-05-19 17:51:59 -0700731
David Tolnay1ebe3972018-01-02 20:14:20 -0800732#[cfg(not(procmacro2_semver_exempt))]
Alex Crichton1eb96a02018-04-04 13:07:35 -0700733fn spanned<'a, T>(
734 input: Cursor<'a>,
735 f: fn(Cursor<'a>) -> PResult<'a, T>,
736) -> PResult<'a, (T, ::Span)> {
737 let (a, b) = f(skip_whitespace(input))?;
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700738 Ok((a, ((b, ::Span::_new_stable(Span {})))))
David Tolnayddfca052017-12-31 10:41:24 -0500739}
740
David Tolnay1ebe3972018-01-02 20:14:20 -0800741#[cfg(procmacro2_semver_exempt)]
Alex Crichton1eb96a02018-04-04 13:07:35 -0700742fn spanned<'a, T>(
743 input: Cursor<'a>,
744 f: fn(Cursor<'a>) -> PResult<'a, T>,
745) -> PResult<'a, (T, ::Span)> {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500746 let input = skip_whitespace(input);
747 let lo = input.off;
Alex Crichton1eb96a02018-04-04 13:07:35 -0700748 let (a, b) = f(input)?;
749 let hi = a.off;
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700750 let span = ::Span::_new_stable(Span { lo: lo, hi: hi });
Alex Crichton1eb96a02018-04-04 13:07:35 -0700751 Ok((a, (b, span)))
752}
753
754fn token_tree(input: Cursor) -> PResult<TokenTree> {
755 let (rest, (mut tt, span)) = spanned(input, token_kind)?;
756 tt.set_span(span);
757 Ok((rest, tt))
Nika Layzellf8d5f212017-12-11 14:07:02 -0500758}
Alex Crichton44bffbc2017-05-19 17:51:59 -0700759
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700760named!(token_kind -> TokenTree, alt!(
761 map!(group, TokenTree::Group)
Alex Crichton44bffbc2017-05-19 17:51:59 -0700762 |
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700763 map!(literal, |l| TokenTree::Literal(::Literal::_new_stable(l))) // must be before symbol
Alex Crichton44bffbc2017-05-19 17:51:59 -0700764 |
Alex Crichtonf3888432018-05-16 09:11:05 -0700765 map!(op, TokenTree::Punct)
Alex Crichton44bffbc2017-05-19 17:51:59 -0700766 |
Alex Crichtonf3888432018-05-16 09:11:05 -0700767 symbol_leading_ws
Alex Crichton44bffbc2017-05-19 17:51:59 -0700768));
769
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700770named!(group -> Group, alt!(
Alex Crichton44bffbc2017-05-19 17:51:59 -0700771 delimited!(
772 punct!("("),
773 token_stream,
774 punct!(")")
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700775 ) => { |ts| Group::new(Delimiter::Parenthesis, ::TokenStream::_new_stable(ts)) }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700776 |
777 delimited!(
778 punct!("["),
779 token_stream,
780 punct!("]")
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700781 ) => { |ts| Group::new(Delimiter::Bracket, ::TokenStream::_new_stable(ts)) }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700782 |
783 delimited!(
784 punct!("{"),
785 token_stream,
786 punct!("}")
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700787 ) => { |ts| Group::new(Delimiter::Brace, ::TokenStream::_new_stable(ts)) }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700788));
789
Alex Crichtonf3888432018-05-16 09:11:05 -0700790fn symbol_leading_ws(input: Cursor) -> PResult<TokenTree> {
791 symbol(skip_whitespace(input))
792}
Alex Crichton44bffbc2017-05-19 17:51:59 -0700793
Alex Crichtonf3888432018-05-16 09:11:05 -0700794fn symbol(input: Cursor) -> PResult<TokenTree> {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700795 let mut chars = input.char_indices();
David Tolnaya202d502017-06-01 12:26:55 -0700796
Alex Crichtonf3888432018-05-16 09:11:05 -0700797 let raw = input.starts_with("r#");
David Tolnaya13d1422018-03-31 21:27:48 +0200798 if raw {
799 chars.next();
800 chars.next();
801 }
802
Alex Crichton44bffbc2017-05-19 17:51:59 -0700803 match chars.next() {
Nicholas Nethercotee3cb3532018-05-28 20:11:58 +1000804 Some((_, ch)) if is_ident_start(ch) => {}
David Tolnay1218e122017-06-01 11:13:45 -0700805 _ => return Err(LexError),
Alex Crichton44bffbc2017-05-19 17:51:59 -0700806 }
807
David Tolnay214c94c2017-06-01 12:42:56 -0700808 let mut end = input.len();
Alex Crichton44bffbc2017-05-19 17:51:59 -0700809 for (i, ch) in chars {
Nicholas Nethercotee3cb3532018-05-28 20:11:58 +1000810 if !is_ident_continue(ch) {
David Tolnay214c94c2017-06-01 12:42:56 -0700811 end = i;
812 break;
Alex Crichton44bffbc2017-05-19 17:51:59 -0700813 }
814 }
815
David Tolnaya13d1422018-03-31 21:27:48 +0200816 let a = &input.rest[..end];
Alex Crichtonf3888432018-05-16 09:11:05 -0700817 if a == "r#_" {
David Tolnay214c94c2017-06-01 12:42:56 -0700818 Err(LexError)
819 } else {
Alex Crichtonf3888432018-05-16 09:11:05 -0700820 let ident = if raw {
821 ::Ident::_new_raw(&a[2..], ::Span::call_site())
822 } else {
823 ::Ident::new(a, ::Span::call_site())
824 };
825 Ok((input.advance(end), ident.into()))
David Tolnay214c94c2017-06-01 12:42:56 -0700826 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700827}
828
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700829fn literal(input: Cursor) -> PResult<Literal> {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700830 let input_no_ws = skip_whitespace(input);
831
832 match literal_nocapture(input_no_ws) {
David Tolnay1218e122017-06-01 11:13:45 -0700833 Ok((a, ())) => {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700834 let start = input.len() - input_no_ws.len();
835 let len = input_no_ws.len() - a.len();
836 let end = start + len;
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700837 Ok((a, Literal::_new(input.rest[start..end].to_string())))
Alex Crichton44bffbc2017-05-19 17:51:59 -0700838 }
David Tolnay1218e122017-06-01 11:13:45 -0700839 Err(LexError) => Err(LexError),
Alex Crichton44bffbc2017-05-19 17:51:59 -0700840 }
841}
842
843named!(literal_nocapture -> (), alt!(
844 string
845 |
846 byte_string
847 |
848 byte
849 |
850 character
851 |
852 float
853 |
854 int
Alex Crichton44bffbc2017-05-19 17:51:59 -0700855));
856
857named!(string -> (), alt!(
858 quoted_string
859 |
860 preceded!(
861 punct!("r"),
862 raw_string
863 ) => { |_| () }
864));
865
866named!(quoted_string -> (), delimited!(
867 punct!("\""),
868 cooked_string,
869 tag!("\"")
870));
871
Nika Layzellf8d5f212017-12-11 14:07:02 -0500872fn cooked_string(input: Cursor) -> PResult<()> {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700873 let mut chars = input.char_indices().peekable();
874 while let Some((byte_offset, ch)) = chars.next() {
875 match ch {
876 '"' => {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500877 return Ok((input.advance(byte_offset), ()));
Alex Crichton44bffbc2017-05-19 17:51:59 -0700878 }
879 '\r' => {
880 if let Some((_, '\n')) = chars.next() {
881 // ...
882 } else {
883 break;
884 }
885 }
David Tolnayb28f38a2018-03-31 22:02:29 +0200886 '\\' => match chars.next() {
887 Some((_, 'x')) => {
888 if !backslash_x_char(&mut chars) {
889 break;
Alex Crichton44bffbc2017-05-19 17:51:59 -0700890 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700891 }
David Tolnayb28f38a2018-03-31 22:02:29 +0200892 Some((_, 'n')) | Some((_, 'r')) | Some((_, 't')) | Some((_, '\\'))
893 | Some((_, '\'')) | Some((_, '"')) | Some((_, '0')) => {}
894 Some((_, 'u')) => {
895 if !backslash_u(&mut chars) {
896 break;
897 }
898 }
899 Some((_, '\n')) | Some((_, '\r')) => {
900 while let Some(&(_, ch)) = chars.peek() {
901 if ch.is_whitespace() {
902 chars.next();
903 } else {
904 break;
905 }
906 }
907 }
908 _ => break,
909 },
Alex Crichton44bffbc2017-05-19 17:51:59 -0700910 _ch => {}
911 }
912 }
David Tolnay1218e122017-06-01 11:13:45 -0700913 Err(LexError)
Alex Crichton44bffbc2017-05-19 17:51:59 -0700914}
915
916named!(byte_string -> (), alt!(
917 delimited!(
918 punct!("b\""),
919 cooked_byte_string,
920 tag!("\"")
921 ) => { |_| () }
922 |
923 preceded!(
924 punct!("br"),
925 raw_string
926 ) => { |_| () }
927));
928
Nika Layzellf8d5f212017-12-11 14:07:02 -0500929fn cooked_byte_string(mut input: Cursor) -> PResult<()> {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700930 let mut bytes = input.bytes().enumerate();
931 'outer: while let Some((offset, b)) = bytes.next() {
932 match b {
933 b'"' => {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500934 return Ok((input.advance(offset), ()));
Alex Crichton44bffbc2017-05-19 17:51:59 -0700935 }
936 b'\r' => {
937 if let Some((_, b'\n')) = bytes.next() {
938 // ...
939 } else {
940 break;
941 }
942 }
David Tolnayb28f38a2018-03-31 22:02:29 +0200943 b'\\' => match bytes.next() {
944 Some((_, b'x')) => {
945 if !backslash_x_byte(&mut bytes) {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700946 break;
947 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700948 }
David Tolnayb28f38a2018-03-31 22:02:29 +0200949 Some((_, b'n')) | Some((_, b'r')) | Some((_, b't')) | Some((_, b'\\'))
950 | Some((_, b'0')) | Some((_, b'\'')) | Some((_, b'"')) => {}
951 Some((newline, b'\n')) | Some((newline, b'\r')) => {
952 let rest = input.advance(newline + 1);
953 for (offset, ch) in rest.char_indices() {
954 if !ch.is_whitespace() {
955 input = rest.advance(offset);
956 bytes = input.bytes().enumerate();
957 continue 'outer;
958 }
959 }
960 break;
961 }
962 _ => break,
963 },
Alex Crichton44bffbc2017-05-19 17:51:59 -0700964 b if b < 0x80 => {}
965 _ => break,
966 }
967 }
David Tolnay1218e122017-06-01 11:13:45 -0700968 Err(LexError)
Alex Crichton44bffbc2017-05-19 17:51:59 -0700969}
970
Nika Layzellf8d5f212017-12-11 14:07:02 -0500971fn raw_string(input: Cursor) -> PResult<()> {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700972 let mut chars = input.char_indices();
973 let mut n = 0;
974 while let Some((byte_offset, ch)) = chars.next() {
975 match ch {
976 '"' => {
977 n = byte_offset;
978 break;
979 }
980 '#' => {}
David Tolnay1218e122017-06-01 11:13:45 -0700981 _ => return Err(LexError),
Alex Crichton44bffbc2017-05-19 17:51:59 -0700982 }
983 }
984 for (byte_offset, ch) in chars {
985 match ch {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500986 '"' if input.advance(byte_offset + 1).starts_with(&input.rest[..n]) => {
987 let rest = input.advance(byte_offset + 1 + n);
David Tolnayb28f38a2018-03-31 22:02:29 +0200988 return Ok((rest, ()));
Alex Crichton44bffbc2017-05-19 17:51:59 -0700989 }
990 '\r' => {}
991 _ => {}
992 }
993 }
David Tolnay1218e122017-06-01 11:13:45 -0700994 Err(LexError)
Alex Crichton44bffbc2017-05-19 17:51:59 -0700995}
996
997named!(byte -> (), do_parse!(
998 punct!("b") >>
999 tag!("'") >>
1000 cooked_byte >>
1001 tag!("'") >>
1002 (())
1003));
1004
Nika Layzellf8d5f212017-12-11 14:07:02 -05001005fn cooked_byte(input: Cursor) -> PResult<()> {
Alex Crichton44bffbc2017-05-19 17:51:59 -07001006 let mut bytes = input.bytes().enumerate();
1007 let ok = match bytes.next().map(|(_, b)| b) {
David Tolnayb28f38a2018-03-31 22:02:29 +02001008 Some(b'\\') => match bytes.next().map(|(_, b)| b) {
1009 Some(b'x') => backslash_x_byte(&mut bytes),
1010 Some(b'n') | Some(b'r') | Some(b't') | Some(b'\\') | Some(b'0') | Some(b'\'')
1011 | Some(b'"') => true,
1012 _ => false,
1013 },
Alex Crichton44bffbc2017-05-19 17:51:59 -07001014 b => b.is_some(),
1015 };
1016 if ok {
1017 match bytes.next() {
Alex Crichton8c030332018-01-16 08:07:36 -08001018 Some((offset, _)) => {
1019 if input.chars().as_str().is_char_boundary(offset) {
1020 Ok((input.advance(offset), ()))
1021 } else {
1022 Err(LexError)
1023 }
1024 }
Nika Layzellf8d5f212017-12-11 14:07:02 -05001025 None => Ok((input.advance(input.len()), ())),
Alex Crichton44bffbc2017-05-19 17:51:59 -07001026 }
1027 } else {
David Tolnay1218e122017-06-01 11:13:45 -07001028 Err(LexError)
Alex Crichton44bffbc2017-05-19 17:51:59 -07001029 }
1030}
1031
1032named!(character -> (), do_parse!(
1033 punct!("'") >>
1034 cooked_char >>
1035 tag!("'") >>
1036 (())
1037));
1038
Nika Layzellf8d5f212017-12-11 14:07:02 -05001039fn cooked_char(input: Cursor) -> PResult<()> {
Alex Crichton44bffbc2017-05-19 17:51:59 -07001040 let mut chars = input.char_indices();
1041 let ok = match chars.next().map(|(_, ch)| ch) {
David Tolnayb28f38a2018-03-31 22:02:29 +02001042 Some('\\') => match chars.next().map(|(_, ch)| ch) {
1043 Some('x') => backslash_x_char(&mut chars),
1044 Some('u') => backslash_u(&mut chars),
1045 Some('n') | Some('r') | Some('t') | Some('\\') | Some('0') | Some('\'') | Some('"') => {
1046 true
Alex Crichton44bffbc2017-05-19 17:51:59 -07001047 }
David Tolnayb28f38a2018-03-31 22:02:29 +02001048 _ => false,
1049 },
Alex Crichton44bffbc2017-05-19 17:51:59 -07001050 ch => ch.is_some(),
1051 };
1052 if ok {
Nika Layzellf8d5f212017-12-11 14:07:02 -05001053 match chars.next() {
1054 Some((idx, _)) => Ok((input.advance(idx), ())),
1055 None => Ok((input.advance(input.len()), ())),
1056 }
Alex Crichton44bffbc2017-05-19 17:51:59 -07001057 } else {
David Tolnay1218e122017-06-01 11:13:45 -07001058 Err(LexError)
Alex Crichton44bffbc2017-05-19 17:51:59 -07001059 }
1060}
1061
1062macro_rules! next_ch {
1063 ($chars:ident @ $pat:pat $(| $rest:pat)*) => {
1064 match $chars.next() {
1065 Some((_, ch)) => match ch {
1066 $pat $(| $rest)* => ch,
1067 _ => return false,
1068 },
1069 None => return false
1070 }
1071 };
1072}
1073
1074fn backslash_x_char<I>(chars: &mut I) -> bool
David Tolnayb28f38a2018-03-31 22:02:29 +02001075where
1076 I: Iterator<Item = (usize, char)>,
Alex Crichton44bffbc2017-05-19 17:51:59 -07001077{
1078 next_ch!(chars @ '0'...'7');
1079 next_ch!(chars @ '0'...'9' | 'a'...'f' | 'A'...'F');
1080 true
1081}
1082
1083fn backslash_x_byte<I>(chars: &mut I) -> bool
David Tolnayb28f38a2018-03-31 22:02:29 +02001084where
1085 I: Iterator<Item = (usize, u8)>,
Alex Crichton44bffbc2017-05-19 17:51:59 -07001086{
1087 next_ch!(chars @ b'0'...b'9' | b'a'...b'f' | b'A'...b'F');
1088 next_ch!(chars @ b'0'...b'9' | b'a'...b'f' | b'A'...b'F');
1089 true
1090}
1091
1092fn backslash_u<I>(chars: &mut I) -> bool
David Tolnayb28f38a2018-03-31 22:02:29 +02001093where
1094 I: Iterator<Item = (usize, char)>,
Alex Crichton44bffbc2017-05-19 17:51:59 -07001095{
1096 next_ch!(chars @ '{');
1097 next_ch!(chars @ '0'...'9' | 'a'...'f' | 'A'...'F');
David Tolnay8d109342017-12-25 18:24:45 -05001098 loop {
1099 let c = next_ch!(chars @ '0'...'9' | 'a'...'f' | 'A'...'F' | '_' | '}');
1100 if c == '}' {
1101 return true;
1102 }
Alex Crichton44bffbc2017-05-19 17:51:59 -07001103 }
Alex Crichton44bffbc2017-05-19 17:51:59 -07001104}
1105
Nika Layzellf8d5f212017-12-11 14:07:02 -05001106fn float(input: Cursor) -> PResult<()> {
David Tolnay744a6b82017-06-01 11:34:29 -07001107 let (rest, ()) = float_digits(input)?;
1108 for suffix in &["f32", "f64"] {
1109 if rest.starts_with(suffix) {
Nika Layzellf8d5f212017-12-11 14:07:02 -05001110 return word_break(rest.advance(suffix.len()));
David Tolnay744a6b82017-06-01 11:34:29 -07001111 }
1112 }
1113 word_break(rest)
1114}
Alex Crichton44bffbc2017-05-19 17:51:59 -07001115
Nika Layzellf8d5f212017-12-11 14:07:02 -05001116fn float_digits(input: Cursor) -> PResult<()> {
Alex Crichton44bffbc2017-05-19 17:51:59 -07001117 let mut chars = input.chars().peekable();
1118 match chars.next() {
1119 Some(ch) if ch >= '0' && ch <= '9' => {}
David Tolnay1218e122017-06-01 11:13:45 -07001120 _ => return Err(LexError),
Alex Crichton44bffbc2017-05-19 17:51:59 -07001121 }
1122
1123 let mut len = 1;
1124 let mut has_dot = false;
1125 let mut has_exp = false;
1126 while let Some(&ch) = chars.peek() {
1127 match ch {
1128 '0'...'9' | '_' => {
1129 chars.next();
1130 len += 1;
1131 }
1132 '.' => {
1133 if has_dot {
1134 break;
1135 }
1136 chars.next();
David Tolnayb28f38a2018-03-31 22:02:29 +02001137 if chars
1138 .peek()
1139 .map(|&ch| ch == '.' || UnicodeXID::is_xid_start(ch))
1140 .unwrap_or(false)
1141 {
David Tolnay1218e122017-06-01 11:13:45 -07001142 return Err(LexError);
Alex Crichton44bffbc2017-05-19 17:51:59 -07001143 }
1144 len += 1;
1145 has_dot = true;
1146 }
1147 'e' | 'E' => {
1148 chars.next();
1149 len += 1;
1150 has_exp = true;
1151 break;
1152 }
1153 _ => break,
1154 }
1155 }
1156
Nika Layzellf8d5f212017-12-11 14:07:02 -05001157 let rest = input.advance(len);
Alex Crichton44bffbc2017-05-19 17:51:59 -07001158 if !(has_dot || has_exp || rest.starts_with("f32") || rest.starts_with("f64")) {
David Tolnay1218e122017-06-01 11:13:45 -07001159 return Err(LexError);
Alex Crichton44bffbc2017-05-19 17:51:59 -07001160 }
1161
1162 if has_exp {
1163 let mut has_exp_value = false;
1164 while let Some(&ch) = chars.peek() {
1165 match ch {
1166 '+' | '-' => {
1167 if has_exp_value {
1168 break;
1169 }
1170 chars.next();
1171 len += 1;
1172 }
1173 '0'...'9' => {
1174 chars.next();
1175 len += 1;
1176 has_exp_value = true;
1177 }
1178 '_' => {
1179 chars.next();
1180 len += 1;
1181 }
1182 _ => break,
1183 }
1184 }
1185 if !has_exp_value {
David Tolnay1218e122017-06-01 11:13:45 -07001186 return Err(LexError);
Alex Crichton44bffbc2017-05-19 17:51:59 -07001187 }
1188 }
1189
Nika Layzellf8d5f212017-12-11 14:07:02 -05001190 Ok((input.advance(len), ()))
Alex Crichton44bffbc2017-05-19 17:51:59 -07001191}
1192
Nika Layzellf8d5f212017-12-11 14:07:02 -05001193fn int(input: Cursor) -> PResult<()> {
David Tolnay744a6b82017-06-01 11:34:29 -07001194 let (rest, ()) = digits(input)?;
1195 for suffix in &[
David Tolnay48ea5042018-04-23 19:17:35 -07001196 "isize", "i8", "i16", "i32", "i64", "i128", "usize", "u8", "u16", "u32", "u64", "u128",
David Tolnay744a6b82017-06-01 11:34:29 -07001197 ] {
1198 if rest.starts_with(suffix) {
Nika Layzellf8d5f212017-12-11 14:07:02 -05001199 return word_break(rest.advance(suffix.len()));
David Tolnay744a6b82017-06-01 11:34:29 -07001200 }
1201 }
1202 word_break(rest)
1203}
Alex Crichton44bffbc2017-05-19 17:51:59 -07001204
Nika Layzellf8d5f212017-12-11 14:07:02 -05001205fn digits(mut input: Cursor) -> PResult<()> {
Alex Crichton44bffbc2017-05-19 17:51:59 -07001206 let base = if input.starts_with("0x") {
Nika Layzellf8d5f212017-12-11 14:07:02 -05001207 input = input.advance(2);
Alex Crichton44bffbc2017-05-19 17:51:59 -07001208 16
1209 } else if input.starts_with("0o") {
Nika Layzellf8d5f212017-12-11 14:07:02 -05001210 input = input.advance(2);
Alex Crichton44bffbc2017-05-19 17:51:59 -07001211 8
1212 } else if input.starts_with("0b") {
Nika Layzellf8d5f212017-12-11 14:07:02 -05001213 input = input.advance(2);
Alex Crichton44bffbc2017-05-19 17:51:59 -07001214 2
1215 } else {
1216 10
1217 };
1218
Alex Crichton44bffbc2017-05-19 17:51:59 -07001219 let mut len = 0;
1220 let mut empty = true;
1221 for b in input.bytes() {
1222 let digit = match b {
1223 b'0'...b'9' => (b - b'0') as u64,
1224 b'a'...b'f' => 10 + (b - b'a') as u64,
1225 b'A'...b'F' => 10 + (b - b'A') as u64,
1226 b'_' => {
1227 if empty && base == 10 {
David Tolnay1218e122017-06-01 11:13:45 -07001228 return Err(LexError);
Alex Crichton44bffbc2017-05-19 17:51:59 -07001229 }
1230 len += 1;
1231 continue;
1232 }
1233 _ => break,
1234 };
1235 if digit >= base {
David Tolnay1218e122017-06-01 11:13:45 -07001236 return Err(LexError);
Alex Crichton44bffbc2017-05-19 17:51:59 -07001237 }
Alex Crichton44bffbc2017-05-19 17:51:59 -07001238 len += 1;
1239 empty = false;
1240 }
1241 if empty {
David Tolnay1218e122017-06-01 11:13:45 -07001242 Err(LexError)
Alex Crichton44bffbc2017-05-19 17:51:59 -07001243 } else {
Nika Layzellf8d5f212017-12-11 14:07:02 -05001244 Ok((input.advance(len), ()))
Alex Crichton44bffbc2017-05-19 17:51:59 -07001245 }
1246}
1247
Alex Crichtonf3888432018-05-16 09:11:05 -07001248fn op(input: Cursor) -> PResult<Punct> {
David Tolnayea75c5f2017-05-31 23:40:33 -07001249 let input = skip_whitespace(input);
1250 match op_char(input) {
Alex Crichtonf3888432018-05-16 09:11:05 -07001251 Ok((rest, '\'')) => {
1252 symbol(rest)?;
1253 Ok((rest, Punct::new('\'', Spacing::Joint)))
1254 }
David Tolnay1218e122017-06-01 11:13:45 -07001255 Ok((rest, ch)) => {
David Tolnayea75c5f2017-05-31 23:40:33 -07001256 let kind = match op_char(rest) {
Alex Crichton1a7f7622017-07-05 17:47:15 -07001257 Ok(_) => Spacing::Joint,
1258 Err(LexError) => Spacing::Alone,
David Tolnayea75c5f2017-05-31 23:40:33 -07001259 };
Alex Crichtonf3888432018-05-16 09:11:05 -07001260 Ok((rest, Punct::new(ch, kind)))
David Tolnayea75c5f2017-05-31 23:40:33 -07001261 }
David Tolnay1218e122017-06-01 11:13:45 -07001262 Err(LexError) => Err(LexError),
Alex Crichton44bffbc2017-05-19 17:51:59 -07001263 }
1264}
1265
Nika Layzellf8d5f212017-12-11 14:07:02 -05001266fn op_char(input: Cursor) -> PResult<char> {
David Tolnay3a592ad2018-04-22 21:20:24 -07001267 if input.starts_with("//") || input.starts_with("/*") {
1268 // Do not accept `/` of a comment as an op.
1269 return Err(LexError);
1270 }
1271
David Tolnayea75c5f2017-05-31 23:40:33 -07001272 let mut chars = input.chars();
1273 let first = match chars.next() {
1274 Some(ch) => ch,
1275 None => {
David Tolnay1218e122017-06-01 11:13:45 -07001276 return Err(LexError);
David Tolnayea75c5f2017-05-31 23:40:33 -07001277 }
1278 };
Alex Crichtonf3888432018-05-16 09:11:05 -07001279 let recognized = "~!@#$%^&*-=+|;:,<.>/?'";
David Tolnayea75c5f2017-05-31 23:40:33 -07001280 if recognized.contains(first) {
Nika Layzellf8d5f212017-12-11 14:07:02 -05001281 Ok((input.advance(first.len_utf8()), first))
Alex Crichton44bffbc2017-05-19 17:51:59 -07001282 } else {
David Tolnay1218e122017-06-01 11:13:45 -07001283 Err(LexError)
Alex Crichton44bffbc2017-05-19 17:51:59 -07001284 }
1285}
1286
Alex Crichton1eb96a02018-04-04 13:07:35 -07001287fn doc_comment(input: Cursor) -> PResult<Vec<TokenTree>> {
1288 let mut trees = Vec::new();
1289 let (rest, ((comment, inner), span)) = spanned(input, doc_comment_contents)?;
Alex Crichtonf3888432018-05-16 09:11:05 -07001290 trees.push(TokenTree::Punct(Punct::new('#', Spacing::Alone)));
Alex Crichton1eb96a02018-04-04 13:07:35 -07001291 if inner {
Alex Crichtonf3888432018-05-16 09:11:05 -07001292 trees.push(Punct::new('!', Spacing::Alone).into());
Alex Crichton1eb96a02018-04-04 13:07:35 -07001293 }
1294 let mut stream = vec![
Alex Crichtonf3888432018-05-16 09:11:05 -07001295 TokenTree::Ident(::Ident::new("doc", span)),
1296 TokenTree::Punct(Punct::new('=', Spacing::Alone)),
Alex Crichton1eb96a02018-04-04 13:07:35 -07001297 TokenTree::Literal(::Literal::string(comment)),
1298 ];
1299 for tt in stream.iter_mut() {
1300 tt.set_span(span);
1301 }
1302 trees.push(Group::new(Delimiter::Bracket, stream.into_iter().collect()).into());
1303 for tt in trees.iter_mut() {
1304 tt.set_span(span);
1305 }
1306 Ok((rest, trees))
1307}
1308
1309named!(doc_comment_contents -> (&str, bool), alt!(
Alex Crichton44bffbc2017-05-19 17:51:59 -07001310 do_parse!(
1311 punct!("//!") >>
Alex Crichton1eb96a02018-04-04 13:07:35 -07001312 s: take_until_newline_or_eof!() >>
1313 ((s, true))
Alex Crichton44bffbc2017-05-19 17:51:59 -07001314 )
1315 |
1316 do_parse!(
1317 option!(whitespace) >>
1318 peek!(tag!("/*!")) >>
Alex Crichton1eb96a02018-04-04 13:07:35 -07001319 s: block_comment >>
1320 ((s, true))
Alex Crichton44bffbc2017-05-19 17:51:59 -07001321 )
1322 |
1323 do_parse!(
1324 punct!("///") >>
1325 not!(tag!("/")) >>
Alex Crichton1eb96a02018-04-04 13:07:35 -07001326 s: take_until_newline_or_eof!() >>
1327 ((s, false))
Alex Crichton44bffbc2017-05-19 17:51:59 -07001328 )
1329 |
1330 do_parse!(
1331 option!(whitespace) >>
1332 peek!(tuple!(tag!("/**"), not!(tag!("*")))) >>
Alex Crichton1eb96a02018-04-04 13:07:35 -07001333 s: block_comment >>
1334 ((s, false))
Alex Crichton44bffbc2017-05-19 17:51:59 -07001335 )
1336));