Download the PHP package dbeurive/lexer without Composer
On this page you can find all versions of the php package dbeurive/lexer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package lexer
Introduction
This repository contains the implementation of a basic lexer.
A lexer explodes a given string into a list of tokens.
Installation
From the command line:
composer require dbeurive\lexer
If you want to include this package to your project, then edit your file composer.json
and add the following entry:
"require": {
"dbeurive/lexer": "*"
}
Synopsis
Specifications
Description
The lexer is configured by a list of tokens specifications:
array(
<token specification>,
<token specification>,
...
)
Each token specification is an array that contains 2 or 3 elements.
<token specification> = array(<regexp>, <type>, [<transformer callback>])
- The first element is a regular expression that describes the token.
- The second element is a name that identifies the type of the token.
- The optional third element is a function that is applied to the token's value before it is returned.
WARNING
Make sure to double all characters "
\
" within the regular expressions that define the tokens. That is:'/\s/'
becomes'/\\s/'.
The signature of the optional third element (<transformer callback>
) must be:
mixed|null function(array $inMatches)
The array ($inMatches
) passed to the function comes from the processing of the regular expression that describes the token.
- The first element of the array (
$inMatches[0]
) contains the text that matches the full pattern. - The second element of the array (
$inMatches[1]
) contains the text that matched the first captured parenthesized subpattern. - The third element of the array (
$inMatches[2]
) contains the text that matched the second captured parenthesized subpattern. - ... and so on.
See the description for the PHP function
preg_match()
.
- If the function returns the value
null
, then the detected token is "ignored". That is: it will not be inserted into the list of extracted tokens. - If the function returns a non-null value, then the token is inserted in the list of detected tokens.
The value of the inserted token will be the value returned by the function (
<transformer callback>
).
Very important note
Be aware that the order of declarations of the tokens is important.
The example 2 illustrates this point.
The result is:
Test1: AAAA AA
type A2 AA
type A2 AA
type A2 AA
Test2: AAAA AA
type A1 A
type A1 A
type A1 A
type A1 A
type A1 A
type A1 A
API
Constructor
Please see the section "specifications" for a detailed description of the parameter.
lex()
This method "parses" a given text and returns a list of detected tokens.
The returned array contains the list of detected tokens.
Each element of the returned array is an instance of the class \dbeurive\Lexer\Token
.