Skip to content

Commit 3747404

Browse files
authored
Merge pull request #7 from mficzel/task/throwExceptionsOnSytaxErrors
feat: improve afx exceptions
2 parents 27b9ffd + b147c90 commit 3747404

3 files changed

Lines changed: 98 additions & 71 deletions

File tree

src/Expression/Children.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace PackageFactory\Afx\Expression;
33

4+
use PackageFactory\Afx\Exception;
45
use PackageFactory\Afx\Lexer;
56

67
class Children
@@ -9,7 +10,11 @@ public static function parse(Lexer $lexer)
910
{
1011
$contents = [];
1112
$currentText = '';
12-
while (!$lexer->isEnd()) {
13+
while (true) {
14+
if ($lexer->isEnd()) {
15+
throw new Exception('Unfinished child-list');
16+
}
17+
1318
if ($lexer->isOpeningBracket()) {
1419
$lexer->consume();
1520

@@ -56,6 +61,8 @@ public static function parse(Lexer $lexer)
5661
continue;
5762
}
5863

64+
65+
5966
$currentText .= $lexer->consume();
6067
}
6168

src/Expression/Identifier.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace PackageFactory\Afx\Expression;
33

4+
use PackageFactory\Afx\Exception;
45
use PackageFactory\Afx\Lexer;
56

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

12-
while ($lexer->isAlphaNumeric() ||
13-
$lexer->isDot() ||
14-
$lexer->isColon() ||
15-
$lexer->isMinus() ||
16-
$lexer->isUnderscore() ||
17-
$lexer->isAt()
18-
) {
19-
$identifier .= $lexer->consume();
13+
while (true) {
14+
switch (true) {
15+
case $lexer->isAlphaNumeric():
16+
case $lexer->isDot():
17+
case $lexer->isColon():
18+
case $lexer->isMinus():
19+
case $lexer->isUnderscore():
20+
case $lexer->isAt():
21+
$identifier .= $lexer->consume();
22+
break;
23+
case $lexer->isEqualSign():
24+
case $lexer->isWhiteSpace():
25+
case $lexer->isClosingBracket():
26+
case $lexer->isForwardSlash():
27+
return $identifier;
28+
break;
29+
default:
30+
$unexpected_character = $lexer->consume();
31+
throw new Exception(sprintf(
32+
'Unexpected character "%s" in identifier "%s"',
33+
$unexpected_character,
34+
$identifier
35+
));
36+
}
2037
}
21-
22-
return $identifier;
2338
}
2439
}

src/Expression/Node.php

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,89 +17,94 @@ public static function parse(Lexer $lexer)
1717
}
1818

1919
$identifier = Identifier::parse($lexer);
20-
$props = [];
21-
$children = [];
2220

23-
if ($lexer->isWhitespace()) {
24-
while ($lexer->isWhitespace()) {
25-
$lexer->consume();
26-
}
27-
while (!$lexer->isForwardSlash() && !$lexer->isClosingBracket()) {
28-
list($propIdentifier, $value) = Prop::parse($lexer);
29-
$props[$propIdentifier] = $value;
21+
try {
22+
$props = [];
23+
$children = [];
24+
25+
if ($lexer->isWhitespace()) {
3026
while ($lexer->isWhitespace()) {
3127
$lexer->consume();
3228
}
29+
while (!$lexer->isForwardSlash() && !$lexer->isClosingBracket()) {
30+
list($propIdentifier, $value) = Prop::parse($lexer);
31+
$props[$propIdentifier] = $value;
32+
while ($lexer->isWhitespace()) {
33+
$lexer->consume();
34+
}
35+
}
3336
}
34-
}
3537

36-
if ($lexer->isForwardSlash()) {
37-
$lexer->consume();
38+
if ($lexer->isForwardSlash()) {
39+
$lexer->consume();
40+
41+
if ($lexer->isClosingBracket()) {
42+
$lexer->consume();
43+
44+
return [
45+
'identifier' => $identifier,
46+
'props' => $props,
47+
'children' => $children,
48+
'selfClosing' => true
49+
];
50+
} else {
51+
throw new Exception(sprintf('Self closing tag "%s" misses closing bracket.', $identifier));
52+
}
53+
}
3854

3955
if ($lexer->isClosingBracket()) {
4056
$lexer->consume();
41-
42-
return [
43-
'identifier' => $identifier,
44-
'props' => $props,
45-
'children' => $children,
46-
'selfClosing' => true
47-
];
4857
} else {
49-
throw new Exception(sprintf('Self closing tag "%s" misses closing bracket.', $identifier));
58+
throw new Exception(sprintf('Tag "%s" did not end with closing bracket.', $identifier));
5059
}
51-
}
52-
53-
if ($lexer->isClosingBracket()) {
54-
$lexer->consume();
55-
} else {
56-
throw new Exception(sprintf('Tag "%s" did not end with closing bracket.', $identifier));
57-
}
58-
59-
$children = Children::parse($lexer);
6060

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

64-
if ($lexer->isForwardSlash()) {
63+
if ($lexer->isOpeningBracket()) {
6564
$lexer->consume();
65+
66+
if ($lexer->isForwardSlash()) {
67+
$lexer->consume();
68+
} else {
69+
throw new Exception(sprintf(
70+
'Opening-bracket for closing of tag "%s" was not followed by slash.',
71+
$identifier
72+
));
73+
}
6674
} else {
6775
throw new Exception(sprintf(
68-
'Opening-bracket for closing of tag "%s" was not followed by slash.',
76+
'Opening-bracket for closing of tag "%s" expected.',
6977
$identifier
7078
));
7179
}
72-
} else {
73-
throw new Exception(sprintf(
74-
'Opening-bracket for closing of tag "%s" expected.',
75-
$identifier
76-
));
77-
}
7880

79-
$closingIdentifier = Identifier::parse($lexer);
81+
$closingIdentifier = Identifier::parse($lexer);
8082

81-
if ($closingIdentifier !== $identifier) {
82-
throw new Exception(sprintf(
83-
'Closing-tag identifier "%s" did not match opening-tag identifier "%s".',
84-
$closingIdentifier,
85-
$identifier
86-
));
87-
}
83+
if ($closingIdentifier !== $identifier) {
84+
throw new Exception(sprintf(
85+
'Closing-tag identifier "%s" did not match opening-tag identifier "%s".',
86+
$closingIdentifier,
87+
$identifier
88+
));
89+
}
8890

89-
if ($lexer->isClosingBracket()) {
90-
$lexer->consume();
91-
return [
92-
'identifier' => $identifier,
93-
'props' => $props,
94-
'children' => $children,
95-
'selfClosing' => false
96-
];
97-
} else {
98-
throw new Exception(sprintf('Closing tag "%s" did not end with closing-bracket.', $identifier));
99-
}
91+
if ($lexer->isClosingBracket()) {
92+
$lexer->consume();
93+
return [
94+
'identifier' => $identifier,
95+
'props' => $props,
96+
'children' => $children,
97+
'selfClosing' => false
98+
];
99+
} else {
100+
throw new Exception(sprintf('Closing tag "%s" did not end with closing-bracket.', $identifier));
101+
}
100102

101-
if ($lexer->isEnd()) {
102-
throw new Exception(sprintf('Tag "%s" was is not closed.', $identifier));
103+
if ($lexer->isEnd()) {
104+
throw new Exception(sprintf('Tag was %s is not closed.', $identifier));
105+
}
106+
} catch (Exception $e) {
107+
throw new Exception(sprintf('<%s> %s', $identifier, $e->getMessage()));
103108
}
104109
}
105110
}

0 commit comments

Comments
 (0)