Learn R Programming

rly (version 1.7.7)

lex: Build a lexer

Description

Build all of the regular expression rules from definitions in the supplied module

Usage

lex(module = NA, args = list(), debug = FALSE, debuglog = NA,
  errorlog = NA)

Value

Lexer ready to use

Arguments

module

R6 class containing lex rules

args

list of arguments that should be passed to constructor

debug

on and off debug mode

debuglog

custom logger for debug messages

errorlog

custom logger for error messages

Examples

Run this code
TOKENS = c('NAME', 'NUMBER')
LITERALS = c('=','+','-','*','/', '(',')')

Lexer <- R6::R6Class("Lexer",
  public = list(
    tokens = TOKENS,
    literals = LITERALS,
    t_NAME = '[a-zA-Z_][a-zA-Z0-9_]*',
    t_NUMBER = function(re='\\d+', t) {
      t$value <- strtoi(t$value)
      return(t)
    },
    t_ignore = " \t",
    t_newline = function(re='\\n+', t) {
       t$lexer$lineno <- t$lexer$lineno + nchar(t$value)
       return(NULL)
    },
    t_error = function(t) {
      cat(sprintf("Illegal character '%s'", t$value[1]))
      t$lexer$skip(1)
      return(t)
    }
  )
)

lexer  <- rly::lex(Lexer)
lexer$input("5 + 3")
print(lexer$token()$value)
# [1] 5
print(lexer$token()$value)
# [1] "+"
print(lexer$token()$value)
# [1] 3

Run the code above in your browser using DataLab