Skip to content

Updates to R syntax with recent language features #4190

Open

Description

@MichaelChirico

Related: #4040

The R language definition (components/prism-r.js in v1 / src/languages/r.ts in v2) has not been updated for R 4.0 / R 4.1 syntax additions:

  1. Raw string literals (?Quotes):
    • Currently tokenized as an identifier r followed by a regular string (which breaks when the raw string contains unescaped quotes or backslashes like r"(C:\path\to\"file")")
  2. Native pipe |> (?pipeOp):
    • Currently split into | + > (both operators).
    • Relatedly, the native placeholder _ is not supported
  3. Shorthand lambda \(...) (?function)
    • \ in \(x) x + 1 is currently unhighlighted.
  4. Variadic ..N should be reserved just as ... is

Prism also lacks support for some very common core language features for R:

  1. Internal namespace operator (:::) and walrus assignment (:=):
    • operator currently matches ::? (leaving the third : in pkg:::fn separate) and does not match := (used heavily in {data.table} and {rlang}).
  2. {roxygen2} documentation comments (#' @param ...):
    • #' doc comments and @tag directives (@param, @return, @export, etc.) are extremely common in R packages.
  3. Function calls:
    • prism-r currently defines no function token rule, unlike most other Prism grammars.
  4. S4 slot extraction operator @ is not recognized (?methods::slot)
  5. Multi-line string support as highlighted in Multiline R strings are not correctly highlighted #4040

Here is an example R snippet demonstrating some of the missing features:

#' @param x_val Input vector
#' @export
`my-fn` <- \(x_val = 1L, ...) {
  raw_str <- r"--(c:\path\to\file)--"
  pkg::pub_fn(x_val, ..1) |>
    lm(y ~ x, data = _) |>
    _$coefficients
  dt[, col := obj@slot]
}

I can propose some PRs to fix this, but I am not sure where best to direct the efforts given the v1/v2 split. Is v2 stable enough to start accepting patches to "more peripheral" grammars like R?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions