]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/mod.rs
Rollup merge of #40521 - TimNN:panic-free-shift, r=alexcrichton
[rust.git] / src / libsyntax / parse / mod.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! The main parser interface
12
13 use ast::{self, CrateConfig};
14 use codemap::CodeMap;
15 use syntax_pos::{self, Span, FileMap};
16 use errors::{Handler, ColorConfig, DiagnosticBuilder};
17 use feature_gate::UnstableFeatures;
18 use parse::parser::Parser;
19 use ptr::P;
20 use str::char_at;
21 use symbol::Symbol;
22 use tokenstream::{TokenStream, TokenTree};
23
24 use std::cell::RefCell;
25 use std::collections::HashSet;
26 use std::iter;
27 use std::path::{Path, PathBuf};
28 use std::rc::Rc;
29 use std::str;
30
31 pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a>>;
32
33 #[macro_use]
34 pub mod parser;
35
36 pub mod lexer;
37 pub mod token;
38 pub mod attr;
39
40 pub mod common;
41 pub mod classify;
42 pub mod obsolete;
43
44 /// Info about a parsing session.
45 pub struct ParseSess {
46     pub span_diagnostic: Handler,
47     pub unstable_features: UnstableFeatures,
48     pub config: CrateConfig,
49     pub missing_fragment_specifiers: RefCell<HashSet<Span>>,
50     /// Used to determine and report recursive mod inclusions
51     included_mod_stack: RefCell<Vec<PathBuf>>,
52     code_map: Rc<CodeMap>,
53 }
54
55 impl ParseSess {
56     pub fn new() -> Self {
57         let cm = Rc::new(CodeMap::new());
58         let handler = Handler::with_tty_emitter(ColorConfig::Auto,
59                                                 true,
60                                                 false,
61                                                 Some(cm.clone()));
62         ParseSess::with_span_handler(handler, cm)
63     }
64
65     pub fn with_span_handler(handler: Handler, code_map: Rc<CodeMap>) -> ParseSess {
66         ParseSess {
67             span_diagnostic: handler,
68             unstable_features: UnstableFeatures::from_environment(),
69             config: HashSet::new(),
70             missing_fragment_specifiers: RefCell::new(HashSet::new()),
71             included_mod_stack: RefCell::new(vec![]),
72             code_map: code_map
73         }
74     }
75
76     pub fn codemap(&self) -> &CodeMap {
77         &self.code_map
78     }
79 }
80
81 #[derive(Clone)]
82 pub struct Directory {
83     pub path: PathBuf,
84     pub ownership: DirectoryOwnership,
85 }
86
87 #[derive(Copy, Clone)]
88 pub enum DirectoryOwnership {
89     Owned,
90     UnownedViaBlock,
91     UnownedViaMod(bool /* legacy warnings? */),
92 }
93
94 // a bunch of utility functions of the form parse_<thing>_from_<source>
95 // where <thing> includes crate, expr, item, stmt, tts, and one that
96 // uses a HOF to parse anything, and <source> includes file and
97 // source_str.
98
99 pub fn parse_crate_from_file<'a>(input: &Path, sess: &'a ParseSess) -> PResult<'a, ast::Crate> {
100     let mut parser = new_parser_from_file(sess, input);
101     parser.parse_crate_mod()
102 }
103
104 pub fn parse_crate_attrs_from_file<'a>(input: &Path, sess: &'a ParseSess)
105                                        -> PResult<'a, Vec<ast::Attribute>> {
106     let mut parser = new_parser_from_file(sess, input);
107     parser.parse_inner_attributes()
108 }
109
110 pub fn parse_crate_from_source_str<'a>(name: String, source: String, sess: &'a ParseSess)
111                                        -> PResult<'a, ast::Crate> {
112     new_parser_from_source_str(sess, name, source).parse_crate_mod()
113 }
114
115 pub fn parse_crate_attrs_from_source_str<'a>(name: String, source: String, sess: &'a ParseSess)
116                                              -> PResult<'a, Vec<ast::Attribute>> {
117     new_parser_from_source_str(sess, name, source).parse_inner_attributes()
118 }
119
120 pub fn parse_expr_from_source_str<'a>(name: String, source: String, sess: &'a ParseSess)
121                                       -> PResult<'a, P<ast::Expr>> {
122     new_parser_from_source_str(sess, name, source).parse_expr()
123 }
124
125 /// Parses an item.
126 ///
127 /// Returns `Ok(Some(item))` when successful, `Ok(None)` when no item was found, and`Err`
128 /// when a syntax error occurred.
129 pub fn parse_item_from_source_str<'a>(name: String, source: String, sess: &'a ParseSess)
130                                       -> PResult<'a, Option<P<ast::Item>>> {
131     new_parser_from_source_str(sess, name, source).parse_item()
132 }
133
134 pub fn parse_meta_from_source_str<'a>(name: String, source: String, sess: &'a ParseSess)
135                                       -> PResult<'a, ast::MetaItem> {
136     new_parser_from_source_str(sess, name, source).parse_meta_item()
137 }
138
139 pub fn parse_stmt_from_source_str<'a>(name: String, source: String, sess: &'a ParseSess)
140                                       -> PResult<'a, Option<ast::Stmt>> {
141     new_parser_from_source_str(sess, name, source).parse_stmt()
142 }
143
144 pub fn parse_stream_from_source_str<'a>(name: String, source: String, sess: &'a ParseSess)
145                                         -> TokenStream {
146     filemap_to_stream(sess, sess.codemap().new_filemap(name, None, source))
147 }
148
149 // Create a new parser from a source string
150 pub fn new_parser_from_source_str<'a>(sess: &'a ParseSess, name: String, source: String)
151                                       -> Parser<'a> {
152     filemap_to_parser(sess, sess.codemap().new_filemap(name, None, source))
153 }
154
155 /// Create a new parser, handling errors as appropriate
156 /// if the file doesn't exist
157 pub fn new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path) -> Parser<'a> {
158     filemap_to_parser(sess, file_to_filemap(sess, path, None))
159 }
160
161 /// Given a session, a crate config, a path, and a span, add
162 /// the file at the given path to the codemap, and return a parser.
163 /// On an error, use the given span as the source of the problem.
164 pub fn new_sub_parser_from_file<'a>(sess: &'a ParseSess,
165                                     path: &Path,
166                                     directory_ownership: DirectoryOwnership,
167                                     module_name: Option<String>,
168                                     sp: Span) -> Parser<'a> {
169     let mut p = filemap_to_parser(sess, file_to_filemap(sess, path, Some(sp)));
170     p.directory.ownership = directory_ownership;
171     p.root_module_name = module_name;
172     p
173 }
174
175 /// Given a filemap and config, return a parser
176 pub fn filemap_to_parser<'a>(sess: &'a ParseSess, filemap: Rc<FileMap>, ) -> Parser<'a> {
177     let end_pos = filemap.end_pos;
178     let mut parser = stream_to_parser(sess, filemap_to_stream(sess, filemap));
179
180     if parser.token == token::Eof && parser.span == syntax_pos::DUMMY_SP {
181         parser.span = syntax_pos::mk_sp(end_pos, end_pos);
182     }
183
184     parser
185 }
186
187 // must preserve old name for now, because quote! from the *existing*
188 // compiler expands into it
189 pub fn new_parser_from_tts<'a>(sess: &'a ParseSess, tts: Vec<TokenTree>) -> Parser<'a> {
190     stream_to_parser(sess, tts.into_iter().collect())
191 }
192
193
194 // base abstractions
195
196 /// Given a session and a path and an optional span (for error reporting),
197 /// add the path to the session's codemap and return the new filemap.
198 fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
199                    -> Rc<FileMap> {
200     match sess.codemap().load_file(path) {
201         Ok(filemap) => filemap,
202         Err(e) => {
203             let msg = format!("couldn't read {:?}: {}", path.display(), e);
204             match spanopt {
205                 Some(sp) => panic!(sess.span_diagnostic.span_fatal(sp, &msg)),
206                 None => panic!(sess.span_diagnostic.fatal(&msg))
207             }
208         }
209     }
210 }
211
212 /// Given a filemap, produce a sequence of token-trees
213 pub fn filemap_to_stream(sess: &ParseSess, filemap: Rc<FileMap>) -> TokenStream {
214     let mut srdr = lexer::StringReader::new(sess, filemap);
215     srdr.real_token();
216     panictry!(srdr.parse_all_token_trees())
217 }
218
219 /// Given stream and the ParseSess, produce a parser
220 pub fn stream_to_parser<'a>(sess: &'a ParseSess, stream: TokenStream) -> Parser<'a> {
221     let mut p = Parser::new(sess, stream, None, false);
222     p.check_unknown_macro_variable();
223     p
224 }
225
226 /// Parse a string representing a character literal into its final form.
227 /// Rather than just accepting/rejecting a given literal, unescapes it as
228 /// well. Can take any slice prefixed by a character escape. Returns the
229 /// character and the number of characters consumed.
230 pub fn char_lit(lit: &str) -> (char, isize) {
231     use std::char;
232
233     // Handle non-escaped chars first.
234     if lit.as_bytes()[0] != b'\\' {
235         // If the first byte isn't '\\' it might part of a multi-byte char, so
236         // get the char with chars().
237         let c = lit.chars().next().unwrap();
238         return (c, 1);
239     }
240
241     // Handle escaped chars.
242     match lit.as_bytes()[1] as char {
243         '"' => ('"', 2),
244         'n' => ('\n', 2),
245         'r' => ('\r', 2),
246         't' => ('\t', 2),
247         '\\' => ('\\', 2),
248         '\'' => ('\'', 2),
249         '0' => ('\0', 2),
250         'x' => {
251             let v = u32::from_str_radix(&lit[2..4], 16).unwrap();
252             let c = char::from_u32(v).unwrap();
253             (c, 4)
254         }
255         'u' => {
256             assert!(lit.as_bytes()[2] == b'{');
257             let idx = lit.find('}').unwrap();
258             let v = u32::from_str_radix(&lit[3..idx], 16).unwrap();
259             let c = char::from_u32(v).unwrap();
260             (c, (idx + 1) as isize)
261         }
262         _ => panic!("lexer should have rejected a bad character escape {}", lit)
263     }
264 }
265
266 /// Parse a string representing a string literal into its final form. Does
267 /// unescaping.
268 pub fn str_lit(lit: &str) -> String {
269     debug!("parse_str_lit: given {}", lit.escape_default());
270     let mut res = String::with_capacity(lit.len());
271
272     // FIXME #8372: This could be a for-loop if it didn't borrow the iterator
273     let error = |i| format!("lexer should have rejected {} at {}", lit, i);
274
275     /// Eat everything up to a non-whitespace
276     fn eat<'a>(it: &mut iter::Peekable<str::CharIndices<'a>>) {
277         loop {
278             match it.peek().map(|x| x.1) {
279                 Some(' ') | Some('\n') | Some('\r') | Some('\t') => {
280                     it.next();
281                 },
282                 _ => { break; }
283             }
284         }
285     }
286
287     let mut chars = lit.char_indices().peekable();
288     loop {
289         match chars.next() {
290             Some((i, c)) => {
291                 match c {
292                     '\\' => {
293                         let ch = chars.peek().unwrap_or_else(|| {
294                             panic!("{}", error(i))
295                         }).1;
296
297                         if ch == '\n' {
298                             eat(&mut chars);
299                         } else if ch == '\r' {
300                             chars.next();
301                             let ch = chars.peek().unwrap_or_else(|| {
302                                 panic!("{}", error(i))
303                             }).1;
304
305                             if ch != '\n' {
306                                 panic!("lexer accepted bare CR");
307                             }
308                             eat(&mut chars);
309                         } else {
310                             // otherwise, a normal escape
311                             let (c, n) = char_lit(&lit[i..]);
312                             for _ in 0..n - 1 { // we don't need to move past the first \
313                                 chars.next();
314                             }
315                             res.push(c);
316                         }
317                     },
318                     '\r' => {
319                         let ch = chars.peek().unwrap_or_else(|| {
320                             panic!("{}", error(i))
321                         }).1;
322
323                         if ch != '\n' {
324                             panic!("lexer accepted bare CR");
325                         }
326                         chars.next();
327                         res.push('\n');
328                     }
329                     c => res.push(c),
330                 }
331             },
332             None => break
333         }
334     }
335
336     res.shrink_to_fit(); // probably not going to do anything, unless there was an escape.
337     debug!("parse_str_lit: returning {}", res);
338     res
339 }
340
341 /// Parse a string representing a raw string literal into its final form. The
342 /// only operation this does is convert embedded CRLF into a single LF.
343 pub fn raw_str_lit(lit: &str) -> String {
344     debug!("raw_str_lit: given {}", lit.escape_default());
345     let mut res = String::with_capacity(lit.len());
346
347     // FIXME #8372: This could be a for-loop if it didn't borrow the iterator
348     let mut chars = lit.chars().peekable();
349     loop {
350         match chars.next() {
351             Some(c) => {
352                 if c == '\r' {
353                     if *chars.peek().unwrap() != '\n' {
354                         panic!("lexer accepted bare CR");
355                     }
356                     chars.next();
357                     res.push('\n');
358                 } else {
359                     res.push(c);
360                 }
361             },
362             None => break
363         }
364     }
365
366     res.shrink_to_fit();
367     res
368 }
369
370 // check if `s` looks like i32 or u1234 etc.
371 fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
372     s.len() > 1 &&
373         first_chars.contains(&char_at(s, 0)) &&
374         s[1..].chars().all(|c| '0' <= c && c <= '9')
375 }
376
377 macro_rules! err {
378     ($opt_diag:expr, |$span:ident, $diag:ident| $($body:tt)*) => {
379         match $opt_diag {
380             Some(($span, $diag)) => { $($body)* }
381             None => return None,
382         }
383     }
384 }
385
386 pub fn lit_token(lit: token::Lit, suf: Option<Symbol>, diag: Option<(Span, &Handler)>)
387                  -> (bool /* suffix illegal? */, Option<ast::LitKind>) {
388     use ast::LitKind;
389
390     match lit {
391        token::Byte(i) => (true, Some(LitKind::Byte(byte_lit(&i.as_str()).0))),
392        token::Char(i) => (true, Some(LitKind::Char(char_lit(&i.as_str()).0))),
393
394         // There are some valid suffixes for integer and float literals,
395         // so all the handling is done internally.
396         token::Integer(s) => (false, integer_lit(&s.as_str(), suf, diag)),
397         token::Float(s) => (false, float_lit(&s.as_str(), suf, diag)),
398
399         token::Str_(s) => {
400             let s = Symbol::intern(&str_lit(&s.as_str()));
401             (true, Some(LitKind::Str(s, ast::StrStyle::Cooked)))
402         }
403         token::StrRaw(s, n) => {
404             let s = Symbol::intern(&raw_str_lit(&s.as_str()));
405             (true, Some(LitKind::Str(s, ast::StrStyle::Raw(n))))
406         }
407         token::ByteStr(i) => {
408             (true, Some(LitKind::ByteStr(byte_str_lit(&i.as_str()))))
409         }
410         token::ByteStrRaw(i, _) => {
411             (true, Some(LitKind::ByteStr(Rc::new(i.to_string().into_bytes()))))
412         }
413     }
414 }
415
416 fn filtered_float_lit(data: Symbol, suffix: Option<Symbol>, diag: Option<(Span, &Handler)>)
417                       -> Option<ast::LitKind> {
418     debug!("filtered_float_lit: {}, {:?}", data, suffix);
419     let suffix = match suffix {
420         Some(suffix) => suffix,
421         None => return Some(ast::LitKind::FloatUnsuffixed(data)),
422     };
423
424     Some(match &*suffix.as_str() {
425         "f32" => ast::LitKind::Float(data, ast::FloatTy::F32),
426         "f64" => ast::LitKind::Float(data, ast::FloatTy::F64),
427         suf => {
428             err!(diag, |span, diag| {
429                 if suf.len() >= 2 && looks_like_width_suffix(&['f'], suf) {
430                     // if it looks like a width, lets try to be helpful.
431                     let msg = format!("invalid width `{}` for float literal", &suf[1..]);
432                     diag.struct_span_err(span, &msg).help("valid widths are 32 and 64").emit()
433                 } else {
434                     let msg = format!("invalid suffix `{}` for float literal", suf);
435                     diag.struct_span_err(span, &msg)
436                         .help("valid suffixes are `f32` and `f64`")
437                         .emit();
438                 }
439             });
440
441             ast::LitKind::FloatUnsuffixed(data)
442         }
443     })
444 }
445 pub fn float_lit(s: &str, suffix: Option<Symbol>, diag: Option<(Span, &Handler)>)
446                  -> Option<ast::LitKind> {
447     debug!("float_lit: {:?}, {:?}", s, suffix);
448     // FIXME #2252: bounds checking float literals is deferred until trans
449     let s = s.chars().filter(|&c| c != '_').collect::<String>();
450     filtered_float_lit(Symbol::intern(&s), suffix, diag)
451 }
452
453 /// Parse a string representing a byte literal into its final form. Similar to `char_lit`
454 pub fn byte_lit(lit: &str) -> (u8, usize) {
455     let err = |i| format!("lexer accepted invalid byte literal {} step {}", lit, i);
456
457     if lit.len() == 1 {
458         (lit.as_bytes()[0], 1)
459     } else {
460         assert!(lit.as_bytes()[0] == b'\\', err(0));
461         let b = match lit.as_bytes()[1] {
462             b'"' => b'"',
463             b'n' => b'\n',
464             b'r' => b'\r',
465             b't' => b'\t',
466             b'\\' => b'\\',
467             b'\'' => b'\'',
468             b'0' => b'\0',
469             _ => {
470                 match u64::from_str_radix(&lit[2..4], 16).ok() {
471                     Some(c) =>
472                         if c > 0xFF {
473                             panic!(err(2))
474                         } else {
475                             return (c as u8, 4)
476                         },
477                     None => panic!(err(3))
478                 }
479             }
480         };
481         return (b, 2);
482     }
483 }
484
485 pub fn byte_str_lit(lit: &str) -> Rc<Vec<u8>> {
486     let mut res = Vec::with_capacity(lit.len());
487
488     // FIXME #8372: This could be a for-loop if it didn't borrow the iterator
489     let error = |i| format!("lexer should have rejected {} at {}", lit, i);
490
491     /// Eat everything up to a non-whitespace
492     fn eat<'a, I: Iterator<Item=(usize, u8)>>(it: &mut iter::Peekable<I>) {
493         loop {
494             match it.peek().map(|x| x.1) {
495                 Some(b' ') | Some(b'\n') | Some(b'\r') | Some(b'\t') => {
496                     it.next();
497                 },
498                 _ => { break; }
499             }
500         }
501     }
502
503     // byte string literals *must* be ASCII, but the escapes don't have to be
504     let mut chars = lit.bytes().enumerate().peekable();
505     loop {
506         match chars.next() {
507             Some((i, b'\\')) => {
508                 let em = error(i);
509                 match chars.peek().expect(&em).1 {
510                     b'\n' => eat(&mut chars),
511                     b'\r' => {
512                         chars.next();
513                         if chars.peek().expect(&em).1 != b'\n' {
514                             panic!("lexer accepted bare CR");
515                         }
516                         eat(&mut chars);
517                     }
518                     _ => {
519                         // otherwise, a normal escape
520                         let (c, n) = byte_lit(&lit[i..]);
521                         // we don't need to move past the first \
522                         for _ in 0..n - 1 {
523                             chars.next();
524                         }
525                         res.push(c);
526                     }
527                 }
528             },
529             Some((i, b'\r')) => {
530                 let em = error(i);
531                 if chars.peek().expect(&em).1 != b'\n' {
532                     panic!("lexer accepted bare CR");
533                 }
534                 chars.next();
535                 res.push(b'\n');
536             }
537             Some((_, c)) => res.push(c),
538             None => break,
539         }
540     }
541
542     Rc::new(res)
543 }
544
545 pub fn integer_lit(s: &str, suffix: Option<Symbol>, diag: Option<(Span, &Handler)>)
546                    -> Option<ast::LitKind> {
547     // s can only be ascii, byte indexing is fine
548
549     let s2 = s.chars().filter(|&c| c != '_').collect::<String>();
550     let mut s = &s2[..];
551
552     debug!("integer_lit: {}, {:?}", s, suffix);
553
554     let mut base = 10;
555     let orig = s;
556     let mut ty = ast::LitIntType::Unsuffixed;
557
558     if char_at(s, 0) == '0' && s.len() > 1 {
559         match char_at(s, 1) {
560             'x' => base = 16,
561             'o' => base = 8,
562             'b' => base = 2,
563             _ => { }
564         }
565     }
566
567     // 1f64 and 2f32 etc. are valid float literals.
568     if let Some(suf) = suffix {
569         if looks_like_width_suffix(&['f'], &suf.as_str()) {
570             let err = match base {
571                 16 => Some("hexadecimal float literal is not supported"),
572                 8 => Some("octal float literal is not supported"),
573                 2 => Some("binary float literal is not supported"),
574                 _ => None,
575             };
576             if let Some(err) = err {
577                 err!(diag, |span, diag| diag.span_err(span, err));
578             }
579             return filtered_float_lit(Symbol::intern(&s), Some(suf), diag)
580         }
581     }
582
583     if base != 10 {
584         s = &s[2..];
585     }
586
587     if let Some(suf) = suffix {
588         if suf.as_str().is_empty() {
589             err!(diag, |span, diag| diag.span_bug(span, "found empty literal suffix in Some"));
590         }
591         ty = match &*suf.as_str() {
592             "isize" => ast::LitIntType::Signed(ast::IntTy::Is),
593             "i8"  => ast::LitIntType::Signed(ast::IntTy::I8),
594             "i16" => ast::LitIntType::Signed(ast::IntTy::I16),
595             "i32" => ast::LitIntType::Signed(ast::IntTy::I32),
596             "i64" => ast::LitIntType::Signed(ast::IntTy::I64),
597             "i128" => ast::LitIntType::Signed(ast::IntTy::I128),
598             "usize" => ast::LitIntType::Unsigned(ast::UintTy::Us),
599             "u8"  => ast::LitIntType::Unsigned(ast::UintTy::U8),
600             "u16" => ast::LitIntType::Unsigned(ast::UintTy::U16),
601             "u32" => ast::LitIntType::Unsigned(ast::UintTy::U32),
602             "u64" => ast::LitIntType::Unsigned(ast::UintTy::U64),
603             "u128" => ast::LitIntType::Unsigned(ast::UintTy::U128),
604             suf => {
605                 // i<digits> and u<digits> look like widths, so lets
606                 // give an error message along those lines
607                 err!(diag, |span, diag| {
608                     if looks_like_width_suffix(&['i', 'u'], suf) {
609                         let msg = format!("invalid width `{}` for integer literal", &suf[1..]);
610                         diag.struct_span_err(span, &msg)
611                             .help("valid widths are 8, 16, 32, 64 and 128")
612                             .emit();
613                     } else {
614                         let msg = format!("invalid suffix `{}` for numeric literal", suf);
615                         diag.struct_span_err(span, &msg)
616                             .help("the suffix must be one of the integral types \
617                                    (`u32`, `isize`, etc)")
618                             .emit();
619                     }
620                 });
621
622                 ty
623             }
624         }
625     }
626
627     debug!("integer_lit: the type is {:?}, base {:?}, the new string is {:?}, the original \
628            string was {:?}, the original suffix was {:?}", ty, base, s, orig, suffix);
629
630     Some(match u128::from_str_radix(s, base) {
631         Ok(r) => ast::LitKind::Int(r, ty),
632         Err(_) => {
633             // small bases are lexed as if they were base 10, e.g, the string
634             // might be `0b10201`. This will cause the conversion above to fail,
635             // but these cases have errors in the lexer: we don't want to emit
636             // two errors, and we especially don't want to emit this error since
637             // it isn't necessarily true.
638             let already_errored = base < 10 &&
639                 s.chars().any(|c| c.to_digit(10).map_or(false, |d| d >= base));
640
641             if !already_errored {
642                 err!(diag, |span, diag| diag.span_err(span, "int literal is too large"));
643             }
644             ast::LitKind::Int(0, ty)
645         }
646     })
647 }
648
649 #[cfg(test)]
650 mod tests {
651     use super::*;
652     use syntax_pos::{self, Span, BytePos, Pos, NO_EXPANSION};
653     use codemap::Spanned;
654     use ast::{self, Ident, PatKind};
655     use abi::Abi;
656     use attr::first_attr_value_str_by_name;
657     use parse;
658     use parse::parser::Parser;
659     use print::pprust::item_to_string;
660     use ptr::P;
661     use tokenstream::{self, TokenTree};
662     use util::parser_testing::{string_to_stream, string_to_parser};
663     use util::parser_testing::{string_to_expr, string_to_item, string_to_stmt};
664     use util::ThinVec;
665
666     // produce a syntax_pos::span
667     fn sp(a: u32, b: u32) -> Span {
668         Span {lo: BytePos(a), hi: BytePos(b), expn_id: NO_EXPANSION}
669     }
670
671     fn str2seg(s: &str, lo: u32, hi: u32) -> ast::PathSegment {
672         ast::PathSegment::from_ident(Ident::from_str(s), sp(lo, hi))
673     }
674
675     #[test] fn path_exprs_1() {
676         assert!(string_to_expr("a".to_string()) ==
677                    P(ast::Expr{
678                     id: ast::DUMMY_NODE_ID,
679                     node: ast::ExprKind::Path(None, ast::Path {
680                         span: sp(0, 1),
681                         segments: vec![str2seg("a", 0, 1)],
682                     }),
683                     span: sp(0, 1),
684                     attrs: ThinVec::new(),
685                    }))
686     }
687
688     #[test] fn path_exprs_2 () {
689         assert!(string_to_expr("::a::b".to_string()) ==
690                    P(ast::Expr {
691                     id: ast::DUMMY_NODE_ID,
692                     node: ast::ExprKind::Path(None, ast::Path {
693                         span: sp(0, 6),
694                         segments: vec![ast::PathSegment::crate_root(),
695                                        str2seg("a", 2, 3),
696                                        str2seg("b", 5, 6)]
697                     }),
698                     span: sp(0, 6),
699                     attrs: ThinVec::new(),
700                    }))
701     }
702
703     #[should_panic]
704     #[test] fn bad_path_expr_1() {
705         string_to_expr("::abc::def::return".to_string());
706     }
707
708     // check the token-tree-ization of macros
709     #[test]
710     fn string_to_tts_macro () {
711         let tts: Vec<_> =
712             string_to_stream("macro_rules! zip (($a)=>($a))".to_string()).trees().collect();
713         let tts: &[TokenTree] = &tts[..];
714
715         match (tts.len(), tts.get(0), tts.get(1), tts.get(2), tts.get(3)) {
716             (
717                 4,
718                 Some(&TokenTree::Token(_, token::Ident(name_macro_rules))),
719                 Some(&TokenTree::Token(_, token::Not)),
720                 Some(&TokenTree::Token(_, token::Ident(name_zip))),
721                 Some(&TokenTree::Delimited(_, ref macro_delimed)),
722             )
723             if name_macro_rules.name == "macro_rules"
724             && name_zip.name == "zip" => {
725                 let tts = &macro_delimed.stream().trees().collect::<Vec<_>>();
726                 match (tts.len(), tts.get(0), tts.get(1), tts.get(2)) {
727                     (
728                         3,
729                         Some(&TokenTree::Delimited(_, ref first_delimed)),
730                         Some(&TokenTree::Token(_, token::FatArrow)),
731                         Some(&TokenTree::Delimited(_, ref second_delimed)),
732                     )
733                     if macro_delimed.delim == token::Paren => {
734                         let tts = &first_delimed.stream().trees().collect::<Vec<_>>();
735                         match (tts.len(), tts.get(0), tts.get(1)) {
736                             (
737                                 2,
738                                 Some(&TokenTree::Token(_, token::Dollar)),
739                                 Some(&TokenTree::Token(_, token::Ident(ident))),
740                             )
741                             if first_delimed.delim == token::Paren && ident.name == "a" => {},
742                             _ => panic!("value 3: {:?}", *first_delimed),
743                         }
744                         let tts = &second_delimed.stream().trees().collect::<Vec<_>>();
745                         match (tts.len(), tts.get(0), tts.get(1)) {
746                             (
747                                 2,
748                                 Some(&TokenTree::Token(_, token::Dollar)),
749                                 Some(&TokenTree::Token(_, token::Ident(ident))),
750                             )
751                             if second_delimed.delim == token::Paren
752                             && ident.name == "a" => {},
753                             _ => panic!("value 4: {:?}", *second_delimed),
754                         }
755                     },
756                     _ => panic!("value 2: {:?}", *macro_delimed),
757                 }
758             },
759             _ => panic!("value: {:?}",tts),
760         }
761     }
762
763     #[test]
764     fn string_to_tts_1() {
765         let tts = string_to_stream("fn a (b : i32) { b; }".to_string());
766
767         let expected = TokenStream::concat(vec![
768             TokenTree::Token(sp(0, 2), token::Ident(Ident::from_str("fn"))).into(),
769             TokenTree::Token(sp(3, 4), token::Ident(Ident::from_str("a"))).into(),
770             TokenTree::Delimited(
771                 sp(5, 14),
772                 tokenstream::Delimited {
773                     delim: token::DelimToken::Paren,
774                     tts: TokenStream::concat(vec![
775                         TokenTree::Token(sp(6, 7), token::Ident(Ident::from_str("b"))).into(),
776                         TokenTree::Token(sp(8, 9), token::Colon).into(),
777                         TokenTree::Token(sp(10, 13), token::Ident(Ident::from_str("i32"))).into(),
778                     ]).into(),
779                 }).into(),
780             TokenTree::Delimited(
781                 sp(15, 21),
782                 tokenstream::Delimited {
783                     delim: token::DelimToken::Brace,
784                     tts: TokenStream::concat(vec![
785                         TokenTree::Token(sp(17, 18), token::Ident(Ident::from_str("b"))).into(),
786                         TokenTree::Token(sp(18, 19), token::Semi).into(),
787                     ]).into(),
788                 }).into()
789         ]);
790
791         assert_eq!(tts, expected);
792     }
793
794     #[test] fn ret_expr() {
795         assert!(string_to_expr("return d".to_string()) ==
796                    P(ast::Expr{
797                     id: ast::DUMMY_NODE_ID,
798                     node:ast::ExprKind::Ret(Some(P(ast::Expr{
799                         id: ast::DUMMY_NODE_ID,
800                         node:ast::ExprKind::Path(None, ast::Path{
801                             span: sp(7, 8),
802                             segments: vec![str2seg("d", 7, 8)],
803                         }),
804                         span:sp(7,8),
805                         attrs: ThinVec::new(),
806                     }))),
807                     span:sp(0,8),
808                     attrs: ThinVec::new(),
809                    }))
810     }
811
812     #[test] fn parse_stmt_1 () {
813         assert!(string_to_stmt("b;".to_string()) ==
814                    Some(ast::Stmt {
815                        node: ast::StmtKind::Expr(P(ast::Expr {
816                            id: ast::DUMMY_NODE_ID,
817                            node: ast::ExprKind::Path(None, ast::Path {
818                                span:sp(0,1),
819                                segments: vec![str2seg("b", 0, 1)],
820                             }),
821                            span: sp(0,1),
822                            attrs: ThinVec::new()})),
823                        id: ast::DUMMY_NODE_ID,
824                        span: sp(0,1)}))
825
826     }
827
828     fn parser_done(p: Parser){
829         assert_eq!(p.token.clone(), token::Eof);
830     }
831
832     #[test] fn parse_ident_pat () {
833         let sess = ParseSess::new();
834         let mut parser = string_to_parser(&sess, "b".to_string());
835         assert!(panictry!(parser.parse_pat())
836                 == P(ast::Pat{
837                 id: ast::DUMMY_NODE_ID,
838                 node: PatKind::Ident(ast::BindingMode::ByValue(ast::Mutability::Immutable),
839                                     Spanned{ span:sp(0, 1),
840                                              node: Ident::from_str("b")
841                     },
842                                     None),
843                 span: sp(0,1)}));
844         parser_done(parser);
845     }
846
847     // check the contents of the tt manually:
848     #[test] fn parse_fundecl () {
849         // this test depends on the intern order of "fn" and "i32"
850         assert_eq!(string_to_item("fn a (b : i32) { b; }".to_string()),
851                   Some(
852                       P(ast::Item{ident:Ident::from_str("a"),
853                             attrs:Vec::new(),
854                             id: ast::DUMMY_NODE_ID,
855                             node: ast::ItemKind::Fn(P(ast::FnDecl {
856                                 inputs: vec![ast::Arg{
857                                     ty: P(ast::Ty{id: ast::DUMMY_NODE_ID,
858                                                   node: ast::TyKind::Path(None, ast::Path{
859                                         span:sp(10,13),
860                                         segments: vec![str2seg("i32", 10, 13)],
861                                         }),
862                                         span:sp(10,13)
863                                     }),
864                                     pat: P(ast::Pat {
865                                         id: ast::DUMMY_NODE_ID,
866                                         node: PatKind::Ident(
867                                             ast::BindingMode::ByValue(ast::Mutability::Immutable),
868                                                 Spanned{
869                                                     span: sp(6,7),
870                                                     node: Ident::from_str("b")},
871                                                 None
872                                                     ),
873                                             span: sp(6,7)
874                                     }),
875                                         id: ast::DUMMY_NODE_ID
876                                     }],
877                                 output: ast::FunctionRetTy::Default(sp(15, 15)),
878                                 variadic: false
879                             }),
880                                     ast::Unsafety::Normal,
881                                     Spanned {
882                                         span: sp(0,2),
883                                         node: ast::Constness::NotConst,
884                                     },
885                                     Abi::Rust,
886                                     ast::Generics{ // no idea on either of these:
887                                         lifetimes: Vec::new(),
888                                         ty_params: Vec::new(),
889                                         where_clause: ast::WhereClause {
890                                             id: ast::DUMMY_NODE_ID,
891                                             predicates: Vec::new(),
892                                         },
893                                         span: syntax_pos::DUMMY_SP,
894                                     },
895                                     P(ast::Block {
896                                         stmts: vec![ast::Stmt {
897                                             node: ast::StmtKind::Semi(P(ast::Expr{
898                                                 id: ast::DUMMY_NODE_ID,
899                                                 node: ast::ExprKind::Path(None,
900                                                       ast::Path{
901                                                         span:sp(17,18),
902                                                         segments: vec![str2seg("b", 17, 18)],
903                                                       }),
904                                                 span: sp(17,18),
905                                                 attrs: ThinVec::new()})),
906                                             id: ast::DUMMY_NODE_ID,
907                                             span: sp(17,19)}],
908                                         id: ast::DUMMY_NODE_ID,
909                                         rules: ast::BlockCheckMode::Default, // no idea
910                                         span: sp(15,21),
911                                     })),
912                             vis: ast::Visibility::Inherited,
913                             span: sp(0,21)})));
914     }
915
916     #[test] fn parse_use() {
917         let use_s = "use foo::bar::baz;";
918         let vitem = string_to_item(use_s.to_string()).unwrap();
919         let vitem_s = item_to_string(&vitem);
920         assert_eq!(&vitem_s[..], use_s);
921
922         let use_s = "use foo::bar as baz;";
923         let vitem = string_to_item(use_s.to_string()).unwrap();
924         let vitem_s = item_to_string(&vitem);
925         assert_eq!(&vitem_s[..], use_s);
926     }
927
928     #[test] fn parse_extern_crate() {
929         let ex_s = "extern crate foo;";
930         let vitem = string_to_item(ex_s.to_string()).unwrap();
931         let vitem_s = item_to_string(&vitem);
932         assert_eq!(&vitem_s[..], ex_s);
933
934         let ex_s = "extern crate foo as bar;";
935         let vitem = string_to_item(ex_s.to_string()).unwrap();
936         let vitem_s = item_to_string(&vitem);
937         assert_eq!(&vitem_s[..], ex_s);
938     }
939
940     fn get_spans_of_pat_idents(src: &str) -> Vec<Span> {
941         let item = string_to_item(src.to_string()).unwrap();
942
943         struct PatIdentVisitor {
944             spans: Vec<Span>
945         }
946         impl<'a> ::visit::Visitor<'a> for PatIdentVisitor {
947             fn visit_pat(&mut self, p: &'a ast::Pat) {
948                 match p.node {
949                     PatKind::Ident(_ , ref spannedident, _) => {
950                         self.spans.push(spannedident.span.clone());
951                     }
952                     _ => {
953                         ::visit::walk_pat(self, p);
954                     }
955                 }
956             }
957         }
958         let mut v = PatIdentVisitor { spans: Vec::new() };
959         ::visit::walk_item(&mut v, &item);
960         return v.spans;
961     }
962
963     #[test] fn span_of_self_arg_pat_idents_are_correct() {
964
965         let srcs = ["impl z { fn a (&self, &myarg: i32) {} }",
966                     "impl z { fn a (&mut self, &myarg: i32) {} }",
967                     "impl z { fn a (&'a self, &myarg: i32) {} }",
968                     "impl z { fn a (self, &myarg: i32) {} }",
969                     "impl z { fn a (self: Foo, &myarg: i32) {} }",
970                     ];
971
972         for &src in &srcs {
973             let spans = get_spans_of_pat_idents(src);
974             let Span{ lo, hi, .. } = spans[0];
975             assert!("self" == &src[lo.to_usize()..hi.to_usize()],
976                     "\"{}\" != \"self\". src=\"{}\"",
977                     &src[lo.to_usize()..hi.to_usize()], src)
978         }
979     }
980
981     #[test] fn parse_exprs () {
982         // just make sure that they parse....
983         string_to_expr("3 + 4".to_string());
984         string_to_expr("a::z.froob(b,&(987+3))".to_string());
985     }
986
987     #[test] fn attrs_fix_bug () {
988         string_to_item("pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
989                    -> Result<Box<Writer>, String> {
990     #[cfg(windows)]
991     fn wb() -> c_int {
992       (O_WRONLY | libc::consts::os::extra::O_BINARY) as c_int
993     }
994
995     #[cfg(unix)]
996     fn wb() -> c_int { O_WRONLY as c_int }
997
998     let mut fflags: c_int = wb();
999 }".to_string());
1000     }
1001
1002     #[test] fn crlf_doc_comments() {
1003         let sess = ParseSess::new();
1004
1005         let name = "<source>".to_string();
1006         let source = "/// doc comment\r\nfn foo() {}".to_string();
1007         let item = parse_item_from_source_str(name.clone(), source, &sess)
1008             .unwrap().unwrap();
1009         let doc = first_attr_value_str_by_name(&item.attrs, "doc").unwrap();
1010         assert_eq!(doc, "/// doc comment");
1011
1012         let source = "/// doc comment\r\n/// line 2\r\nfn foo() {}".to_string();
1013         let item = parse_item_from_source_str(name.clone(), source, &sess)
1014             .unwrap().unwrap();
1015         let docs = item.attrs.iter().filter(|a| a.path == "doc")
1016                     .map(|a| a.value_str().unwrap().to_string()).collect::<Vec<_>>();
1017         let b: &[_] = &["/// doc comment".to_string(), "/// line 2".to_string()];
1018         assert_eq!(&docs[..], b);
1019
1020         let source = "/** doc comment\r\n *  with CRLF */\r\nfn foo() {}".to_string();
1021         let item = parse_item_from_source_str(name, source, &sess).unwrap().unwrap();
1022         let doc = first_attr_value_str_by_name(&item.attrs, "doc").unwrap();
1023         assert_eq!(doc, "/** doc comment\n *  with CRLF */");
1024     }
1025
1026     #[test]
1027     fn ttdelim_span() {
1028         let sess = ParseSess::new();
1029         let expr = parse::parse_expr_from_source_str("foo".to_string(),
1030             "foo!( fn main() { body } )".to_string(), &sess).unwrap();
1031
1032         let tts: Vec<_> = match expr.node {
1033             ast::ExprKind::Mac(ref mac) => mac.node.stream().trees().collect(),
1034             _ => panic!("not a macro"),
1035         };
1036
1037         let span = tts.iter().rev().next().unwrap().span();
1038
1039         match sess.codemap().span_to_snippet(span) {
1040             Ok(s) => assert_eq!(&s[..], "{ body }"),
1041             Err(_) => panic!("could not get snippet"),
1042         }
1043     }
1044 }