diff --git a/src/Expression/Children.php b/src/Expression/Children.php index 5825772..78e4508 100644 --- a/src/Expression/Children.php +++ b/src/Expression/Children.php @@ -1,6 +1,7 @@ isEnd()) { + while (true) { + if ($lexer->isEnd()) { + throw new Exception('Unfinished child-list'); + } + if ($lexer->isOpeningBracket()) { $lexer->consume(); @@ -56,6 +61,8 @@ public static function parse(Lexer $lexer) continue; } + + $currentText .= $lexer->consume(); } diff --git a/src/Expression/Identifier.php b/src/Expression/Identifier.php index ce8df46..171a83d 100644 --- a/src/Expression/Identifier.php +++ b/src/Expression/Identifier.php @@ -1,6 +1,7 @@ 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; } } diff --git a/src/Expression/Node.php b/src/Expression/Node.php index ab1c694..ecb1aec 100644 --- a/src/Expression/Node.php +++ b/src/Expression/Node.php @@ -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())); } } }