parser_util: add `ANode#collect_annotations_by_name`
authorJean Privat <jean@pryen.org>
Thu, 25 Jul 2013 17:31:53 +0000 (13:31 -0400)
committerJean Privat <jean@pryen.org>
Thu, 25 Jul 2013 17:36:04 +0000 (13:36 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/parser_util.nit

index f9ea467..8ca59e8 100644 (file)
@@ -92,6 +92,15 @@ redef class ANode
                v.enter_visit(self)
                return v.result
        end
+
+       # Return an array of node that are annotated
+       # The attached node can be retrieved by two invocation of parent
+       fun collect_annotations_by_name(name: String): Array[AAnnotation]
+       do
+               var v = new CollectAnnotationsByNameVisitor(name)
+               v.enter_visit(self)
+               return v.result
+       end
 end
 
 private class CollectTokensByTextVisitor
@@ -106,3 +115,16 @@ private class CollectTokensByTextVisitor
                if node isa Token and node.text == text then result.add(node)
        end
 end
+
+private class CollectAnnotationsByNameVisitor
+       super Visitor
+       var name: String
+       init(name: String) do self.name = name
+       var result = new Array[AAnnotation]
+       redef fun visit(node)
+       do
+               if node == null then return
+               node.visit_all(self)
+               if node isa AAnnotation and node.n_atid.n_id.text == name then result.add(node)
+       end
+end