def generate_vim_syntax_file_from_clang(self, filename, compiler_args, project_root_directory): # Generate the tokens tokenizer = ClangTokenizer() tokenizer.run(filename, compiler_args, project_root_directory) # Build Vim syntax highlight rules vim_syntax_element = ['call clearmatches()\n'] ast_node_list = tokenizer.get_ast_node_list() for ast_node in ast_node_list: ast_node_id = tokenizer.get_ast_node_id(ast_node) if ast_node_id != TokenIdentifier.getUnsupportedId(): highlight_rule = self.__tag_id_to_vim_syntax_group(ast_node_id) + " " + tokenizer.get_ast_node_name(ast_node) vim_syntax_element.append( "call matchaddpos('" + str(self.__tag_id_to_vim_syntax_group(ast_node_id)) + "', [[" + str(tokenizer.get_ast_node_line(ast_node)) + ", " + str(tokenizer.get_ast_node_column(ast_node)) + ", " + str(len(tokenizer.get_ast_node_name(ast_node))) + "]], -1)" + "\n" ) else: logging.debug("Unsupported token id: [{0}, {1}]: {2} '{3}'".format(ast_node.location.line, ast_node.location.column, ast_node.kind, tokenizer.get_ast_node_name(ast_node))) # Write Vim syntax file vim_syntax_file = open(self.output_syntax_file, "w") vim_syntax_file.writelines(vim_syntax_element) # Write some debug information tokenizer.dump_ast_nodes()
def generate_vim_syntax_file_from_clang(self, filename, compiler_args, project_root_directory): # Generate the tokens tokenizer = ClangTokenizer() tokenizer.run(filename, compiler_args, project_root_directory) # Build Vim syntax highlight rules vim_syntax_element = ['call clearmatches()\n'] token_list = tokenizer.get_token_list() for token in token_list: token_id = tokenizer.get_token_id(token) if token_id != TokenIdentifier.getUnsupportedId(): highlight_rule = self.__tag_id_to_vim_syntax_group(token_id) + " " + tokenizer.get_token_name(token) vim_syntax_element.append( "call matchaddpos('" + str(self.__tag_id_to_vim_syntax_group(token_id)) + "', [[" + str(tokenizer.get_token_line(token)) + ", " + str(tokenizer.get_token_column(token)) + ", " + str(len(tokenizer.get_token_name(token))) + "]], -1)" + "\n" ) else: logging.debug("Unsupported token id: [{0}, {1}]: {2} '{3}'".format(token.location.line, token.location.column, token.kind, tokenizer.get_token_name(token))) # Write Vim syntax file vim_syntax_file = open(self.output_syntax_file, "w") vim_syntax_file.writelines(vim_syntax_element) # Write some debug information tokenizer.dump_token_list()
def to_token_id(kind): if (kind == clang.cindex.CursorKind.NAMESPACE): return TokenIdentifier.getNamespaceId() if (kind in [clang.cindex.CursorKind.CLASS_DECL, clang.cindex.CursorKind.CLASS_TEMPLATE, clang.cindex.CursorKind.CLASS_TEMPLATE_PARTIAL_SPECIALIZATION]): return TokenIdentifier.getClassId() if (kind == clang.cindex.CursorKind.STRUCT_DECL): return TokenIdentifier.getStructId() if (kind == clang.cindex.CursorKind.ENUM_DECL): return TokenIdentifier.getEnumId() if (kind == clang.cindex.CursorKind.ENUM_CONSTANT_DECL): return TokenIdentifier.getEnumValueId() if (kind == clang.cindex.CursorKind.UNION_DECL): return TokenIdentifier.getUnionId() if (kind == clang.cindex.CursorKind.FIELD_DECL): return TokenIdentifier.getFieldId() if (kind == clang.cindex.CursorKind.VAR_DECL): return TokenIdentifier.getLocalVariableId() if (kind in [clang.cindex.CursorKind.FUNCTION_DECL, clang.cindex.CursorKind.FUNCTION_TEMPLATE]): return TokenIdentifier.getFunctionId() if (kind in [clang.cindex.CursorKind.CXX_METHOD, clang.cindex.CursorKind.CONSTRUCTOR, clang.cindex.CursorKind.DESTRUCTOR]): return TokenIdentifier.getMethodId() if (kind == clang.cindex.CursorKind.PARM_DECL): return TokenIdentifier.getFunctionParameterId() if (kind == clang.cindex.CursorKind.TEMPLATE_TYPE_PARAMETER): return TokenIdentifier.getTemplateTypeParameterId() if (kind == clang.cindex.CursorKind.TEMPLATE_NON_TYPE_PARAMETER): return TokenIdentifier.getTemplateNonTypeParameterId() if (kind == clang.cindex.CursorKind.TEMPLATE_TEMPLATE_PARAMETER): return TokenIdentifier.getTemplateTemplateParameterId() if (kind == clang.cindex.CursorKind.MACRO_DEFINITION): return TokenIdentifier.getMacroDefinitionId() if (kind == clang.cindex.CursorKind.MACRO_INSTANTIATION): return TokenIdentifier.getMacroInstantiationId() if (kind in [clang.cindex.CursorKind.TYPEDEF_DECL, clang.cindex.CursorKind.TYPE_ALIAS_DECL]): return TokenIdentifier.getTypedefId() if (kind == clang.cindex.CursorKind.NAMESPACE_ALIAS): return TokenIdentifier.getNamespaceAliasId() if (kind == clang.cindex.CursorKind.USING_DIRECTIVE): return TokenIdentifier.getUsingDirectiveId() if (kind == clang.cindex.CursorKind.USING_DECLARATION): return TokenIdentifier.getUsingDeclarationId() return TokenIdentifier.getUnsupportedId()