|
| 1 | +#' Work with parser versions |
| 2 | +#' |
| 3 | +#' The structure of the parse data affects many operations in styler. There was |
| 4 | +#' unexpected behaviour of the parser that styler was initially designed to work |
| 5 | +#' around. Examples are [#187](https://github.com/r-lib/styler/issues/187), |
| 6 | +#' [#216](https://github.com/r-lib/styler/issues/216), |
| 7 | +#' [#100](https://github.com/r-lib/styler/issues/100) and others. With |
| 8 | +#' [#419](https://github.com/r-lib/styler/issues/419), the structrure of the parse |
| 9 | +#' data changes and we need to dispatch for older versions. As it is inconvenient |
| 10 | +#' to pass a parser version down in the call stack in various places, the |
| 11 | +#' environment `env_current` is used to store the current version *globally* |
| 12 | +#' but internally. |
| 13 | +#' |
| 14 | +#' We version the parser as follows: |
| 15 | +#' |
| 16 | +#' * version 1: Before fix mentioned in #419. |
| 17 | +#' * version 2: After #419. |
| 18 | +#' |
| 19 | +#'The following utilities are available: |
| 20 | +#' |
| 21 | +#' * `parser_version_set()` sets the parser version in the environment |
| 22 | +#' `env_current`. |
| 23 | +#' * `parser_version_get()` retrieves the parser version from the |
| 24 | +#' environment `env_current`. |
| 25 | +#' * `parser_version_find()` determines the version of the parser from parse |
| 26 | +#' data. This does not necessarily mean that the version found is the |
| 27 | +#' actual version, but it *behaves* like it. For example, code that does not |
| 28 | +#' contain `EQ_ASSIGN` is parsed the same way with version 1 and 2. If the |
| 29 | +#' behaviour is identical, the version is set to 1. |
| 30 | +#' @param version The version of the parser to be used. |
| 31 | +#' @param pd A parse table such as the output from |
| 32 | +#' `utils::getParseData(parse(text = text))`. |
| 33 | +#' @keywords internal |
| 34 | +parser_version_set <- function(version) { |
| 35 | + env_current$parser_version <- version |
| 36 | +} |
| 37 | + |
| 38 | +#' @rdname parser_version_set |
| 39 | +parser_version_get <- function() { |
| 40 | + env_current$parser_version |
| 41 | +} |
| 42 | + |
| 43 | +#' @rdname parser_version_set |
| 44 | +parser_version_find <- function(pd) { |
| 45 | + ifelse(any(pd$token == "equal_assign"), 2, 1) |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +env_current <- rlang::new_environment(parent = rlang::empty_env()) |
0 commit comments