-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
192 Improve get_code_dependency
(Code Parser)
#201
Conversation
Pushed a small alternative 3224a48 but this is not finished yet |
Hey @insightsengineering/nest-core-dev I prepared curated version of Code Parser. I would appreciate your review! If anybody is willing to dive deeper into the code I think the biggest help that I need is simplifying the Lastly I did not decide to export |
Co-authored-by: Aleksander Chlebowski <[email protected]> Signed-off-by: Marcin <[email protected]>
Co-authored-by: Aleksander Chlebowski <[email protected]> Signed-off-by: Marcin <[email protected]>
…ightsengineering/teal.data into 192_improve_code_parser@main
Signed-off-by: Marcin <[email protected]>
…ightsengineering/teal.data into 192_improve_code_parser@main
Closes #192
Overview
The Code Parser feature accepts code in text form and outputs all necessary code to recreate any object from the original input.
If the code contains side effects that don't precisely specify the influenced object(s), users can add a comment with the tag
# @linksto object_name1 object_name2
to explicitly identify the affected objects. This ensures that the side effects are included when generating the code needed to create the objects. This is also why the input needs to be acharacter
, notexpression
orlanguage
because comments are preserved incharacter
and are removed inexpression
andlanguage
.Historical Background
CodeDepends
package was evaluated to check if it provided the features required to accomplish our goals (#238).CodeDepends
was found to have excessive dependencies and was deemed more than what we needed.CodeDepends
and utilizesutils::getParseData
.utils::getParseData
works on asrcref
data.frame structure,srcref
is created by callingattr(parse(text = code, keep.source = TRUE), 'srcref')
code
is limited to character orexpression
withsrcref
attribute.teal.code
(PR #146), designed to take theqenv
class as input.teal_data
with code inteal_data@code
slot, leading to the migration of this functionality to theteal.data
(PR #194).Implementation Plan
Code Parser consists of:
code_dependency
/code_graph
function constructs the structure of dependencies between objects and their occurrences in specific calls derived from the code.get_code_dependency
/get_object_code
function take a code graph as input and the names of objects existing in the code. It returns the code, including all necessary dependencies, to recreate the specified object and its influencers.Pseudo code / algorithm
Code Graph
code
as an input (acharacter
or anexpression
withsrcref
attribute.utils::getParseData
to extract information about the parsed code with built-in functions.utils::getParseData
creates adata.frame
structure (pd
) enumerating each call, and enumerating each object/symbol within calls. Each object/symbol has atoken
metadata specifying how it's treated by R (e.g.,SYMBOL
,ASSIGNMENT OPERATOR
,FUNCTION_CALL
,SYMBOL_FORMALS
etc).pd
, we are able to bind all elements of all calls of the input code into a list. The list has a length equal to the number of calls (calls_pd
).token
) so that we seek for"SYMBOL", "SYMBOL_FUNCTION_CALL"
and grep forASSIGN
operators to understand which object is influenced by other objects in this call.COMMENT
tokens that contain@linksto
tag to understand whether some calls should be assigned as influencers of other objects.Graph Parser
Having the Code Graph we take an object name (of multiple
names
) and wecalls_pd
of all calls until call X (let's call itcalls_pd_x
)calls_pd_x
calls_pd_x
until all considered objects no longer have influencersNotes
<-
,=
, or->
assignment operators. No other object creation methods (such asassign
,<<-
, or any non-standard-evaluation method) are supported. This is addressed by using the# @linktso
tag.# @linktso
tag is meant for it.# @linktso
tag at the end of the line where the side effect is created.