Merge: Nit for mac
[nit.git] / src / parser_util.nit
index 5287b34..f23ccda 100644 (file)
@@ -69,7 +69,7 @@ redef class ToolContext
        do
                var mod_string = "do\n{string}\nend"
                var nmodule = parse_module(mod_string)
-               var nblock = nmodule.n_classdefs.first.n_propdefs.first.as(AMainMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(ADoExpr).n_block.as(not null)
+               var nblock = nmodule.n_classdefs.first.n_propdefs.first.as(AMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(ADoExpr).n_block.as(not null)
                return nblock
        end
 
@@ -79,7 +79,7 @@ redef class ToolContext
        do
                var mod_string = "var dummy = \n{string}"
                var nmodule = parse_module(mod_string)
-               var nexpr = nmodule.n_classdefs.first.n_propdefs.first.as(AMainMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_expr.as(not null)
+               var nexpr = nmodule.n_classdefs.first.n_propdefs.first.as(AMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_expr.as(not null)
                return nexpr
        end
 
@@ -120,18 +120,19 @@ redef class ToolContext
                tree = (new Parser(lexer)).parse
                eof = tree.n_eof
                if not eof isa AError then
-                       var ntype = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMainMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_type.n_types.first
+                       var ntype = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_type.n_types.first
+                       ntype.parent = null
                        return ntype
                end
                error = eof
 
                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
-                               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)
@@ -143,7 +144,8 @@ redef class ToolContext
                tree = (new Parser(lexer)).parse
                eof = tree.n_eof
                if not eof isa AError then
-                       var nexpr = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMainMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_expr.as(AParExpr).n_expr
+                       var nexpr = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_expr.as(AParExpr).n_expr
+                       nexpr.parent = null
                        return nexpr
                end
                if eof.location > error.location then error = eof
@@ -156,7 +158,9 @@ redef class ToolContext
                tree = (new Parser(lexer)).parse
                eof = tree.n_eof
                if not eof isa AError then
-                       var nblock = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMainMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(ADoExpr).n_block.as(not null)
+                       var nblock = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(ADoExpr).n_block.as(ABlockExpr)
+                       nblock.n_kwend = null # drop injected token
+                       nblock.parent = null
                        return nblock
                end
                if eof.location > error.location then error = eof
@@ -171,6 +175,37 @@ redef class ToolContext
 
                return error
        end
+
+       # Parse the input of the user as something
+       fun interactive_parse(prompt: String): ANode
+       do
+               var oldtext = ""
+
+               loop
+                       printn prompt
+                       printn " "
+                       var s = sys.stdin.read_line
+                       if s == "" then continue
+                       if s.chars.first == ':' then
+                               var res = new TString
+                               res.text = s
+                               return res
+                       end
+
+                       var text = oldtext + s + "\n"
+                       oldtext = ""
+                       var n = parse_something(text)
+
+                       if n isa AParserError and n.token isa EOF then
+                               # Unexpected end of file, thus continuing
+                               if oldtext == "" then prompt = "." * prompt.length
+                               oldtext = text
+                               continue
+                       end
+
+                       return n
+               end
+       end
 end
 
 class InjectedLexer
@@ -184,7 +219,6 @@ class InjectedLexer
        do
                if not injected_before.is_empty then
                        var tok = injected_before.shift
-                       if tok._location == null then tok._location = new Location(file, 1, 1, 1, 0)
                        return tok
                end
                if not is_finished then
@@ -195,7 +229,6 @@ class InjectedLexer
                end
 
                var tok = injected_after.shift
-               if tok._location == null then tok._location = new Location(file, 1, 1, 1, 0)
                return tok
        end
 end