def __init__(self, lexer): self.lexer_object = lexer # Create logger for parser self.log = logger.getLogger('parser-%d' % logger.sysid(self)) # Create ply parser log_wrapper = logger.PlyLoggerWrapping(self.log) self.parser_object = yacc.yacc(module=self, debuglog=log_wrapper, errorlog=log_wrapper, debug=1, outputdir='/tmp', write_tables=False) self.log.info('Created parser; class ID=%d' % logger.sysid(self.__class__))
def __init__(self, source_name, error_context): self.source_name = source_name self.error_context = error_context self.current_location = Location(self.source_name, 0, 0, 0) self.data = '' # Create logger for lexer self.log = logger.getLogger('lexer-%d' % logger.sysid(self)) # Create ply lexer object log_wrapper = logger.PlyLoggerWrapping(self.log) self.lexer_object = lex.lex(module=self, debuglog=log_wrapper, errorlog=log_wrapper, debug=1) self.log.info('Created lexer; class ID=lexer-class-%d' % logger.sysid(self.__class__))
def create(self, source_name): '''Create the new Lexer object.''' error_context = self._error_context or ErrorContext() cls = self.create_class() instance = cls(source_name, error_context) self.log.debug('Created lexer class; id=lexer-%d, context ID=%d' % (logger.sysid(instance), logger.sysid(error_context))) return instance
def __init__(self, lexer): self.lexer_object = lexer # Create logger for parser self.log = logger.getLogger('parser-%d' % logger.sysid(self)) # The temporary parsetab.py file should be generated in /tmp. parse_tab = tempfile.NamedTemporaryFile(prefix='parsetab-') # This is a hack, since ply appends a .py to the supplied tabmodule name. # So not only do we create a file for nothing but if not cleaned up manually # a tempfile will be leaked. Thanks, David Beazley. ;-) cleanup_file_list.append(parse_tab.name + '.py') # Create ply parser log_wrapper = logger.PlyLoggerWrapping(self.log) self.parser_object = yacc.yacc(module=self, debuglog=log_wrapper, errorlog=log_wrapper, debug=1, tabmodule=parse_tab.name) self.log.info('Created parser; class ID=%d' % logger.sysid(self.__class__))
def __init__(self): self._tokens = {} self._literals = set() self._ignored = set() self._error_tokens = {} self._warning_tokens = {} self.log = logger.getLogger('lexer-factory-%d' % logger.sysid(self)) self.log.info('Initialized lexer factory')
def create_class(self): '''Dynamically create a new lexer class.''' cls_dict = {} literal = set(self._literals) for key, value in self._tokens.items(): self.log.debug("Binding token %s(%r); remaining literals: %s" % (key, value, ', '.join(literal))) if key in literal: # The fate of how literals are handled depends whether # any other rule matches the given literal continue if key in self._ignored: name = 't_ignored_%s' % key action = lex.TOKEN(value)(lambda self, token: self.ignore_token(token)) else: name = 't_%s' % key action = None literal, action = self.__make_lex_member(value, literal, action=action) cls_dict[name] = action for key in literal: self.log.debug("Binding remaining literal token %s" % key) cls_dict['t_%s' % key] = re.escape(self._tokens[key]) if self._error_message is not None: cls_dict['error_message'] = self._error_message cls_dict['token_list'] = filter(lambda key: key not in self._ignored, self._tokens.keys()) cls_dict['error_tokens'] = self._error_tokens cls_dict['warning_tokens'] = self._warning_tokens lexer_class = type('AutoLexer', (Lexer,), cls_dict) self.log.debug('Created lexer class; class ID=lexer-class-%d' % logger.sysid(lexer_class)) return lexer_class
def set_error_context(self, context, message=None): '''Set error context with an error message template.''' self._error_context = context self._error_message = message self.log.debug("Set error context and message; context ID=%d, message=%r" % (logger.sysid(context), message))