# limitations under the License.
# Program used to test the NIT parser
-package test_parser
+module test_parser
import parser
class PrintTreeVisitor
super Visitor
var _rank: Int
- redef fun visit(n: nullable ANode)
+ redef fun visit(n)
do
- if n == null then return
if n isa Token then
- printn(" " * _rank, n.to_s, " ... ", n.location, "\n")
+ printn(" " * _rank, n.class_name, " \"", n.text.escape_to_c, "\" ", n.location, "\n")
else
- printn(" " * _rank, n.location, "\n")
+ printn(" " * _rank, n.class_name, " ", n.location, "\n")
end
_rank = _rank + 1
n.visit_all(self)
var no_print = false
var only_lexer = false
var need_help = false
+var no_file = false
-while not args.is_empty and args.first.first == '-' do
+while not args.is_empty and args.first.chars.first == '-' do
if args.first == "-n" then
no_print = true
else if args.first == "-l" then
only_lexer = true
else if args.first == "-p" then
- only_lexer = false
+ only_lexer = false
+ else if args.first == "-e" then
+ no_file = true
else if args.first == "-h" or args.first == "-?" then
need_help = true
else
print(" -n do not print anything")
print(" -l only lexer")
print(" -p lexer and parser (default)")
+ print(" -e instead on files, each argument is a content to parse")
print(" -h print this help")
else
for a in args do
- var f = new IFStream.open(a)
- var lexer = new Lexer(new SourceFile(a, f))
+ var source
+ if no_file then
+ source = new SourceFile.from_string("", a)
+ else
+ var f = new IFStream.open(a)
+ source = new SourceFile(a, f)
+ f.close
+ end
+ var lexer = new Lexer(source)
if only_lexer then
var token = lexer.next
while not token isa EOF do
end
token = lexer.next
end
- f.close
else
var parser = new Parser(lexer)
var tree = parser.parse
- f.close
+
+ var error = tree.n_eof
+ if error isa AError then
+ print("Error at {error.location}:\n\t{error.message}")
+ return
+ end
if not no_print then
(new PrintTreeVisitor).enter_visit(tree)