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
23 class PrintTreeVisitor
25 private var rank
: Int = 0
29 printn
(" " * rank
, n
.class_name
, " \"", n.text.escape_to_c, "\
" ", n
.location
, "\n")
31 printn
(" " * rank
, n
.class_name
, " ", n
.location
, "\n")
40 var only_lexer
= false
43 var interactive
= false
45 while not args
.is_empty
and args
.first
.chars
.first
== '-' do
46 if args
.first
== "-n" then
48 else if args
.first
== "-l" then
50 else if args
.first
== "-p" then
52 else if args
.first
== "-e" then
54 else if args
.first
== "-i" then
56 else if args
.first
== "-h" or args
.first
== "-?" then
59 stderr
.write
("Unknown option {args.first}.\n")
65 if (args
.is_empty
and not interactive
) or need_help
then
67 print
(" test_parser [options]... <filename.nit>...")
68 print
(" test_parser -e [options]... <text>...")
69 print
(" test_parser -i [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
79 print
"Error: -l and -i are incompatibles"
82 print
"Error: -e and -i are incompatibles"
84 else if not args
.is_empty
then
85 print
"Error: -i works without arguments"
89 var tc
= new ToolContext
92 var n
= tc
.interactive_parse
("-->")
104 print
"{n.location.colored_line("0;31")}: {n.message}"
109 (new PrintTreeVisitor).enter_visit
(n
)
116 source
= new SourceFile.from_string
("", a
)
118 var f
= new IFStream.open
(a
)
119 source
= new SourceFile(a
, f
)
122 var lexer
= new Lexer(source
)
124 var token
= lexer
.next
125 while not token
isa EOF do
127 print
("Read token at {token.location} text='{token.text}'")
132 var parser
= new Parser(lexer
)
133 var tree
= parser
.parse
135 var error
= tree
.n_eof
136 if error
isa AError then
137 print
("Error at {error.location}:\n\t{error.message}")
142 (new PrintTreeVisitor).enter_visit
(tree
)