def generate(inputfilename, outputfilename='', dump=0, **flags):
    """Generate a grammar, given an input filename (X.g)
    and an output filename (defaulting to X.py)."""

    if not outputfilename:
        if inputfilename.endswith('.g'):
            outputfilename = inputfilename[:-2] + '.py'
        else:
            raise Exception('Must specify output filename if input filename is not *.g')
        
    DIVIDER = '\n%%\n' # This pattern separates the pre/post parsers
    preparser, postparser = None, None # Code before and after the parser desc

    # Read the entire file
    s = open(inputfilename,'r').read()

    # See if there's a separation between the pre-parser and parser
    f = s.find(DIVIDER)
    if f >= 0: preparser, s = s[:f]+'\n\n', s[f+len(DIVIDER):]

    # See if there's a separation between the parser and post-parser
    f = s.find(DIVIDER)
    if f >= 0: s, postparser = s[:f], '\n\n'+s[f+len(DIVIDER):]

    # Create the parser and scanner and parse the text
    scanner = grammar.ParserDescriptionScanner(s)
    if preparser: scanner.first_line_number = 1 + preparser.count('\n')
    parser = grammar.ParserDescription(scanner)
    t = yappsrt.wrap_error_reporter(parser, 'Parser')
    if t is None: return # Failure
    if preparser is not None: t.preparser = preparser
    if postparser is not None: t.postparser = postparser

    # Check the options
    for f in t.options.keys():
        for opt,_,_ in yapps_options:
            if f == opt: break
        else:
            print >>sys.stderr, 'Warning: unrecognized option', f
    # Add command line options to the set
    for f in flags.keys(): t.options[f] = flags[f]
            
    # Generate the output
    if dump:
        t.dump_information()
    else:
        t.output = open(outputfilename, 'w')
        # jca: added so that runtime found correctly
        t.output.write("#!/usr/bin/python\n")
        t.output.write("import os, sys\n")
        t.output.write("sys.path.append(os.path.abspath('yapps'))\n")
        t.generate_output()
def parse(rule, text):
    P = ParserDescription(ParserDescriptionScanner(text))
    return yappsrt.wrap_error_reporter(P, rule)
예제 #3
0
def generate(title, inputfilename, outputfilename=''):
    """Generate an HTML version of the grammar,
    given a title, an input filename (X.g)
    and an output filename (defaulting to X.py)."""

    import sys, codecs
    from toXML import XMLWriter
    dummy, dummy, dummy, encWriter = codecs.lookup('utf-8')

    if not outputfilename:
        if inputfilename[-2:] == '.g':
            outputfilename = inputfilename[:-2] + '.html'
        else:
            raise "Invalid Filename", outputfilename

    DIVIDER = '\n%%\n'  # This pattern separates the pre/post parsers
    preparser, postparser = None, None  # Code before and after the parser desc

    # Read the entire file
    s = open(inputfilename, 'r').read()

    # See if there's a separation between the pre-parser and parser
    f = find(s, DIVIDER)
    if f >= 0: preparser, s = s[:f] + '\n\n', s[f + len(DIVIDER):]

    # See if there's a separation between the parser and post-parser
    f = find(s, DIVIDER)
    if f >= 0: s, postparser = s[:f], '\n\n' + s[f + len(DIVIDER):]

    # Create the parser and scanner
    p = yapps2.ParserDescription(yapps2.ParserDescriptionScanner(s))
    if not p: return

    # Now parse the file
    t = yappsrt.wrap_error_reporter(p, 'Parser')
    if not t: return  # Error

    # Generate the output
    xwr = XMLWriter(encWriter(sys.stdout))
    xwr.startElement('html')  #@@ xmlns
    xwr.startElement('head')
    xwr.startElement('title')
    xwr.data(title)
    xwr.endElement()
    xwr.endElement()
    xwr.startElement('body')
    xwr.startElement('h1')
    xwr.data(title)
    xwr.endElement()  # h1
    xwr.startElement('address')
    xwr.data('source: ')
    xwr.startElement('a', [('href', inputfilename)])
    xwr.data(inputfilename)
    xwr.endElement()  #a
    xwr.data(', a ')
    xwr.startElement('a',
                     [('href', 'http://theory.stanford.edu/~amitp/Yapps/')])
    xwr.data('YAPPS')
    xwr.endElement()  #a
    xwr.data(' grammar')
    xwr.endElement()  #address

    toHTML(t, xwr)
    xwr.endElement()  # body
    xwr.endElement()  # html
예제 #4
0
파일: gram2html.py 프로젝트: AwelEshetu/cwm
def generate(title, inputfilename, outputfilename=''):
    """Generate an HTML version of the grammar,
    given a title, an input filename (X.g)
    and an output filename (defaulting to X.py)."""

    import sys, codecs
    from toXML import XMLWriter
    dummy, dummy, dummy, encWriter = codecs.lookup('utf-8')

    if not outputfilename:
        if inputfilename[-2:]=='.g': outputfilename = inputfilename[:-2]+'.html'
        else: raise "Invalid Filename", outputfilename
        
    DIVIDER = '\n%%\n' # This pattern separates the pre/post parsers
    preparser, postparser = None, None # Code before and after the parser desc

    # Read the entire file
    s = open(inputfilename,'r').read()

    # See if there's a separation between the pre-parser and parser
    f = find(s, DIVIDER)
    if f >= 0: preparser, s = s[:f]+'\n\n', s[f+len(DIVIDER):]

    # See if there's a separation between the parser and post-parser
    f = find(s, DIVIDER)
    if f >= 0: s, postparser = s[:f], '\n\n'+s[f+len(DIVIDER):]

    # Create the parser and scanner
    p = yapps2.ParserDescription(yapps2.ParserDescriptionScanner(s))
    if not p: return
    
    # Now parse the file
    t = yappsrt.wrap_error_reporter(p, 'Parser')
    if not t: return # Error

    # Generate the output
    xwr = XMLWriter(encWriter(sys.stdout))
    xwr.startElement('html') #@@ xmlns
    xwr.startElement('head')
    xwr.startElement('title')
    xwr.data(title)
    xwr.endElement()
    xwr.endElement()
    xwr.startElement('body')
    xwr.startElement('h1')
    xwr.data(title)
    xwr.endElement() # h1
    xwr.startElement('address')
    xwr.data('source: ')
    xwr.startElement('a', [('href', inputfilename)])
    xwr.data(inputfilename)
    xwr.endElement() #a
    xwr.data(', a ')
    xwr.startElement('a', [('href', 'http://theory.stanford.edu/~amitp/Yapps/')])
    xwr.data('YAPPS')
    xwr.endElement() #a
    xwr.data(' grammar')
    xwr.endElement() #address
    
    toHTML(t, xwr)
    xwr.endElement() # body
    xwr.endElement() # html
예제 #5
0
def parse(rule, text):
    P = plsql(plsqlScanner(text))
    return yappsrt.wrap_error_reporter(P, rule)