Rename REAMDE to README.md
[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 import astutil
23
24 # A basic visitor that prints AST trees to the screen
25 class PrintTreeVisitor
26 super Visitor
27 private var rank: Int = 0
28 redef fun visit(n)
29 do
30 if n isa Token then
31 printn(" " * rank, n.class_name, " \"", n.text.escape_to_c, "\" ", n.location, "\n")
32 else
33 printn(" " * rank, n.class_name, " ", n.location, "\n")
34 end
35 rank = rank + 1
36 n.visit_all(self)
37 rank = rank - 1
38 end
39 end
40
41 var no_print = false
42 var only_lexer = false
43 var need_help = false
44 var no_file = false
45 var interactive = false
46 var xml = false
47
48 while not args.is_empty and args.first.chars.first == '-' do
49 if args.first == "-n" then
50 no_print = true
51 else if args.first == "-l" then
52 only_lexer = true
53 else if args.first == "-p" then
54 only_lexer = false
55 else if args.first == "-x" then
56 xml = true
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(" -x instead of a ascii tree, output a XML document")
80 print(" -e instead on files, each argument is a content to parse")
81 print(" -i tree to parse are read interactively")
82 print(" -h print this help")
83 else if interactive then
84 if only_lexer then
85 print "Error: -l and -i are incompatible"
86 exit 1
87 else if no_file then
88 print "Error: -e and -i are incompatible"
89 exit 1
90 else if not args.is_empty then
91 print "Error: -i works without arguments"
92 exit 1
93 end
94
95 var tc = new ToolContext
96
97 loop
98 var n = tc.interactive_parse("-->")
99 if n isa TString then
100 var s = n.text
101 if s == ":q" then
102 break
103 else
104 print "`:q` to quit"
105 end
106 continue
107 end
108
109 if n isa AError then
110 print "{n.location.colored_line("0;31")}: {n.message}"
111 continue
112 end
113
114 if not no_print then
115 (new PrintTreeVisitor).enter_visit(n)
116 end
117 end
118 else
119 for a in args do
120 var source
121 if no_file then
122 source = new SourceFile.from_string("", a)
123 else
124 var f = new FileReader.open(a)
125 source = new SourceFile(a, f)
126 f.close
127 end
128 var lexer = new Lexer(source)
129 if only_lexer then
130 var token = lexer.next
131 while not token isa EOF do
132 if not no_print then
133 print("Read token at {token.location} text='{token.text}'")
134 end
135 token = lexer.next
136 end
137 else
138 var parser = new Parser(lexer)
139 var tree = parser.parse
140
141 var error = tree.n_eof
142 if error isa AError then
143 print("Error at {error.location}:\n\t{error.message}")
144 return
145 end
146
147 if xml then
148 tree.parentize_tokens
149 tree.to_xml.write_to(stdout)
150 else if not no_print then
151 (new PrintTreeVisitor).enter_visit(tree)
152 end
153 end
154 end
155 end