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