annotate win32-mingw/include/capnp/schema-parser.h @ 169:223a55898ab9 tip default

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