#!/usr/bin/env python

# Copyright 2005, W. Borgert <debacle@debian.org>
# GPL v2
# 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 = "20060801"

def process_opts(argv):
    # options not implemented - only for compatibility w/ older versions
    optparser = optparse.OptionParser()
    optparser.add_option("-f", "--no-flattening", dest="no_flattening",
                         action="store_true",
			 help="disable flattened output of AST")
    optparser.add_option("-p", "--pretty-print", dest="pretty_print",
			 action="store_true",
                         help="format the TTCN-3 document")
    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 "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()
    for f in files:
        start = 0
        try:
            start = time.time()
            tokens = bnf.parseFile(f)
        except pyparsing.ParseException, err:
            print "File:", f
            print err.line
            print " "*(err.column-1) + "^"
            print err
        end = time.time()
        if options.print_tree:
            print tokens.asXML()
        print "Parsed file %s in %.2f seconds" % (f, end - start)
