annotate win64-msvc/include/capnp/schema-parser.h @ 166:cbd6d7e562c7

Merge build update
author Chris Cannam <cannam@all-day-breakfast.com>
date Thu, 31 Oct 2019 13:36:58 +0000
parents b4bfdf10c4b3
children
rev   line source
cannam@148 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@148 2 // Licensed under the MIT License:
cannam@148 3 //
cannam@148 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@148 5 // of this software and associated documentation files (the "Software"), to deal
cannam@148 6 // in the Software without restriction, including without limitation the rights
cannam@148 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@148 8 // copies of the Software, and to permit persons to whom the Software is
cannam@148 9 // furnished to do so, subject to the following conditions:
cannam@148 10 //
cannam@148 11 // The above copyright notice and this permission notice shall be included in
cannam@148 12 // all copies or substantial portions of the Software.
cannam@148 13 //
cannam@148 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@148 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@148 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@148 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@148 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@148 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@148 20 // THE SOFTWARE.
cannam@148 21
cannam@148 22 #ifndef CAPNP_SCHEMA_PARSER_H_
cannam@148 23 #define CAPNP_SCHEMA_PARSER_H_
cannam@148 24
cannam@148 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@148 26 #pragma GCC system_header
cannam@148 27 #endif
cannam@148 28
cannam@148 29 #include "schema-loader.h"
cannam@148 30 #include <kj/string.h>
cannam@148 31
cannam@148 32 namespace capnp {
cannam@148 33
cannam@148 34 class ParsedSchema;
cannam@148 35 class SchemaFile;
cannam@148 36
cannam@148 37 class SchemaParser {
cannam@148 38 // Parses `.capnp` files to produce `Schema` objects.
cannam@148 39 //
cannam@148 40 // This class is thread-safe, hence all its methods are const.
cannam@148 41
cannam@148 42 public:
cannam@148 43 SchemaParser();
cannam@148 44 ~SchemaParser() noexcept(false);
cannam@148 45
cannam@148 46 ParsedSchema parseDiskFile(kj::StringPtr displayName, kj::StringPtr diskPath,
cannam@148 47 kj::ArrayPtr<const kj::StringPtr> importPath) const;
cannam@148 48 // Parse a file located on disk. Throws an exception if the file dosen't exist.
cannam@148 49 //
cannam@148 50 // Parameters:
cannam@148 51 // * `displayName`: The name that will appear in the file's schema node. (If the file has
cannam@148 52 // already been parsed, this will be ignored and the display name from the first time it was
cannam@148 53 // parsed will be kept.)
cannam@148 54 // * `diskPath`: The path to the file on disk.
cannam@148 55 // * `importPath`: Directories to search when resolving absolute imports within this file
cannam@148 56 // (imports that start with a `/`). Must remain valid until the SchemaParser is destroyed.
cannam@148 57 // (If the file has already been parsed, this will be ignored and the import path from the
cannam@148 58 // first time it was parsed will be kept.)
cannam@148 59 //
cannam@148 60 // This method is a shortcut, equivalent to:
cannam@148 61 // parser.parseFile(SchemaFile::newDiskFile(displayName, diskPath, importPath))`;
cannam@148 62 //
cannam@148 63 // This method throws an exception if any errors are encountered in the file or in anything the
cannam@148 64 // file depends on. Note that merely importing another file does not count as a dependency on
cannam@148 65 // anything in the imported file -- only the imported types which are actually used are
cannam@148 66 // "dependencies".
cannam@148 67
cannam@148 68 ParsedSchema parseFile(kj::Own<SchemaFile>&& file) const;
cannam@148 69 // Advanced interface for parsing a file that may or may not be located in any global namespace.
cannam@148 70 // Most users will prefer `parseDiskFile()`.
cannam@148 71 //
cannam@148 72 // If the file has already been parsed (that is, a SchemaFile that compares equal to this one
cannam@148 73 // was parsed previously), the existing schema will be returned again.
cannam@148 74 //
cannam@148 75 // This method reports errors by calling SchemaFile::reportError() on the file where the error
cannam@148 76 // is located. If that call does not throw an exception, `parseFile()` may in fact return
cannam@148 77 // normally. In this case, the result is a best-effort attempt to compile the schema, but it
cannam@148 78 // may be invalid or corrupt, and using it for anything may cause exceptions to be thrown.
cannam@148 79
cannam@148 80 template <typename T>
cannam@148 81 inline void loadCompiledTypeAndDependencies() {
cannam@148 82 // See SchemaLoader::loadCompiledTypeAndDependencies().
cannam@148 83 getLoader().loadCompiledTypeAndDependencies<T>();
cannam@148 84 }
cannam@148 85
cannam@148 86 private:
cannam@148 87 struct Impl;
cannam@148 88 class ModuleImpl;
cannam@148 89 kj::Own<Impl> impl;
cannam@148 90 mutable bool hadErrors = false;
cannam@148 91
cannam@148 92 ModuleImpl& getModuleImpl(kj::Own<SchemaFile>&& file) const;
cannam@148 93 SchemaLoader& getLoader();
cannam@148 94
cannam@148 95 friend class ParsedSchema;
cannam@148 96 };
cannam@148 97
cannam@148 98 class ParsedSchema: public Schema {
cannam@148 99 // ParsedSchema is an extension of Schema which also has the ability to look up nested nodes
cannam@148 100 // by name. See `SchemaParser`.
cannam@148 101
cannam@148 102 public:
cannam@148 103 inline ParsedSchema(): parser(nullptr) {}
cannam@148 104
cannam@148 105 kj::Maybe<ParsedSchema> findNested(kj::StringPtr name) const;
cannam@148 106 // Gets the nested node with the given name, or returns null if there is no such nested
cannam@148 107 // declaration.
cannam@148 108
cannam@148 109 ParsedSchema getNested(kj::StringPtr name) const;
cannam@148 110 // Gets the nested node with the given name, or throws an exception if there is no such nested
cannam@148 111 // declaration.
cannam@148 112
cannam@148 113 private:
cannam@148 114 inline ParsedSchema(Schema inner, const SchemaParser& parser): Schema(inner), parser(&parser) {}
cannam@148 115
cannam@148 116 const SchemaParser* parser;
cannam@148 117 friend class SchemaParser;
cannam@148 118 };
cannam@148 119
cannam@148 120 // =======================================================================================
cannam@148 121 // Advanced API
cannam@148 122
cannam@148 123 class SchemaFile {
cannam@148 124 // Abstract interface representing a schema file. You can implement this yourself in order to
cannam@148 125 // gain more control over how the compiler resolves imports and reads files. For the
cannam@148 126 // common case of files on disk or other global filesystem-like namespaces, use
cannam@148 127 // `SchemaFile::newDiskFile()`.
cannam@148 128
cannam@148 129 public:
cannam@148 130 class FileReader {
cannam@148 131 public:
cannam@148 132 virtual bool exists(kj::StringPtr path) const = 0;
cannam@148 133 virtual kj::Array<const char> read(kj::StringPtr path) const = 0;
cannam@148 134 };
cannam@148 135
cannam@148 136 class DiskFileReader final: public FileReader {
cannam@148 137 // Implementation of FileReader that uses the local disk. Files are read using mmap() if
cannam@148 138 // possible.
cannam@148 139
cannam@148 140 public:
cannam@148 141 static const DiskFileReader instance;
cannam@148 142
cannam@148 143 bool exists(kj::StringPtr path) const override;
cannam@148 144 kj::Array<const char> read(kj::StringPtr path) const override;
cannam@148 145 };
cannam@148 146
cannam@148 147 static kj::Own<SchemaFile> newDiskFile(
cannam@148 148 kj::StringPtr displayName, kj::StringPtr diskPath,
cannam@148 149 kj::ArrayPtr<const kj::StringPtr> importPath,
cannam@148 150 const FileReader& fileReader = DiskFileReader::instance);
cannam@148 151 // Construct a SchemaFile representing a file on disk (or located in the filesystem-like
cannam@148 152 // namespace represented by `fileReader`).
cannam@148 153 //
cannam@148 154 // Parameters:
cannam@148 155 // * `displayName`: The name that will appear in the file's schema node.
cannam@148 156 // * `diskPath`: The path to the file on disk.
cannam@148 157 // * `importPath`: Directories to search when resolving absolute imports within this file
cannam@148 158 // (imports that start with a `/`). The array content must remain valid as long as the
cannam@148 159 // SchemaFile exists (which is at least as long as the SchemaParser that parses it exists).
cannam@148 160 // * `fileReader`: Allows you to use a filesystem other than the actual local disk. Although,
cannam@148 161 // if you find yourself using this, it may make more sense for you to implement SchemaFile
cannam@148 162 // yourself.
cannam@148 163 //
cannam@148 164 // The SchemaFile compares equal to any other SchemaFile that has exactly the same disk path,
cannam@148 165 // after canonicalization.
cannam@148 166 //
cannam@148 167 // The SchemaFile will throw an exception if any errors are reported.
cannam@148 168
cannam@148 169 // -----------------------------------------------------------------
cannam@148 170 // For more control, you can implement this interface.
cannam@148 171
cannam@148 172 virtual kj::StringPtr getDisplayName() const = 0;
cannam@148 173 // Get the file's name, as it should appear in the schema.
cannam@148 174
cannam@148 175 virtual kj::Array<const char> readContent() const = 0;
cannam@148 176 // Read the file's entire content and return it as a byte array.
cannam@148 177
cannam@148 178 virtual kj::Maybe<kj::Own<SchemaFile>> import(kj::StringPtr path) const = 0;
cannam@148 179 // Resolve an import, relative to this file.
cannam@148 180 //
cannam@148 181 // `path` is exactly what appears between quotes after the `import` keyword in the source code.
cannam@148 182 // It is entirely up to the `SchemaFile` to decide how to map this to another file. Typically,
cannam@148 183 // a leading '/' means that the file is an "absolute" path and is searched for in some list of
cannam@148 184 // schema file repositories. On the other hand, a path that doesn't start with '/' is relative
cannam@148 185 // to the importing file.
cannam@148 186
cannam@148 187 virtual bool operator==(const SchemaFile& other) const = 0;
cannam@148 188 virtual bool operator!=(const SchemaFile& other) const = 0;
cannam@148 189 virtual size_t hashCode() const = 0;
cannam@148 190 // Compare two SchemaFiles to see if they refer to the same underlying file. This is an
cannam@148 191 // optimization used to avoid the need to re-parse a file to check its ID.
cannam@148 192
cannam@148 193 struct SourcePos {
cannam@148 194 uint byte;
cannam@148 195 uint line;
cannam@148 196 uint column;
cannam@148 197 };
cannam@148 198 virtual void reportError(SourcePos start, SourcePos end, kj::StringPtr message) const = 0;
cannam@148 199 // Report that the file contains an error at the given interval.
cannam@148 200
cannam@148 201 private:
cannam@148 202 class DiskSchemaFile;
cannam@148 203 };
cannam@148 204
cannam@148 205 } // namespace capnp
cannam@148 206
cannam@148 207 #endif // CAPNP_SCHEMA_PARSER_H_