diff --git a/include/slang/syntax/SyntaxNode.h b/include/slang/syntax/SyntaxNode.h index 32a6bbd82..c3d052ee4 100644 --- a/include/slang/syntax/SyntaxNode.h +++ b/include/slang/syntax/SyntaxNode.h @@ -129,6 +129,11 @@ class SLANG_EXPORT SyntaxNode { /// an empty Token. Token childToken(size_t index) const; + /// Gets a pointer to the child token at the specified index. If the + /// child at the given index is not a token (probably a node) then + /// this returns null. + Token* childTokenPtr(size_t index); + /// Gets the number of (direct) children underneath this node in the tree. size_t getChildCount() const; // Note: implemented in AllSyntax.cpp diff --git a/source/syntax/SyntaxNode.cpp b/source/syntax/SyntaxNode.cpp index e6ead4d4f..8e3d8c7be 100644 --- a/source/syntax/SyntaxNode.cpp +++ b/source/syntax/SyntaxNode.cpp @@ -169,6 +169,13 @@ parsing::Token SyntaxNode::childToken(size_t index) const { return child.token(); } +parsing::Token* SyntaxNode::childTokenPtr(size_t index) { + auto child = getChildPtr(index); + if (!child.isToken()) + return nullptr; + return child.token(); +} + bool SyntaxNode::isEquivalentTo(const SyntaxNode& other) const { size_t childCount = getChildCount(); if (kind != other.kind || childCount != other.getChildCount())