Example #1
0
def _has_parser_changed(picklefile):
    # FIXME: private function to determine whether ply will need to write to
    # the pickled grammar file. Highly implementation dependent.
    from ply.yacc import PlyLogger, get_caller_module_dict, ParserReflect, YaccError, LRTable
    errorlog = PlyLogger(sys.stderr)

    pdict = get_caller_module_dict(2)

    # Collect parser information from the dictionary
    pinfo = ParserReflect(pdict, log=errorlog)
    pinfo.get_all()

    if pinfo.error:
        raise YaccError("Unable to build parser")

    # Check signature against table files (if any)
    signature = pinfo.signature()

    # Read the tables
    try:
        lr = LRTable()
        read_signature = lr.read_pickle(picklefile)
        return read_signature != signature
    except Exception:
        return True
Example #2
0
def _has_parser_changed(picklefile):
    # FIXME: private function to determine whether ply will need to write to
    # the pickled grammar file. Highly implementation dependent.
    from ply.yacc import PlyLogger, get_caller_module_dict, ParserReflect, YaccError, LRTable
    errorlog = PlyLogger(sys.stderr)

    pdict = get_caller_module_dict(2)

    # Collect parser information from the dictionary
    pinfo = ParserReflect(pdict, log=errorlog)
    pinfo.get_all()

    if pinfo.error:
        raise YaccError("Unable to build parser")

    # Check signature against table files (if any)
    signature = pinfo.signature()

    # Read the tables
    try:
        lr = LRTable()
        read_signature = lr.read_pickle(picklefile)
        return read_signature != signature
    except Exception:
        return True
Example #3
0
def yaccfrom(module, tabmodule, lexer):
    # Get the module dictionary used for the parser
    _items = [(k,getattr(module,k)) for k in dir(module)]
    pdict = dict(_items)

    # Collect parser information from the dictionary
    pinfo = ParserReflect(pdict)
    pinfo.get_all()

    # Read the tables
    lr = LRTable()
    lr.read_table(tabmodule)

    lr.bind_callables(pinfo.pdict)
    return LRParser(lr,pinfo.error_func)
Example #4
0
def yaccfrom(module, tabmodule, lexer):
    # Get the module dictionary used for the parser
    _items = [(k, getattr(module, k)) for k in dir(module)]
    pdict = dict(_items)

    # Collect parser information from the dictionary
    pinfo = ParserReflect(pdict)
    pinfo.get_all()

    # Read the tables
    lr = LRTable()
    lr.read_table(tabmodule)

    lr.bind_callables(pinfo.pdict)
    return LRParser(lr, pinfo.error_func)
Example #5
0
for nt, rhs in prodtups:
    if nt not in prodmap:
        prodmap[nt] = set()
    prodmap[nt].add(rhs)

import pprint
pprint.pprint(prodmap)

# ______________________________________________________________________
# Another way to do this:

print "_" * 70

from ply.yacc import ParserReflect, parse_grammar

pinfo = ParserReflect(myg.__dict__)
pinfo.get_all()

prodparse = [ parse_grammar(doc_str, file_name, line_no)
              for line_no, file_name, func_name, doc_str in pinfo.pfuncs ]

pprint.pprint(prodparse)

prodmap2 = {}
for parse_result in prodparse:
    for file_name, line_no, nt, rhs in parse_result:
        if nt not in prodmap2:
            prodmap2[nt] = set()
        prodmap2[nt].add(tuple(rhs))

print "_" * 60
Example #6
0
def yacc(method='LALR',
         debug=yaccdebug,
         module=None,
         tabmodule=tab_module,
         start=None,
         check_recursion=True,
         optimize=False,
         write_tables=True,
         debugfile=debug_file,
         outputdir=None,
         debuglog=None,
         errorlog=None,
         picklefile=None):

    if tabmodule is None:
        tabmodule = tab_module

    # Reference to the parsing method of the last built parser
    global parse

    # If pickling is enabled, table files are not created
    if picklefile:
        write_tables = 0

    if errorlog is None:
        errorlog = PlyLogger(sys.stderr)

    # Get the module dictionary used for the parser
    if module:
        _items = [(k, getattr(module, k)) for k in dir(module)]
        pdict = dict(_items)
        if '__file__' not in pdict:
            pdict['__file__'] = sys.modules[pdict['__module__']].__file__
    else:
        pdict = get_caller_module_dict(2)

    if outputdir is None:
        if isinstance(tabmodule, types.ModuleType):
            srcfile = tabmodule.__file__
        else:
            if '.' not in tabmodule:
                srcfile = pdict['__file__']
            else:
                parts = tabmodule.split('.')
                pkgname = '.'.join(parts[:-1])
                exec('import %s' % pkgname)
                srcfile = getattr(sys.modules[pkgname], '__file__', '')
        outputdir = os.path.dirname(srcfile)

    pkg = pdict.get('__package__')
    if pkg and isinstance(tabmodule, str):
        if '.' not in tabmodule:
            tabmodule = pkg + '.' + tabmodule

    if start is not None:
        pdict['start'] = start

    pinfo = ParserReflect(pdict, log=errorlog)
    pinfo.get_all()

    if pinfo.error:
        raise YaccError('Unable to build parser')

    signature = pinfo.signature()

    # Read the tables
    try:
        lr = LRTable()
        if picklefile:
            read_signature = lr.read_pickle(picklefile)
        else:
            read_signature = lr.read_table(tabmodule)
        if optimize or (read_signature == signature):
            try:
                lr.bind_callables(pinfo.pdict)
                parser = TerraformLRParser(lr, pinfo.error_func)
                parse = parser.parse
                return parser
            except Exception as e:
                errorlog.warning(
                    'There was a problem loading the table file: %r', e)
    except VersionError as e:
        errorlog.warning(str(e))
    except ImportError:
        pass