Lexeme
Defines a pattern for recognizing and producing tokens from character input.
A Lexeme encapsulates the logic for matching a specific lexical pattern at the current position in the input stream. When a match is found, it returns a LexemeMatch describing the match length and a factory for creating the corresponding Token.
Implementations should define the match method to perform pattern matching within a LexerContext.
Note on equals and hashCode
Lexeme implementations may optionally override equals and hashCode to provide custom equality semantics. By default, Lexemes use the semantics of Any for these functions.
If an implementation overrides these functions, ensure:
Both
equalsandhashCodeare overridden together (not just one or the other)Both
equalsandhashCodefollow the semantic contracts specified in AnyFor Lexemes
aandb, the implementation ofa.equals(b)must satisfy (at minimum) the following semantics:a) Return
truewhena === bb) Return
falsewhen the Lexemes have different name valuesc) Return
falsewhen there exists any input (i.e. LexerContext state) wherea.match()?.length != b.match()?.lengthd) If none of the above apply,
equalsmay (but is not required to) returntrue
Parameters
The type of Token produced by this Lexeme
Inheritors
Properties
A default Token factory that creates new instances of T for matches of this Lexeme, given the CharSequence which captures the Token (equal to Token.value).