class VariableFormatter(object): def __init__(self, plugin_options, reserved_ids): def section_id_formatter(section_id): path = section_id.split('.') if section_id is not None else None path = [i.upper().replace(':', '') for i in path] if path is not None else None return '_'.join(path[1:]) if section_id is not None and len(path) > 0 else '' def token_id_formatter(id): return id.upper() if id is not None else None self.plugin_options = plugin_options self.cache = CachedFormatter(limit=64, reserved=reserved_ids) self.cache.add_cache('section_id', section_id_formatter, cache_name='section_and_tokens') self.cache.add_cache('token_id', token_id_formatter, cache_name='section_and_tokens') self.cache.add_token_id(None, 'ANONYMOUS') self.cache.add_token_id('endofstream', 'ENDOFSTREAM') self.cache.add_token_id('invalidcharacter', 'INVALIDCHARACTER') self.cache.add_section_id(None, '') self.cache.add_section_id('::main::', 'MAIN') self.reserved_ids = reserved_ids for attr in dir(self.cache): if any(attr.startswith(i) for i in ('get_', 'add_', 'clear_')): setattr(self, attr, getattr(self.cache, attr)) def get_class_name(self): return self.plugin_options.class_name def get_scoped(self, id, is_relative): if is_relative: return id else: return '{class_name}::{type}'.format(class_name=self.get_class_name(), type=id) def get_state_machine_method_name(self, section, is_relative): section_id = self.cache.get_section_id(section).lower() if section_id != '': section_id = '_' + section_id method_name = 'get_token{id}'.format(id=section_id) return self.get_scoped(method_name, is_relative) def get_type(self, type_name, is_relative): if type_name == 'mode_stack': return "std::stack<{mode_type}>".format(mode_type=self.get_type('mode', is_relative)) elif type_name == 'mode': type_text = 'Mode' elif type_name == 'token': type_text = 'Token' elif type_name == 'state': return 'State' else: raise Exception("unrecognized type '{id}'".format(id=type_name)) return self.get_scoped(type_text, is_relative) def get_state_id_formatter(self, rules): return StateIdFormatter(rules, self.reserved_ids)
class VariableFormatter(object): def __init__(self, plugin_options, reserved_ids, poodle_namespace): def section_id_formatter(section_id): path = section_id.split('.') if section_id is not None else None path = [i.replace(':', '') for i in path] if path is not None else None return ''.join(path[1:]) if section_id is not None and len(path) > 0 else '' def token_id_formatter(id): return id self.poodle_namespace = poodle_namespace self.plugin_options = plugin_options self.reserved_ids = reserved_ids self.cache = CachedFormatter(limit=64, reserved=reserved_ids) self.cache.add_cache('section_id', section_id_formatter, cache_name='section_and_tokens') self.cache.add_cache('token_id', token_id_formatter, cache_name='section_and_tokens') self.cache.add_section_id(None, '') self.cache.add_section_id('::main::', 'Main') for attr in dir(self.cache): if any(attr.startswith(i) for i in ('get_', 'add_', 'clear_')): setattr(self, attr, getattr(self.cache, attr)) def get_class_name(self): return self.plugin_options.class_name def get_default_encoding(self): return self.get_scoped('Unicode.DefaultStringEncoding', is_relative=True, is_custom_namespace=False) def get_mode_stack_class_name(self): return self.get_class_name() + "Mode" def get_namespace(self): return self.plugin_options.namespace def get_scoped(self, id, is_relative, is_custom_namespace): if is_relative: if not is_custom_namespace and self.plugin_options.namespace != self.poodle_namespace: return '{poodle}.{type}'.format(poodle=self.poodle_namespace, type=type_text) else: return id else: return '{namespace}.{type}'.format(namespace=self.plugin_options.namespace, type=id) def get_state_machine_method_name(self, section, is_relative): method_name = 'GetToken{id}'.format(id=self.cache.get_section_id(section)) if not is_relative: method_name = '{class_name}.{method_name}'.format( class_name=self.get_class_name(), method_name=method_name) return self.get_scoped(method_name, is_relative, is_custom_namespace=True) def get_type(self, type_name, is_relative): if type_name == 'mode': is_custom_namespace = True type_text = "{class_name}.ModeId".format(class_name=self.get_mode_stack_class_name()) elif type_name == 'token': is_custom_namespace = True type_text = "{class_name}Token".format(class_name=self.plugin_options.class_name) elif type_name == 'text': is_custom_namespace = False type_text = 'Unicode.Text' elif type_name == 'encoding': is_custom_namespace = False type_text = 'Unicode.StringEncoding' elif type_name == 'character': is_custom_namespace = False type_text = 'Unicode.Codepoint' elif type_name == 'stream': is_custom_namespace = False type_text = 'CharacterStream' else: raise Exception("unrecognized type '{id}'".format(id=type_name)) return self.get_scoped(type_text, is_relative, is_custom_namespace) def get_state_id_formatter(self, rules): return StateIdFormatter(rules, self.reserved_ids) def get_unicode_char_name(self, codepoint): try: unicode_name = unicodedata.name(unichr(codepoint)).replace(' ', '_').replace('-', '_') return "{namespace}_UCS_{name}".format( namespace = self.get_namespace().upper(), name = unicode_name.upper())[:64] except ValueError: return "&h%02x" % codepoint
class VariableFormatter(object): def __init__(self, plugin_options, reserved_ids): def section_id_formatter(section_id): path = section_id.split('.') if section_id is not None else None path = [i.upper().replace(':', '') for i in path] if path is not None else None return '_'.join( path[1:]) if section_id is not None and len(path) > 0 else '' def token_id_formatter(id): return id.upper() if id is not None else None self.plugin_options = plugin_options self.cache = CachedFormatter(limit=64, reserved=reserved_ids) self.cache.add_cache('section_id', section_id_formatter, cache_name='section_and_tokens') self.cache.add_cache('token_id', token_id_formatter, cache_name='section_and_tokens') self.cache.add_token_id(None, 'ANONYMOUS') self.cache.add_token_id('endofstream', 'ENDOFSTREAM') self.cache.add_token_id('invalidcharacter', 'INVALIDCHARACTER') self.cache.add_section_id(None, '') self.cache.add_section_id('::main::', 'MAIN') self.reserved_ids = reserved_ids for attr in dir(self.cache): if any(attr.startswith(i) for i in ('get_', 'add_', 'clear_')): setattr(self, attr, getattr(self.cache, attr)) def get_class_name(self): return self.plugin_options.class_name def get_scoped(self, id, is_relative): if is_relative: return id else: return '{class_name}::{type}'.format( class_name=self.get_class_name(), type=id) def get_state_machine_method_name(self, section, is_relative): section_id = self.cache.get_section_id(section).lower() if section_id != '': section_id = '_' + section_id method_name = 'get_token{id}'.format(id=section_id) return self.get_scoped(method_name, is_relative) def get_type(self, type_name, is_relative): if type_name == 'mode_stack': return "std::stack<{mode_type}>".format( mode_type=self.get_type('mode', is_relative)) elif type_name == 'mode': type_text = 'Mode' elif type_name == 'token': type_text = 'Token' elif type_name == 'state': return 'State' else: raise Exception("unrecognized type '{id}'".format(id=type_name)) return self.get_scoped(type_text, is_relative) def get_state_id_formatter(self, rules): return StateIdFormatter(rules, self.reserved_ids)