Skip to content

Commit 27b9ffd

Browse files
authored
Merge pull request #6 from mficzel/task/throwExceptionsOnSytaxErrors
Task/throw exceptions on sytax errors
2 parents 37d875f + 69f3994 commit 27b9ffd

5 files changed

Lines changed: 65 additions & 47 deletions

File tree

src/Expression/Expression.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ public static function parse(Lexer $lexer)
1010
{
1111
$contents = '';
1212
$braceCount = 0;
13+
1314
if ($lexer->isOpeningBrace()) {
1415
$lexer->consume();
16+
} else {
17+
throw new Exception('Expression without braces');
1518
}
1619

1720
while (true) {
1821
if ($lexer->isEnd()) {
19-
throw new Exception('Unclosed Expression');
22+
throw new Exception(sprintf('Unfinished Expression "%s"', $contents));
2023
}
2124

2225
if ($lexer->isOpeningBrace()) {

src/Expression/Identifier.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ class Identifier
88
public static function parse(Lexer $lexer)
99
{
1010
$identifier = '';
11-
if ($lexer->isAlpha()) {
12-
$identifier .= $lexer->consume();
13-
}
1411

1512
while ($lexer->isAlphaNumeric() ||
1613
$lexer->isDot() ||

src/Expression/Node.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,61 @@ public static function parse(Lexer $lexer)
4545
'children' => $children,
4646
'selfClosing' => true
4747
];
48+
} else {
49+
throw new Exception(sprintf('Self closing tag "%s" misses closing bracket.', $identifier));
4850
}
4951
}
5052

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

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

57-
while ($lexer->isWhitespace()) {
58-
$lexer->consume();
59-
}
60-
6161
if ($lexer->isOpeningBracket()) {
6262
$lexer->consume();
6363

6464
if ($lexer->isForwardSlash()) {
6565
$lexer->consume();
66+
} else {
67+
throw new Exception(sprintf(
68+
'Opening-bracket for closing of tag "%s" was not followed by slash.',
69+
$identifier
70+
));
6671
}
72+
} else {
73+
throw new Exception(sprintf(
74+
'Opening-bracket for closing of tag "%s" expected.',
75+
$identifier
76+
));
6777
}
6878

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

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+
}
88+
7189
if ($lexer->isClosingBracket()) {
7290
$lexer->consume();
73-
74-
if ($closingIdentifier === $identifier) {
75-
return [
76-
'identifier' => $identifier,
77-
'props' => $props,
78-
'children' => $children,
79-
'selfClosing' => false
80-
];
81-
}
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));
8299
}
83100

84101
if ($lexer->isEnd()) {
85-
throw new Exception(sprintf('Tag %s was is not closed', $identifier));
102+
throw new Exception(sprintf('Tag "%s" was is not closed.', $identifier));
86103
}
87104
}
88105
}

src/Expression/Prop.php

Lines changed: 27 additions & 28 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 Prop
@@ -12,39 +13,37 @@ public static function parse(Lexer $lexer)
1213
}
1314

1415
$identifier = Identifier::parse($lexer);
15-
$value = [
16-
'type' => 'boolean',
17-
'payload' => false
18-
];
1916

2017
if ($lexer->isEqualSign()) {
2118
$lexer->consume();
22-
}
23-
24-
switch (true) {
25-
case $lexer->isSingleQuote():
26-
case $lexer->isDoubleQuote():
27-
$value = [
28-
'type' => 'string',
29-
'payload' => StringLiteral::parse($lexer)
30-
];
31-
break;
32-
33-
case $lexer->isOpeningBrace():
34-
$value = [
35-
'type' => 'expression',
36-
'payload' => Expression::parse($lexer)
37-
];
38-
break;
39-
40-
case $lexer->isWhiteSpace():
41-
case $lexer->isForwardSlash():
42-
case $lexer->isClosingBracket():
43-
$value = [
19+
switch (true) {
20+
case $lexer->isSingleQuote():
21+
case $lexer->isDoubleQuote():
22+
$value = [
23+
'type' => 'string',
24+
'payload' => StringLiteral::parse($lexer)
25+
];
26+
break;
27+
28+
case $lexer->isOpeningBrace():
29+
$value = [
30+
'type' => 'expression',
31+
'payload' => Expression::parse($lexer)
32+
];
33+
break;
34+
default:
35+
throw new Exception(sprintf(
36+
'Prop-assignment "%s" was not followed by quotes or braces',
37+
$identifier
38+
));
39+
}
40+
} elseif ($lexer->isWhiteSpace() || $lexer->isForwardSlash() || $lexer->isClosingBracket()) {
41+
$value = [
4442
'type' => 'boolean',
4543
'payload' => true
46-
];
47-
break;
44+
];
45+
} else {
46+
throw new Exception(sprintf('Prop identifier "%s" is neither assignment nor boolean', $identifier));
4847
}
4948

5049
return [$identifier, $value];

src/Expression/StringLiteral.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ public static function parse(Lexer $lexer)
1313
$willBeEscaped = false;
1414
if ($lexer->isSingleQuote() || $lexer->isDoubleQuote()) {
1515
$openingQuoteSign = $lexer->consume();
16+
} else {
17+
throw new Exception('Unquoted String literal');
1618
}
1719

1820
while (true) {
1921
if ($lexer->isEnd()) {
20-
throw new Exception('Unclosed string literal');
22+
throw new Exception(sprintf('Unfinished string literal "%s"', $contents));
2123
}
2224

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

0 commit comments

Comments
 (0)