Skip to content

Commit

Permalink
Merge pull request #6 from mficzel/task/throwExceptionsOnSytaxErrors
Browse files Browse the repository at this point in the history
Task/throw exceptions on sytax errors
  • Loading branch information
grebaldi authored Apr 24, 2017
2 parents 37d875f + 69f3994 commit 27b9ffd
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 47 deletions.
5 changes: 4 additions & 1 deletion src/Expression/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ public static function parse(Lexer $lexer)
{
$contents = '';
$braceCount = 0;

if ($lexer->isOpeningBrace()) {
$lexer->consume();
} else {
throw new Exception('Expression without braces');
}

while (true) {
if ($lexer->isEnd()) {
throw new Exception('Unclosed Expression');
throw new Exception(sprintf('Unfinished Expression "%s"', $contents));
}

if ($lexer->isOpeningBrace()) {
Expand Down
3 changes: 0 additions & 3 deletions src/Expression/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ class Identifier
public static function parse(Lexer $lexer)
{
$identifier = '';
if ($lexer->isAlpha()) {
$identifier .= $lexer->consume();
}

while ($lexer->isAlphaNumeric() ||
$lexer->isDot() ||
Expand Down
45 changes: 31 additions & 14 deletions src/Expression/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,61 @@ public static function parse(Lexer $lexer)
'children' => $children,
'selfClosing' => true
];
} else {
throw new Exception(sprintf('Self closing tag "%s" misses 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);

while ($lexer->isWhitespace()) {
$lexer->consume();
}

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" expected.',
$identifier
));
}

$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 ($lexer->isClosingBracket()) {
$lexer->consume();

if ($closingIdentifier === $identifier) {
return [
'identifier' => $identifier,
'props' => $props,
'children' => $children,
'selfClosing' => false
];
}
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));
throw new Exception(sprintf('Tag "%s" was is not closed.', $identifier));
}
}
}
55 changes: 27 additions & 28 deletions src/Expression/Prop.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 Prop
Expand All @@ -12,39 +13,37 @@ public static function parse(Lexer $lexer)
}

$identifier = Identifier::parse($lexer);
$value = [
'type' => 'boolean',
'payload' => false
];

if ($lexer->isEqualSign()) {
$lexer->consume();
}

switch (true) {
case $lexer->isSingleQuote():
case $lexer->isDoubleQuote():
$value = [
'type' => 'string',
'payload' => StringLiteral::parse($lexer)
];
break;

case $lexer->isOpeningBrace():
$value = [
'type' => 'expression',
'payload' => Expression::parse($lexer)
];
break;

case $lexer->isWhiteSpace():
case $lexer->isForwardSlash():
case $lexer->isClosingBracket():
$value = [
switch (true) {
case $lexer->isSingleQuote():
case $lexer->isDoubleQuote():
$value = [
'type' => 'string',
'payload' => StringLiteral::parse($lexer)
];
break;

case $lexer->isOpeningBrace():
$value = [
'type' => 'expression',
'payload' => Expression::parse($lexer)
];
break;
default:
throw new Exception(sprintf(
'Prop-assignment "%s" was not followed by quotes or braces',
$identifier
));
}
} elseif ($lexer->isWhiteSpace() || $lexer->isForwardSlash() || $lexer->isClosingBracket()) {
$value = [
'type' => 'boolean',
'payload' => true
];
break;
];
} else {
throw new Exception(sprintf('Prop identifier "%s" is neither assignment nor boolean', $identifier));
}

return [$identifier, $value];
Expand Down
4 changes: 3 additions & 1 deletion src/Expression/StringLiteral.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public static function parse(Lexer $lexer)
$willBeEscaped = false;
if ($lexer->isSingleQuote() || $lexer->isDoubleQuote()) {
$openingQuoteSign = $lexer->consume();
} else {
throw new Exception('Unquoted String literal');
}

while (true) {
if ($lexer->isEnd()) {
throw new Exception('Unclosed string literal');
throw new Exception(sprintf('Unfinished string literal "%s"', $contents));
}

if ($lexer->isBackSlash() && !$willBeEscaped) {
Expand Down

0 comments on commit 27b9ffd

Please sign in to comment.