test_parser: add option `-i` for interactive parsing
[nit.git] / src / test_parser.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Program used to test the NIT parser
18 module test_parser
19
20 import parser
21 import parser_util
22
23 class PrintTreeVisitor
24 super Visitor
25 private var rank: Int
26 redef fun visit(n)
27 do
28 if n isa Token then
29 printn(" " * rank, n.class_name, " \"", n.text.escape_to_c, "\" ", n.location, "\n")
30 else
31 printn(" " * rank, n.class_name, " ", n.location, "\n")
32 end
33 rank = rank + 1
34 n.visit_all(self)
35 rank = rank - 1
36 end
37
38 init
39 do
40 rank = 0
41 end
42 end
43
44 var no_print = false
45 var only_lexer = false
46 var need_help = false
47 var no_file = false
48 var interactive = false
49
50 while not args.is_empty and args.first.chars.first == '-' do
51 if args.first == "-n" then
52 no_print = true
53 else if args.first == "-l" then
54 only_lexer = true
55 else if args.first == "-p" then
56 only_lexer = false
57 else if args.first == "-e" then
58 no_file = true
59 else if args.first == "-i" then
60 interactive = true
61 else if args.first == "-h" or args.first == "-?" then
62 need_help = true
63 else
64 stderr.write("Unknown option {args.first}.\n")
65 exit(0)
66 end
67 args.shift
68 end
69
70 if (args.is_empty and not interactive) or need_help then
71 print("usage:")
72 print(" test_parser [options]... <filename.nit>...")
73 print(" test_parser -e [options]... <text>...")
74 print(" test_parser -i [options]...")
75 print("options:")
76 print(" -n do not print anything")
77 print(" -l only lexer")
78 print(" -p lexer and parser (default)")
79 print(" -e instead on files, each argument is a content to parse")
80 print(" -i tree to parse are read interactively")
81 print(" -h print this help")
82 else if interactive then
83 if only_lexer then
84 print "Error: -l and -i are incompatibles"
85 exit 1
86 else if no_file then
87 print "Error: -e and -i are incompatibles"
88 exit 1
89 else if not args.is_empty then
90 print "Error: -i works without arguments"
91 exit 1
92 end
93
94 var tc = new ToolContext
95
96 loop
97 var n = tc.interactive_parse("-->")
98 if n isa TString then
99 var s = n.text
100 if s == ":q" then
101 break
102 else
103 print "`:q` to quit"
104 end
105 continue
106 end
107
108 if n isa AError then
109 print "{n.location.colored_line("0;31")}: {n.message}"
110 continue
111 end
112
113 if not no_print then
114 (new PrintTreeVisitor).enter_visit(n)
115 end
116 end
117 else
118 for a in args do
119 var source
120 if no_file then
121 source = new SourceFile.from_string("", a)
122 else
123 var f = new IFStream.open(a)
124 source = new SourceFile(a, f)
125 f.close
126 end
127 var lexer = new Lexer(source)
128 if only_lexer then
129 var token = lexer.next
130 while not token isa EOF do
131 if not no_print then
132 print("Read token at {token.location} text='{token.text}'")
133 end
134 token = lexer.next
135 end
136 else
137 var parser = new Parser(lexer)
138 var tree = parser.parse
139
140 var error = tree.n_eof
141 if error isa AError then
142 print("Error at {error.location}:\n\t{error.message}")
143 return
144 end
145
146 if not no_print then
147 (new PrintTreeVisitor).enter_visit(tree)
148 end
149 end
150 end
151 end