诸暨麻将添加redis
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

336 linhas
14 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This file is the public interface to the .proto file parser.
  35. #ifndef GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
  36. #define GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
  37. #include <set>
  38. #include <string>
  39. #include <utility>
  40. #include <vector>
  41. #include <google/protobuf/compiler/parser.h>
  42. #include <google/protobuf/descriptor.h>
  43. #include <google/protobuf/descriptor_database.h>
  44. #include <google/protobuf/port_def.inc>
  45. namespace google {
  46. namespace protobuf {
  47. namespace io {
  48. class ZeroCopyInputStream;
  49. }
  50. namespace compiler {
  51. // Defined in this file.
  52. class Importer;
  53. class MultiFileErrorCollector;
  54. class SourceTree;
  55. class DiskSourceTree;
  56. // TODO(kenton): Move all SourceTree stuff to a separate file?
  57. // An implementation of DescriptorDatabase which loads files from a SourceTree
  58. // and parses them.
  59. //
  60. // Note: This class is not thread-safe since it maintains a table of source
  61. // code locations for error reporting. However, when a DescriptorPool wraps
  62. // a DescriptorDatabase, it uses mutex locking to make sure only one method
  63. // of the database is called at a time, even if the DescriptorPool is used
  64. // from multiple threads. Therefore, there is only a problem if you create
  65. // multiple DescriptorPools wrapping the same SourceTreeDescriptorDatabase
  66. // and use them from multiple threads.
  67. //
  68. // Note: This class does not implement FindFileContainingSymbol() or
  69. // FindFileContainingExtension(); these will always return false.
  70. class PROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabase {
  71. public:
  72. SourceTreeDescriptorDatabase(SourceTree* source_tree);
  73. // If non-NULL, fallback_database will be checked if a file doesn't exist in
  74. // the specified source_tree.
  75. SourceTreeDescriptorDatabase(SourceTree* source_tree,
  76. DescriptorDatabase* fallback_database);
  77. ~SourceTreeDescriptorDatabase();
  78. // Instructs the SourceTreeDescriptorDatabase to report any parse errors
  79. // to the given MultiFileErrorCollector. This should be called before
  80. // parsing. error_collector must remain valid until either this method
  81. // is called again or the SourceTreeDescriptorDatabase is destroyed.
  82. void RecordErrorsTo(MultiFileErrorCollector* error_collector) {
  83. error_collector_ = error_collector;
  84. }
  85. // Gets a DescriptorPool::ErrorCollector which records errors to the
  86. // MultiFileErrorCollector specified with RecordErrorsTo(). This collector
  87. // has the ability to determine exact line and column numbers of errors
  88. // from the information given to it by the DescriptorPool.
  89. DescriptorPool::ErrorCollector* GetValidationErrorCollector() {
  90. using_validation_error_collector_ = true;
  91. return &validation_error_collector_;
  92. }
  93. // implements DescriptorDatabase -----------------------------------
  94. bool FindFileByName(const std::string& filename,
  95. FileDescriptorProto* output) override;
  96. bool FindFileContainingSymbol(const std::string& symbol_name,
  97. FileDescriptorProto* output) override;
  98. bool FindFileContainingExtension(const std::string& containing_type,
  99. int field_number,
  100. FileDescriptorProto* output) override;
  101. private:
  102. class SingleFileErrorCollector;
  103. SourceTree* source_tree_;
  104. DescriptorDatabase* fallback_database_;
  105. MultiFileErrorCollector* error_collector_;
  106. class PROTOBUF_EXPORT ValidationErrorCollector
  107. : public DescriptorPool::ErrorCollector {
  108. public:
  109. ValidationErrorCollector(SourceTreeDescriptorDatabase* owner);
  110. ~ValidationErrorCollector();
  111. // implements ErrorCollector ---------------------------------------
  112. void AddError(const std::string& filename, const std::string& element_name,
  113. const Message* descriptor, ErrorLocation location,
  114. const std::string& message) override;
  115. void AddWarning(const std::string& filename,
  116. const std::string& element_name, const Message* descriptor,
  117. ErrorLocation location,
  118. const std::string& message) override;
  119. private:
  120. SourceTreeDescriptorDatabase* owner_;
  121. };
  122. friend class ValidationErrorCollector;
  123. bool using_validation_error_collector_;
  124. SourceLocationTable source_locations_;
  125. ValidationErrorCollector validation_error_collector_;
  126. };
  127. // Simple interface for parsing .proto files. This wraps the process
  128. // of opening the file, parsing it with a Parser, recursively parsing all its
  129. // imports, and then cross-linking the results to produce a FileDescriptor.
  130. //
  131. // This is really just a thin wrapper around SourceTreeDescriptorDatabase.
  132. // You may find that SourceTreeDescriptorDatabase is more flexible.
  133. //
  134. // TODO(kenton): I feel like this class is not well-named.
  135. class PROTOBUF_EXPORT Importer {
  136. public:
  137. Importer(SourceTree* source_tree, MultiFileErrorCollector* error_collector);
  138. ~Importer();
  139. // Import the given file and build a FileDescriptor representing it. If
  140. // the file is already in the DescriptorPool, the existing FileDescriptor
  141. // will be returned. The FileDescriptor is property of the DescriptorPool,
  142. // and will remain valid until it is destroyed. If any errors occur, they
  143. // will be reported using the error collector and Import() will return NULL.
  144. //
  145. // A particular Importer object will only report errors for a particular
  146. // file once. All future attempts to import the same file will return NULL
  147. // without reporting any errors. The idea is that you might want to import
  148. // a lot of files without seeing the same errors over and over again. If
  149. // you want to see errors for the same files repeatedly, you can use a
  150. // separate Importer object to import each one (but use the same
  151. // DescriptorPool so that they can be cross-linked).
  152. const FileDescriptor* Import(const std::string& filename);
  153. // The DescriptorPool in which all imported FileDescriptors and their
  154. // contents are stored.
  155. inline const DescriptorPool* pool() const { return &pool_; }
  156. void AddUnusedImportTrackFile(const std::string& file_name);
  157. void ClearUnusedImportTrackFiles();
  158. private:
  159. SourceTreeDescriptorDatabase database_;
  160. DescriptorPool pool_;
  161. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Importer);
  162. };
  163. // If the importer encounters problems while trying to import the proto files,
  164. // it reports them to a MultiFileErrorCollector.
  165. class PROTOBUF_EXPORT MultiFileErrorCollector {
  166. public:
  167. inline MultiFileErrorCollector() {}
  168. virtual ~MultiFileErrorCollector();
  169. // Line and column numbers are zero-based. A line number of -1 indicates
  170. // an error with the entire file (e.g. "not found").
  171. virtual void AddError(const std::string& filename, int line, int column,
  172. const std::string& message) = 0;
  173. virtual void AddWarning(const std::string& filename, int line, int column,
  174. const std::string& message) {}
  175. private:
  176. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MultiFileErrorCollector);
  177. };
  178. // Abstract interface which represents a directory tree containing proto files.
  179. // Used by the default implementation of Importer to resolve import statements
  180. // Most users will probably want to use the DiskSourceTree implementation,
  181. // below.
  182. class PROTOBUF_EXPORT SourceTree {
  183. public:
  184. inline SourceTree() {}
  185. virtual ~SourceTree();
  186. // Open the given file and return a stream that reads it, or NULL if not
  187. // found. The caller takes ownership of the returned object. The filename
  188. // must be a path relative to the root of the source tree and must not
  189. // contain "." or ".." components.
  190. virtual io::ZeroCopyInputStream* Open(const std::string& filename) = 0;
  191. // If Open() returns NULL, calling this method immediately will return an
  192. // description of the error.
  193. // Subclasses should implement this method and return a meaningful value for
  194. // better error reporting.
  195. // TODO(xiaofeng): change this to a pure virtual function.
  196. virtual std::string GetLastErrorMessage();
  197. private:
  198. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SourceTree);
  199. };
  200. // An implementation of SourceTree which loads files from locations on disk.
  201. // Multiple mappings can be set up to map locations in the DiskSourceTree to
  202. // locations in the physical filesystem.
  203. class PROTOBUF_EXPORT DiskSourceTree : public SourceTree {
  204. public:
  205. DiskSourceTree();
  206. ~DiskSourceTree();
  207. // Map a path on disk to a location in the SourceTree. The path may be
  208. // either a file or a directory. If it is a directory, the entire tree
  209. // under it will be mapped to the given virtual location. To map a directory
  210. // to the root of the source tree, pass an empty string for virtual_path.
  211. //
  212. // If multiple mapped paths apply when opening a file, they will be searched
  213. // in order. For example, if you do:
  214. // MapPath("bar", "foo/bar");
  215. // MapPath("", "baz");
  216. // and then you do:
  217. // Open("bar/qux");
  218. // the DiskSourceTree will first try to open foo/bar/qux, then baz/bar/qux,
  219. // returning the first one that opens successfuly.
  220. //
  221. // disk_path may be an absolute path or relative to the current directory,
  222. // just like a path you'd pass to open().
  223. void MapPath(const std::string& virtual_path, const std::string& disk_path);
  224. // Return type for DiskFileToVirtualFile().
  225. enum DiskFileToVirtualFileResult {
  226. SUCCESS,
  227. SHADOWED,
  228. CANNOT_OPEN,
  229. NO_MAPPING
  230. };
  231. // Given a path to a file on disk, find a virtual path mapping to that
  232. // file. The first mapping created with MapPath() whose disk_path contains
  233. // the filename is used. However, that virtual path may not actually be
  234. // usable to open the given file. Possible return values are:
  235. // * SUCCESS: The mapping was found. *virtual_file is filled in so that
  236. // calling Open(*virtual_file) will open the file named by disk_file.
  237. // * SHADOWED: A mapping was found, but using Open() to open this virtual
  238. // path will end up returning some different file. This is because some
  239. // other mapping with a higher precedence also matches this virtual path
  240. // and maps it to a different file that exists on disk. *virtual_file
  241. // is filled in as it would be in the SUCCESS case. *shadowing_disk_file
  242. // is filled in with the disk path of the file which would be opened if
  243. // you were to call Open(*virtual_file).
  244. // * CANNOT_OPEN: The mapping was found and was not shadowed, but the
  245. // file specified cannot be opened. When this value is returned,
  246. // errno will indicate the reason the file cannot be opened. *virtual_file
  247. // will be set to the virtual path as in the SUCCESS case, even though
  248. // it is not useful.
  249. // * NO_MAPPING: Indicates that no mapping was found which contains this
  250. // file.
  251. DiskFileToVirtualFileResult DiskFileToVirtualFile(
  252. const std::string& disk_file, std::string* virtual_file,
  253. std::string* shadowing_disk_file);
  254. // Given a virtual path, find the path to the file on disk.
  255. // Return true and update disk_file with the on-disk path if the file exists.
  256. // Return false and leave disk_file untouched if the file doesn't exist.
  257. bool VirtualFileToDiskFile(const std::string& virtual_file,
  258. std::string* disk_file);
  259. // implements SourceTree -------------------------------------------
  260. io::ZeroCopyInputStream* Open(const std::string& filename) override;
  261. std::string GetLastErrorMessage() override;
  262. private:
  263. struct Mapping {
  264. std::string virtual_path;
  265. std::string disk_path;
  266. inline Mapping(const std::string& virtual_path_param,
  267. const std::string& disk_path_param)
  268. : virtual_path(virtual_path_param), disk_path(disk_path_param) {}
  269. };
  270. std::vector<Mapping> mappings_;
  271. std::string last_error_message_;
  272. // Like Open(), but returns the on-disk path in disk_file if disk_file is
  273. // non-NULL and the file could be successfully opened.
  274. io::ZeroCopyInputStream* OpenVirtualFile(const std::string& virtual_file,
  275. std::string* disk_file);
  276. // Like Open() but given the actual on-disk path.
  277. io::ZeroCopyInputStream* OpenDiskFile(const std::string& filename);
  278. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DiskSourceTree);
  279. };
  280. } // namespace compiler
  281. } // namespace protobuf
  282. } // namespace google
  283. #include <google/protobuf/port_undef.inc>
  284. #endif // GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__