dev-master
9999999-devA parser generator for PHP
MIT
The Requires
- php ~5.5 || ~7.0
- ext-mbstring *
The Development Requires
by Peter van Dommelen
parser generator
A parser generator for PHP
A parser generator written in PHP., (*1)
Supports backtracking., (*2)
Detects left recursion., (*3)
Directly matches strings so it contains no lexing (tokenizer) step, (*4)
The available helper class and its static methods should be used:, (*5)
PeterVanDommelen\Parser\ParserHelper::compile(ExpressionInterface $expression) PeterVanDommelen\Parser\ParserHelper::compileWithGrammar(ExpressionInterface $expression, Grammar $grammar)
These methods accept an expression (see below which ones are available) and maybe a grammar (see below). The return value is the parser which offers one method:, (*6)
PeterVanDommelen\Parser\Parser\ParserInterface::parse(string $string)
The parse method will return null if the parser was unable to match the string. The parser will always match from the start. If succesfully matched a result object corresponding to the parsed expression will be returned., (*7)
The result object implements ExpressionResultInterface which has the following two methods describing the matched string:, (*8)
PeterVanDommelen\Parser\Expression\ExpressionResultInterface::getString() PeterVanDommelen\Parser\Expression\ExpressionResultInterface::getLength()
The specific implementation depends on the expression used and may have additional methods available, see the examples., (*9)
PeterVanDommelen\Parser\Expression\Constant\ConstantExpression, (*10)
new ConstantExpression(string $string)
Basic terminal., (*11)
PeterVanDommelen\Parser\Expression\Concatenated\ConcatenatedExpression, (*12)
new ConcatenatedExpression(ExpressionInterface[] $parts);
Example:, (*13)
$result = ParserHelper::compile(new ConcatenatedExpression(array( new ConstantExpression("a"), new ConstantExpression("b"), )))->parse("abc"); $result->getString(); // "ab" $result->getPart(0)->getString(); // "a" $result->getPart(1)->getString(); // "b"
PeterVanDommelen\Parser\Expression\Alternative\AlternativeExpression, (*14)
new AlternativeExpression(ExpressionInterface[] $alternatives)
Matches one of the expressions., (*15)
$result = ParserHelper::compile(new AlternativeExpression(array( new ConstantExpression("a"), new ConstantExpression("b"), )))->parse("abc"); $result->getString(); // "a" $result->getKey(); // 0 $result->getResult()->getString(); // "a"
PeterVanDommelen\Parser\Expression\Repeater\RepeaterExpression, (*16)
new RepeaterExpression(ExpressionInterface $inner_expression, bool $is_lazy = false, int $minimum = 0, int|null $maximum = null)
Repeats the inner expression with the specified minimum and maximum., (*17)
$result = ParserHelper::compile(new RepeaterExpression(new ConstantExpression("a"))->parse("aaabc"); $result->getString(); // "aaa" $result->getResults()[0]->getString(); // "a"
PeterVanDommelen\Parser\Expression\Joined\JoinedExpression, (*18)
new JoinedExpression(ExpressionInterface $inner_expression, ExpressionInterface $seperator_expression, bool $is_lazy = false, int $minimum = 0, int|null $maximum = null)
A variation of the RepeaterExpression but with a seperator between elements, (*19)
$result->getResults(); $result->getSeperators(); //will have a size of count($result->getResults) - 1
PeterVanDommelen\Parser\Expression\Not\NotExpression, (*20)
new NotExpression(ExpressionInterface $inner_expression)
Matches a single character only if the inner expression does not match. The example below would match any character but "a" or "b":, (*21)
new NotExpression(new AlternativeExpression(array( new ConstantExpression("a"), new ConstantExpression("b") ))
PeterVanDommelen\Parser\Expression\Any\AnyExpression, (*22)
new AnyExpression()
Matches any single character., (*23)
use PeterVanDommelen\Parser\Expression\Named\NamedExpression, (*24)
new NamedExpression(string $name)
See grammar below, (*25)
The compiler supports using a grammar where entries can be referenced by using a NamedExpression., (*26)
For example, if we want to find the string not enclosed within pairs of matching brackets:, (*27)
$no_opening_brace = new RepeaterExpression(new NotExpression(new AlternativeExpression(array( new ConstantExpression("(") )))); $grammar = new Grammar(array( "expression" => new AlternativeExpression(array( new ConcatenatedExpression(array( $no_opening_brace, new ConstantExpression("("), new NamedExpression("expression"), new ConstantExpression(")"), $no_opening_brace, )), $no_opening_brace, )), )); $parser = ParserHelper::compileWithGrammar(new NamedExpression("expression"), $grammar); $result = $parser->parse("ab(c(d)ef)g"); $result->getString(); // "ab(c(d)ef)g" switch ($result->getKey()) { case 0: return $result->getResult()->getPart(0)->getString() . $result->getResult()->getPart(4)->getString(); // "abg" case 1: // this branch is not reached return $result->getResult()->getString(); // }
Ignoring compilation, you should expect this parser to be roughly 100x slower than the native preg_match., (*28)
A parser generator for PHP
MIT
parser generator