slang::SyntaxTree class

The SyntaxTree is the easiest way to interface with the lexer / preprocessor / parser stack. Give it some source text and it produces a parse tree.

The SyntaxTree object owns all of the memory for the parse tree, so it must live for as long as you need to access its syntax nodes.

Public static functions

static auto fromFile(string_view path) -> std::shared_ptr<SyntaxTree>
static auto fromText(string_view text, string_view name = "") -> std::shared_ptr<SyntaxTree>
static auto fromFile(string_view path, SourceManager& sourceManager, const Bag& options = {}) -> std::shared_ptr<SyntaxTree>
static auto fromText(string_view text, SourceManager& sourceManager, string_view name = "", const Bag& options = {}) -> std::shared_ptr<SyntaxTree>
static auto fromBuffer(const SourceBuffer& buffer, SourceManager& sourceManager, const Bag& options = {}) -> std::shared_ptr<SyntaxTree>
static auto fromBuffers(span<const SourceBuffer> buffers, SourceManager& sourceManager, const Bag& options = {}) -> std::shared_ptr<SyntaxTree>
static auto getDefaultSourceManager() -> SourceManager&

Public functions

auto diagnostics() -> Diagnostics&
Gets any diagnostics generated while parsing.
auto allocator() -> BumpAllocator&
Gets the allocator containing the memory for the parse tree.
auto sourceManager() -> SourceManager&
Gets the source manager used to build the syntax tree.
auto sourceManager() const -> const SourceManager&
Gets the source manager used to build the syntax tree.
auto root() -> SyntaxNode&
Gets the root of the syntax tree.
auto root() const -> const SyntaxNode&
Gets the root of the syntax tree.
auto getEOFToken() const -> Token
auto options() const -> const Bag&
The options used to construct the syntax tree.
auto getParentTree() const -> const SyntaxTree*
auto getMetadataMap() const -> const Parser::MetadataMap&
auto getGlobalInstantiations() const -> const Parser::NameSet&

Function documentation

static std::shared_ptr<SyntaxTree> slang::SyntaxTree::fromFile(string_view path)

Returns the created and parsed syntax tree.

Creates a syntax tree from a full compilation unit. path is the path to the source file on disk.

static std::shared_ptr<SyntaxTree> slang::SyntaxTree::fromText(string_view text, string_view name = "")

Returns the created and parsed syntax tree.

Creates a syntax tree by guessing at what might be in the given source snippet. text is the actual source code text. name is an optional name to give to the loaded source buffer.

static std::shared_ptr<SyntaxTree> slang::SyntaxTree::fromFile(string_view path, SourceManager& sourceManager, const Bag& options = {})

Returns the created and parsed syntax tree.

Creates a syntax tree from a full compilation unit. path is the path to the source file on disk. sourceManager is the manager that owns all of the loaded source code. options is an optional bag of lexer, preprocessor, and parser options.

static std::shared_ptr<SyntaxTree> slang::SyntaxTree::fromText(string_view text, SourceManager& sourceManager, string_view name = "", const Bag& options = {})

Returns the created and parsed syntax tree.

Creates a syntax tree by guessing at what might be in the given source snippet. text is the actual source code text. sourceManager is the manager that owns all of the loaded source code. name is an optional name to give to the loaded source buffer. options is an optional bag of lexer, preprocessor, and parser options.

static std::shared_ptr<SyntaxTree> slang::SyntaxTree::fromBuffer(const SourceBuffer& buffer, SourceManager& sourceManager, const Bag& options = {})

Returns the created and parsed syntax tree.

Creates a syntax tree from an already loaded source buffer. buffer is the loaded source buffer. sourceManager is the manager that owns the buffer. options is an optional bag of lexer, preprocessor, and parser options.

static std::shared_ptr<SyntaxTree> slang::SyntaxTree::fromBuffers(span<const SourceBuffer> buffers, SourceManager& sourceManager, const Bag& options = {})

Returns the created and parsed syntax tree.

Creates a syntax tree by concatenating several loaded source buffers. buffers is the list of buffers that should be concatenated to form the compilation unit to parse. sourceManager is the manager that owns the buffers. options is an optional bag of lexer, preprocessor, and parser options.

static SourceManager& slang::SyntaxTree::getDefaultSourceManager()

This is a shared default source manager for cases where the user doesn't care about managing the lifetime of loaded source. Note that all of the source loaded by this thing will live in memory for the lifetime of the process.

Token slang::SyntaxTree::getEOFToken() const

Gets the EndOfFile token marking the end of the input source text. This is useful if, for example, the tree doesn't represent a whole compilation unit and you still want to see the trailing trivia.

const SyntaxTree* slang::SyntaxTree::getParentTree() const

Gets the parent syntax tree, if there is one. Otherwise returns nullptr. Most syntax trees don't have a parent; this is for cases where a given tree is derived from another and relies on the parent tree's memory remaining valid for the lifetime of the child tree.

const Parser::MetadataMap& slang::SyntaxTree::getMetadataMap() const

Gets metadata that was in effect when various syntax nodes were parsed (this includes bits of preprocessor state like the current timescale or default nettype).

const Parser::NameSet& slang::SyntaxTree::getGlobalInstantiations() const

Gets a set of names of all instantiations of global modules/interfaces/programs. This can be used to determine which modules should be considered as top-level roots of the design.