@@ -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