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