Skip to content
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

Debug exceptions added #127

Merged
merged 6 commits into from
Sep 18, 2016
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
4 changes: 2 additions & 2 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private function executeTssRule($rule, $template, $config) {
$rule->touch();

$pseudoMatcher = $config->createPseudoMatcher($rule->pseudo);
$hook = new Hook\PropertyHook($rule->properties, $this->baseDir, $rule->baseDir, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet());
$hook = new Hook\PropertyHook($rule->properties, $this->baseDir, $config->getLine(), $rule->file, $rule->line, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet());
$config->loadProperties($hook);
$template->addHook($rule->query, $hook);
}
Expand All @@ -107,7 +107,7 @@ private function getRules($template, $config) {
//Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet
$rules = $this->cache->load($key, filemtime($this->tss));

if (!$rules) return $this->cache->write($key, (new Parser\Sheet(file_get_contents($this->tss), $this->baseDir, $config->getCssToXpath(), $config->getValueParser()))->parse());
if (!$rules) return $this->cache->write($key, (new Parser\Sheet(file_get_contents($this->tss), $this->tss, $config->getCssToXpath(), $config->getValueParser()))->parse());
else return $rules;
}
else return (new Parser\Sheet($this->tss, $this->baseDir, $config->getCssToXpath(), $config->getValueParser()))->parse();
Expand Down
5 changes: 5 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Config {
private $headers;
private $formatter;
private $baseDir;
private $line = 0;
private $elementData;
private $xPath;
private $valueParser;
Expand All @@ -38,6 +39,10 @@ public function &getBaseDir() {
return $this->baseDir;
}

public function &getLine() {
return $this->line;
}

public function registerFormatter($formatter) {
$this->formatter->register($formatter);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Transphporm;
class Exception extends \Exception {
const PROPERTY = 'property';
const TSS_FUNCTION = 'function';
const PSEUDO = 'pseudo';
const FORMATTER = 'formatter';

public function __construct(RunException $runException, $file, $line) {
$message = $runException->getMessage() . ' on Line ' . $line . ' of ' . ($file === null ? 'tss' : $file);

parent::__construct($message, 0, $runException->getPrevious());
}
}
23 changes: 14 additions & 9 deletions src/FunctionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ public function __construct(Hook\ElementData $elementData) {
}

public function __call($name, $args) {
if (!($this->functions[$name] instanceof TSSFunction\Data)) {
$tokens = $args[0];
$parser = new \Transphporm\Parser\Value($this);
$args[0] = $parser->parseTokens($tokens, $this->elementData->getData($this->element));
try {
if (!($this->functions[$name] instanceof TSSFunction\Data)) {
$tokens = $args[0];
$parser = new \Transphporm\Parser\Value($this);
$args[0] = $parser->parseTokens($tokens, $this->elementData->getData($this->element));
}
else if ($args[0] instanceof Parser\Tokens) {
$args[0] = iterator_to_array($args[0]);
}
if (isset($this->functions[$name])) {
return $this->functions[$name]->run($args[0], $this->element);
}
}
else if ($args[0] instanceof Parser\Tokens) {
$args[0] = iterator_to_array($args[0]);
}
if (isset($this->functions[$name])) {
return $this->functions[$name]->run($args[0], $this->element);
catch (\Exception $e) {
throw new RunException(Exception::TSS_FUNCTION, $name, $e);
}
return true;
}
Expand Down
16 changes: 13 additions & 3 deletions src/Hook/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@ public function register($formatter) {
public function format($value, $rules) {
if (!isset($rules['format'])) return $value;
$tokens = $rules['format'];

$functionName = $tokens->from(\Transphporm\Parser\Tokenizer::NAME, true)->read();

$options = [];
foreach (new \Transphporm\Parser\TokenFilterIterator($tokens->from(\Transphporm\Parser\Tokenizer::NAME), [\Transphporm\Parser\Tokenizer::WHITESPACE]) as $token) {
foreach (new \Transphporm\Parser\TokenFilterIterator($tokens->from(\Transphporm\Parser\Tokenizer::NAME),
[\Transphporm\Parser\Tokenizer::WHITESPACE]) as $token) {
$options[] = $token['value'];
}
return $this->processFormat($options, $functionName, $value);

try {
return $this->processFormat($options, $functionName, $value);
}
catch (\Exception $e) {
throw new \Transphporm\RunException(\Transphporm\Exception::FORMATTER, $functionName, $e);
}
}

private function processFormat($format, $functionName, $value) {
$functionExists = false;
foreach ($value as &$val) {
foreach ($this->formatters as $formatter) {
if (is_callable([$formatter, $functionName])) {
$val = call_user_func_array([$formatter, $functionName], array_merge([$val], $format));
$functionExists = true;
}
}
}
if (!$functionExists) throw new \Exception("Formatter '$functionName' does not exist");
return $value;
}
}
48 changes: 33 additions & 15 deletions src/Hook/PropertyHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,45 @@
/** Hooks into the template system, gets assigned as `ul li` or similar and `run()` is called with any elements that match */
class PropertyHook implements \Transphporm\Hook {
private $rules;
private $origBaseDir;
private $newBaseDir;
private $baseDir;
private $configLine;
private $file;
private $line;
private $valueParser;
private $pseudoMatcher;
private $properties = [];
private $functionSet;

public function __construct(array $rules, &$origBaseDir, $newBaseDir, PseudoMatcher $pseudoMatcher, \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet) {
public function __construct(array $rules, &$baseDir, &$configLine, $file, $line, PseudoMatcher $pseudoMatcher, \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet) {
$this->rules = $rules;
$this->origBaseDir = $origBaseDir;
$this->newBaseDir = $newBaseDir;
$this->baseDir = &$baseDir;
$this->configLine = &$configLine;
$this->file = $file;
$this->line = $line;
$this->valueParser = $valueParser;
$this->pseudoMatcher = $pseudoMatcher;
$this->functionSet = $functionSet;
}

public function run(\DomElement $element) {
$this->functionSet->setElement($element);
$this->origBaseDir = $this->newBaseDir;
//Don't run if there's a pseudo element like nth-child() and this element doesn't match it
if (!$this->pseudoMatcher->matches($element)) return;
if ($this->file !== null) $this->baseDir = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR;
$this->configLine = $this->line;
try {
//Don't run if there's a pseudo element like nth-child() and this element doesn't match it
if (!$this->pseudoMatcher->matches($element)) return;

// TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens
// problem with this is that anything in data changed by run properties is not shown
// TODO: Allow `update-frequency` to be parsed before it is accessed in rule (might need to switch location of rule check)
// TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens
// problem with this is that anything in data changed by run properties is not shown
// TODO: Allow `update-frequency` to be parsed before it is accessed in rule (might need to switch location of rule check)

foreach ($this->rules as $name => $value) {
$result = $this->callProperty($name, $element, $this->getArgs($value));
if ($result === false) break;
foreach ($this->rules as $name => $value) {
$result = $this->callProperty($name, $element, $this->getArgs($value));
if ($result === false) break;
}
}
catch (\Transphporm\RunException $e) {
throw new \Transphporm\Exception($e, $this->file, $this->line);
}
}

Expand All @@ -49,6 +59,14 @@ public function registerProperty($name, \Transphporm\Property $property) {
}

private function callProperty($name, $element, $value) {
if (isset($this->properties[$name])) return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties);
if (isset($this->properties[$name])) {
try {
return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties);
}
catch (\Exception $e) {
if ($e instanceof \Transphporm\RunException) throw $e;
throw new \Transphporm\RunException(\Transphporm\Exception::PROPERTY, $name, $e);
}
}
}
}
10 changes: 7 additions & 3 deletions src/Hook/PseudoMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public function registerFunction(\Transphporm\Pseudo $pseudo) {

public function matches($element) {
$matches = true;

foreach ($this->pseudo as $tokens) {
foreach ($this->functions as $function) {
$parts = $this->getFuncParts($tokens);
$matches = $matches && $function->match($parts['name'], $parts['args'], $element);
try {
$parts = $this->getFuncParts($tokens);
$matches = $matches && $function->match($parts['name'], $parts['args'], $element);
}
catch (\Exception $e) {
throw new \Transphporm\RunException(\Transphporm\Exception::PSEUDO, $parts['name'], $e);
}
}
}
return $matches;
Expand Down
2 changes: 1 addition & 1 deletion src/Module/Basics.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function load(\Transphporm\Config $config) {
$headers = &$config->getHeaders();

$config->registerProperty('content', new \Transphporm\Property\Content($headers, $config->getFormatter()));
$config->registerProperty('repeat', new \Transphporm\Property\Repeat($data, $config->getElementData(), $config->getBaseDir()));
$config->registerProperty('repeat', new \Transphporm\Property\Repeat($data, $config->getElementData(), $config->getBaseDir(), $config->getLine()));
$config->registerProperty('display', new \Transphporm\Property\Display);
$config->registerProperty('bind', new \Transphporm\Property\Bind($config->getElementData()));
}
Expand Down
22 changes: 14 additions & 8 deletions src/Parser/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,39 @@
/** Parses a .tss file into individual rules, each rule has a query e,g, `ul li` and a set of rules e.g. `display: none; bind: iteration(id);` */
class Sheet {
private $tss;
private $baseDir;
private $file;
private $valueParser;
private $xPath;
private $tokenizer;

public function __construct($tss, $baseDir, CssToXpath $xPath, Value $valueParser) {
public function __construct($tss, $file, CssToXpath $xPath, Value $valueParser) {
$this->tss = $this->stripComments($tss, '//', "\n");
$this->tss = $this->stripComments($this->tss, '/*', '*/');
$this->tokenizer = new Tokenizer($this->tss);
$this->tss = $this->tokenizer->getTokens();
$this->baseDir = $baseDir;
$this->file = $file;
$this->xPath = $xPath;
$this->valueParser = $valueParser;
}

public function parse($indexStart = 0) {
$rules = [];
$line = 1;
foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) {
if ($processing = $this->processingInstructions($token, count($rules)+$indexStart)) {
$this->tss->skip($processing['skip']+1);
$rules = array_merge($rules, $processing['rules']);
continue;
}
else if ($token['type'] === Tokenizer::NEW_LINE) {
$line++;
continue;
}
$selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE);
$this->tss->skip(count($selector));
if (count($selector) === 0) break;

$newRules = $this->cssToRules($selector, count($rules)+$indexStart, $this->getProperties($this->tss->current()['value']));
$newRules = $this->cssToRules($selector, count($rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $line);
$rules = $this->writeRule($rules, $newRules);
}
usort($rules, [$this, 'sortRules']);
Expand All @@ -47,12 +52,12 @@ private function checkError($rules) {
if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed');
}

private function CssToRules($selector, $index, $properties) {
private function CssToRules($selector, $index, $properties, $line) {
$parts = $selector->trim()->splitOnToken(Tokenizer::ARG);
$rules = [];
foreach ($parts as $part) {
$part = $part->trim();
$rules[$this->tokenizer->serialize($part)] = new \Transphporm\Rule($this->xPath->getXpath($part), $this->xPath->getPseudo($part), $this->xPath->getDepth($part), $index++, $this->baseDir);
$rules[$this->tokenizer->serialize($part)] = new \Transphporm\Rule($this->xPath->getXpath($part), $this->xPath->getPseudo($part), $this->xPath->getDepth($part), $index++, $this->file, $line);
$rules[$this->tokenizer->serialize($part)]->properties = $properties;
}
return $rules;
Expand Down Expand Up @@ -81,8 +86,9 @@ private function processingInstructions($token, $indexStart) {
}

private function import($args, $indexStart) {
$fileName = $args[0];
$sheet = new Sheet(file_get_contents($this->baseDir . $fileName), dirname(realpath($this->baseDir . $fileName)) . DIRECTORY_SEPARATOR, $this->xPath, $this->valueParser);
if ($this->file !== null) $fileName = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR . $args[0];
else $fileName = $args[0];
$sheet = new Sheet(file_get_contents($fileName), $fileName, $this->xPath, $this->valueParser);
return $sheet->parse($indexStart);
}

Expand Down
9 changes: 5 additions & 4 deletions src/Parser/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Tokenizer {
const CONCAT = 'CONCAT';
const ARG = 'ARG';
const WHITESPACE = 'WHITESPACE';
const NEW_LINE = 'NEW_LINE';
const DOT = 'DOT';
const NUMERIC = 'NUMERIC';
const EQUALS = 'EQUALS';
Expand Down Expand Up @@ -49,7 +50,7 @@ class Tokenizer {
'>' => self::GREATER_THAN,
'@' => self::AT_SIGN,
' ' => self::WHITESPACE,
"\n" => self::WHITESPACE,
"\n" => self::NEW_LINE,
"\r" => self::WHITESPACE,
"\t" => self::WHITESPACE
];
Expand All @@ -75,7 +76,7 @@ public function getTokens($returnObj = true) {

private function doSimpleTokens(&$tokens, $char) {
if (in_array($char, [Tokenizer::ARG, Tokenizer::CONCAT, Tokenizer::DOT, Tokenizer::NOT,
Tokenizer::EQUALS, Tokenizer::COLON, Tokenizer::SEMI_COLON, Tokenizer::WHITESPACE,
Tokenizer::EQUALS, Tokenizer::COLON, Tokenizer::SEMI_COLON, Tokenizer::WHITESPACE, Tokenizer::NEW_LINE,
Tokenizer::NUM_SIGN, Tokenizer::GREATER_THAN, Tokenizer::AT_SIGN])) {
$tokens[] = ['type' => $char];
}
Expand Down Expand Up @@ -170,7 +171,7 @@ public function serialize($tokens) {
private function serializeValue($token) {
if (isset($token['value'])) {
if ($token['value'] instanceof Tokens) return $this->serialize($token['value']);
else return $token['value'];
}
else return $token['value'];
}
}
}
2 changes: 1 addition & 1 deletion src/Parser/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function parseTokens($tokens, $data = null) {

if (count($tokens) <= 0) return [$data];

foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE]) as $token) {
foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE]) as $token) {
$this->{$this->tokenFuncs[$token['type']]}($token);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Property/Repeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class Repeat implements \Transphporm\Property {
private $functionSet;
private $elementData;
private $baseDir;
private $line;

public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$baseDir) {
public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$baseDir, &$line) {
$this->functionSet = $functionSet;
$this->elementData = $elementData;
$this->baseDir = &$baseDir;
$this->line = &$line;
}

public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
Expand Down Expand Up @@ -60,7 +62,7 @@ private function getMax($values) {
}

private function createHook($newRules, $pseudoMatcher, $properties) {
$hook = new \Transphporm\Hook\PropertyHook($newRules, $this->baseDir, $this->baseDir, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet);
$hook = new \Transphporm\Hook\PropertyHook($newRules, $this->baseDir, $this->line, $this->baseDir, $this->line, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet);
foreach ($properties as $name => $property) $hook->registerProperty($name, $property);
return $hook;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Pseudo/Nth.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class Nth implements \Transphporm\Pseudo {
private $count = 0;

public function match($name, $args, \DomElement $element) {

if ($name !== 'nth-child') return true;

$this->count++;
$criteria = $args[0];


if (is_callable([$this, $criteria])) return $this->$criteria($this->count);
else if (!is_numeric($criteria)) throw new \Exception("Argument passed to 'nth-child' must be 'odd', 'even', or of type int");
else return $this->count == $criteria;
}

Expand Down
Loading