1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
{#######################################################################
# pd_function.R
# This file is part of the R package `parsetools`.
#
# Author: Andrew Redd
# Copyright: 2017 University of Utah
#
# LICENSE
# ========
# The R package `parsetools` is free software:
# you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software
# Foundation, either version 2 of the License, or (at your option)
# any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
}#######################################################################
#' @name function-nodes
#' @title Function Nodes
#' @description
#' These function help identify and navigate noses associated with
#' function definition.
#'
#' @details
#' A function node is the node for the expression that has as it's
#' children the function keyword(firstborn), the arguments, including the
#' nodes representing the opening closing parentheses in the definition,
#' and finally a node, as the youngest, for the body of the function.
#'
#' @inheritParams pd_is_assignment
#' @example inst/examples/example-pd.R
#' @example inst/examples/example-roots.R
#' @example inst/examples/example-function.R
NULL
#' @describeIn function-nodes Test if the \code{id} points to a function.
pd_is_function <-
function( id, pd, .check=TRUE){
if(.check){
pd <- ._check_parse_data(pd)
id <- ._check_id(id, pd)
}
!is.na(firstborn(id)) & token(firstborn(id)) == 'FUNCTION'
}
all_function_ids <- make_get_all(pd_is_function)
is_function <- internal(pd_is_function)
if(F){#! @testthat pd_is_function
pd <- get_parse_data(parse(text="function(){}", keep.source=TRUE))
expect_true(pd_is_function(roots(pd), pd))
pd <- get_parse_data(parse(text="fun <- function(){}", keep.source=TRUE))
expect_false(pd_is_function(roots(pd), pd))
expect_length(all_function_ids(pd), 1L)
}
#' @describeIn function-nodes test if a node is contained in a function definition.
pd_is_in_function <-
function( id, pd, .check=TRUE){
if(.check){
pd <- ._check_parse_data(pd)
id <- ._check_id(id, pd)
}
if (length(id)>1L) return(sapply(id, pd_is_in_function, pd=pd, .check=FALSE))
any(is_function(ancestors(id)))
}
is_in_function <- internal(pd_is_in_function)
if(FALSE){#@testing
ex.file <- system.file("examples", "example.R", package="parsetools")
exprs <- parse(ex.file, keep.source = TRUE)
pd <- get_parse_data(exprs)
id <- .find_text('"Congratulations!"')
expect_true(pd_is_in_function(id, pd))
id <- .find_text('"myClass"')
expect_identical(is_in_function(id), c(FALSE, FALSE))
}
#' @describeIn function-nodes Obtain the body of a function
pd_get_function_body_id <-
function( id, pd, .check=TRUE){
if(.check){
pd <- ._check_parse_data(pd)
id <- ._check_id(id, pd)
stopifnot(all(is_function(id, pd)))
}
if (length(id)>1L) return(sapply(id, pd_get_function_body_id, pd=pd))
max(children(id, pd))
}
function_body <- internal(pd_get_function_body_id, all_function_ids(pd))
if(F){#@testing
pd <- get_parse_data(parse(text="hello_world <- function(){
print('hello world')
}
", keep.source=TRUE))
id <- all_function_ids(pd)
expect_equal(pd_get_function_body_id(id, pd), parent(.find_text('{')))
pd <- get_parse_data(parse(text='function(l,r)paste(l,r)', keep.source=TRUE))
expect_identical( pd_get_function_body_id(all_function_ids(pd), pd=pd)
, parent(parent(.find_text('paste')), pd)
)
}
if(FALSE){#@testing function_body vectorizing
pd <- get_parse_data(parse(text="
hello_world <- function(){
print('hello world')
}
goodby_earth <- function(){
print('goodby earth')
}
", keep.source=TRUE))
id <- all_function_ids(pd)
expect_equal(pd_get_function_body_id(id, pd), parent(.find_text('{')))
}
#' @describeIn function-nodes Obtain the ids for the arguments of a function
pd_get_function_arg_ids <-
function( id, pd, .check=TRUE){
if(.check){
pd <- ._check_parse_data(pd)
id <- ._check_id(id, pd)
stopifnot( length(id) == 1L
, pd_is_function(id, pd)
)
}
utils::tail(utils::head(children(id=id, pd=pd), -1), -1)
}
function_args <- internal(pd_get_function_arg_ids, all_function_ids(pd))
if(F){#@testing
pd <- get_parse_data(parse(text='pd_get_function_arg_ids <-
function( pd #< parse data
, id = pd_all_root_ids(pd) #< id number
){}', keep.source=TRUE))
id <- all_function_ids(pd)
expect_identical( text(pd_get_function_arg_ids(id, pd), pd=pd)
, c('(', 'pd', '#< parse data', ','
, 'id', '=', '', '#< id number', ')'
)
)
}
#' @describeIn function-nodes Retrieve the variable for a function argument
pd_get_function_arg_variable_ids <-
function( id, pd, .check = TRUE){
if(.check){
pd <- ._check_parse_data(pd)
id <- ._check_id(id, pd)
stopifnot( length(id) == 1L
, pd_is_function(id, pd)
)
}
arg.ids <- function_args(id, pd)
arg.ids[token(arg.ids, pd=pd) == 'SYMBOL_FORMALS']
}
function_arg_variables <- internal(pd_get_function_arg_variable_ids, all_function_ids(pd))
if(F){#@testing
pd <- get_parse_data(parse(text='pd_get_function_arg_ids <-
function( pd #< parse data
, id = pd_all_root_ids(pd) #< id number
){}', keep.source=TRUE))
id <- assign_value(all_assignment_ids(pd))
expected <- pd[pd$parent==id & pd$text %in% c('pd', 'id'), 'id']
expect_identical(pd_get_function_arg_variable_ids(id, pd), expected)
expect_error(pd_get_function_arg_variable_ids(roots(pd), pd))
}
#' @describeIn function-nodes Get the variable names for a function definition.
pd_get_function_arg_variable_text <-
function(id, pd, .check=TRUE){
text(pd_get_function_arg_variable_ids(id=id, pd=pd, .check=.check))
}
if(FALSE){#@testing
pd <- get_parse_data(parse(text='
function( a, b = 1){
cat("hello world")
}', keep.source=TRUE))
id <- roots(pd)
expect_identical( pd_get_function_arg_variable_text(id, pd)
, c("a", "b")
)
}
#' @describeIn function-nodes is `id` a function argument?
pd_is_function_arg <-
function(id, pd, .check=TRUE){
if(.check){
pd <- ._check_parse_data(pd)
id <- ._check_id(id, pd)
}
is_function(parent(id)) &
(
token(id) %in% c('SYMBOL_FORMALS', 'EQ_FORMALS') |
(
token(id) == 'expr' &
!is.na(next_sibling(id)) & token(next_sibling(id)) != "'}'" &
!is.na(prev_sibling(id)) & token(prev_sibling(id)) != "'{'"
)
)
}
is_function_arg <- internal(pd_is_function_arg)
if(F){#@testing
pd <- get_parse_data(parse(text='
function( a, b = 1){
cat("hello world")
}', keep.source=TRUE))
id <- .find_text('a')
expect_true(pd_is_function_arg(id, pd))
expect_false(pd_is_function_arg(.find_text('"hello world"'), pd))
expect_length(is_function_arg(pd$id, pd), nrow(pd))
expect_equal(sum(is_function_arg(pd$id, pd)), 4)
}
#' @describeIn function-nodes Retrieve relative documentation comments associated with function arguments.
pd_get_function_arg_associated_comment_ids <-
function( id, pd, .check = TRUE){
if (.check){
pd <- ._check_parse_data(pd)
id <- ._check_id(id, pd)
stopifnot( length(id) == 1L
, pd_is_function_arg(id, pd)
)
}
stopifnot(length(id)==1)
sibling.args <- function_arg_variables(parent(id, pd), pd)
all.siblings <- siblings(id, pd)
comments <- intersect(all_relative_comment_ids(pd), all.siblings)
comments[relative_comment_associateds(comments) == id]
}
function_arg_associated_comments <- internal(pd_get_function_arg_associated_comment_ids)
if(F){#@testing
pd <- get_parse_data(parse(text='pd_get_function_arg_ids <-
function( pd #< parse data
#< continuation comment
, id = pd_all_root_ids(pd)
){}', keep.source=TRUE))
function.id <- assign_value(all_assignment_ids(pd), pd)
arg.ids <- function_arg_variables(function.id, pd)
id <- arg.ids[[1]]
expect_identical(text(pd_get_function_arg_associated_comment_ids(id, pd), pd=pd)
, c('#< parse data', '#< continuation comment'))
expect_length(pd_get_function_arg_associated_comment_ids(arg.ids[[2]], pd), 0)
}
|