ni_nitdoc: added fast copy past utility to signatures.
[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 package test_parser
19
20 import parser
21
22 class PrintTreeVisitor
23 super Visitor
24 var _rank: Int
25 redef fun visit(n: nullable ANode)
26 do
27 if n == null then return
28 if n isa Token then
29 printn(" " * _rank, n.to_s, " ... ", n.location, "\n")
30 else
31 printn(" " * _rank, 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
49 while not args.is_empty and args.first.first == '-' do
50 if args.first == "-n" then
51 no_print = true
52 else if args.first == "-l" then
53 only_lexer = true
54 else if args.first == "-p" then
55 only_lexer = false
56 else if args.first == "-e" then
57 no_file = true
58 else if args.first == "-h" or args.first == "-?" then
59 need_help = true
60 else
61 stderr.write("Unknown option {args.first}.\n")
62 exit(0)
63 end
64 args.shift
65 end
66
67 if args.is_empty or need_help then
68 print("usage:")
69 print(" test_parser [options]... <filename.nit>...")
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(" -h print this help")
76 else
77 for a in args do
78 var source
79 if no_file then
80 source = new SourceFile.from_string("", a)
81 else
82 var f = new IFStream.open(a)
83 source = new SourceFile(a, f)
84 f.close
85 end
86 var lexer = new Lexer(source)
87 if only_lexer then
88 var token = lexer.next
89 while not token isa EOF do
90 if not no_print then
91 print("Read token at {token.location} text='{token.text}'")
92 end
93 token = lexer.next
94 end
95 else
96 var parser = new Parser(lexer)
97 var tree = parser.parse
98
99 var error = tree.n_eof
100 if error isa AError then
101 print("Error at {error.location}:\n\t{error.message}")
102 return
103 end
104
105 if not no_print then
106 (new PrintTreeVisitor).enter_visit(tree)
107 end
108 end
109 end
110 end