lexer.js Documentation

<- Back to the documentation index

Methods

lexer.lex(str, spec)
Given a string and a lexer specification, return matched Lexeme objects (see below) as an array. The expected format of the lexer specification is described below.

Specification Object Properties

states
An object with user-defined keys whose values are arrays of pattern objects as described below. These pattern objects are attempted to be used to match the input string to the lexer in the order specified by the array.
start_state
A string matching one of the keys of states.

Pattern Object Properties

regex
A regular expression to be matched against the part of the lexer input string that hasn't yet been matched.
next_state (optional)
A string corresponding to the key of a state in the specification object described above. If the regex matches, transition to this state.
type (optional)
The type of lexeme, to be returned as part of the Lexeme object (see below).

Lexeme Object Properties

type
The type of lexeme, as specified in the Pattern object (see above).
val
The value of the matched regex pattern as specified in the Pattern object (see above).
line
The 0-indexed line of the source file on which the lexeme appears.