R/Token.R

Defines functions print.Token

Documented in print.Token

# That’s a token: a bundle containing the raw lexeme along with
# the other things the scanner learned about it.
Token = R6::R6Class("Token",
  public = list(
    type = NULL,
    lexeme = NULL,
    literal = NULL,
    initialize = function(type = NULL, lexeme = NULL, literal = NULL) {
      self$type = type
      self$lexeme = lexeme
      self$literal = literal
    },
    to_string = function() {
      paste(self$type, self$lexeme, self$literal)
    },

    str = function() {
      str = paste0(
        "Token(type = ", self$type, ", lexeme = '", self$lexeme, "'"
        )
      if (!is.null(self$literal)) {
        str = paste0(str, ", literal = ", self$literal, ")")
      } else {
        str = paste0(str, ")")
      }
      str
    }
  )
)



#' Print a Token object.
#'
#' @param x A `Token` object.
#' @param ... Further arguments passed to or from other methods.
#'
#' @return The string representation of the token, invisibly.
#' @export
print.Token <- function(x, ...) {
  str <- x$str()
  cat(str)
  invisible(str)
}

Try the latexr package in your browser

Any scripts or data that you put into this service are public.

latexr documentation built on Sept. 30, 2026, 5:08 p.m.