public string $type — Token type name (e.g. "IDENT", "NUMBER", "EOF").
public string $value — Matched token text (for strings, escape sequences are decoded).
public int $line — 1-based starting line number.
public int $column — 1-based starting column number.
public int $offset — 0-based byte offset in the input string.
is(string $type): bool — Returns true if $this->type === $type.
isAny(array $types): bool — Returns true if $this->type matches any type in $types.
toString(): string — Formats the token as TYPE(value) at line:column.
Lexer
Fluent rule-based tokenizer. Rules are evaluated in the order they are added (first match wins), so place longer literals (e.g. ==) before shorter prefixes (e.g. =) and keywords before identifiers.
addKeyword(string $type, string $keyword, string $identExtra = "_"): Lexer — Matches an exact keyword only when not followed by an identifier character.
tokenize(string $input, bool $includeEof = true): array — Tokenizes $input into an array of Token objects (appending an "EOF" token when $includeEof is true).
Helper Functions
lexer_create(): Lexer — Creates and returns a new Lexer instance.
lexer_token_at(array $tokens, int $index): Token — Returns the typed Token at $index (throws if out of bounds).
lexer_format_tokens(array $tokens): string — Formats an array of Token objects into a newline-separated string.
Error Handling
All errors throw Exception with a lexer: prefix and exact line:column location where applicable:
lexer: unexpected character '@' at 2:9
lexer: unterminated string literal at 1:9
lexer: unterminated escape sequence in string at 1:15
lexer: unterminated block comment at 1:12
lexer: literal pattern must be non-empty
lexer: string quote must be a single character
Examples
01Quick start
Bundled with the compiler — import "lexer"; works offline in any file, or add it to a project with hphp install lexer.