MathParser\Lexing\TokenDefinition Class Reference

Token definitions using regular expressions to match input. More...

Public Member Functions

 __construct ($pattern, $tokenType, $value=null)
 Constructor. More...
 
 match ($input)
 Try to match the given input to the current TokenDefinition. More...
 

Private Member Functions

 getTokenFromMatch ($match)
 Convert matching string to an actual Token. More...
 

Private Attributes

 $pattern
 string $pattern Regular expression defining the rule for matching a token. More...
 
 $value
 string $value Standarized value of token
 
 $tokenType
 inst $tokenType Type of token, as defined in TokenType class. More...
 

Detailed Description

Token definitions using regular expressions to match input.

To get the Lexer to recognize tokens, they need to be defined. This is the task of the TokenDefinition class. Each TokenDefinition consists of a regular expression used to match the input string, a corresponding token type and an optional token value (making it possible to standarize the token value for synonyms, e.g. both ln and log can be tokenized into the same token with value 'log'.)

Example usage (excerpt from StdMathLexer):

$lexer = new Lexer();
$lexer->add(new TokenDefinition('/\d+\.\d+/', TokenType::RealNumber));
$lexer->add(new TokenDefinition('/\d+/', TokenType::PosInt));
$lexer->add(new TokenDefinition('/sin/', TokenType::FunctionName));
$lexer->add(new TokenDefinition('/arcsin|asin/', TokenType::FunctionName, 'arcsin'));
$lexer->add(new TokenDefinition('/\+/', TokenType::AdditionOperator));
$lexer->add(new TokenDefinition('/\-/', TokenType::SubtractionOperator));

Constructor & Destructor Documentation

MathParser\Lexing\TokenDefinition::__construct (   $pattern,
  $tokenType,
  $value = null 
)

Constructor.

Create a TokenDefinition with given pattern and type.

Member Function Documentation

MathParser\Lexing\TokenDefinition::getTokenFromMatch (   $match)
private

Convert matching string to an actual Token.

Parameters
string$matchMatching text.
Return values
TokenCorresponding token.
MathParser\Lexing\TokenDefinition::match (   $input)

Try to match the given input to the current TokenDefinition.

Parameters
string$inputInput string
Return values
Token|nullToken matching the regular expression defining the TokenDefinition

Member Data Documentation

MathParser\Lexing\TokenDefinition::$pattern
private

string $pattern Regular expression defining the rule for matching a token.

MathParser\Lexing\TokenDefinition::$tokenType
private

inst $tokenType Type of token, as defined in TokenType class.


The documentation for this class was generated from the following file: