2008-04-08 08:17:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A tokenised regular expression is a parser, similar to a regular expression, that acts on tokens rather than characters.
|
|
|
|
* This is a crucial component of the ManifestBuilder.
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-06-15 15:33:53 +02:00
|
|
|
* @subpackage core
|
2008-04-08 08:17:58 +02:00
|
|
|
*/
|
2011-02-17 08:05:44 +01:00
|
|
|
class TokenisedRegularExpression {
|
2008-04-08 08:17:58 +02:00
|
|
|
/**
|
|
|
|
* The regular expression definition
|
|
|
|
*/
|
|
|
|
protected $expression;
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($expression) {
|
2008-04-08 08:17:58 +02:00
|
|
|
$this->expression = $expression;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function findAll($tokens) {
|
2008-04-08 08:17:58 +02:00
|
|
|
$tokenTypes = array();
|
|
|
|
foreach($tokens as $i => $token) {
|
|
|
|
if(is_array($token)) {
|
|
|
|
$tokenTypes[$i] = $token[0];
|
|
|
|
} else {
|
|
|
|
$tokenTypes[$i] = $token;
|
|
|
|
// Pre-process string tokens for matchFrom()
|
|
|
|
$tokens[$i] = array($token, $token);
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 03:33:11 +01:00
|
|
|
|
|
|
|
$startKeys = array_keys($tokenTypes, is_array($this->expression[0]) ? $this->expression[0][0] : $this->expression[0]);
|
2008-04-08 08:17:58 +02:00
|
|
|
$allMatches = array();
|
|
|
|
|
|
|
|
foreach($startKeys as $startKey) {
|
|
|
|
$matches = array();
|
|
|
|
if($this->matchFrom($startKey, 0, $tokens, $matches)) {
|
|
|
|
$allMatches[] = $matches;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $allMatches;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function matchFrom($tokenPos, $expressionPos, &$tokens, &$matches) {
|
2008-04-08 08:17:58 +02:00
|
|
|
$expressionRule = $this->expression[$expressionPos];
|
|
|
|
$expectation = is_array($expressionRule) ? $expressionRule[0] : $expressionRule;
|
|
|
|
if(!is_array($expressionRule)) $expressionRule = array();
|
|
|
|
|
|
|
|
if($expectation == $tokens[$tokenPos][0]) {
|
|
|
|
if(isset($expressionRule['save_to'])) {
|
|
|
|
// Append to an array
|
|
|
|
if(substr($expressionRule['save_to'],-2) == '[]') $matches[substr($expressionRule['save_to'],0,-2)][] = $tokens[$tokenPos][1];
|
|
|
|
// Regular variable setting
|
|
|
|
else $matches[$expressionRule['save_to']] = $tokens[$tokenPos][1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of the expression
|
|
|
|
if(!isset($this->expression[$expressionPos+1])) {
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Process next step as normal
|
|
|
|
} else if($this->matchFrom($tokenPos+1, $expressionPos+1, $tokens, $matches)) {
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// This step is optional
|
|
|
|
} else if(isset($expressionRule['optional']) && $this->matchFrom($tokenPos, $expressionPos+1, $tokens, $matches)) {
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Process jumps
|
|
|
|
} else if(isset($expressionRule['can_jump_to'])) {
|
|
|
|
if(is_array($expressionRule['can_jump_to'])) foreach($expressionRule['can_jump_to'] as $canJumpTo) {
|
|
|
|
// can_jump_to & optional both set
|
|
|
|
if(isset($expressionRule['optional']) && $this->matchFrom($tokenPos, $canJumpTo, $tokens, $matches)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// can_jump_to set (optional may or may not be set)
|
|
|
|
if($this->matchFrom($tokenPos+1, $canJumpTo, $tokens, $matches)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// can_jump_to & optional both set
|
|
|
|
if(isset($expressionRule['optional']) && $this->matchFrom($tokenPos, $expressionRule['can_jump_to'], $tokens, $matches)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// can_jump_to set (optional may or may not be set)
|
|
|
|
if($this->matchFrom($tokenPos+1, $expressionRule['can_jump_to'], $tokens, $matches)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(isset($expressionRule['optional'])) {
|
|
|
|
if(isset($this->expression[$expressionPos+1])) return $this->matchFrom($tokenPos, $expressionPos+1, $tokens, $matches);
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|