parser_util: add `interactive_parse` to have a simple AST from the user
authorJean Privat <jean@pryen.org>
Sun, 27 Apr 2014 01:33:45 +0000 (21:33 -0400)
committerJean Privat <jean@pryen.org>
Sun, 27 Apr 2014 01:33:45 +0000 (21:33 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/parser_util.nit

index 5287b34..32fe720 100644 (file)
@@ -171,6 +171,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 = 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