annotate win64-msvc/include/capnp/schema-parser.h @ 132:42a73082be24

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