slang::parsing::Trivia class

The Trivia class holds on to a piece of source text that should otherwise not turn into a token; for example, a preprocessor directive, a line continuation character, or a comment.

Public functions

auto withLocation(BumpAllocator& alloc, SourceLocation location) const -> Trivia
auto getExplicitLocation() const -> std::optional<SourceLocation>
auto syntax() const -> syntax::SyntaxNode*
auto getRawText() const -> std::string_view
Get the raw text of the trivia, if any.
auto getSkippedTokens() const -> std::span<Token const>

Function documentation

Trivia slang::parsing::Trivia::withLocation(BumpAllocator& alloc, SourceLocation location) const

If the trivia is raw source text, creates a new trivia with the specified location (instead of implicitly offset from the parent token). If this trivia is for a directive or skipped tokens, returns a copy without modification.

std::optional<SourceLocation> slang::parsing::Trivia::getExplicitLocation() const

Gets the source location of the trivia if one is explicitly known. If not, nullopt is returned to signify that the location is implicitly relative to the parent token.

syntax::SyntaxNode* slang::parsing::Trivia::syntax() const

If this trivia is tracking a skipped syntax node or a directive, returns that node. Otherwise returns nullptr.

std::span<Token const> slang::parsing::Trivia::getSkippedTokens() const

If the trivia represents skipped tokens, returns the list of tokens that were skipped. Otherwise returns an empty span.