Пример #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
Пример #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
Пример #3
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