1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Program used to test the NIT parser
22 class PrintTreeVisitor
28 printn
(" " * _rank
, n
.to_s
, " ... ", n
.location
, "\n")
30 printn
(" " * _rank
, n
.location
, "\n")
44 var only_lexer
= false
48 while not args
.is_empty
and args
.first
.first
== '-' do
49 if args
.first
== "-n" then
51 else if args
.first
== "-l" then
53 else if args
.first
== "-p" then
55 else if args
.first
== "-e" then
57 else if args
.first
== "-h" or args
.first
== "-?" then
60 stderr
.write
("Unknown option {args.first}.\n")
66 if args
.is_empty
or need_help
then
68 print
(" test_parser [options]... <filename.nit>...")
70 print
(" -n do not print anything")
71 print
(" -l only lexer")
72 print
(" -p lexer and parser (default)")
73 print
(" -e instead on files, each argument is a content to parse")
74 print
(" -h print this help")
79 source
= new SourceFile.from_string
("", a
)
81 var f
= new IFStream.open
(a
)
82 source
= new SourceFile(a
, f
)
85 var lexer
= new Lexer(source
)
87 var token
= lexer
.next
88 while not token
isa EOF do
90 print
("Read token at {token.location} text='{token.text}'")
95 var parser
= new Parser(lexer
)
96 var tree
= parser
.parse
98 var error
= tree
.n_eof
99 if error
isa AError then
100 print
("Error at {error.location}:\n\t{error.message}")
105 (new PrintTreeVisitor).enter_visit
(tree
)