main.py revision 6657:ef5fae93a3b2
113942Sodanrc@yahoo.com.br# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
213942Sodanrc@yahoo.com.br# Copyright (c) 2009 The Hewlett-Packard Development Company
313942Sodanrc@yahoo.com.br# All rights reserved.
413942Sodanrc@yahoo.com.br#
513942Sodanrc@yahoo.com.br# Redistribution and use in source and binary forms, with or without
613942Sodanrc@yahoo.com.br# modification, are permitted provided that the following conditions are
713942Sodanrc@yahoo.com.br# met: redistributions of source code must retain the above copyright
813942Sodanrc@yahoo.com.br# notice, this list of conditions and the following disclaimer;
913942Sodanrc@yahoo.com.br# redistributions in binary form must reproduce the above copyright
1013942Sodanrc@yahoo.com.br# notice, this list of conditions and the following disclaimer in the
1113942Sodanrc@yahoo.com.br# documentation and/or other materials provided with the distribution;
1213942Sodanrc@yahoo.com.br# neither the name of the copyright holders nor the names of its
1313942Sodanrc@yahoo.com.br# contributors may be used to endorse or promote products derived from
1413942Sodanrc@yahoo.com.br# this software without specific prior written permission.
1513942Sodanrc@yahoo.com.br#
1613942Sodanrc@yahoo.com.br# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1713942Sodanrc@yahoo.com.br# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1813942Sodanrc@yahoo.com.br# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1913942Sodanrc@yahoo.com.br# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2013942Sodanrc@yahoo.com.br# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2113942Sodanrc@yahoo.com.br# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2213942Sodanrc@yahoo.com.br# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2313942Sodanrc@yahoo.com.br# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2413942Sodanrc@yahoo.com.br# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2513942Sodanrc@yahoo.com.br# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2613942Sodanrc@yahoo.com.br# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2713942Sodanrc@yahoo.com.br
2813942Sodanrc@yahoo.com.brimport os
2913942Sodanrc@yahoo.com.brimport sys
3013942Sodanrc@yahoo.com.br
3113942Sodanrc@yahoo.com.brfrom slicc.parser import SLICC
3213942Sodanrc@yahoo.com.br
3313942Sodanrc@yahoo.com.brusage="%prog [options] <files> ... "
3413942Sodanrc@yahoo.com.brversion="%prog v0.4"
3513942Sodanrc@yahoo.com.brbrief_copyright='''
3613942Sodanrc@yahoo.com.brCopyright (c) 1999-2008 Mark D. Hill and David A. Wood
3713942Sodanrc@yahoo.com.brCopyright (c) 2009 The Hewlett-Packard Development Company
3813942Sodanrc@yahoo.com.brAll Rights Reserved.
3913943Sodanrc@yahoo.com.br'''
4013942Sodanrc@yahoo.com.br
4113942Sodanrc@yahoo.com.brdef nprint(format, *args):
4213942Sodanrc@yahoo.com.br    pass
4313942Sodanrc@yahoo.com.br
4413942Sodanrc@yahoo.com.brdef eprint(format, *args):
4513942Sodanrc@yahoo.com.br    if args:
4613942Sodanrc@yahoo.com.br        format = format % args
4713942Sodanrc@yahoo.com.br
4813942Sodanrc@yahoo.com.br    print >>sys.stderr, format
4913942Sodanrc@yahoo.com.br
5013942Sodanrc@yahoo.com.brdef main(args=None):
5113942Sodanrc@yahoo.com.br    import optparse
5213942Sodanrc@yahoo.com.br
5313942Sodanrc@yahoo.com.br    parser = optparse.OptionParser(usage=usage, version=version,
5413942Sodanrc@yahoo.com.br                                   description=brief_copyright)
5513942Sodanrc@yahoo.com.br    parser.add_option("-d", "--debug", default=False, action="store_true",
5613942Sodanrc@yahoo.com.br                      help="Turn on PLY debugging")
5713942Sodanrc@yahoo.com.br    parser.add_option("-C", "--code-path", default="generated",
5813942Sodanrc@yahoo.com.br                      help="Path where C++ code output code goes")
5913942Sodanrc@yahoo.com.br    parser.add_option("-H", "--html-path",
6013942Sodanrc@yahoo.com.br                      help="Path where html output goes")
6113942Sodanrc@yahoo.com.br    parser.add_option("-F", "--print-files",
6213942Sodanrc@yahoo.com.br                      help="Print files that SLICC will generate")
6313942Sodanrc@yahoo.com.br    parser.add_option("-q", "--quiet",
6413942Sodanrc@yahoo.com.br                      help="don't print messages")
6513942Sodanrc@yahoo.com.br    opts,files = parser.parse_args(args=args)
6613942Sodanrc@yahoo.com.br
6713942Sodanrc@yahoo.com.br    if len(files) < 1:
6813942Sodanrc@yahoo.com.br        parser.print_help()
6913942Sodanrc@yahoo.com.br        sys.exit(2)
7013942Sodanrc@yahoo.com.br
7113942Sodanrc@yahoo.com.br    output = nprint if opts.quiet else eprint
7213942Sodanrc@yahoo.com.br
7313942Sodanrc@yahoo.com.br    output("SLICC v0.4")
7413942Sodanrc@yahoo.com.br    slicc = SLICC(debug=opts.debug)
7513942Sodanrc@yahoo.com.br
7613942Sodanrc@yahoo.com.br    output("Parsing...")
7713942Sodanrc@yahoo.com.br    for filename in slicc.load(files, verbose=True):
7813942Sodanrc@yahoo.com.br        output("    %s", filename)
7913942Sodanrc@yahoo.com.br
8013942Sodanrc@yahoo.com.br    if opts.print_files:
8113942Sodanrc@yahoo.com.br        hh, cc = slicc.files()
8213942Sodanrc@yahoo.com.br        hh = sorted(hh)
8313942Sodanrc@yahoo.com.br        cc = sorted(cc)
8413942Sodanrc@yahoo.com.br        print 'Headers:'
8513942Sodanrc@yahoo.com.br        for i in hh:
8613942Sodanrc@yahoo.com.br            print '    %s' % i
8713942Sodanrc@yahoo.com.br
8813942Sodanrc@yahoo.com.br        print 'Sources:'
8913942Sodanrc@yahoo.com.br        for i in cc:
9013942Sodanrc@yahoo.com.br            print '    %s' % i
9113942Sodanrc@yahoo.com.br    else:
9213942Sodanrc@yahoo.com.br        output("Generator pass 1...")
9313942Sodanrc@yahoo.com.br        slicc.findMachines()
9413942Sodanrc@yahoo.com.br
9513942Sodanrc@yahoo.com.br        output("Generator pass 2...")
9613942Sodanrc@yahoo.com.br        slicc.generate()
9713942Sodanrc@yahoo.com.br
9813942Sodanrc@yahoo.com.br        output("Generating C++ files...")
9913942Sodanrc@yahoo.com.br        slicc.writeCodeFiles(opts.code_path)
10013942Sodanrc@yahoo.com.br
10113942Sodanrc@yahoo.com.br        if opts.html_path:
10213942Sodanrc@yahoo.com.br            nprint("Writing HTML files...")
10313942Sodanrc@yahoo.com.br            slicc.writeHTMLFiles(opts.html_path)
10413943Sodanrc@yahoo.com.br
10513943Sodanrc@yahoo.com.br    eprint("SLICC is Done.")
10613943Sodanrc@yahoo.com.br
10713942Sodanrc@yahoo.com.brif __name__ == "__main__":
10813942Sodanrc@yahoo.com.br    main()
10913942Sodanrc@yahoo.com.br