about summary refs log tree commit diff
path: root/src/libsyntax/ast_map
diff options
context:
space:
mode:
authorFelix S. Klock II <pnkfelix@pnkfx.org>2014-08-07 14:21:15 +0200
committerFelix S. Klock II <pnkfelix@pnkfx.org>2014-08-09 10:17:40 +0200
commit8a80e0fdab93cb6fb9eeb592857b8eeb35d14f15 (patch)
treeec7fdccf64056b21317c826a27ec92ea3b7fa352 /src/libsyntax/ast_map
parenta9b1a3b40f29795d5f049e342ec4a494e83125e9 (diff)
downloadrust-8a80e0fdab93cb6fb9eeb592857b8eeb35d14f15.tar.gz
rust-8a80e0fdab93cb6fb9eeb592857b8eeb35d14f15.zip
Helper method for `pprust::State` for printing instances of `ast_map::Node`.
Diffstat (limited to 'src/libsyntax/ast_map')
-rw-r--r--src/libsyntax/ast_map/mod.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/libsyntax/ast_map/mod.rs b/src/libsyntax/ast_map/mod.rs
index 94ffad40c6f..881ee4fd8d1 100644
--- a/src/libsyntax/ast_map/mod.rs
+++ b/src/libsyntax/ast_map/mod.rs
@@ -21,6 +21,7 @@ use util::small_vector::SmallVector;
 use std::cell::RefCell;
 use std::fmt;
 use std::gc::{Gc, GC};
+use std::io::IoResult;
 use std::iter;
 use std::slice;
 
@@ -819,6 +820,34 @@ pub fn map_decoded_item<F: FoldOps>(map: &Map,
     ii
 }
 
+pub trait NodePrinter {
+    fn print_node(&mut self, node: &Node) -> IoResult<()>;
+}
+
+impl<'a> NodePrinter for pprust::State<'a> {
+    fn print_node(&mut self, node: &Node) -> IoResult<()> {
+        match *node {
+            NodeItem(a)        => self.print_item(&*a),
+            NodeForeignItem(a) => self.print_foreign_item(&*a),
+            NodeTraitMethod(a) => self.print_trait_method(&*a),
+            NodeMethod(a)      => self.print_method(&*a),
+            NodeVariant(a)     => self.print_variant(&*a),
+            NodeExpr(a)        => self.print_expr(&*a),
+            NodeStmt(a)        => self.print_stmt(&*a),
+            NodePat(a)         => self.print_pat(&*a),
+            NodeBlock(a)       => self.print_block(&*a),
+            NodeLifetime(a)    => self.print_lifetime(&*a),
+
+            // these cases do not carry enough information in the
+            // ast_map to reconstruct their full structure for pretty
+            // printing.
+            NodeLocal(_)       => fail!("cannot print isolated Local"),
+            NodeArg(_)         => fail!("cannot print isolated Arg"),
+            NodeStructCtor(_)  => fail!("cannot print isolated StructCtor"),
+        }
+    }
+}
+
 fn node_id_to_string(map: &Map, id: NodeId) -> String {
     match map.find(id) {
         Some(NodeItem(item)) => {