[pkg/ottl] Add grammar utility to extract paths from statements #35174
+831
−81
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This PR is part of #29017, and adds a grammar utility to allow extracting
ottl.path
from parsed statements and expressions. The new functions will power the context inferrer utility (see draft), which implementation will basically gather all declared statement'sPath.Context
, and choose the highest priority one.For the statements rewriter utility purpose, it changes the
grammar.go
file including a new fieldPos lexer.Position
into theottl.path
struct. This value is automatically set by theparticiple
lexer and holds the path's offsets, being useful for identifying their positions in the raw statement, without using regexes an being coupled to the grammar's definition.Additional changes:
This proposal uses the visitor pattern to gather all path's from the parsed statements. Considering the grammar's custom error mechanism (
checkForCustomError
) also works with a similar pattern, I've added the visitor implementation as part of the grammar objects, and refactored allcheckForCustomError
functions to be part of a validator visitor. Differently of the current implementation, it joins and displays all errors at once instead of one by one, IMO, it's specially useful for statements with more than one error, for example:set(name, int(2), float(1))
.* We can change it back do be error-by-error if necessary.
* If modifying the custom validator is not desired, I can roll those changes back keeping only the necessary code to extract the path's +
Pos
field.Link to tracking Issue: #29017
Testing: Unit tests were added
Documentation: No changes