Skip to content

Commit

Permalink
Fix parsing of equal assign in R 3.6.x (#6)
Browse files Browse the repository at this point in the history
Equal assign r36 bug
  • Loading branch information
gaborcsardi authored Aug 14, 2019
2 parents 99a30b1 + e54f28e commit af0f0cf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

# devel

* Ensure that closing xml-tags for code expressions that end at the same
position in a file respect start-first-end-last ordering in the produced xml.
Ensures that the new `equal_assign` token in `getParseData()` for R-3.6 is
handled appropriately. #5 @russHyde

# 1.0.2

* Remove control characters `\003`, `\007`, `\010`, `\027`, as they are
Expand Down
6 changes: 5 additions & 1 deletion R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ xml_parse_data <- function(x, includeText = NA, pretty = FALSE) {
}

## Order the nodes properly
pd <- pd[ order(pd$line1, pd$col1, -pd$line2, -pd$col2, pd$terminal), ]
## - the terminal nodes from pd2 may be nested inside each other, when
## this happens they will have the same line1, col1, line2, col2 and
## terminal status; and 'start' is used to break ties
ord <- order(pd$line1, pd$col1, -pd$line2, -pd$col2, pd$terminal, -pd$start)
pd <- pd[ord, ]

if (pretty) {
str <- ! pd$terminal
Expand Down
15 changes: 14 additions & 1 deletion tests/testthat/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ test_that("data frame input", {
})



test_that("Control-C character", {
src <- "# Control-C \003
# Bell \007
Expand All @@ -105,3 +104,17 @@ test_that("Control-C character", {
expect_is(x, "xml_document")
})


test_that("equal_assign is handled on R 3.6", {
# `a = 1` is an example of an R statement that gets parsed into nested xml
# nodes that have different token / tagnames (following the introduction of
# the `equal_assign` token to getParseData() in R-3.6), but the same ending
# position in the original code. Tokens/expressions that start before should
# end after any nested subexpressions in the resulting xml:

xml <- xml_parse_data(parse(text = "a = 1", keep.source = TRUE))
expect_true(is.character(xml))
expect_true(length(xml) == 1)
expect_silent(x <- xml2::read_xml(xml))
})

0 comments on commit af0f0cf

Please sign in to comment.