Skip to content

Fix eq assign parsing #276

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

Merged
merged 6 commits into from
Nov 10, 2017

Conversation

lorenzwalthert
Copy link
Collaborator

Closes #259. As outlined in #259, we

  • Walk all nests
  • Within one nest, we split the nest into sub-nests that contain one assignment expression (e.g. a = b = c) at most.
  • Within every such sub-nest, we relocate the assignment expression.

However, we only do that if there is an EQ_ASSIGN token in the flat parse data since the whole procedure is rather costly and we can expect the probability that there is an EQ_ASSIGN in the parse data to be small.

@codecov-io
Copy link

codecov-io commented Nov 8, 2017

Codecov Report

Merging #276 into master will increase coverage by 0.22%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #276      +/-   ##
==========================================
+ Coverage   92.95%   93.17%   +0.22%     
==========================================
  Files          28       28              
  Lines        1136     1173      +37     
==========================================
+ Hits         1056     1093      +37     
  Misses         80       80
Impacted Files Coverage Δ
R/nest.R 100% <100%> (ø) ⬆️
R/relevel.R 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9077126...c51460b. Read the comment docs.

Copy link
Member

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. What a job to work around a nasty minor upstream problem...

R/relevel.R Outdated
#'
#' Although syntactically identical, [utils::getParseData()] does not produce
#' the same hierarchy of the parse table (parent and id relationship) for `<-`
#' and `=` (See 'Examles').
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uups.

R/relevel.R Outdated
cumsum((is_eq_assign - lag(is_eq_assign, default = is_eq_assign[1])) > 2)
empty_seq <- rep(0, nrow(pd))
empty_seq[lead(pd$token == "EQ_ASSIGN", default = FALSE)] <- eq_belongs_to_block
block_id <- cumsum(empty_seq)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the value this function returns?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Lets add

block_id

So it is explicit.

eq_expr$parent <- NA
non_eq_expr <- pd[-eq_ind,]
pd <- bind_rows(eq_expr, non_eq_expr) %>%
arrange(pos_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

R/relevel.R Outdated
find_block_id <- function(pd) {
is_eq_assign <- which(pd$token == "EQ_ASSIGN")
eq_belongs_to_block <-
cumsum((is_eq_assign - lag(is_eq_assign, default = is_eq_assign[1])) > 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x - lag(x) isn't much different from diff(x), and cumsum(diff(x)) is about the same as x. Does this need to be that complicated?

Copy link
Collaborator Author

@lorenzwalthert lorenzwalthert Nov 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I checked diff(x) from a previous comment from you but the thing is we also need 0 to be the first element. Because with diff(), you get n - 1 elements back, not n. So we could do

c(0, diff(x)) # instad of 
x - lag(x, default = x[1])

Then, cumsum(c(0, diff(x)) > 2) can be simplified by pushing the zero out of cumsum, i.e.

c(0, cumsum(diff(is_eq_assign)) > 2)

Which is, I admit, simpler, but it's maybe less clear to the programmer how we arrived at that... Anyways I can change it accordingly.

Copy link
Collaborator Author

@lorenzwalthert lorenzwalthert Nov 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or can you simplify it even further? Cause it's not immediately clear to me how one could do that.

Copy link
Member

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe simplify a bit further?

R/relevel.R Outdated
#' @param pd A parse table.
find_block_id <- function(pd) {
is_eq_assign <- which(pd$token == "EQ_ASSIGN")
eq_belongs_to_block <- c(0, cumsum(diff(is_eq_assign)) > 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it's not c(0, cumsum(diff(is_eq_assign) > 2)) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uups yes.

R/relevel.R Outdated
#' `EQ_ASSING` already belongs to the `EQ_ASSING` after it.
#' @param pd A parse table.
find_block_id <- function(pd) {
is_eq_assign <- which(pd$token == "EQ_ASSIGN")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming: is_ hints to a logical, maybe idx_?

R/relevel.R Outdated
eq_belongs_to_block <- c(0, cumsum(diff(is_eq_assign)) > 2)

empty_seq <- rep(0, nrow(pd))
empty_seq[lead(pd$token == "EQ_ASSIGN", default = FALSE)] <- eq_belongs_to_block
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty_seq[idx_eq_assign + 1] <- ... ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a minus 😄 .

@lorenzwalthert lorenzwalthert merged commit 72bc092 into r-lib:master Nov 10, 2017
@lorenzwalthert lorenzwalthert deleted the fix-eq_assign_parsing branch November 10, 2017 22:34
lorenzwalthert added a commit to lorenzwalthert/styler that referenced this pull request Nov 21, 2017
lorenzwalthert added a commit to lorenzwalthert/styler that referenced this pull request Nov 21, 2017
krlmlr added a commit that referenced this pull request Nov 27, 2017
- Adapt documentation (#290).
- Add roundtrip (#287).
- Fix AppVeyor builds.
- Fix token insertion / comment interaction (#279).
- Clarify labelling strategy (#285).
- Fixing and extending Rstudioaddins (#283).
- Fix eq assign parsing (#276).
- style_files -> vectorized style_file (#273).
- Refactoring (#270).
- Fix CI (#275).
- Fix covr (#274).
- Renaming files (#271).
- Handle styling of an unsaved active file (#243).
- Test R 3.1 and R 3.2 (#249).
- Allow empty {} without line break (#261).
- Wrap expr in expr before enclosing with curly braces (#263).
- Avoid checking for hard-coded dot (#262).
- Account for dependency renaming (utf8 changed to enc) (#264).
- Indention of function declaration and closing braces (#260).
- Only remove line break before closing with strict option (#252).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants