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
|
{#######################################################################
# grouping.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/.
#
}#######################################################################
pd_is_grouping <-
function( id, pd, .check=TRUE){
#' @title test if an id is a grouping element
#' @inheritParams pd_get_children_ids
#'
if (.check){
pd <- ._check_parse_data(pd)
id <- ._check_id(id, pd)
}
if(length(id) > 1) return(sapply(id, pd_is_grouping, pd=pd, .check=FALSE))
#' @description
#' A grouping is defined as a non empty set
return( length(children(id))
#' starting with a curly brace token and
&& token(firstborn(id)) == "'{'"
#' and for which there is no parent or the parent is also a grouping.
&& ( parent(id) == 0
|| pd_is_grouping(parent(id), pd, .check=FALSE)
)
)
#! @return a logical indicating if the root node(s) is a grouping node or not
}
is_grouping <- internal(pd_is_grouping)
if(FALSE){#@testing
pd <- get_parse_data(parse(text='{
this(is+a-grouping)
}', keep.source=TRUE))
id <- pd[match("'{'", pd$token), 'id']
gid <- parent(id)
expect_true (pd_is_grouping(gid, pd))
expect_false(pd_is_grouping( 1L, pd))
expect_is(pd_is_grouping(pd$id, pd=pd), 'logical')
expect_equal(sum(pd_is_grouping(pd$id, pd=pd)), 1)
expect_equal(sum(pd_is_grouping(pd$id, pd=pd)), 1L)
pd <- get_parse_data(parse(text='
{# first Group
{# nested group
"expression in double nested group"
}
}
', keep.source=TRUE))
expect_equal(sum(pd_is_grouping(pd$id, pd=pd)), 2)
}
#' @title get the grouping ids
#' @inheritParams pd_get_children_ids
#' @description get the ids that represent the grouping nodes.
#' @return an integer vector of ids.
all_grouping_ids <- make_get_all(pd_is_grouping)
if(FALSE){#@testing
pd <- get_parse_data(parse(text='{
this(is+a-grouping)
}', keep.source=TRUE))
expect_is(all_grouping_ids(pd), 'integer')
expect_equal(length(all_grouping_ids(pd)), 1)
expect_equal(all_grouping_ids(pd), pd[match("'{'", pd$token), 'parent'])
}
fix_grouping_comment_association <-
function( id = all_grouping_ids(pd)
, pd = get('pd', parent.frame())
, .check=TRUE
){
if (.check){
pd <- ._check_parse_data(pd)
id <- ._check_id(id, pd)
stopifnot(all(pd_is_grouping(id, pd)))
}
for (i in id) {
cids <- children(i, pd)
for (cid in cids)
if (is_comment(cid, pd)) {
n <- next_sibling(cid)
while (!is.na(n) && (is_comment(n,pd) || token(n) == "'}'"))
n <- next_sibling(n)
if (!is.na(n)) {
if (is_root(n, pd))
pd[ pd$id == cid, 'parent'] <- -n
else
pd[ pd$id == cid, 'parent'] <- -ascend_to_root(n, pd) # nocov
}
}
}
return(pd)
}
if(FALSE){#@testing
pd <- get_parse_data(parse(text={"
{# grouped code
# normal comment
#' Documenation before
hw <- function(){
#! documentation comment inside.
print('hello world')
}
}
{# Second Group
1+2
}
# Comment 3
4+5
"}, keep.source=TRUE))
ids <- all_grouping_ids(pd)
fixed <- fix_grouping_comment_association(ids, pd)
expect_identical( -parent(.find_text("#' Documenation before", fixed), fixed)
, parent(.find_text('<-'))
)
expect_identical(fixed[-6], pd[-6])
text(all_comment_ids(fixed), fixed)
x <- pd_get_ancestor_ids(.find_text('print', fixed), fixed)
inside.parent <- max(x[is_in_function(x, fixed) & ! is_function(x)])
expect_equal( abs(parent(all_comment_ids(fixed), fixed))
, c( rep(ascend_to_root(.find_text('hw', fixed), fixed), 3)
, inside.parent
, parent(.find_text('+'))
)
)
}
if(FALSE){#@test fix_grouping_comment_association Special case
pd <- get_parse_data(parse(text={"
{#' Documenation before
hw <- function(){
#! documentation comment inside.
print('hello world')
}
}"}, keep.source=TRUE))
fixed <- fix_grouping_comment_association(roots(pd), pd)
expect_equal(nrow(pd), nrow(fixed))
expect_identical(pd$id, fixed$id)
cid <- .find_text("#' Documenation before")
expect_true( parent(cid, fixed) != parent(cid, pd))
expect_true(is_assignment(abs(parent(cid, fixed)), fixed))
expect_true(!any(is_comment(children(roots(fixed), fixed), fixed)))
}
|