Ejemplo n.º 1
0
 def visitor(ast_node, ast_parent_node, client_data):
     if ast_node.location.file and ast_node.location.file.name == tunit.spelling:  # we're only interested in symbols from associated translation unit
         ast_node_id = client_data.clang_parser.get_ast_node_id(ast_node)
         if ast_node_id != ASTNodeId.getUnsupportedId():
             highlight_rule = VimSyntaxGenerator.__tag_id_to_vim_syntax_group(ast_node_id) + " " + client_data.clang_parser.get_ast_node_name(ast_node)
             client_data.vim_syntax_element.append(
                 "call matchaddpos('" +
                 str(VimSyntaxGenerator.__tag_id_to_vim_syntax_group(ast_node_id)) +
                 "', [[" +
                 str(client_data.clang_parser.get_ast_node_line(ast_node)) +
                 ", " +
                 str(client_data.clang_parser.get_ast_node_column(ast_node)) +
                 ", " +
                 str(len(client_data.clang_parser.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, client_data.clang_parser.get_ast_node_name(ast_node)
                 )
             )
         return ChildVisitResult.RECURSE.value  # If we are positioned in TU of interest, then we'll traverse through all descendants
     return ChildVisitResult.CONTINUE.value  # Otherwise, we'll skip to the next sibling
Ejemplo n.º 2
0
 def to_ast_node_id(kind):
     if (kind == clang.cindex.CursorKind.NAMESPACE):
         return ASTNodeId.getNamespaceId()
     if (kind in [
             clang.cindex.CursorKind.CLASS_DECL,
             clang.cindex.CursorKind.CLASS_TEMPLATE,
             clang.cindex.CursorKind.CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
     ]):
         return ASTNodeId.getClassId()
     if (kind == clang.cindex.CursorKind.STRUCT_DECL):
         return ASTNodeId.getStructId()
     if (kind == clang.cindex.CursorKind.ENUM_DECL):
         return ASTNodeId.getEnumId()
     if (kind == clang.cindex.CursorKind.ENUM_CONSTANT_DECL):
         return ASTNodeId.getEnumValueId()
     if (kind == clang.cindex.CursorKind.UNION_DECL):
         return ASTNodeId.getUnionId()
     if (kind == clang.cindex.CursorKind.FIELD_DECL):
         return ASTNodeId.getFieldId()
     if (kind == clang.cindex.CursorKind.VAR_DECL):
         return ASTNodeId.getLocalVariableId()
     if (kind in [
             clang.cindex.CursorKind.FUNCTION_DECL,
             clang.cindex.CursorKind.FUNCTION_TEMPLATE
     ]):
         return ASTNodeId.getFunctionId()
     if (kind in [
             clang.cindex.CursorKind.CXX_METHOD,
             clang.cindex.CursorKind.CONSTRUCTOR,
             clang.cindex.CursorKind.DESTRUCTOR
     ]):
         return ASTNodeId.getMethodId()
     if (kind == clang.cindex.CursorKind.PARM_DECL):
         return ASTNodeId.getFunctionParameterId()
     if (kind == clang.cindex.CursorKind.TEMPLATE_TYPE_PARAMETER):
         return ASTNodeId.getTemplateTypeParameterId()
     if (kind == clang.cindex.CursorKind.TEMPLATE_NON_TYPE_PARAMETER):
         return ASTNodeId.getTemplateNonTypeParameterId()
     if (kind == clang.cindex.CursorKind.TEMPLATE_TEMPLATE_PARAMETER):
         return ASTNodeId.getTemplateTemplateParameterId()
     if (kind == clang.cindex.CursorKind.MACRO_DEFINITION):
         return ASTNodeId.getMacroDefinitionId()
     if (kind == clang.cindex.CursorKind.MACRO_INSTANTIATION):
         return ASTNodeId.getMacroInstantiationId()
     if (kind in [
             clang.cindex.CursorKind.TYPEDEF_DECL,
             clang.cindex.CursorKind.TYPE_ALIAS_DECL
     ]):
         return ASTNodeId.getTypedefId()
     if (kind == clang.cindex.CursorKind.NAMESPACE_ALIAS):
         return ASTNodeId.getNamespaceAliasId()
     if (kind == clang.cindex.CursorKind.USING_DIRECTIVE):
         return ASTNodeId.getUsingDirectiveId()
     if (kind == clang.cindex.CursorKind.USING_DECLARATION):
         return ASTNodeId.getUsingDeclarationId()
     return ASTNodeId.getUnsupportedId()
Ejemplo n.º 3
0
 def to_ast_node_id(kind):
     if (kind == clang.cindex.CursorKind.NAMESPACE):
         return ASTNodeId.getNamespaceId()
     if (kind in [clang.cindex.CursorKind.CLASS_DECL, clang.cindex.CursorKind.CLASS_TEMPLATE, clang.cindex.CursorKind.CLASS_TEMPLATE_PARTIAL_SPECIALIZATION]):
         return ASTNodeId.getClassId()
     if (kind == clang.cindex.CursorKind.STRUCT_DECL):
         return ASTNodeId.getStructId()
     if (kind == clang.cindex.CursorKind.ENUM_DECL):
         return ASTNodeId.getEnumId()
     if (kind == clang.cindex.CursorKind.ENUM_CONSTANT_DECL):
         return ASTNodeId.getEnumValueId()
     if (kind == clang.cindex.CursorKind.UNION_DECL):
         return ASTNodeId.getUnionId()
     if (kind == clang.cindex.CursorKind.FIELD_DECL):
         return ASTNodeId.getFieldId()
     if (kind == clang.cindex.CursorKind.VAR_DECL):
         return ASTNodeId.getLocalVariableId()
     if (kind in [clang.cindex.CursorKind.FUNCTION_DECL, clang.cindex.CursorKind.FUNCTION_TEMPLATE]):
         return ASTNodeId.getFunctionId()
     if (kind in [clang.cindex.CursorKind.CXX_METHOD, clang.cindex.CursorKind.CONSTRUCTOR, clang.cindex.CursorKind.DESTRUCTOR]):
         return ASTNodeId.getMethodId()
     if (kind == clang.cindex.CursorKind.PARM_DECL):
         return ASTNodeId.getFunctionParameterId()
     if (kind == clang.cindex.CursorKind.TEMPLATE_TYPE_PARAMETER):
         return ASTNodeId.getTemplateTypeParameterId()
     if (kind == clang.cindex.CursorKind.TEMPLATE_NON_TYPE_PARAMETER):
         return ASTNodeId.getTemplateNonTypeParameterId()
     if (kind == clang.cindex.CursorKind.TEMPLATE_TEMPLATE_PARAMETER):
         return ASTNodeId.getTemplateTemplateParameterId()
     if (kind == clang.cindex.CursorKind.MACRO_DEFINITION):
         return ASTNodeId.getMacroDefinitionId()
     if (kind == clang.cindex.CursorKind.MACRO_INSTANTIATION):
         return ASTNodeId.getMacroInstantiationId()
     if (kind in [clang.cindex.CursorKind.TYPEDEF_DECL, clang.cindex.CursorKind.TYPE_ALIAS_DECL]):
         return ASTNodeId.getTypedefId()
     if (kind == clang.cindex.CursorKind.NAMESPACE_ALIAS):
         return ASTNodeId.getNamespaceAliasId()
     if (kind == clang.cindex.CursorKind.USING_DIRECTIVE):
         return ASTNodeId.getUsingDirectiveId()
     if (kind == clang.cindex.CursorKind.USING_DECLARATION):
         return ASTNodeId.getUsingDeclarationId()
     return ASTNodeId.getUnsupportedId()
Ejemplo n.º 4
0
    def __call__(self, clang_parser, args):
        start = time.clock()

        # Build Vim syntax highlight rules
        vim_syntax_element = ['call clearmatches()\n']
        ast_node_list = clang_parser.get_ast_node_list()
        for ast_node in ast_node_list:
            ast_node_id = clang_parser.get_ast_node_id(ast_node)
            if ast_node_id != ASTNodeId.getUnsupportedId():
                highlight_rule = self.__tag_id_to_vim_syntax_group(ast_node_id) + " " + clang_parser.get_ast_node_name(ast_node)
                vim_syntax_element.append(
                    "call matchaddpos('" +
                    str(self.__tag_id_to_vim_syntax_group(ast_node_id)) +
                    "', [[" +
                    str(clang_parser.get_ast_node_line(ast_node)) +
                    ", " +
                    str(clang_parser.get_ast_node_column(ast_node)) +
                    ", " +
                    str(len(clang_parser.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, clang_parser.get_ast_node_name(ast_node)
                    )
                )

        # Write Vim syntax file
        vim_syntax_file = open(self.output_syntax_file, "w", 0)
        vim_syntax_file.writelines(vim_syntax_element)
        time_elapsed = time.clock() - start

        # Apply newly generated syntax rules
        YavideUtils.call_vim_remote_function(self.yavide_instance, "Y_SrcCodeHighlighter_Apply('" + clang_parser.filename + "'" + ", '" + self.output_syntax_file + "')")

        # Write some debug information
        clang_parser.dump_ast_nodes()

        # Log how long generating Vim syntax file took
        logging.info("Vim syntax generator for '{0}' took {1}.".format(clang_parser.filename, time_elapsed))