diff options
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/lexer/comments.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/parse/lexer/mod.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax/parse/mod.rs | 24 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 30 | ||||
| -rw-r--r-- | src/libsyntax/parse/token.rs | 7 |
5 files changed, 36 insertions, 32 deletions
diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index fb558d1a58f..23449ee69ab 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -12,7 +12,7 @@ pub use self::CommentStyle::*; use ast; use codemap::CodeMap; -use syntax_pos::{BytePos, CharPos, Pos}; +use syntax_pos::{BytePos, CharPos, Pos, FileName}; use parse::lexer::{is_block_doc_comment, is_pattern_whitespace}; use parse::lexer::{self, ParseSess, StringReader, TokenAndSpan}; use print::pprust; @@ -343,7 +343,7 @@ pub struct Literal { // it appears this function is called only from pprust... that's // probably not a good thing. -pub fn gather_comments_and_literals(sess: &ParseSess, path: String, srdr: &mut Read) +pub fn gather_comments_and_literals(sess: &ParseSess, path: FileName, srdr: &mut Read) -> (Vec<Comment>, Vec<Literal>) { let mut src = Vec::new(); srdr.read_to_end(&mut src).unwrap(); diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index d9c33fa50bd..798dfc6d209 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -1714,6 +1714,7 @@ mod tests { use std::cell::RefCell; use std::collections::HashSet; use std::io; + use std::path::PathBuf; use std::rc::Rc; fn mk_sess(cm: Rc<CodeMap>) -> ParseSess { @@ -1735,7 +1736,7 @@ mod tests { sess: &'a ParseSess, teststr: String) -> StringReader<'a> { - let fm = cm.new_filemap("zebra.rs".to_string(), teststr); + let fm = cm.new_filemap(PathBuf::from("zebra.rs").into(), teststr); StringReader::new(sess, fm) } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index c679efd41ea..4d435665d3c 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -12,7 +12,7 @@ use ast::{self, CrateConfig}; use codemap::{CodeMap, FilePathMapping}; -use syntax_pos::{self, Span, FileMap, NO_EXPANSION}; +use syntax_pos::{self, Span, FileMap, NO_EXPANSION, FileName}; use errors::{Handler, ColorConfig, DiagnosticBuilder}; use feature_gate::UnstableFeatures; use parse::parser::Parser; @@ -107,17 +107,17 @@ pub fn parse_crate_attrs_from_file<'a>(input: &Path, sess: &'a ParseSess) parser.parse_inner_attributes() } -pub fn parse_crate_from_source_str(name: String, source: String, sess: &ParseSess) +pub fn parse_crate_from_source_str(name: FileName, source: String, sess: &ParseSess) -> PResult<ast::Crate> { new_parser_from_source_str(sess, name, source).parse_crate_mod() } -pub fn parse_crate_attrs_from_source_str(name: String, source: String, sess: &ParseSess) +pub fn parse_crate_attrs_from_source_str(name: FileName, source: String, sess: &ParseSess) -> PResult<Vec<ast::Attribute>> { new_parser_from_source_str(sess, name, source).parse_inner_attributes() } -pub fn parse_expr_from_source_str(name: String, source: String, sess: &ParseSess) +pub fn parse_expr_from_source_str(name: FileName, source: String, sess: &ParseSess) -> PResult<P<ast::Expr>> { new_parser_from_source_str(sess, name, source).parse_expr() } @@ -126,29 +126,29 @@ pub fn parse_expr_from_source_str(name: String, source: String, sess: &ParseSess /// /// Returns `Ok(Some(item))` when successful, `Ok(None)` when no item was found, and `Err` /// when a syntax error occurred. -pub fn parse_item_from_source_str(name: String, source: String, sess: &ParseSess) +pub fn parse_item_from_source_str(name: FileName, source: String, sess: &ParseSess) -> PResult<Option<P<ast::Item>>> { new_parser_from_source_str(sess, name, source).parse_item() } -pub fn parse_meta_from_source_str(name: String, source: String, sess: &ParseSess) +pub fn parse_meta_from_source_str(name: FileName, source: String, sess: &ParseSess) -> PResult<ast::MetaItem> { new_parser_from_source_str(sess, name, source).parse_meta_item() } -pub fn parse_stmt_from_source_str(name: String, source: String, sess: &ParseSess) +pub fn parse_stmt_from_source_str(name: FileName, source: String, sess: &ParseSess) -> PResult<Option<ast::Stmt>> { new_parser_from_source_str(sess, name, source).parse_stmt() } -pub fn parse_stream_from_source_str(name: String, source: String, sess: &ParseSess, +pub fn parse_stream_from_source_str(name: FileName, source: String, sess: &ParseSess, override_span: Option<Span>) -> TokenStream { filemap_to_stream(sess, sess.codemap().new_filemap(name, source), override_span) } // Create a new parser from a source string -pub fn new_parser_from_source_str(sess: &ParseSess, name: String, source: String) +pub fn new_parser_from_source_str(sess: &ParseSess, name: FileName, source: String) -> Parser { let mut parser = filemap_to_parser(sess, sess.codemap().new_filemap(name, source)); parser.recurse_into_file_modules = false; @@ -1018,7 +1018,7 @@ mod tests { #[test] fn crlf_doc_comments() { let sess = ParseSess::new(FilePathMapping::empty()); - let name = "<source>".to_string(); + let name = FileName::Custom("source".to_string()); let source = "/// doc comment\r\nfn foo() {}".to_string(); let item = parse_item_from_source_str(name.clone(), source, &sess) .unwrap().unwrap(); @@ -1042,7 +1042,7 @@ mod tests { #[test] fn ttdelim_span() { let sess = ParseSess::new(FilePathMapping::empty()); - let expr = parse::parse_expr_from_source_str("foo".to_string(), + let expr = parse::parse_expr_from_source_str(PathBuf::from("foo").into(), "foo!( fn main() { body } )".to_string(), &sess).unwrap(); let tts: Vec<_> = match expr.node { @@ -1065,7 +1065,7 @@ mod tests { fn out_of_line_mod() { let sess = ParseSess::new(FilePathMapping::empty()); let item = parse_item_from_source_str( - "foo".to_owned(), + PathBuf::from("foo").into(), "mod foo { struct S; mod this_does_not_exist; }".to_owned(), &sess, ).unwrap().unwrap(); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 726db733482..b3ef70fd18e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -41,7 +41,7 @@ use ast::{BinOpKind, UnOp}; use ast::{RangeEnd, RangeSyntax}; use {ast, attr}; use codemap::{self, CodeMap, Spanned, respan}; -use syntax_pos::{self, Span, BytePos}; +use syntax_pos::{self, Span, BytePos, FileName, DUMMY_SP}; use errors::{self, DiagnosticBuilder}; use parse::{self, classify, token}; use parse::common::SeqSep; @@ -527,9 +527,11 @@ impl<'a> Parser<'a> { if let Some(directory) = directory { parser.directory = directory; - } else if parser.span != syntax_pos::DUMMY_SP { - parser.directory.path = sess.codemap().span_to_unmapped_path(parser.span); - parser.directory.path.pop(); + } else if !parser.span.source_equal(&DUMMY_SP) { + if let FileName::Real(path) = sess.codemap().span_to_unmapped_path(parser.span) { + parser.directory.path = path; + parser.directory.path.pop(); + } } parser.process_potential_macro_variable(); @@ -5764,15 +5766,17 @@ impl<'a> Parser<'a> { let mut err = self.diagnostic().struct_span_err(id_sp, "cannot declare a new module at this location"); if id_sp != syntax_pos::DUMMY_SP { - let src_path = PathBuf::from(self.sess.codemap().span_to_filename(id_sp)); - if let Some(stem) = src_path.file_stem() { - let mut dest_path = src_path.clone(); - dest_path.set_file_name(stem); - dest_path.push("mod.rs"); - err.span_note(id_sp, - &format!("maybe move this module `{}` to its own \ - directory via `{}`", src_path.to_string_lossy(), - dest_path.to_string_lossy())); + let src_path = self.sess.codemap().span_to_filename(id_sp); + if let FileName::Real(src_path) = src_path { + if let Some(stem) = src_path.file_stem() { + let mut dest_path = src_path.clone(); + dest_path.set_file_name(stem); + dest_path.push("mod.rs"); + err.span_note(id_sp, + &format!("maybe move this module `{}` to its own \ + directory via `{}`", src_path.display(), + dest_path.display())); + } } } if paths.path_exists { diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 26f39f60880..05368c52d2c 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -21,7 +21,7 @@ use ptr::P; use serialize::{Decodable, Decoder, Encodable, Encoder}; use symbol::keywords; use syntax::parse::parse_stream_from_source_str; -use syntax_pos::{self, Span}; +use syntax_pos::{self, Span, FileName}; use tokenstream::{TokenStream, TokenTree}; use tokenstream; @@ -495,9 +495,8 @@ impl Token { tokens.unwrap_or_else(|| { nt.1.force(|| { // FIXME(jseyfried): Avoid this pretty-print + reparse hack - let name = "<macro expansion>".to_owned(); let source = pprust::token_to_string(self); - parse_stream_from_source_str(name, source, sess, Some(span)) + parse_stream_from_source_str(FileName::MacroExpansion, source, sess, Some(span)) }) }) } @@ -629,7 +628,7 @@ fn prepend_attrs(sess: &ParseSess, assert_eq!(attr.style, ast::AttrStyle::Outer, "inner attributes should prevent cached tokens from existing"); // FIXME: Avoid this pretty-print + reparse hack as bove - let name = "<macro expansion>".to_owned(); + let name = FileName::MacroExpansion; let source = pprust::attr_to_string(attr); let stream = parse_stream_from_source_str(name, source, sess, Some(span)); builder.push(stream); |
