Refactor syntax::ext::base::Resolver::resolve_invoc
.
This commit is contained in:
parent
d34318dd53
commit
d902963b6d
@ -19,7 +19,7 @@ use syntax::ast;
|
||||
use syntax::errors::DiagnosticBuilder;
|
||||
use syntax::ext::base::{self, Determinacy, MultiModifier, MultiDecorator, MultiItemModifier};
|
||||
use syntax::ext::base::{NormalTT, SyntaxExtension};
|
||||
use syntax::ext::expand::{Expansion, Invocation, InvocationKind};
|
||||
use syntax::ext::expand::Expansion;
|
||||
use syntax::ext::hygiene::Mark;
|
||||
use syntax::ext::tt::macro_rules;
|
||||
use syntax::parse::token::intern;
|
||||
@ -162,21 +162,13 @@ impl<'a> base::Resolver for Resolver<'a> {
|
||||
None
|
||||
}
|
||||
|
||||
fn resolve_invoc(&mut self, scope: Mark, invoc: &Invocation, force: bool)
|
||||
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
|
||||
-> Result<Rc<SyntaxExtension>, Determinacy> {
|
||||
let (name, span) = match invoc.kind {
|
||||
InvocationKind::Bang { ref mac, .. } => {
|
||||
let path = &mac.node.path;
|
||||
if path.segments.len() > 1 || path.global ||
|
||||
!path.segments[0].parameters.is_empty() {
|
||||
self.session.span_err(path.span,
|
||||
"expected macro name without module separators");
|
||||
return Err(Determinacy::Determined);
|
||||
}
|
||||
(path.segments[0].identifier.name, path.span)
|
||||
}
|
||||
InvocationKind::Attr { ref attr, .. } => (intern(&*attr.name()), attr.span),
|
||||
};
|
||||
if path.segments.len() > 1 || path.global || !path.segments[0].parameters.is_empty() {
|
||||
self.session.span_err(path.span, "expected macro name without module separators");
|
||||
return Err(Determinacy::Determined);
|
||||
}
|
||||
let name = path.segments[0].identifier.name;
|
||||
|
||||
let invocation = self.invocations[&scope];
|
||||
if let LegacyScope::Expansion(parent) = invocation.legacy_scope.get() {
|
||||
@ -184,8 +176,8 @@ impl<'a> base::Resolver for Resolver<'a> {
|
||||
}
|
||||
self.resolve_macro_name(invocation.legacy_scope.get(), name, true).ok_or_else(|| {
|
||||
if force {
|
||||
let mut err =
|
||||
self.session.struct_span_err(span, &format!("macro undefined: '{}!'", name));
|
||||
let msg = format!("macro undefined: '{}!'", name);
|
||||
let mut err = self.session.struct_span_err(path.span, &msg);
|
||||
self.suggest_macro_name(&name.as_str(), &mut err);
|
||||
err.emit();
|
||||
Determinacy::Determined
|
||||
|
@ -15,7 +15,7 @@ use attr::HasAttrs;
|
||||
use codemap::{self, CodeMap, ExpnInfo, Spanned, respan};
|
||||
use syntax_pos::{Span, ExpnId, NO_EXPANSION};
|
||||
use errors::DiagnosticBuilder;
|
||||
use ext::expand::{self, Invocation, Expansion};
|
||||
use ext::expand::{self, Expansion};
|
||||
use ext::hygiene::Mark;
|
||||
use fold::{self, Folder};
|
||||
use parse::{self, parser};
|
||||
@ -522,7 +522,7 @@ pub trait Resolver {
|
||||
fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>);
|
||||
|
||||
fn find_attr_invoc(&mut self, attrs: &mut Vec<Attribute>) -> Option<Attribute>;
|
||||
fn resolve_invoc(&mut self, scope: Mark, invoc: &Invocation, force: bool)
|
||||
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
|
||||
-> Result<Rc<SyntaxExtension>, Determinacy>;
|
||||
fn resolve_derive_mode(&mut self, ident: ast::Ident) -> Option<Rc<MultiItemModifier>>;
|
||||
}
|
||||
@ -546,7 +546,7 @@ impl Resolver for DummyResolver {
|
||||
|
||||
fn find_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>) -> Option<Attribute> { None }
|
||||
fn resolve_derive_mode(&mut self, _ident: ast::Ident) -> Option<Rc<MultiItemModifier>> { None }
|
||||
fn resolve_invoc(&mut self, _scope: Mark, _invoc: &Invocation, _force: bool)
|
||||
fn resolve_macro(&mut self, _scope: Mark, _path: &ast::Path, _force: bool)
|
||||
-> Result<Rc<SyntaxExtension>, Determinacy> {
|
||||
Err(Determinacy::Determined)
|
||||
}
|
||||
|
@ -240,7 +240,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
||||
|
||||
let scope =
|
||||
if self.monotonic { invoc.expansion_data.mark } else { orig_expansion_data.mark };
|
||||
let ext = match self.cx.resolver.resolve_invoc(scope, &invoc, force) {
|
||||
let resolution = match invoc.kind {
|
||||
InvocationKind::Bang { ref mac, .. } => {
|
||||
self.cx.resolver.resolve_macro(scope, &mac.node.path, force)
|
||||
}
|
||||
InvocationKind::Attr { ref attr, .. } => {
|
||||
let ident = ast::Ident::with_empty_ctxt(intern(&*attr.name()));
|
||||
let path = ast::Path::from_ident(attr.span, ident);
|
||||
self.cx.resolver.resolve_macro(scope, &path, force)
|
||||
}
|
||||
};
|
||||
let ext = match resolution {
|
||||
Ok(ext) => Some(ext),
|
||||
Err(Determinacy::Determined) => None,
|
||||
Err(Determinacy::Undetermined) => {
|
||||
|
Loading…
Reference in New Issue
Block a user