Including non-terminals in non-default tree constructed nodes' offsets

SyntaxEditor for WPF Forum

Posted 11 months ago by Will Gauthier
Version: 22.1.0
Avatar

How can I make sure an ast node coming from a non-default tree constructor has offsets that include non-terminal keywords when said keywords are otherwise discarded?

For example, this is one of the alternations for a statement production:

statement.Production = @doKeyword + block["block"] + @endKeyword > Ast<ExplicitBlockStatement>().SetProperty(ebs => ebs.ExplicitBlock, AstFrom("block"))

The do and end keywords don't matter for the ExplicitBlockStatement node, but I need the node's offsets to encompass their source characters for comparison purposes later.

Similarly, I have the same question for non-built-in tree constructors, like in this production:

statement.Production = @functionKeyword + functionName["funcName"] + functionBody["funcBody"] > new FuncDefStatTreeNodeConstructor()

Thanks in advance.

Comments (2)

Answer - Posted 11 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Will,

I would expect the ExplicitBlockStatement AST node from the first production to include the start offset of the first match (the @doKeyword) and the end offset of the last match (the @endKeyword).  I did a test here and it seemed to be including the offsets of the keywords surrounding another non-terminal reference.  Are you not seeing that?

For a custom tree constructor, it's up to you to set the resulting IAstNode.StartOffset and EndOffset.  The IAstNodeMatchCollection passed into the CreateNode method has StartOffset and EndOffset properties that give the range of offsets matched.  In the last sample, that range should encompass the @functionKeyword and functionBody.  You would then want to set those offsets to the IAstNode created.


Actipro Software Support

Posted 11 months ago by Will Gauthier
Avatar

Thank you for the response.

I double-checked and confirmed that you are correct: the ExplicitBlockStatement node's offsets do include the do and end keywords. I must have misremembered which node types were giving me problems.

Knowing that the IAstNodeMatchCollection stores the StartOffset and EndOffset of everything matched was the missing piece I needed for my custom tree constructors. Thanks!

The latest build of this product (v24.1.1) was released 4 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.