annotate win32-mingw/include/capnp/schema-parser.h @ 50:37d53a7e8262

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