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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
{#######################################################################
# accessors.R
# This file is part of the R package `parsetools`.
#
# Author: Andrew Redd
# Copyright: 2017 The R Consortium
#
# 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/.
#
}#######################################################################
#' @include internal.R
#' @name internal
#' @title Internal Functions
#' @param pd the parse data.
#' @param id the ID of the expression
#' @param line a line number
#' @description These functions are for internal use but are documented
#' here for reference.
NULL
#@internal
token <- function(id=pd$id, pd=get('pd', parent.frame())){
#' @describeIn internal Extract the token
pd[match(id, pd$id), 'token']
}
if(FALSE){#!@testing
pd <- get_parse_data(parse(text={"
x <- rnorm(10, 0, 1)
y <- runif(10)
plot(x, y)
"}, keep.source=TRUE))
expect_equal(token(), pd$token)
ids <- pd$id[match(c('rnorm', 'x', '<-'), pd$text)]
expect_equal( token(ids, pd)
, c("SYMBOL_FUNCTION_CALL", "SYMBOL", "LEFT_ASSIGN"))
}
#@internal
text <- function(id=pd$id, pd=get('pd', parent.frame())){
#' @describeIn internal Extract the text
pd[match(id, pd$id), 'text']
}
if(FALSE){#!@testing
pd <- get_parse_data(parse(text={"
x <- rnorm(10, 0, 1)
y <- runif(10)
plot(x, y)
"}, keep.source=TRUE))
text <- c('rnorm', 'x', '<-')
ids <- pd$id[match(c('rnorm', 'x', '<-'), pd$text)]
expect_equal(text(pd$id, pd), pd$text)
expect_equal(text(ids), text)
expect_equal(text(ids, pd), text)
}
#@internal
nodes <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal Extract only the specified node(s).
pd[match(id, pd$id), ]
}
if(FALSE){#!@testing
pd <- get_parse_data(parse(text={"
x <- rnorm(10, 0, 1)
y <- runif(10)
plot(x, y)
"}, keep.source=TRUE))
expect_equal(nodes(pd$id, pd), pd)
expect_equal(nodes(pd$id), pd)
expect_equal(nodes(c(45,3, 58), pd), pd[c('45', '3', '58'), ])
}
#@internal
start_line <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal Get the line the expression starts on.
pd[match(id, pd$id), 'line1']
}
#@internal
start_col <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal Get the column the expression starts on.
pd[match(id, pd$id), 'col1']
}
#@internal
end_line <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal Get the line the expression ends on.
pd[match(id, pd$id), 'line2']
}
#@internal
end_col <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal Get the column the expression ends on.
pd[match(id, pd$id), 'col2']
}
#@internal
filename <- function(pd=get('pd', parent.frame())){
#' @describeIn internal Extract the filename if available, otherwise return "<UNKNOWN>".
src <- attr(pd, 'srcfile')
if (!is.null(src)) src$filename else "<UNKNOWN>"
}
if(FALSE){#@test
pd <- get_parse_data(parse(text="1+1"))
expect_identical(filename(pd), "<text>")
attr(pd, 'srcfile') <- NULL
expect_identical(filename(pd), "<UNKNOWN>")
}
#@internal
lines <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal Extract the lines of text.
text <- utils::getParseText(pd, id)
unlist(strsplit(text, '\n', fixed=TRUE))
}
#@internal
is_terminal <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal does id represent a terminal node.
pd[match(id, pd$id), 'terminal']
}
#@internal
is_first_on_line <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal is an expression the first one on a line?
c(T, utils::head(pd$line2, -1) != utils::tail(pd$line1, -1)) [match(id, pd$id)]
}
if(FALSE){#@testing
pd <- get_parse_data(parse(text="'
' -> a.multiline.string", keep.source=TRUE))
expect_true (is_first_on_line(1))
expect_false(is_first_on_line(2))
pd <- get_parse_data(parse(text={
"function(x, y){
x+
y+
1
}
"}, keep.source=TRUE))
}
#@internal
is_last_on_line <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal Is expression the last terminal node on the line?
if (!is_terminal(id, pd)) return(FALSE)
max(pd[pd$line2 == end_line(id, pd), 'col2']) == end_col(id, pd)
}
if(FALSE){#@testing
pd <- get_parse_data(parse(text="'
' -> a.multiline.string", keep.source=TRUE))
expect_false(is_last_on_line(1, pd))
expect_true(is_last_on_line(4, pd))
expect_false(is_last_on_line(6, pd))
}
#@internal
spans_multiple_lines <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal does the expression span multiple lines?
start_line(id) != end_line(id)
}
if(FALSE){#@testing
pd <- get_parse_data(parse(text="'
' -> a.multiline.string", keep.source=TRUE))
expect_true(spans_multiple_lines(1, pd))
expect_false(spans_multiple_lines(4, pd))
expect_true(spans_multiple_lines(pd_all_root_ids(pd), pd))
}
#@internal
terminal_ids_on_line <- function(line, pd=get('pd', parent.frame())){
#' @describeIn internal Get the ids on a given line that are terminal nodes.
pd$id[pd$line1 <= line & pd$line2 >= line & pd$terminal]
}
if(F){#@testing
pd <- get_parse_data(parse(text=" {
{1 + 3}
{2 + sin(pi)}
}
", keep.source=TRUE))
expect_equal(terminal_ids_on_line(1), 1)
expect_equal(text(terminal_ids_on_line(2)), c('{', '1', '+', '3', '}'))
pd <- get_parse_data(parse(text="'
' -> a.multiline.string", keep.source=TRUE))
expect_equal(text(terminal_ids_on_line(1, pd)), "'\n\n'")
expect_equal(terminal_ids_on_line(2, pd), 1)
expect_equal(terminal_ids_on_line(4, pd), integer(0))
}
#@internal
ids_starting_on_line <- function(line, pd=get('pd', parent.frame())){
#' @describeIn internal Get ids for nodes that start on the given line
pd$id[pd$line1 == line]
}
#@internal
ids_ending_on_line <- function(line, pd=get('pd', parent.frame())){
#' @describeIn internal Get ids for nodes that end on the given line
pd$id[pd$line2 == line]
}
if(FALSE){#@testing
pd <- get_parse_data(parse(text={"((1+
2)+
3)+
4"}, keep.source=TRUE))
expect_identical(ids_starting_on_line(1), head(pd$id, 10))
expect_identical(ids_starting_on_line(4), tail(pd$id, 2))
expect_identical(ids_ending_on_line(1), 1:5)
expect_identical(ids_ending_on_line(4), c(26L, 23L, 24L))
}
#@internal
prev_terminal <- function(id=pd$id, pd=get('pd', parent.frame())){
#' @describeIn internal Get the id for the terminal expression that is immediately prior to the one given.
if (length(id)>1) return (sapply(id, prev_terminal, pd=pd))
ix <- which( pd$line1 <= start_line(id)
& pd$col1 < start_col(id)
& pd$terminal
)
if (!any(ix)) return (NA_integer_)
pd$id[max(ix)]
}
if(FALSE){#@testing
pd <- get_parse_data(parse(text=" rnorm( 10, 0, 3)", keep.source=TRUE))
ids <- pd$id[match(c('10', '(', 'rnorm'), pd$text)]
id <- ids[[1]]
expect_equal( prev_terminal(ids[[1]], pd), ids[[2]])
expect_equal( prev_terminal(ids[[2]], pd), ids[[3]])
expect_equal( prev_terminal(ids[[3]], pd), NA_integer_)
expect_equal( prev_terminal(ids, pd=pd)
, c(utils::tail(ids, -1), NA_integer_)
)
}
#@internal
expr_text <- function(id, pd=get('pd', parent.frame())){
#' @describeIn internal
#' If id represents an `expr` token reiterate on the firstborn.
#' Throws an error if anything but an expression or text if found.
if (length(id)>1L) return(sapply(id, expr_text, pd=pd))
while (token(id) == 'expr' && n_children(id) == 1L)
id <- firstborn(id)
if (token(id) != 'STR_CONST')
col_error(id, "a string constant is expected.")
unquote(text(id))
}
if(FALSE){#@testing
pd <- get_parse_data(parse(text="
signature(x='hello', y='world')
", keep.source=TRUE))
ids <- c( parent(.find_text("'hello'"))
, parent(.find_text("'world'"))
)
expect_identical(expr_text(ids, pd), c("hello", "world"))
expect_error( expr_text(pd_all_root_ids(pd))
, "<text>:2:9: a string constant is expected."
)
}
#' @rdname accessors
#' @title Accessor functions
#'
#' @param pd the parse data.
#' @param id the ID of the expression
#' @description
#' This collection of function can be used to easily access elements of
#' the parse data information.
#'
#' @aliases pd_text pd_token pd_start_line pd_end_line pd_filename pd_start_col pd_end_col
pd_text <- external(text)
pd_token <- external(token)
pd_start_line <- external(start_line)
pd_end_line <- external(end_line)
pd_filename <- external(filename)
pd_start_col <- external(start_col)
pd_end_col <- external(end_col)
|