compiler: the type poset contains only directed casts
[nit.git] / src / parser_util.nit
index 9652a1f..82aec57 100644 (file)
@@ -83,6 +83,20 @@ redef class ToolContext
                return nexpr
        end
 
+       # Parse a super class declaration
+       # Fatal error if the `string` is not a syntactically correct super class declaration
+       fun parse_superclass(string: String): ASuperclass
+       do
+               var mod_string = "class Dummy\nsuper {string}\nend"
+               var nclassdef = parse_classdef(mod_string).as(AStdClassdef)
+               var nsuperclasses = nclassdef.n_superclasses
+               if nsuperclasses.length != 1 then
+                       self.fatal_error(null, "Fatal Error: not a super class declaration")
+                       abort
+               end
+               return nsuperclasses.first
+       end
+
        # Try to parse the `string` as something
        #
        # Returns the first possible syntacticaly correct type among:
@@ -128,12 +142,11 @@ redef class ToolContext
 
                lexer = new Lexer(source)
                var first = lexer.next
-               if not first isa EOF then
-                       var second = lexer.next
-                       if second isa EOF and not second isa AError then
-                               first.parent = null
-                               return first
-                       end
+               if first isa EOF then return first
+               var second = lexer.next
+               if second isa EOF and not second isa AError then
+                       first.parent = null
+                       return first
                end
 
                lexer = new InjectedLexer(source)
@@ -235,7 +248,7 @@ class InjectedLexer
 end
 
 redef class ANode
-       # Return an array of tokens that match a given text
+       # Do a deep search and return an array of tokens that match a given text
        fun collect_tokens_by_text(text: String): Array[Token]
        do
                var v = new CollectTokensByTextVisitor(text)
@@ -243,7 +256,7 @@ redef class ANode
                return v.result
        end
 
-       # Return an array of node that are annotated
+       # Do a deep search and 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
@@ -256,7 +269,6 @@ end
 private class CollectTokensByTextVisitor
        super Visitor
        var text: String
-       init(text: String) do self.text = text
        var result = new Array[Token]
        redef fun visit(node)
        do
@@ -268,7 +280,6 @@ 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