R: Build a lexer

lex {rly} | R Documentation

## 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)

### 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

### Value

Lexer ready to use

### Examples

    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)

[Package rly version 1.6.2 Index]