Skip to content

Commit

Permalink
Merge pull request #7 from mficzel/task/throwExceptionsOnSytaxErrors
Browse files Browse the repository at this point in the history
feat: improve afx exceptions
  • Loading branch information
mficzel authored Apr 25, 2017
2 parents 27b9ffd + b147c90 commit 3747404
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 71 deletions.
9 changes: 8 additions & 1 deletion src/Expression/Children.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace PackageFactory\Afx\Expression;

use PackageFactory\Afx\Exception;
use PackageFactory\Afx\Lexer;

class Children
Expand All @@ -9,7 +10,11 @@ public static function parse(Lexer $lexer)
{
$contents = [];
$currentText = '';
while (!$lexer->isEnd()) {
while (true) {
if ($lexer->isEnd()) {
throw new Exception('Unfinished child-list');
}

if ($lexer->isOpeningBracket()) {
$lexer->consume();

Expand Down Expand Up @@ -56,6 +61,8 @@ public static function parse(Lexer $lexer)
continue;
}



$currentText .= $lexer->consume();
}

Expand Down
35 changes: 25 additions & 10 deletions src/Expression/Identifier.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace PackageFactory\Afx\Expression;

use PackageFactory\Afx\Exception;
use PackageFactory\Afx\Lexer;

class Identifier
Expand All @@ -9,16 +10,30 @@ public static function parse(Lexer $lexer)
{
$identifier = '';

while ($lexer->isAlphaNumeric() ||
$lexer->isDot() ||
$lexer->isColon() ||
$lexer->isMinus() ||
$lexer->isUnderscore() ||
$lexer->isAt()
) {
$identifier .= $lexer->consume();
while (true) {
switch (true) {
case $lexer->isAlphaNumeric():
case $lexer->isDot():
case $lexer->isColon():
case $lexer->isMinus():
case $lexer->isUnderscore():
case $lexer->isAt():
$identifier .= $lexer->consume();
break;
case $lexer->isEqualSign():
case $lexer->isWhiteSpace():
case $lexer->isClosingBracket():
case $lexer->isForwardSlash():
return $identifier;
break;
default:
$unexpected_character = $lexer->consume();
throw new Exception(sprintf(
'Unexpected character "%s" in identifier "%s"',
$unexpected_character,
$identifier
));
}
}

return $identifier;
}
}
125 changes: 65 additions & 60 deletions src/Expression/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,89 +17,94 @@ public static function parse(Lexer $lexer)
}

$identifier = Identifier::parse($lexer);
$props = [];
$children = [];

if ($lexer->isWhitespace()) {
while ($lexer->isWhitespace()) {
$lexer->consume();
}
while (!$lexer->isForwardSlash() && !$lexer->isClosingBracket()) {
list($propIdentifier, $value) = Prop::parse($lexer);
$props[$propIdentifier] = $value;
try {
$props = [];
$children = [];

if ($lexer->isWhitespace()) {
while ($lexer->isWhitespace()) {
$lexer->consume();
}
while (!$lexer->isForwardSlash() && !$lexer->isClosingBracket()) {
list($propIdentifier, $value) = Prop::parse($lexer);
$props[$propIdentifier] = $value;
while ($lexer->isWhitespace()) {
$lexer->consume();
}
}
}
}

if ($lexer->isForwardSlash()) {
$lexer->consume();
if ($lexer->isForwardSlash()) {
$lexer->consume();

if ($lexer->isClosingBracket()) {
$lexer->consume();

return [
'identifier' => $identifier,
'props' => $props,
'children' => $children,
'selfClosing' => true
];
} else {
throw new Exception(sprintf('Self closing tag "%s" misses closing bracket.', $identifier));
}
}

if ($lexer->isClosingBracket()) {
$lexer->consume();

return [
'identifier' => $identifier,
'props' => $props,
'children' => $children,
'selfClosing' => true
];
} else {
throw new Exception(sprintf('Self closing tag "%s" misses closing bracket.', $identifier));
throw new Exception(sprintf('Tag "%s" did not end with closing bracket.', $identifier));
}
}

if ($lexer->isClosingBracket()) {
$lexer->consume();
} else {
throw new Exception(sprintf('Tag "%s" did not end with closing bracket.', $identifier));
}

$children = Children::parse($lexer);

if ($lexer->isOpeningBracket()) {
$lexer->consume();
$children = Children::parse($lexer);

if ($lexer->isForwardSlash()) {
if ($lexer->isOpeningBracket()) {
$lexer->consume();

if ($lexer->isForwardSlash()) {
$lexer->consume();
} else {
throw new Exception(sprintf(
'Opening-bracket for closing of tag "%s" was not followed by slash.',
$identifier
));
}
} else {
throw new Exception(sprintf(
'Opening-bracket for closing of tag "%s" was not followed by slash.',
'Opening-bracket for closing of tag "%s" expected.',
$identifier
));
}
} else {
throw new Exception(sprintf(
'Opening-bracket for closing of tag "%s" expected.',
$identifier
));
}

$closingIdentifier = Identifier::parse($lexer);
$closingIdentifier = Identifier::parse($lexer);

if ($closingIdentifier !== $identifier) {
throw new Exception(sprintf(
'Closing-tag identifier "%s" did not match opening-tag identifier "%s".',
$closingIdentifier,
$identifier
));
}
if ($closingIdentifier !== $identifier) {
throw new Exception(sprintf(
'Closing-tag identifier "%s" did not match opening-tag identifier "%s".',
$closingIdentifier,
$identifier
));
}

if ($lexer->isClosingBracket()) {
$lexer->consume();
return [
'identifier' => $identifier,
'props' => $props,
'children' => $children,
'selfClosing' => false
];
} else {
throw new Exception(sprintf('Closing tag "%s" did not end with closing-bracket.', $identifier));
}
if ($lexer->isClosingBracket()) {
$lexer->consume();
return [
'identifier' => $identifier,
'props' => $props,
'children' => $children,
'selfClosing' => false
];
} else {
throw new Exception(sprintf('Closing tag "%s" did not end with closing-bracket.', $identifier));
}

if ($lexer->isEnd()) {
throw new Exception(sprintf('Tag "%s" was is not closed.', $identifier));
if ($lexer->isEnd()) {
throw new Exception(sprintf('Tag was %s is not closed.', $identifier));
}
} catch (Exception $e) {
throw new Exception(sprintf('<%s> %s', $identifier, $e->getMessage()));
}
}
}

0 comments on commit 3747404

Please sign in to comment.