Chris@50: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@50: // Licensed under the MIT License: Chris@50: // Chris@50: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@50: // of this software and associated documentation files (the "Software"), to deal Chris@50: // in the Software without restriction, including without limitation the rights Chris@50: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@50: // copies of the Software, and to permit persons to whom the Software is Chris@50: // furnished to do so, subject to the following conditions: Chris@50: // Chris@50: // The above copyright notice and this permission notice shall be included in Chris@50: // all copies or substantial portions of the Software. Chris@50: // Chris@50: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@50: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@50: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@50: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@50: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@50: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@50: // THE SOFTWARE. Chris@50: Chris@50: #ifndef CAPNP_SCHEMA_PARSER_H_ Chris@50: #define CAPNP_SCHEMA_PARSER_H_ Chris@50: Chris@50: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) Chris@50: #pragma GCC system_header Chris@50: #endif Chris@50: Chris@50: #include "schema-loader.h" Chris@50: #include Chris@50: Chris@50: namespace capnp { Chris@50: Chris@50: class ParsedSchema; Chris@50: class SchemaFile; Chris@50: Chris@50: class SchemaParser { Chris@50: // Parses `.capnp` files to produce `Schema` objects. Chris@50: // Chris@50: // This class is thread-safe, hence all its methods are const. Chris@50: Chris@50: public: Chris@50: SchemaParser(); Chris@50: ~SchemaParser() noexcept(false); Chris@50: Chris@50: ParsedSchema parseDiskFile(kj::StringPtr displayName, kj::StringPtr diskPath, Chris@50: kj::ArrayPtr importPath) const; Chris@50: // Parse a file located on disk. Throws an exception if the file dosen't exist. Chris@50: // Chris@50: // Parameters: Chris@50: // * `displayName`: The name that will appear in the file's schema node. (If the file has Chris@50: // already been parsed, this will be ignored and the display name from the first time it was Chris@50: // parsed will be kept.) Chris@50: // * `diskPath`: The path to the file on disk. Chris@50: // * `importPath`: Directories to search when resolving absolute imports within this file Chris@50: // (imports that start with a `/`). Must remain valid until the SchemaParser is destroyed. Chris@50: // (If the file has already been parsed, this will be ignored and the import path from the Chris@50: // first time it was parsed will be kept.) Chris@50: // Chris@50: // This method is a shortcut, equivalent to: Chris@50: // parser.parseFile(SchemaFile::newDiskFile(displayName, diskPath, importPath))`; Chris@50: // Chris@50: // This method throws an exception if any errors are encountered in the file or in anything the Chris@50: // file depends on. Note that merely importing another file does not count as a dependency on Chris@50: // anything in the imported file -- only the imported types which are actually used are Chris@50: // "dependencies". Chris@50: Chris@50: ParsedSchema parseFile(kj::Own&& file) const; Chris@50: // Advanced interface for parsing a file that may or may not be located in any global namespace. Chris@50: // Most users will prefer `parseDiskFile()`. Chris@50: // Chris@50: // If the file has already been parsed (that is, a SchemaFile that compares equal to this one Chris@50: // was parsed previously), the existing schema will be returned again. Chris@50: // Chris@50: // This method reports errors by calling SchemaFile::reportError() on the file where the error Chris@50: // is located. If that call does not throw an exception, `parseFile()` may in fact return Chris@50: // normally. In this case, the result is a best-effort attempt to compile the schema, but it Chris@50: // may be invalid or corrupt, and using it for anything may cause exceptions to be thrown. Chris@50: Chris@50: template Chris@50: inline void loadCompiledTypeAndDependencies() { Chris@50: // See SchemaLoader::loadCompiledTypeAndDependencies(). Chris@50: getLoader().loadCompiledTypeAndDependencies(); Chris@50: } Chris@50: Chris@50: private: Chris@50: struct Impl; Chris@50: class ModuleImpl; Chris@50: kj::Own impl; Chris@50: mutable bool hadErrors = false; Chris@50: Chris@50: ModuleImpl& getModuleImpl(kj::Own&& file) const; Chris@50: SchemaLoader& getLoader(); Chris@50: Chris@50: friend class ParsedSchema; Chris@50: }; Chris@50: Chris@50: class ParsedSchema: public Schema { Chris@50: // ParsedSchema is an extension of Schema which also has the ability to look up nested nodes Chris@50: // by name. See `SchemaParser`. Chris@50: Chris@50: public: Chris@50: inline ParsedSchema(): parser(nullptr) {} Chris@50: Chris@50: kj::Maybe findNested(kj::StringPtr name) const; Chris@50: // Gets the nested node with the given name, or returns null if there is no such nested Chris@50: // declaration. Chris@50: Chris@50: ParsedSchema getNested(kj::StringPtr name) const; Chris@50: // Gets the nested node with the given name, or throws an exception if there is no such nested Chris@50: // declaration. Chris@50: Chris@50: private: Chris@50: inline ParsedSchema(Schema inner, const SchemaParser& parser): Schema(inner), parser(&parser) {} Chris@50: Chris@50: const SchemaParser* parser; Chris@50: friend class SchemaParser; Chris@50: }; Chris@50: Chris@50: // ======================================================================================= Chris@50: // Advanced API Chris@50: Chris@50: class SchemaFile { Chris@50: // Abstract interface representing a schema file. You can implement this yourself in order to Chris@50: // gain more control over how the compiler resolves imports and reads files. For the Chris@50: // common case of files on disk or other global filesystem-like namespaces, use Chris@50: // `SchemaFile::newDiskFile()`. Chris@50: Chris@50: public: Chris@50: class FileReader { Chris@50: public: Chris@50: virtual bool exists(kj::StringPtr path) const = 0; Chris@50: virtual kj::Array read(kj::StringPtr path) const = 0; Chris@50: }; Chris@50: Chris@50: class DiskFileReader final: public FileReader { Chris@50: // Implementation of FileReader that uses the local disk. Files are read using mmap() if Chris@50: // possible. Chris@50: Chris@50: public: Chris@50: static const DiskFileReader instance; Chris@50: Chris@50: bool exists(kj::StringPtr path) const override; Chris@50: kj::Array read(kj::StringPtr path) const override; Chris@50: }; Chris@50: Chris@50: static kj::Own newDiskFile( Chris@50: kj::StringPtr displayName, kj::StringPtr diskPath, Chris@50: kj::ArrayPtr importPath, Chris@50: const FileReader& fileReader = DiskFileReader::instance); Chris@50: // Construct a SchemaFile representing a file on disk (or located in the filesystem-like Chris@50: // namespace represented by `fileReader`). Chris@50: // Chris@50: // Parameters: Chris@50: // * `displayName`: The name that will appear in the file's schema node. Chris@50: // * `diskPath`: The path to the file on disk. Chris@50: // * `importPath`: Directories to search when resolving absolute imports within this file Chris@50: // (imports that start with a `/`). The array content must remain valid as long as the Chris@50: // SchemaFile exists (which is at least as long as the SchemaParser that parses it exists). Chris@50: // * `fileReader`: Allows you to use a filesystem other than the actual local disk. Although, Chris@50: // if you find yourself using this, it may make more sense for you to implement SchemaFile Chris@50: // yourself. Chris@50: // Chris@50: // The SchemaFile compares equal to any other SchemaFile that has exactly the same disk path, Chris@50: // after canonicalization. Chris@50: // Chris@50: // The SchemaFile will throw an exception if any errors are reported. Chris@50: Chris@50: // ----------------------------------------------------------------- Chris@50: // For more control, you can implement this interface. Chris@50: Chris@50: virtual kj::StringPtr getDisplayName() const = 0; Chris@50: // Get the file's name, as it should appear in the schema. Chris@50: Chris@50: virtual kj::Array readContent() const = 0; Chris@50: // Read the file's entire content and return it as a byte array. Chris@50: Chris@50: virtual kj::Maybe> import(kj::StringPtr path) const = 0; Chris@50: // Resolve an import, relative to this file. Chris@50: // Chris@50: // `path` is exactly what appears between quotes after the `import` keyword in the source code. Chris@50: // It is entirely up to the `SchemaFile` to decide how to map this to another file. Typically, Chris@50: // a leading '/' means that the file is an "absolute" path and is searched for in some list of Chris@50: // schema file repositories. On the other hand, a path that doesn't start with '/' is relative Chris@50: // to the importing file. Chris@50: Chris@50: virtual bool operator==(const SchemaFile& other) const = 0; Chris@50: virtual bool operator!=(const SchemaFile& other) const = 0; Chris@50: virtual size_t hashCode() const = 0; Chris@50: // Compare two SchemaFiles to see if they refer to the same underlying file. This is an Chris@50: // optimization used to avoid the need to re-parse a file to check its ID. Chris@50: Chris@50: struct SourcePos { Chris@50: uint byte; Chris@50: uint line; Chris@50: uint column; Chris@50: }; Chris@50: virtual void reportError(SourcePos start, SourcePos end, kj::StringPtr message) const = 0; Chris@50: // Report that the file contains an error at the given interval. Chris@50: Chris@50: private: Chris@50: class DiskSchemaFile; Chris@50: }; Chris@50: Chris@50: } // namespace capnp Chris@50: Chris@50: #endif // CAPNP_SCHEMA_PARSER_H_