Skip to content

Commit

Permalink
Last Grammar cleanup
Browse files Browse the repository at this point in the history
Over the process of our cleanups, we went from static function to numerous
instances of the class, which meant str_replace(ish) things being done
more often than necessary.

I've brought this back to just a single instance which a lot of classes
have a reference to. What we get as an advantage over the original
situation is that not only do we now just have cleaner code that is more
readable, it is now also possible to reuse large portions of the code with
a different Grammar.
  • Loading branch information
JasperHorn committed Aug 14, 2013
1 parent fc3dbaa commit c066116
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 39 deletions.
31 changes: 14 additions & 17 deletions Good/Looking/AbstractSyntax/ElementWithStatements.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@

abstract class ElementWithStatements implements Element
{
protected static $grammar = null;
private $grammar;

public function __construct()
public function __construct(Grammar $grammar)
{
if (self::$grammar == null)
{
self::$grammar = new Grammar();
}
$this->grammar = $grammar;
}

protected function evaluate($evaluateString)
Expand All @@ -30,7 +27,7 @@ protected function evaluate($evaluateString)
throw new Exception('Empty statement is not a statement at all.');
}

if (\preg_match('/' . self::$grammar->expression . '/', $evaluateString) == 0)
if (\preg_match('/' . $this->grammar->expression . '/', $evaluateString) == 0)
{
throw new \Exception("Syntax error");
}
Expand All @@ -39,28 +36,28 @@ protected function evaluate($evaluateString)

