lex | R Documentation |
Build all of the regular expression rules from definitions in the supplied module
lex(module = NA, args = list(), debug = FALSE, debuglog = NA, errorlog = NA)
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 |
Lexer ready to use
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.