Protobuf-Compiler相关类&代码生成流程

本文详细介绍了Protobuf的代码生成流程,包括CommandLineInterface、SourceTree、Importer、Tokenizer等核心类的作用和工作原理。同时,阐述了Parser如何将tokenizer对象转化为FileDescriptorProto,以及CodeGenerator如何生成代码的相关知识。
摘要由CSDN通过智能技术生成

代码生成流程:

核心流程如下图所示:

avatar

核心数据结构

类CommandLineInterface

  1. generators_: map<string, GeneratorInfo>,提供从”–cpp_out” -> CppGenerator的映射,从protoc参数中获取需要的generator的名称;
  2. plugins_map<string, string> ,plugin提供非protobuf已有的CodeGenerator服务,plugin采用进程方式提供服务。plugins_记录的是:plugin名称 -> plugin可执行程序在磁盘上的path
  3. plugin_prefix_: 设置为”protoc-“

类SourceTree

接口类,表示.proto文件的目录树。

类DiskSourceTree

类SourceTree的子类,用于加载磁盘上的多个文件,并且提供 从 物理磁盘路径/文件 ->SourceTree上的节点的map关系.还可以设置”” -> SourceTree上的root节点。如果多个路径设置对应了同一个文件,那么搜索时会按照设置的顺序来处理。

类Importer

根据.proto文件的name,返回对应的FileDescriptor。实际是通过DescriptorPool提供的服务。

类io::Tokenizer

词法分析器,1个Tokenizer对象处理一个ZeroCopyInputStream,将raw text的stream转化为能够被parser解析的stream(token序列)。外部使用者仅需循环调用Tokenizer::Next()和Tokenizer::current(),就可以按照顺序获得对应的token,就像一个token化的stream一样。

token的定义如下:
      struct Token {
        TokenType type;
        string text;       // The exact text of the token as it appeared in
                           // the input.  e.g. tokens of TYPE_STRING will still
                           // be escaped and in quotes.

        // "line" and "column" specify the position of the first character of
        // the token within the input stream.  They are zero-based.
        int line;
        int column;
        int end_column;
      };
token类型定义:
      enum TokenType {
        TYPE_START,       // Next() has not yet been called.
        TYPE_END,         // End of input reached.  "text" is empty.

        TYPE_IDENTIFIER,  // A sequence of letters, digits, and underscores, not
                          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值