X-Git-Url: http://nitlanguage.org diff --git a/src/test_parser.nit b/src/test_parser.nit index f19857d..6ad8038 100644 --- a/src/test_parser.nit +++ b/src/test_parser.nit @@ -15,29 +15,26 @@ # limitations under the License. # Program used to test the NIT parser -package test_parser +module test_parser import parser +import parser_util +import astutil +# A basic visitor that prints AST trees to the screen class PrintTreeVisitor super Visitor - var _rank: Int - redef fun visit(n: nullable ANode) + private var rank: Int = 0 + redef fun visit(n) do - if n == null then return if n isa Token then - printn(" " * _rank, n.to_s, " ... ", n.location, "\n") + printn(" " * rank, n.class_name, " \"", n.text.escape_to_c, "\" ", n.location, "\n") else - printn(" " * _rank, n.location, "\n") + printn(" " * rank, n.class_name, " ", n.location, "\n") end - _rank = _rank + 1 + rank = rank + 1 n.visit_all(self) - _rank = _rank - 1 - end - - init - do - _rank = 0 + rank = rank - 1 end end @@ -45,16 +42,22 @@ var no_print = false var only_lexer = false var need_help = false var no_file = false +var interactive = false +var xml = false -while not args.is_empty and args.first.first == '-' do +while not args.is_empty and args.first.chars.first == '-' do if args.first == "-n" then no_print = true else if args.first == "-l" then only_lexer = true else if args.first == "-p" then only_lexer = false + else if args.first == "-x" then + xml = true else if args.first == "-e" then no_file = true + else if args.first == "-i" then + interactive = true else if args.first == "-h" or args.first == "-?" then need_help = true else @@ -64,22 +67,61 @@ while not args.is_empty and args.first.first == '-' do args.shift end -if args.is_empty or need_help then +if (args.is_empty and not interactive) or need_help then print("usage:") print(" test_parser [options]... ...") + print(" test_parser -e [options]... ...") + print(" test_parser -i [options]...") print("options:") print(" -n do not print anything") print(" -l only lexer") print(" -p lexer and parser (default)") + print(" -x instead of a ascii tree, output a XML document") print(" -e instead on files, each argument is a content to parse") + print(" -i tree to parse are read interactively") print(" -h print this help") +else if interactive then + if only_lexer then + print "Error: -l and -i are incompatible" + exit 1 + else if no_file then + print "Error: -e and -i are incompatible" + exit 1 + else if not args.is_empty then + print "Error: -i works without arguments" + exit 1 + end + + var tc = new ToolContext + + loop + var n = tc.interactive_parse("-->") + if n isa TString then + var s = n.text + if s == ":q" then + break + else + print "`:q` to quit" + end + continue + end + + if n isa AError then + print "{n.location.colored_line("0;31")}: {n.message}" + continue + end + + if not no_print then + (new PrintTreeVisitor).enter_visit(n) + end + end else for a in args do var source if no_file then source = new SourceFile.from_string("", a) else - var f = new IFStream.open(a) + var f = new FileReader.open(a) source = new SourceFile(a, f) f.close end @@ -102,7 +144,10 @@ else return end - if not no_print then + if xml then + tree.parentize_tokens + tree.to_xml.write_to(stdout) + else if not no_print then (new PrintTreeVisitor).enter_visit(tree) end end