Skip to content

Allow hashtags in single-line input strings #193

@gadenbuie

Description

@gadenbuie

I've been playing with something akin to the collapse_transformer() example, but I'd like to be able to include # in one-line input strings.

What I want to do is essentially the following, where I would parse the template string in the transformer, but unfortunately it doesn't make it past the parser.

glue::glue("{#b a}")
# no output due to #b

Here are a few examples using an identity transformer that just returns the text that reaches the transformer.

identity <- function(x, ...) x

glue::glue("{a}", .transformer = identity)
#> a

"{a*}" works and the transformer receives "a*", but "{a#}" does not work: the transformer isn’t called …

glue::glue("{a*}", .transformer = identity)
#> a*
glue::glue("{a#}", .transformer = identity)

…unless the # is followed by a new line.

glue::glue("{a#\n}", .transformer = identity)
#> a#

Another example without the identity transformer.

a <- "apple"
glue::glue("{a#}")
glue::glue("{a#\n}")
#> apple

I can see where problems could arise with incomplete expressions due to comments, but it also makes sense that such problems would throw parsing errors.

glue::glue("{c(a,#\na)}")
#> apple
#> apple
glue::glue("{c(a,#a)}")
glue::glue("{c(a,#a)\n}")
#> Error in parse(text = text, keep.source = FALSE): <text>:3:0: unexpected end of input
#> 1: c(a,#a)
#> 2: 
#>   ^

That said, one place where changing the parser might break current behavior is that currently empty expressions cause the entire result to be zero-length. Except for the last, the following all return character(0).

glue::glue("{#a} {'banana'}")
glue::glue("{#a\n} {'banana'}")
glue::glue("{a#a} {'banana'}")
glue::glue("{a#a\n} {'banana'}")
#> apple banana

Created on 2020-06-04 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions