jexer
jexer
is a lexer.
jexer
is not a lexer generator. lex
and flex
are classic examples of lexer
generators.
Currently, rule configuration and lexing are performed at runtime.
Installation
Install jexer
with npm.
$ npm install [--save] jexer
Usage
const Jexer = ;const jexer = ;jexer;const tokens = jexer;
A quick lesson in compilation
A scanner scans a file into a sequence of characters. (If using Node.js,
you can rely on the built-in fs
module for this.)
A lexer (or tokenizer) lexes a sequence of characters into a sequence of
tokens, using a defined set of rules. (This is what jexer
does.)
A parser parses a sequence of tokens into a syntax tree, using a defined
set of rules. (I recommend jarser
for this stage:
jarser
on npm
jarser
on GitHub)
A compiler compiles the syntax tree into target code, such as assembly.
An assembler assembles that code into machine code, which a processor can execute directly.
API
Jexer
object
The new Jexer()
- Returns an instance of
Jexer
.
Jexer.addRule([state,] pattern, [tokenName [, newState]])
state
<string> Default:'INITIAL'
(the initial/default state)pattern
<RegExp>tokenName
<string> | <Function> Default:undefined
(swallow token)text
<string>line
<integer>column
<integer>
newState
<string> Default:undefined
(do not change state)
Add a rule to the lexer. A match pattern
is required.
If a state
is provided, the rule only applies to the state specified. If no
state
is provided, the rule only applies in the initial/default state, which
is 'INITIAL'
.
If tokenName
is a string
, the lexed token is added to the output with the
given name.
If tokenName
is a function
, it is called with three arguments (text, line, column)
, where test
is the text that matched the rule's pattern and line
and column
indicate the position of the text within the source code that is
being tokenized. The function should return a tokenName
; functions without
return values (equivalent to return undefined;
) result in the token being
swallowed. Passing a function is useful for debugging, logging, or for
validating state. Throwing an error in this function halts execution of the
lexer.
If tokenName
is undefined
, whether implicitly or explicitly,
the token is swallowed and omitted from the output. If you want to swallow a
token and enter a newState
, you must explicitly pass undefined
for
tokenName
.
If newState
if provided, the lexer will enter this state after this rule is
matched. Pass 'INITIAL'
to return to the default/initial state.
Jexer.tokenize(sourceCode)
sourceCode
<string>- returns <Array<Token>>
Returns an Array
of Token
objects.
Throws an Error
if there is a problem parsing the source code, i.e. if the
lexer encounters text that doesn't match any defined rules.
Token
object
The A Token
is a 'plain old javascript object' with these keys:
name
<string> the name of the tokentext
<string> the text that was matchedline
<integer> the line number of the first character of the tokencolumn
<integer> the column number of the first character of the token
Examples
// ignore whitespacejexer; // operatorsjexer;jexer;jexer;jexer;jexer; // keywordsjexer;jexer;jexer; // variablesjexer;
// log and lex every character as its own tokenjexer;
// throw an error on tokens reserved for future usejexer;
Common uses for changing state:
// SINGLE LINE COMMENTS// match '//', ignore it, and enter the 'COMMENT' statejexer;// in the 'COMMENT' state, watch for a newline, and return to 'INITIAL' statejexer;// ignore any other characters in the commentjexer;
// MULTI LINE COMMENTS// match '/*', ignore it, and enter the 'COMMENT' statejexer;// match '*/' newline, and return to 'INITIAL' statejexer;// ignore all characters in the comment, even new linesjexer;
Below is a full example of the definition of the lexical grammar for a simple
calculator language which ignores whitespace; has semicolon terminated
statements, single-line comments, and variables; has addition, subtraction,
and assignment operators; and supports integer, floating point, binary, and
hexadecimal number literals. Any other token (such as *
or /
) will throw
a lexing error.
jexer;jexer;jexer;jexer;jexer;jexer;jexer;jexer;jexer;jexer;jexer;jexer;jexer;