Skip to content

Pattern expander mechanism #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"php": ">=5.3.0",
"coduo/php-to-string": "~1.0",
"symfony/property-access": "~2.3",
"symfony/expression-language": "~2.4"
"symfony/expression-language": "~2.4",
"doctrine/lexer": "1.0.*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
Expand Down
57 changes: 57 additions & 0 deletions src/Coduo/PHPMatcher/AST/Expander.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Coduo\PHPMatcher\AST;

class Expander implements Node
{
/**
* @var
*/
private $name;

/**
* @var array
*/
private $arguments;

/**
* @param $name
*/
public function __construct($name)
{
$this->name = $name;
$this->arguments = array();
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

/**
* @param $argument
*/
public function addArgument($argument)
{
$this->arguments[] = $argument;
}

/**
* @return bool
*/
public function hasArguments()
{
return (boolean) count($this->arguments);
}

/**
* @return array
*/
public function getArguments()
{
return $this->arguments;
}
}
8 changes: 8 additions & 0 deletions src/Coduo/PHPMatcher/AST/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Coduo\PHPMatcher\AST;

interface Node
{

}
57 changes: 57 additions & 0 deletions src/Coduo/PHPMatcher/AST/Pattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Coduo\PHPMatcher\AST;

class Pattern implements Node
{
/**
* @var Type
*/
private $type;

/**
* @var Expander[]|array
*/
private $expanders;

/**
* @param Type $type
*/
public function __construct(Type $type)
{
$this->expanders = array();
$this->type = $type;
}

/**
* @return mixed
*/
public function getType()
{
return $this->type;
}

/**
* @return bool
*/
public function hasExpanders()
{
return (boolean) count($this->expanders);
}

/**
* @return Expander[]|array
*/
public function getExpanders()
{
return $this->expanders;
}

/**
* @param Expander $expander
*/
public function addExpander(Expander $expander)
{
$this->expanders[] = $expander;
}
}
24 changes: 24 additions & 0 deletions src/Coduo/PHPMatcher/AST/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Coduo\PHPMatcher\AST;

class Type implements Node
{
/**
* @var string
*/
private $type;

/**
* @param string $type
*/
public function __construct($type)
{
$this->type = $type;
}

public function __toString()
{
return $this->type;
}
}
7 changes: 7 additions & 0 deletions src/Coduo/PHPMatcher/Exception/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Coduo\PHPMatcher\Exception;

class Exception extends \Exception
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Coduo\PHPMatcher\Exception;

class InvalidExpanderTypeException extends Exception
{
}
11 changes: 11 additions & 0 deletions src/Coduo/PHPMatcher/Exception/PatternException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Coduo\PHPMatcher\Exception;

class PatternException extends Exception
{
public static function syntaxError($message, $previous = null)
{
return new self('[Syntax Error] ' . $message, 0, $previous);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Coduo\PHPMatcher\Exception;

class UnknownExpanderClassException extends Exception
{
}
7 changes: 7 additions & 0 deletions src/Coduo/PHPMatcher/Exception/UnknownExpanderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Coduo\PHPMatcher\Exception;

class UnknownExpanderException extends Exception
{
}
1 change: 1 addition & 0 deletions src/Coduo/PHPMatcher/Factory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Coduo\PHPMatcher;

interface Factory
Expand Down
20 changes: 16 additions & 4 deletions src/Coduo/PHPMatcher/Factory/SimpleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Coduo\PHPMatcher\Factory;

use Coduo\PHPMatcher\Factory;
use Coduo\PHPMatcher\Lexer;
use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Parser;

class SimpleFactory implements Factory
{
Expand All @@ -21,7 +23,7 @@ public function createMatcher()
protected function buildMatchers()
{
$scalarMatchers = $this->buildScalarMatchers();
$arrayMatcher = new Matcher\ArrayMatcher($scalarMatchers);
$arrayMatcher = new Matcher\ArrayMatcher($scalarMatchers, $this->buildParser());

return new Matcher\ChainMatcher(array(
$scalarMatchers,
Expand All @@ -35,17 +37,27 @@ protected function buildMatchers()
*/
protected function buildScalarMatchers()
{
$parser = $this->buildParser();

return new Matcher\ChainMatcher(array(
new Matcher\CallbackMatcher(),
new Matcher\ExpressionMatcher(),
new Matcher\NullMatcher(),
new Matcher\StringMatcher(),
new Matcher\IntegerMatcher(),
new Matcher\StringMatcher($parser),
new Matcher\IntegerMatcher($parser),
new Matcher\BooleanMatcher(),
new Matcher\DoubleMatcher(),
new Matcher\DoubleMatcher($parser),
new Matcher\NumberMatcher(),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher()
));
}

/**
* @return Parser
*/
protected function buildParser()
{
return new Parser(new Lexer());
}
}
Loading