parser: Parser Objects

Description Usage Arguments Details Value Examples

View source: R/parser.R

Description

Create or test for parser objects. These objects will be used by templates to identify a field within a log file.

Usage

1
2
3
parser(x, f, name = NULL)

is.parser(x)

Arguments

x

A regex string, a parser, or a list of either; Or object to be tested

f

A function to format the captured output, or a named list of such functions if x is a list

name

An optional name for the parser

Details

Parser objects contain 3 things:

  1. A regex expression that matches the given field

  2. A 'formatter'; a function that will in some way modify the captured text

    • By default, this the identity function

  3. (Optional) A name for the parser

Value

parser and its S3 methods coerce x to a parser object, returning said parser object. is.parser returns TRUE or FALSE

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Captures integers
parser('[0-9]+')

# Captures integers, cast to integers
parser('[0-9]+', as.integer)

# List of parsers, all named (inferred from list names), some with parsers
parser(
  list(
    ip = '[0-9]{1,3}(\\.[0-9]{1,3}){3}',
    int = '[0-9]+',
    date = '[0-9]{4}\\-[0-9]{2}\\-[0-9]{2}'
  ),
  list(int = as.integer, date = as.Date)
)

is.parser(parser('[0-9]+')) #TRUE
is.parser(100)              #FALSE

tabulog documentation built on Aug. 9, 2019, 5:07 p.m.