1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#!/usr/bin/env python2.5
# Copyright 2005-2008, W. Martin Borgert <debacle@debian.org>
# GPL v3 or later
# Complete BNF: http://people.debian.org/~debacle/ttcn-bnf.html
try:
import psyco
psyco.full()
except:
pass
import optparse
import sys
import time
import pyparsing
import ttcn3parser
version = "20080407"
def process_opts(argv):
# options not implemented - only for compatibility w/ older versions
optparser = optparse.OptionParser()
optparser.add_option("-d", "--debug", dest="debug",
action="store_true",
help="debug the parser, developers only")
optparser.add_option("-f", "--no-flattening", dest="no_flattening",
action="store_true",
help="disable flattened output of AST")
optparser.add_option("-k", "--keywords", dest="keywords",
action="store_true",
help="print list of TTCN-3 keywords")
optparser.add_option("-p", "--progress", dest="progress",
action="store_true",
help="show progress while parsing")
optparser.add_option("-T", "--print-symbol-tables",
dest="print_symbol_tables", action="store_true",
help="print out the symbols in the test suite")
optparser.add_option("-t", "--print-tree",
dest="print_tree", action="store_true",
help="print abstract syntax tree")
optparser.add_option("-s", "--read-stdin",
dest="read_stdin", action="store_true",
help="read from standard input")
optparser.add_option("-v", "--version",
dest="version", action="store_true",
help="show version of program")
return optparser.parse_args()
def usage(name):
print >>sys.stderr, "Usage: " + name + " [options] files(s)\n"
if __name__ == "__main__":
options, files = process_opts(sys.argv)
if options.version:
print version
sys.exit(0)
bnf = ttcn3parser.BNF(options.progress, options.debug)
if options.keywords:
ttcn3parser.keywords()
if options.debug:
bnf.validate()
print "grammar OK"
for f in files:
start = 0
try:
start = time.time()
tokens = bnf.parseFile(f)
except pyparsing.ParseException, err:
print >>sys.stderr, "File:", f
print >>sys.stderr, err.line
print >>sys.stderr, " "*(err.column-1) + "^"
print >>sys.stderr, err
except KeyboardInterrupt:
print >>sys.stderr, "Parsing interrupted by user"
end = time.time()
if options.print_tree:
print '<?xml version="1.0"?>'
print tokens.asXML()
print >>sys.stderr, "\rParsed file %s in %.2f seconds" % \
(f, end - start)
if options.debug:
kwds = sorted(ttcn3parser.bnf.kwTally.items(),
key = lambda kn: kn[1], reverse = True)
if len(kwds):
print >>sys.stderr, "production" + 35 * " " + "test" \
+ 5 * " " + "fail" + 5 * " " + "succ"
for kw, cnt in kwds:
print >>sys.stderr, "%-40s , %6d , %6d , %6d" % \
(kw, cnt[0], cnt[1], cnt[2])
|