|
11 | 11 | use abi;
|
12 | 12 | use ast::*;
|
13 | 13 | use ast_util;
|
14 |
| -use codemap::Span; |
| 14 | +use codemap::{Span, Spanned}; |
15 | 15 | use fold::Folder;
|
16 | 16 | use fold;
|
17 | 17 | use parse::token;
|
@@ -203,6 +203,10 @@ pub struct Map {
|
203 | 203 | }
|
204 | 204 |
|
205 | 205 | impl Map {
|
| 206 | + fn entry_count(&self) -> uint { |
| 207 | + self.map.borrow().len() |
| 208 | + } |
| 209 | + |
206 | 210 | fn find_entry(&self, id: NodeId) -> Option<MapEntry> {
|
207 | 211 | let map = self.map.borrow();
|
208 | 212 | if map.len() > id as uint {
|
@@ -405,6 +409,20 @@ impl Map {
|
405 | 409 | f(attrs)
|
406 | 410 | }
|
407 | 411 |
|
| 412 | + /// Returns an iterator that yields the node id's with paths that |
| 413 | + /// match `parts`. (Requires `parts` is non-empty.) |
| 414 | + /// |
| 415 | + /// For example, if given `parts` equal to `["bar", "quux"]`, then |
| 416 | + /// the iterator will produce node id's for items with paths |
| 417 | + /// such as `foo::bar::quux`, `bar::quux`, `other::bar::quux`, and |
| 418 | + /// any other such items it can find in the map. |
| 419 | + pub fn nodes_matching_suffix<'a, S:Str>(&'a self, parts: &'a [S]) -> NodesMatchingSuffix<'a,S> { |
| 420 | + NodesMatchingSuffix { map: self, |
| 421 | + item_name: parts.last().unwrap(), |
| 422 | + where: parts.slice_to(parts.len() - 1), |
| 423 | + idx: 0 } |
| 424 | + } |
| 425 | + |
408 | 426 | pub fn opt_span(&self, id: NodeId) -> Option<Span> {
|
409 | 427 | let sp = match self.find(id) {
|
410 | 428 | Some(NodeItem(item)) => item.span,
|
@@ -438,6 +456,119 @@ impl Map {
|
438 | 456 | }
|
439 | 457 | }
|
440 | 458 |
|
| 459 | +pub struct NodesMatchingSuffix<'a, S> { |
| 460 | + map: &'a Map, |
| 461 | + item_name: &'a S, |
| 462 | + where: &'a [S], |
| 463 | + idx: NodeId, |
| 464 | +} |
| 465 | + |
| 466 | +impl<'a,S:Str> NodesMatchingSuffix<'a,S> { |
| 467 | + /// Returns true only if some suffix of the module path for parent |
| 468 | + /// matches `self.where`. |
| 469 | + /// |
| 470 | + /// In other words: let `[x_0,x_1,...,x_k]` be `self.where`; |
| 471 | + /// returns true if parent's path ends with the suffix |
| 472 | + /// `x_0::x_1::...::x_k`. |
| 473 | + fn suffix_matches(&self, parent: NodeId) -> bool { |
| 474 | + let mut cursor = parent; |
| 475 | + for part in self.where.iter().rev() { |
| 476 | + let (mod_id, mod_name) = match find_first_mod_parent(self.map, cursor) { |
| 477 | + None => return false, |
| 478 | + Some((node_id, name)) => (node_id, name), |
| 479 | + }; |
| 480 | + if part.as_slice() != mod_name.as_str() { |
| 481 | + return false; |
| 482 | + } |
| 483 | + cursor = self.map.get_parent(mod_id); |
| 484 | + } |
| 485 | + return true; |
| 486 | + |
| 487 | + // Finds the first mod in parent chain for `id`, along with |
| 488 | + // that mod's name. |
| 489 | + // |
| 490 | + // If `id` itself is a mod named `m` with parent `p`, then |
| 491 | + // returns `Some(id, m, p)`. If `id` has no mod in its parent |
| 492 | + // chain, then returns `None`. |
| 493 | + fn find_first_mod_parent<'a>(map: &'a Map, mut id: NodeId) -> Option<(NodeId, Name)> { |
| 494 | + loop { |
| 495 | + match map.find(id) { |
| 496 | + None => return None, |
| 497 | + Some(NodeItem(item)) if item_is_mod(&*item) => |
| 498 | + return Some((id, item.ident.name)), |
| 499 | + _ => {} |
| 500 | + } |
| 501 | + let parent = map.get_parent(id); |
| 502 | + if parent == id { return None } |
| 503 | + id = parent; |
| 504 | + } |
| 505 | + |
| 506 | + fn item_is_mod(item: &Item) -> bool { |
| 507 | + match item.node { |
| 508 | + ItemMod(_) => true, |
| 509 | + _ => false, |
| 510 | + } |
| 511 | + } |
| 512 | + } |
| 513 | + } |
| 514 | + |
| 515 | + // We are looking at some node `n` with a given name and parent |
| 516 | + // id; do their names match what I am seeking? |
| 517 | + fn matches_names(&self, parent_of_n: NodeId, name: Name) -> bool { |
| 518 | + name.as_str() == self.item_name.as_slice() && |
| 519 | + self.suffix_matches(parent_of_n) |
| 520 | + } |
| 521 | +} |
| 522 | + |
| 523 | +impl<'a,S:Str> Iterator<NodeId> for NodesMatchingSuffix<'a,S> { |
| 524 | + fn next(&mut self) -> Option<NodeId> { |
| 525 | + loop { |
| 526 | + let idx = self.idx; |
| 527 | + if idx as uint >= self.map.entry_count() { |
| 528 | + return None; |
| 529 | + } |
| 530 | + self.idx += 1; |
| 531 | + let (p, name) = match self.map.find_entry(idx) { |
| 532 | + Some(EntryItem(p, n)) => (p, n.name()), |
| 533 | + Some(EntryForeignItem(p, n)) => (p, n.name()), |
| 534 | + Some(EntryTraitMethod(p, n)) => (p, n.name()), |
| 535 | + Some(EntryMethod(p, n)) => (p, n.name()), |
| 536 | + Some(EntryVariant(p, n)) => (p, n.name()), |
| 537 | + _ => continue, |
| 538 | + }; |
| 539 | + if self.matches_names(p, name) { |
| 540 | + return Some(idx) |
| 541 | + } |
| 542 | + } |
| 543 | + } |
| 544 | +} |
| 545 | + |
| 546 | +trait Named { |
| 547 | + fn name(&self) -> Name; |
| 548 | +} |
| 549 | + |
| 550 | +impl<T:Named> Named for Spanned<T> { fn name(&self) -> Name { self.node.name() } } |
| 551 | + |
| 552 | +impl Named for Item { fn name(&self) -> Name { self.ident.name } } |
| 553 | +impl Named for ForeignItem { fn name(&self) -> Name { self.ident.name } } |
| 554 | +impl Named for Variant_ { fn name(&self) -> Name { self.name.name } } |
| 555 | +impl Named for TraitMethod { |
| 556 | + fn name(&self) -> Name { |
| 557 | + match *self { |
| 558 | + Required(ref tm) => tm.ident.name, |
| 559 | + Provided(m) => m.name(), |
| 560 | + } |
| 561 | + } |
| 562 | +} |
| 563 | +impl Named for Method { |
| 564 | + fn name(&self) -> Name { |
| 565 | + match self.node { |
| 566 | + MethDecl(i, _, _, _, _, _, _, _) => i.name, |
| 567 | + MethMac(_) => fail!("encountered unexpanded method macro."), |
| 568 | + } |
| 569 | + } |
| 570 | +} |
| 571 | + |
441 | 572 | pub trait FoldOps {
|
442 | 573 | fn new_id(&self, id: NodeId) -> NodeId {
|
443 | 574 | id
|
|
0 commit comments