while ($evaluateString != '')
{
if (\preg_match('/^\s*' . self::$grammar->term .
if (\preg_match('/^\s*' . $this->grammar->term .
'\s*(?P<op>(?P>operator))?\s*/', $evaluateString, $matches) == 0)
{
throw new \Exception("Syntax Error");
}

$evaluateString = \preg_replace('/^\s*' . self::$grammar->term .
$evaluateString = \preg_replace('/^\s*' . $this->grammar->term .
'\s*(?P<op>(?P>operator))?\s*/', '', $evaluateString);

$term = $matches['term'];
$operator = \array_key_exists('op', $matches) ? $matches['op'] : '';

if (\preg_match('/^\(' . self::$grammar->expression . '\)$/', $term) != 0)
if (\preg_match('/^\(' . $this->grammar->expression . '\)$/', $term) != 0)
{
$output .= '(' . $this->evaluate(substr($term, 1, -1)) . ')';
}
else if (\preg_match('/^' . self::$grammar->literalBoolean . '$/',
else if (\preg_match('/^' . $this->grammar->literalBoolean . '$/',
$term, $matches) != 0)
{
$output .= $matches['boolean'];
}
else if (\preg_match('/^' . self::$grammar->variable . '$/',
else if (\preg_match('/^' . $this->grammar->variable . '$/',
$term, $matches) != 0)
{
$templateVariable = '$this->getVar(\'' . $matches['varName'] . '\')';
Expand All @@ -69,10 +66,10 @@ protected function evaluate($evaluateString)

while ($arrayItemSelector != '')
{
\preg_match('/^\[' . self::$grammar->expression . '\]/',
\preg_match('/^\[' . $this->grammar->expression . '\]/',
$arrayItemSelector, $matches);
$arrayItemSelector = \preg_replace('/^\['
. self::$grammar->expression . '\]/',
. $this->grammar->expression . '\]/',
'', $arrayItemSelector);

$templateVariable = '$this->arrayItem(' . $templateVariable . ', ' .
Expand All @@ -81,15 +78,15 @@ protected function evaluate($evaluateString)

$output .= $templateVariable;
}
else if (preg_match('/^' . self::$grammar->literalString . '$/', $term) != 0)
else if (preg_match('/^' . $this->grammar->literalString . '$/', $term) != 0)
{
$output .= $term;
}
else if (preg_match('/^' . self::$grammar->literalNumber . '$/', $term) != 0)
else if (preg_match('/^' . $this->grammar->literalNumber . '$/', $term) != 0)
{
$output .= $term;
}
else if (preg_match('/^' . self::$grammar->func . '$/', $term) != 0)
else if (preg_match('/^' . $this->grammar->func . '$/', $term) != 0)
{
// as of yet, functions are unsupported
throw new \Exception("Function call found while functions are currently unsupported");
Expand Down
19 changes: 14 additions & 5 deletions Good/Looking/AbstractSyntax/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

namespace Good\Looking\AbstractSyntax;

use Good\Looking\Grammar;

class Factory
{
private $grammar;

public function __construct(Grammar $grammar)
{
$this->grammar = $grammar;
}

public function createAbstractDocument($statements)
{
return new Document($statements);
Expand All @@ -16,27 +25,27 @@ public function createTextBlock($text)

public function createStatement($code)
{
return new Statement($code);
return new Statement($this->grammar, $code);
}

public function createIfStructure($condition, $statements)
{
return new IfStructure($condition, $statements);
return new IfStructure($this->grammar, $condition, $statements);
}

public function createIfElseStructure($condition, $statements, $elseStatements)
{
return new IfElseStructure($condition, $statements, $elseStatements);
return new IfElseStructure($this->grammar, $condition, $statements, $elseStatements);
}

public function createForStructure($from, $to, $statements)
{
return new ForStructure($from, $to, $statements);
return new ForStructure($this->grammar, $from, $to, $statements);
}

public function createForeachStructure($array, $varName, $statements)
{
return new ForeachStructure($array, $varName, $statements);
return new ForeachStructure($this->grammar, $array, $varName, $statements);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Good/Looking/AbstractSyntax/ForStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class ForStructure extends ElementWithStatements
private $to;
private $statements;

public function __construct($from, $to, $statements)
public function __construct($grammar, $from, $to, $statements)
{
parent::__construct();
parent::__construct($grammar);

$this->statements = $statements;
$this->from = $from;
Expand Down
4 changes: 2 additions & 2 deletions Good/Looking/AbstractSyntax/ForeachStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class ForeachStructure extends ElementWithStatements
private $arrayStatement;
private $statements;

public function __construct($array, $varName, $statements)
public function __construct($grammar, $array, $varName, $statements)
{
parent::__construct();
parent::__construct($grammar);

$this->statements = $statements;
$this->arrayStatement = $array;
Expand Down
4 changes: 2 additions & 2 deletions Good/Looking/AbstractSyntax/IfElseStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class IfElseStructure extends ElementWithStatements
private $statements;
private $elseStatements;

public function __construct($condition, $statements, $elseStatements)
public function __construct($grammar, $condition, $statements, $elseStatements)
{
parent::__construct();
parent::__construct($grammar);

$this->condition = $condition;
$this->statements = $statements;
Expand Down
4 changes: 2 additions & 2 deletions Good/Looking/AbstractSyntax/IfStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class IfStructure extends ElementWithStatements
private $condition;
private $statements;

public function __construct($condition, $statements)
public function __construct($grammar, $condition, $statements)
{
parent::__construct();
parent::__construct($grammar);

$this->condition = $condition;
$this->statements = $statements;
Expand Down
4 changes: 2 additions & 2 deletions Good/Looking/AbstractSyntax/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Statement extends ElementWithStatements
{
private $code;

public function __construct($code)
public function __construct($grammar, $code)
{
parent::__construct();
parent::__construct($grammar);

$this->code = $code;
}
Expand Down
9 changes: 6 additions & 3 deletions Good/Looking/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

Class Compiler
{
public function __construct()
private $grammar;

public function __construct(Grammar $grammar)
{
$this->grammar = $grammar;
}

public function compile($input, $output)
Expand All @@ -20,8 +23,8 @@ public function compile($input, $output)

private function compileTemplate($input)
{
$factory = new AbstractSyntax\Factory();
$parser = new Parser($factory);
$factory = new AbstractSyntax\Factory($this->grammar);
$parser = new Parser($this->grammar, $factory);

$document = $parser->parseDocument($input);

Expand Down
4 changes: 2 additions & 2 deletions Good/Looking/Looking.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct($fileName)
public function registerVar($varName, $varValue)
{
if (\preg_match('/'. $this->grammar->varName .'/', $varName) === 1)
// if the varName matches variable syntax and does not match a control structure
// if the varName matches variable syntax (and does not match a keyword)
{
$this->registeredVars[$varName] = $varValue;

Expand Down Expand Up @@ -47,7 +47,7 @@ public function display()
if (!\file_exists($this->templateFileName . '.compiledTemplate') ||
\filemtime($this->templateFileName) > \filemtime($this->templateFileName . '.compiledTemplate'))
{
$compiler = new Compiler();
$compiler = new Compiler($this->grammar);
$compiler->compile($this->templateFileName, $this->templateFileName . '.compiledTemplate');
}

Expand Down
4 changes: 2 additions & 2 deletions Good/Looking/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class Parser
protected $inTextMode;
private $grammar;

public function __construct(AbstractSyntax\Factory $factory)
public function __construct(Grammar $grammar, AbstractSyntax\Factory $factory)
{
$this->factory = $factory;
$this->grammar = new Grammar();
$this->grammar = $grammar;
}

public function parseDocument($input)
Expand Down

0 comments on commit c066116

Please sign in to comment.