Lexer
Lexes a source-string into tokens.
Example:
const lex = perplex('...')
.token('ID', /my-id-regex/)
.token('(', /\(/)
.token(')', /\)/)
.token('WS', /\s+/, true) // true means 'skip'
while ((let t = lex.next()).type != 'EOF') {
console.log(t)
}
// alternatively:
console.log(lex.toArray())
Constructor Summary
Public Constructor | ||
public |
constructor(source: string) Creates a new Lexer instance |
Member Summary
Public Members | ||
public get |
Gets the current lexer position |
|
public set |
Sets the current lexer position |
|
public get |
Gets the source the lexer is operating on |
|
public set |
Sets the source the lexer is operating on |
Method Summary
Public Methods | ||
public |
Attaches this lexer to another lexer's state |
|
public |
Disables a token type |
|
public |
Enables a token type |
|
public |
Like next, but throws an exception if the next token is not of the required type. |
|
public |
Consumes and returns the next Token in the source string. |
|
public |
Returns the next Token in the source string, but does not consume it. |
|
public |
Converts a string-index (relative to the source string) to a line and a column. |
|
public |
toArray(): Token<T>[] Converts the token stream to an array of Tokens |
|
public |
Creates a new token type |
Public Constructors
Public Members
Public Methods
public attachTo(other: Lexer<T>) source
Attaches this lexer to another lexer's state
Params:
Name | Type | Attribute | Description |
other | Lexer<T> | The other lexer to attach to |
public disable(type: T): Lexer<T> source
Disables a token type
Params:
Name | Type | Attribute | Description |
type | T | The token type to disable |
public enable(type: T, enabled: boolean): Lexer<T> source
Enables a token type
Params:
Name | Type | Attribute | Description |
type | T | The token type to enalbe |
|
enabled | boolean |
|
Whether to enable/disable the specified token type |
public expect(type: T): Token<T> source
Like next, but throws an exception if the next token is not of the required type.
Params:
Name | Type | Attribute | Description |
type | T | The token type expected from next |
public next(): Token<T> source
Consumes and returns the next Token in the source string.
If there are no more tokens, it returns a Token of type $EOF
public peek(position: number): Token<T> source
Returns the next Token in the source string, but does
not consume it.
If there are no more tokens, it returns a Token of type $EOF
Params:
Name | Type | Attribute | Description |
position | number |
|
The position at which to start reading |
public strpos(i: number): Position source
Converts a string-index (relative to the source string) to a line and a column.
Params:
Name | Type | Attribute | Description |
i | number | The index to compute |
public toArray(): Token<T>[] source
Converts the token stream to an array of Tokens
Return:
Token<T>[] | The array of tokens (not including (EOF)) |