Skip to content
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

#133 code parser alternative #146

Merged
merged 135 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
135 commits
Select commit Hold shift + click to select a range
6de10cb
#133 code parser
m7pr Sep 29, 2023
fe2f93b
#133 as.character -> format_expression
m7pr Sep 29, 2023
0e3e9e0
#133 as.character -> format_expression
m7pr Sep 29, 2023
c8fc389
fix code parser for character input
m7pr Oct 2, 2023
fb0dd19
print error with specific (failing) call message
gogonzo Oct 2, 2023
d3e5f38
simplify affected names detection
m7pr Oct 3, 2023
b59e860
Update R/qenv-class.R
m7pr Oct 3, 2023
d2ac886
Update R/qenv-class.R
m7pr Oct 3, 2023
b619c3b
Update R/qenv-get_code.R
m7pr Oct 3, 2023
e14814b
Update tests/testthat/test-code_dependency.R
m7pr Oct 3, 2023
0226f38
Update tests/testthat/test-code_dependency.R
m7pr Oct 3, 2023
69133ac
Update tests/testthat/test-code_dependency.R
m7pr Oct 3, 2023
307e6f8
Update tests/testthat/test-code_dependency.R
m7pr Oct 3, 2023
cd91891
Update tests/testthat/test-code_dependency.R
m7pr Oct 3, 2023
f3a0a42
Update tests/testthat/test-code_dependency.R
m7pr Oct 3, 2023
3b515dc
Update tests/testthat/test-code_dependency.R
m7pr Oct 3, 2023
0def2a8
set character as type for code in generic
m7pr Oct 3, 2023
05bc2c0
change default names to character() and put assertion
m7pr Oct 3, 2023
2c64c2d
change default value of names to character() in get_code
m7pr Oct 3, 2023
f6adc39
change return value for get_code
m7pr Oct 3, 2023
105ff27
build manual pages
m7pr Oct 3, 2023
fe288d5
one more test
m7pr Oct 3, 2023
7ed5ad1
add remove_curly_brackets for expression as an input
m7pr Oct 3, 2023
88d6699
add remove_enclosing_curly_braces for language
m7pr Oct 3, 2023
fc71138
bring back possibility to have empty qenv
m7pr Oct 3, 2023
2f665d5
Merge fc711381452800920484f2f5e6047c6d79f1a073 into 41f660b0b8ffdd543…
m7pr Oct 3, 2023
9b95997
[skip actions] Restyle files
github-actions[bot] Oct 3, 2023
57be841
rewrite old test for new qenv@code character format
m7pr Oct 3, 2023
3ce6da7
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 3, 2023
e6ebc3f
fix old tests so they apply new character format of qenv@code
m7pr Oct 3, 2023
58dfb91
update NEWS
m7pr Oct 3, 2023
33f01bd
remove examples from code_dependency
m7pr Oct 3, 2023
5d01a3b
Update tests/testthat/test-qenv_constructor.R
m7pr Oct 4, 2023
d9b9a5f
Update NEWS.md
m7pr Oct 4, 2023
5313d5d
Update NEWS.md
m7pr Oct 4, 2023
11fe04c
Update R/qenv-eval_code.R
m7pr Oct 4, 2023
5542833
update NEWS
m7pr Oct 4, 2023
d481454
Update R/qenv-eval_code.R
m7pr Oct 4, 2023
9ca4747
in consturoctor language is just expression and then expression is pa…
m7pr Oct 4, 2023
d0af36b
rename tests
m7pr Oct 4, 2023
3d4bb8f
more tests for functions usage
m7pr Oct 4, 2023
2119035
fix issue with data() calls - move to symbols and not ls(env)
m7pr Oct 4, 2023
e585ab2
Merge 2119035d19aabb3b5e44c47de67c73127bcff499 into 41f660b0b8ffdd543…
m7pr Oct 4, 2023
111d36c
[skip actions] Restyle files
github-actions[bot] Oct 4, 2023
48d3d91
trim occurence when looking for side-effects
m7pr Oct 4, 2023
7cf01bc
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 4, 2023
5bb5aae
Merge 7cf01bc9549ecbf5c4ff98f573ed40be5b1389f6 into 41f660b0b8ffdd543…
m7pr Oct 4, 2023
3d75a77
[skip actions] Restyle files
github-actions[bot] Oct 4, 2023
e4b6ba6
rewrite documentation for code_dependency()
m7pr Oct 4, 2023
915fa7a
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 4, 2023
7fb17cb
allow for multiclass input in code_dependency
m7pr Oct 4, 2023
c279f5a
Merge 7fb17cb7cdfddf64121b5cdfea7665280767cb47 into 41f660b0b8ffdd543…
m7pr Oct 4, 2023
285c051
[skip actions] Restyle files
github-actions[bot] Oct 4, 2023
17f4254
get_code ignores occurrence in function definition
m7pr Oct 4, 2023
14f841f
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 4, 2023
8519fe8
Merge 14f841fef2e0d9abe4f21981c969f86bfffd35a5 into 41f660b0b8ffdd543…
m7pr Oct 4, 2023
94f7812
[skip actions] Restyle files
github-actions[bot] Oct 4, 2023
3711c11
fix test - get_code detects occurrence of the function object
m7pr Oct 4, 2023
d2beafb
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 4, 2023
5784a50
fix tests for qenv-concat
m7pr Oct 4, 2023
34b405d
allow object names to be empty
m7pr Oct 4, 2023
b79b02f
add 2 more tests
m7pr Oct 5, 2023
ecd5dbf
Merge b79b02fce86883fe5eef8d28a7ff5381c35c734e into 41f660b0b8ffdd543…
m7pr Oct 5, 2023
97f7ec7
[skip actions] Restyle files
github-actions[bot] Oct 5, 2023
ede944e
get_code understands $ usage and do not treat rhs of $ as objects (on…
m7pr Oct 5, 2023
9a8728c
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 5, 2023
9442082
Merge 9a8728c861ff1f211bbaa8c9d8e206c6f6f1ad38 into 41f660b0b8ffdd543…
m7pr Oct 5, 2023
53eed36
[skip actions] Restyle files
github-actions[bot] Oct 5, 2023
5f93d48
test for @
m7pr Oct 5, 2023
cc1b769
remove b after the test
m7pr Oct 5, 2023
0f23c72
`format_expression` changes (#152)
chlebowa Oct 6, 2023
cb38bd9
Merge 0f23c7243107da9363fcc996476e5882bd483b59 into 1aa0ba2fa5b6dd60b…
m7pr Oct 6, 2023
ce8d27a
[skip actions] Restyle files
github-actions[bot] Oct 6, 2023
bac6699
Update tests/testthat/test-code_dependency.R
m7pr Oct 6, 2023
ebc306f
Update R/qenv-eval_code.R
m7pr Oct 6, 2023
6e6a2e9
Update R/utils-code_dependency.R
m7pr Oct 6, 2023
c47ec2e
bind -> bound
m7pr Oct 6, 2023
81987d4
rename @effect to @linksto
m7pr Oct 6, 2023
ade5d21
remove default values of parameters in return_code
m7pr Oct 6, 2023
0cd4f3d
allow to keep source in parse(text = )/expressions during R CMD CHECK
m7pr Oct 6, 2023
a41bfbc
add keep.source = TRUE to parse(text =
m7pr Oct 6, 2023
66c71c4
Merge a41bfbc0b9aa0c4accd4eb65c61e465946ff86ee into 1aa0ba2fa5b6dd60b…
m7pr Oct 6, 2023
393c904
[skip actions] Restyle files
github-actions[bot] Oct 6, 2023
7562f80
remove browser lol
m7pr Oct 6, 2023
cd87091
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 6, 2023
83232ea
Update tests/testthat/test-code_dependency.R
m7pr Oct 6, 2023
a8f0408
linktso -> linksto
m7pr Oct 6, 2023
4d20505
revert GHA worfklow config changes
m7pr Oct 6, 2023
37093cd
simplify influence detection
m7pr Oct 6, 2023
dceae49
Update R/utils-code_dependency.R
m7pr Oct 6, 2023
9b4ed1d
lapply+names -> sapply(simplify = FALSE)
m7pr Oct 6, 2023
d61ec8d
fix a unit test for within
Oct 6, 2023
c2695d7
style
Oct 6, 2023
3c88ec3
fix another unit test for within
Oct 6, 2023
d102317
extend NEWS
m7pr Oct 6, 2023
4369a34
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 6, 2023
2fee6d8
fix documentation of get_children
m7pr Oct 6, 2023
d84ea4e
properly document pd and remove pd where not needed
m7pr Oct 6, 2023
55023fd
Update R/utils-code_dependency.R
m7pr Oct 6, 2023
db3b5c8
fix lintr
m7pr Oct 6, 2023
55c0c57
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 6, 2023
173c228
update gitignore
m7pr Oct 6, 2023
a6c6711
Merge 173c2285e5c34b4044f888415647be8642057a82 into 1aa0ba2fa5b6dd60b…
m7pr Oct 6, 2023
ee9cc9a
[skip actions] Restyle files
github-actions[bot] Oct 6, 2023
5ee165b
explain parsed_code in cooccuerrence element returned by code_dependency
m7pr Oct 6, 2023
d65f5a6
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 6, 2023
4fbbb51
remove unlist from detect_symbol
m7pr Oct 6, 2023
82830e4
get_code_dependency uses code instead of qenv
m7pr Oct 6, 2023
0e5e6ea
detect if objects are used inside a function call when they are forma…
m7pr Oct 6, 2023
6c14144
Merge 0e5e6ea6445376d174a082b85478bf611997225a into 1aa0ba2fa5b6dd60b…
m7pr Oct 6, 2023
3db405a
[skip actions] Restyle files
github-actions[bot] Oct 6, 2023
7b9bb3d
change representation to slots in S4@ test for code parser
m7pr Oct 6, 2023
861943b
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 6, 2023
b0943a7
tighten lang2calls
Oct 6, 2023
2a8e7b5
Merge b0943a756af112d763a62905c9fdd2543b6562ad into 1aa0ba2fa5b6dd60b…
m7pr Oct 6, 2023
644877d
[skip actions] Restyle files
github-actions[bot] Oct 6, 2023
ab35a74
improve tests for lang2calls
Oct 6, 2023
c96af3e
improve unit test
Oct 6, 2023
3410f8e
update lang2calls
Oct 6, 2023
424f38b
update unit tests
Oct 6, 2023
af2c44c
update unit tests further
Oct 6, 2023
d6a46f7
add testthat prefix
Oct 6, 2023
193a52e
final stage of get_code_dependency
m7pr Oct 9, 2023
d7c91dc
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 9, 2023
f832b54
lintr fixes
m7pr Oct 9, 2023
27edd70
Merge f832b5453eb65052069e22b9c8a0e5bb072fa9b7 into 1aa0ba2fa5b6dd60b…
m7pr Oct 9, 2023
efbf38c
[skip actions] Restyle files
github-actions[bot] Oct 9, 2023
ed4c1a6
fix tests
m7pr Oct 9, 2023
3c0e6c9
Merge branch '133_code_parser_characters@main' of https://github.com/…
m7pr Oct 9, 2023
e1576f9
text cleanup
m7pr Oct 9, 2023
42e3a6d
make used_in_function internal
m7pr Oct 9, 2023
3bdb310
Merge 42e3a6deef22bb7320ce3a120f602417374444ec into 1aa0ba2fa5b6dd60b…
m7pr Oct 9, 2023
8de96c4
[skip actions] Restyle files
github-actions[bot] Oct 9, 2023
7f9a862
Empty-Commit
m7pr Oct 9, 2023
864cc52
R CMD NOTES
m7pr Oct 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ tmp.*
vignettes/*.R
vignettes/*.html
vignettes/*.md
tests/testthat/Rplots.pdf
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ Collate:
'qenv-show.R'
'qenv-within.R'
'teal.code-package.R'
'utils-code_dependency.R'
'utils.R'
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# teal.code 0.4.1.9001

* The `@code` field in the `qenv` class now holds `character`, not `expression`.

# teal.code 0.4.1

### Miscellaneous
Expand Down
6 changes: 3 additions & 3 deletions R/qenv-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Reproducible class with environment and code.
#' @name qenv-class
#' @rdname qenv-class
#' @slot code (`expression`) to reproduce the environment
#' @slot code (`character`) representing code necessary to reproduce the environment
#' @slot env (`environment`) environment which content was generated by the evaluation
#' of the `code` slot.
#' @slot id (`integer`) random identifier of the code element to make sure uniqueness
Expand All @@ -13,9 +13,9 @@
#' @keywords internal
setClass(
"qenv",
slots = c(env = "environment", code = "expression", id = "integer", warnings = "character", messages = "character"),
slots = c(env = "environment", code = "character", id = "integer", warnings = "character", messages = "character"),
prototype = list(
env = new.env(parent = parent.env(.GlobalEnv)), code = expression(), id = integer(0),
env = new.env(parent = parent.env(.GlobalEnv)), code = character(0), id = integer(0),
warnings = character(0), messages = character(0)
)
)
Expand Down
24 changes: 12 additions & 12 deletions R/qenv-constructor.R
m7pr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,21 @@
#'
#' @examples
#' new_qenv(env = list2env(list(a = 1)), code = quote(a <- 1))
#' new_qenv(env = list2env(list(a = 1)), code = parse(text = "a <- 1"))
#' new_qenv(env = list2env(list(a = 1)), code = parse(text = "a <- 1", keep.source = TRUE))
#' new_qenv(env = list2env(list(a = 1)), code = "a <- 1")
#'
#' @return `qenv` object.
#'
#' @export
setGeneric("new_qenv", function(env = new.env(parent = parent.env(.GlobalEnv)), code = expression()) standardGeneric("new_qenv")) # nolint
setGeneric("new_qenv", function(env = new.env(parent = parent.env(.GlobalEnv)), code = character()) standardGeneric("new_qenv")) # nolint

#' @rdname new_qenv
#' @export
setMethod(
"new_qenv",
signature = c(env = "environment", code = "expression"),
function(env, code) {
new_env <- rlang::env_clone(env, parent = parent.env(.GlobalEnv))
lockEnvironment(new_env, bindings = TRUE)
id <- sample.int(.Machine$integer.max, size = length(code))
methods::new(
"qenv",
env = new_env, code = code, warnings = rep("", length(code)), messages = rep("", length(code)), id = id
)
new_qenv(env, format_expression(code))
m7pr marked this conversation as resolved.
Show resolved Hide resolved
}
)

Expand All @@ -40,7 +34,14 @@ setMethod(
"new_qenv",
signature = c(env = "environment", code = "character"),
function(env, code) {
new_qenv(env, code = parse(text = code, keep.source = FALSE))
new_env <- rlang::env_clone(env, parent = parent.env(.GlobalEnv))
lockEnvironment(new_env, bindings = TRUE)
if (length(code) > 0) code <- paste(code, collapse = "\n")
id <- sample.int(.Machine$integer.max, size = length(code))
methods::new(
"qenv",
env = new_env, code = code, warnings = rep("", length(code)), messages = rep("", length(code)), id = id
)
}
)

Expand All @@ -50,8 +51,7 @@ setMethod(
"new_qenv",
signature = c(env = "environment", code = "language"),
function(env, code) {
code_expr <- as.expression(code)
new_qenv(env = env, code = code_expr)
new_qenv(env = env, code = format_expression(code))
}
)

Expand Down
32 changes: 18 additions & 14 deletions R/qenv-eval_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,33 @@ setGeneric("eval_code", function(object, code) standardGeneric("eval_code"))

#' @rdname eval_code
#' @export
setMethod("eval_code", signature = c("qenv", "expression"), function(object, code) {
id <- sample.int(.Machine$integer.max, size = length(code))
setMethod("eval_code", signature = c("qenv", "character"), function(object, code) {
id <- sample.int(.Machine$integer.max, size = 1)

object@id <- c(object@id, id)
object@env <- rlang::env_clone(object@env, parent = parent.env(.GlobalEnv))
code <- paste(code, collapse = "\n")
object@code <- c(object@code, code)
m7pr marked this conversation as resolved.
Show resolved Hide resolved

current_warnings <- ""
current_messages <- ""

for (code_line in code) {
# Using withCallingHandlers to capture ALL warnings and messages.
# Using tryCatch to capture the FIRST error and abort further evaluation.
parsed_code <- parse(text = code, keep.source = TRUE)
for (single_call in parsed_code) {
# Using withCallingHandlers to capture warnings and messages.
# Using tryCatch to capture the error and abort further evaluation.
x <- withCallingHandlers(
tryCatch(
{
eval(code_line, envir = object@env)
eval(single_call, envir = object@env)
NULL
},
error = function(e) {
errorCondition(
message = sprintf(
"%s \n when evaluating qenv code:\n%s",
.ansi_strip(conditionMessage(e)),
paste(format_expression(code), collapse = "\n")
deparse1(single_call)
),
class = c("qenv.error", "try-error", "simpleError"),
trace = object@code
Expand All @@ -61,28 +63,30 @@ setMethod("eval_code", signature = c("qenv", "expression"), function(object, cod
invokeRestart("muffleMessage")
}
)

if (!is.null(x)) {
return(x)
}

object@warnings <- c(object@warnings, current_warnings)
object@messages <- c(object@messages, current_messages)
}


object@warnings <- c(object@warnings, current_warnings)
object@messages <- c(object@messages, current_messages)

lockEnvironment(object@env, bindings = TRUE)
object
})

#' @rdname eval_code
#' @export
setMethod("eval_code", signature = c("qenv", "language"), function(object, code) {
code_char <- as.expression(code)
eval_code(object, code_char)
eval_code(object, code = format_expression(code))
})

#' @rdname eval_code
#' @export
setMethod("eval_code", signature = c("qenv", "character"), function(object, code) {
eval_code(object, code = parse(text = code, keep.source = FALSE))
setMethod("eval_code", signature = c("qenv", "expression"), function(object, code) {
eval_code(object, code = format_expression(code))
m7pr marked this conversation as resolved.
Show resolved Hide resolved
})

#' @rdname eval_code
Expand Down
4 changes: 2 additions & 2 deletions R/qenv-get_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ setGeneric("get_code", function(object, deparse = TRUE) {
setMethod("get_code", signature = "qenv", function(object, deparse = TRUE) {
checkmate::assert_flag(deparse)
if (deparse) {
format_expression(object@code)
} else {
object@code
} else {
parse(text = object@code, keep.source = TRUE)
}
})

Expand Down
Loading