parser.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright (c) 2020 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __RPC_PARSER_H__
  14. #define __RPC_PARSER_H__
  15. #define PARSER_ST_BLOCK_MASK 0x01
  16. #define PARSER_ST_OUTSIDE_BLOCK 0x00
  17. #define PARSER_ST_INSIDE_BLOCK 0x01
  18. #define PARSER_ST_OUTSIDE_BLOCK_MASK 0xFFFE
  19. #define PARSER_ST_COMMENT_MASK 0x10
  20. #define PARSER_ST_INSIDE_COMMENT 0x10
  21. #define PARSER_ST_OUTSIDE_COMMENT_MASK 0xFFFD
  22. #define PARSER_ST_INSIDE_COMMENT_MASK 0xFFFF
  23. #define DIRECTORY_LENGTH 2048
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <vector>
  28. #include <map>
  29. #include <list>
  30. #include <string>
  31. #include <algorithm>
  32. #include <unordered_map>
  33. #include "descriptor.h"
  34. class Parser
  35. {
  36. public:
  37. bool parse(const std::string& proto_file, idl_info& info);
  38. std::string find_typedef_mapping_type(std::string& type_name,
  39. size_t& cur, idl_info& info);
  40. void build_typedef_mapping(idl_info& info);
  41. int find_valid(const std::string& line);
  42. bool check_block_begin(FILE *in, std::string& line);
  43. bool check_block_begin(const std::string& line);
  44. bool check_block_end(const std::string& line);
  45. void check_single_comments(std::string& line);
  46. bool parse_block_name(const std::string& line,
  47. std::string& block_name,
  48. std::string& block_name_value,
  49. std::string& extend_type);
  50. bool parse_service_pb(const std::string& block, Descriptor& desc);
  51. std::vector<std::string> split_thrift_rpc(const std::string &str);
  52. bool parse_thrift_typedef(const std::string& line,
  53. std::string& old_type_name,
  54. std::string& new_type_name,
  55. idl_info& info);
  56. bool parse_rpc_param_thrift(const std::string& file_name_prefix,
  57. const std::string &str,
  58. std::vector<rpc_param> &params,
  59. idl_info& info);
  60. bool parse_service_thrift(const std::string& file_name_prefix,
  61. const std::string& block,
  62. Descriptor& desc,
  63. idl_info& info);
  64. bool parse_struct_thrift(const std::string& file_name_prefix,
  65. const std::string& block,
  66. Descriptor& desc, idl_info& info);
  67. bool parse_enum_thrift(const std::string& block, Descriptor& desc);
  68. bool parse_package_name(const std::string& line,
  69. std::vector<std::string>& package_name);
  70. bool parse_include_file(const std::string& line, std::string& file_name);
  71. bool check_multi_comments_begin(std::string& line);
  72. bool check_multi_comments_end(std::string& line);
  73. int parse_pb_rpc_option(const std::string& line);
  74. Parser(bool is_thrift) { this->is_thrift = is_thrift; }
  75. private:
  76. bool is_thrift;
  77. };
  78. #endif