89682cc48748cf8a6053bf31acd47ff75043dd4b
[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 = 0
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 end
38
39 var no_print = false
40 var only_lexer = false
41 var need_help = false
42 var no_file = false
43 var interactive = false
44
45 while not args.is_empty and args.first.chars.first == '-' do
46 if args.first == "-n" then
47 no_print = true
48 else if args.first == "-l" then
49 only_lexer = true
50 else if args.first == "-p" then
51 only_lexer = false
52 else if args.first == "-e" then
53 no_file = true
54 else if args.first == "-i" then
55 interactive = true
56 else if args.first == "-h" or args.first == "-?" then
57 need_help = true
58 else
59 stderr.write("Unknown option {args.first}.\n")
60 exit(0)
61 end
62 args.shift
63 end
64
65 if (args.is_empty and not interactive) or need_help then
66 print("usage:")
67 print(" test_parser [options]... <filename.nit>...")
68 print(" test_parser -e [options]... <text>...")
69 print(" test_parser -i [options]...")
70 print("options:")
71 print(" -n do not print anything")
72 print(" -l only lexer")
73 print(" -p lexer and parser (default)")
74 print(" -e instead on files, each argument is a content to parse")
75 print(" -i tree to parse are read interactively")
76 print(" -h print this help")
77 else if interactive then
78 if only_lexer then
79 print "Error: -l and -i are incompatibles"
80 exit 1
81 else if no_file then
82 print "Error: -e and -i are incompatibles"
83 exit 1
84 else if not args.is_empty then
85 print "Error: -i works without arguments"
86 exit 1
87 end
88
89 var tc = new ToolContext
90
91 loop
92 var n = tc.interactive_parse("-->")
93 if n isa TString then
94 var s = n.text
95 if s == ":q" then
96 break
97 else
98 print "`:q` to quit"
99 end
100 continue
101 end
102
103 if n isa AError then
104 print "{n.location.colored_line("0;31")}: {n.message}"
105 continue
106 end
107
108 if not no_print then
109 (new PrintTreeVisitor).enter_visit(n)
110 end
111 end
112 else
113 for a in args do
114 var source
115 if no_file then
116 source = new SourceFile.from_string("", a)
117 else
118 var f = new FileReader.open(a)
119 source = new SourceFile(a, f)
120 f.close
121 end
122 var lexer = new Lexer(source)
123 if only_lexer then
124 var token = lexer.next
125 while not token isa EOF do
126 if not no_print then
127 print("Read token at {token.location} text='{token.text}'")
128 end
129 token = lexer.next
130 end
131 else
132 var parser = new Parser(lexer)
133 var tree = parser.parse
134
135 var error = tree.n_eof
136 if error isa AError then
137 print("Error at {error.location}:\n\t{error.message}")
138 return
139 end
140
141 if not no_print then
142 (new PrintTreeVisitor).enter_visit(tree)
143 end
144 end
145 end
146 end