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