@@ -147,7 +147,7 @@ public function parseSourceFile(string $fileContents, string $uri = null) : Sour
147147 $ this ->reset ();
148148
149149 $ sourceFile = new SourceFileNode ();
150- $ this ->sourceFile = & $ sourceFile ;
150+ $ this ->sourceFile = $ sourceFile ;
151151 $ sourceFile ->fileContents = $ fileContents ;
152152 $ sourceFile ->uri = $ uri ;
153153 $ sourceFile ->statementList = array ();
@@ -400,6 +400,19 @@ private function eatOptional(...$kinds) {
400400 return null ;
401401 }
402402
403+ /**
404+ * @param int $kind a single kind
405+ * @return Token|null
406+ */
407+ private function eatOptional1 ($ kind ) {
408+ $ token = $ this ->token ;
409+ if ($ token ->kind === $ kind ) {
410+ $ this ->token = $ this ->lexer ->scanNextToken ();
411+ return $ token ;
412+ }
413+ return null ;
414+ }
415+
403416 private $ token ;
404417
405418 private function getCurrentToken () : Token {
@@ -601,14 +614,14 @@ private function parseParameterFn() {
601614 return function ($ parentNode ) {
602615 $ parameter = new Parameter ();
603616 $ parameter ->parent = $ parentNode ;
604- $ parameter ->questionToken = $ this ->eatOptional (TokenKind::QuestionToken);
617+ $ parameter ->questionToken = $ this ->eatOptional1 (TokenKind::QuestionToken);
605618 $ parameter ->typeDeclaration = $ this ->tryParseParameterTypeDeclaration ($ parameter );
606- $ parameter ->byRefToken = $ this ->eatOptional (TokenKind::AmpersandToken);
619+ $ parameter ->byRefToken = $ this ->eatOptional1 (TokenKind::AmpersandToken);
607620 // TODO add post-parse rule that prevents assignment
608621 // TODO add post-parse rule that requires only last parameter be variadic
609- $ parameter ->dotDotDotToken = $ this ->eatOptional (TokenKind::DotDotDotToken);
622+ $ parameter ->dotDotDotToken = $ this ->eatOptional1 (TokenKind::DotDotDotToken);
610623 $ parameter ->variableName = $ this ->eat (TokenKind::VariableName);
611- $ parameter ->equalsToken = $ this ->eatOptional (TokenKind::EqualsToken);
624+ $ parameter ->equalsToken = $ this ->eatOptional1 (TokenKind::EqualsToken);
612625 if ($ parameter ->equalsToken !== null ) {
613626 // TODO add post-parse rule that checks for invalid assignments
614627 $ parameter ->default = $ this ->parseExpression ($ parameter );
@@ -1204,7 +1217,7 @@ private function parseQualifiedNameFn() {
12041217 $ node ->parent = $ parentNode ;
12051218 $ node ->relativeSpecifier = $ this ->parseRelativeSpecifier ($ node );
12061219 if (!isset ($ node ->relativeSpecifier )) {
1207- $ node ->globalSpecifier = $ this ->eatOptional (TokenKind::BackslashToken);
1220+ $ node ->globalSpecifier = $ this ->eatOptional1 (TokenKind::BackslashToken);
12081221 }
12091222
12101223 $ nameParts =
@@ -1243,7 +1256,7 @@ function ($parentNode) {
12431256 private function parseRelativeSpecifier ($ parentNode ) {
12441257 $ node = new RelativeSpecifier ();
12451258 $ node ->parent = $ parentNode ;
1246- $ node ->namespaceKeyword = $ this ->eatOptional (TokenKind::NamespaceKeyword);
1259+ $ node ->namespaceKeyword = $ this ->eatOptional1 (TokenKind::NamespaceKeyword);
12471260 if ($ node ->namespaceKeyword !== null ) {
12481261 $ node ->backslash = $ this ->eat (TokenKind::BackslashToken);
12491262 }
@@ -1255,7 +1268,7 @@ private function parseRelativeSpecifier($parentNode) {
12551268
12561269 private function parseFunctionType (Node $ functionDeclaration , $ canBeAbstract = false , $ isAnonymous = false ) {
12571270 $ functionDeclaration ->functionKeyword = $ this ->eat (TokenKind::FunctionKeyword);
1258- $ functionDeclaration ->byRefToken = $ this ->eatOptional (TokenKind::AmpersandToken);
1271+ $ functionDeclaration ->byRefToken = $ this ->eatOptional1 (TokenKind::AmpersandToken);
12591272 $ functionDeclaration ->name = $ isAnonymous
12601273 ? $ this ->eatOptional ($ this ->nameOrKeywordOrReservedWordTokens )
12611274 : $ this ->eat ($ this ->nameOrKeywordOrReservedWordTokens );
@@ -1283,12 +1296,12 @@ private function parseFunctionType(Node $functionDeclaration, $canBeAbstract = f
12831296
12841297 if ($ this ->checkToken (TokenKind::ColonToken)) {
12851298 $ functionDeclaration ->colonToken = $ this ->eat (TokenKind::ColonToken);
1286- $ functionDeclaration ->questionToken = $ this ->eatOptional (TokenKind::QuestionToken);
1299+ $ functionDeclaration ->questionToken = $ this ->eatOptional1 (TokenKind::QuestionToken);
12871300 $ functionDeclaration ->returnType = $ this ->parseReturnTypeDeclaration ($ functionDeclaration );
12881301 }
12891302
12901303 if ($ canBeAbstract ) {
1291- $ functionDeclaration ->compoundStatementOrSemicolon = $ this ->eatOptional (TokenKind::SemicolonToken);
1304+ $ functionDeclaration ->compoundStatementOrSemicolon = $ this ->eatOptional1 (TokenKind::SemicolonToken);
12921305 }
12931306
12941307 if (!isset ($ functionDeclaration ->compoundStatementOrSemicolon )) {
@@ -1359,7 +1372,7 @@ private function parseIfStatement($parentNode) {
13591372 $ ifStatement ->elseClause = $ this ->parseElseClause ($ ifStatement );
13601373 }
13611374
1362- $ ifStatement ->endifKeyword = $ this ->eatOptional (TokenKind::EndIfKeyword);
1375+ $ ifStatement ->endifKeyword = $ this ->eatOptional1 (TokenKind::EndIfKeyword);
13631376 if ($ ifStatement ->endifKeyword ) {
13641377 $ ifStatement ->semicolon = $ this ->eatSemicolonOrAbortStatement ();
13651378 }
@@ -1403,8 +1416,8 @@ private function parseSwitchStatement($parentNode) {
14031416 $ switchStatement ->openParen = $ this ->eat (TokenKind::OpenParenToken);
14041417 $ switchStatement ->expression = $ this ->parseExpression ($ switchStatement );
14051418 $ switchStatement ->closeParen = $ this ->eat (TokenKind::CloseParenToken);
1406- $ switchStatement ->openBrace = $ this ->eatOptional (TokenKind::OpenBraceToken);
1407- $ switchStatement ->colon = $ this ->eatOptional (TokenKind::ColonToken);
1419+ $ switchStatement ->openBrace = $ this ->eatOptional1 (TokenKind::OpenBraceToken);
1420+ $ switchStatement ->colon = $ this ->eatOptional1 (TokenKind::ColonToken);
14081421 $ switchStatement ->caseStatements = $ this ->parseList ($ switchStatement , ParseContext::SwitchStatementElements);
14091422 if ($ switchStatement ->colon !== null ) {
14101423 $ switchStatement ->endswitch = $ this ->eat (TokenKind::EndSwitchKeyword);
@@ -1438,7 +1451,7 @@ private function parseWhileStatement($parentNode) {
14381451 $ whileStatement ->openParen = $ this ->eat (TokenKind::OpenParenToken);
14391452 $ whileStatement ->expression = $ this ->parseExpression ($ whileStatement );
14401453 $ whileStatement ->closeParen = $ this ->eat (TokenKind::CloseParenToken);
1441- $ whileStatement ->colon = $ this ->eatOptional (TokenKind::ColonToken);
1454+ $ whileStatement ->colon = $ this ->eatOptional1 (TokenKind::ColonToken);
14421455 if ($ whileStatement ->colon !== null ) {
14431456 $ whileStatement ->statements = $ this ->parseList ($ whileStatement , ParseContext::WhileStatementElements);
14441457 $ whileStatement ->endWhile = $ this ->eat (TokenKind::EndWhileKeyword);
@@ -1627,7 +1640,7 @@ private function parseBinaryExpressionOrHigher($precedence, $parentNode) {
16271640 $ this ->advanceToken ();
16281641
16291642 if ($ token ->kind === TokenKind::EqualsToken) {
1630- $ byRefToken = $ this ->eatOptional (TokenKind::AmpersandToken);
1643+ $ byRefToken = $ this ->eatOptional1 (TokenKind::AmpersandToken);
16311644 }
16321645
16331646 $ leftOperand = $ token ->kind === TokenKind::QuestionToken ?
@@ -1789,7 +1802,7 @@ private function parseForStatement($parentNode) {
17891802 $ forStatement ->exprGroupSemicolon2 = $ this ->eat (TokenKind::SemicolonToken);
17901803 $ forStatement ->forEndOfLoop = $ this ->parseExpressionList ($ forStatement );
17911804 $ forStatement ->closeParen = $ this ->eat (TokenKind::CloseParenToken);
1792- $ forStatement ->colon = $ this ->eatOptional (TokenKind::ColonToken);
1805+ $ forStatement ->colon = $ this ->eatOptional1 (TokenKind::ColonToken);
17931806 if ($ forStatement ->colon !== null ) {
17941807 $ forStatement ->statements = $ this ->parseList ($ forStatement , ParseContext::ForStatementElements);
17951808 $ forStatement ->endFor = $ this ->eat (TokenKind::EndForKeyword);
@@ -1810,7 +1823,7 @@ private function parseForeachStatement($parentNode) {
18101823 $ foreachStatement ->foreachKey = $ this ->tryParseForeachKey ($ foreachStatement );
18111824 $ foreachStatement ->foreachValue = $ this ->parseForeachValue ($ foreachStatement );
18121825 $ foreachStatement ->closeParen = $ this ->eat (TokenKind::CloseParenToken);
1813- $ foreachStatement ->colon = $ this ->eatOptional (TokenKind::ColonToken);
1826+ $ foreachStatement ->colon = $ this ->eatOptional1 (TokenKind::ColonToken);
18141827 if ($ foreachStatement ->colon !== null ) {
18151828 $ foreachStatement ->statements = $ this ->parseList ($ foreachStatement , ParseContext::ForeachStatementElements);
18161829 $ foreachStatement ->endForeach = $ this ->eat (TokenKind::EndForEachKeyword);
@@ -1845,7 +1858,7 @@ private function tryParseForeachKey($parentNode) {
18451858 private function parseForeachValue ($ parentNode ) {
18461859 $ foreachValue = new ForeachValue ();
18471860 $ foreachValue ->parent = $ parentNode ;
1848- $ foreachValue ->ampersand = $ this ->eatOptional (TokenKind::AmpersandToken);
1861+ $ foreachValue ->ampersand = $ this ->eatOptional1 (TokenKind::AmpersandToken);
18491862 $ foreachValue ->expression = $ this ->parseExpression ($ foreachValue );
18501863 return $ foreachValue ;
18511864 }
@@ -2095,7 +2108,7 @@ private function parseArrayElementFn() {
20952108 if ($ this ->checkToken (TokenKind::DoubleArrowToken)) {
20962109 $ arrayElement ->elementKey = $ expression ;
20972110 $ arrayElement ->arrowToken = $ this ->eat (TokenKind::DoubleArrowToken);
2098- $ arrayElement ->byRef = $ this ->eatOptional (TokenKind::AmpersandToken); // TODO not okay for list expressions
2111+ $ arrayElement ->byRef = $ this ->eatOptional1 (TokenKind::AmpersandToken); // TODO not okay for list expressions
20992112 $ arrayElement ->elementValue = $ this ->parseExpression ($ arrayElement );
21002113 } else {
21012114 $ arrayElement ->elementValue = $ expression ;
@@ -2132,7 +2145,7 @@ private function parseArrayCreationExpression($parentNode) {
21322145 $ arrayExpression = new ArrayCreationExpression ();
21332146 $ arrayExpression ->parent = $ parentNode ;
21342147
2135- $ arrayExpression ->arrayKeyword = $ this ->eatOptional (TokenKind::ArrayKeyword);
2148+ $ arrayExpression ->arrayKeyword = $ this ->eatOptional1 (TokenKind::ArrayKeyword);
21362149
21372150 $ arrayExpression ->openParenOrBracket = $ arrayExpression ->arrayKeyword !== null
21382151 ? $ this ->eat (TokenKind::OpenParenToken)
@@ -2198,7 +2211,7 @@ private function parseExitIntrinsicExpression($parentNode) {
21982211 $ exitExpression ->parent = $ parentNode ;
21992212
22002213 $ exitExpression ->exitOrDieKeyword = $ this ->eat (TokenKind::ExitKeyword, TokenKind::DieKeyword);
2201- $ exitExpression ->openParen = $ this ->eatOptional (TokenKind::OpenParenToken);
2214+ $ exitExpression ->openParen = $ this ->eatOptional1 (TokenKind::OpenParenToken);
22022215 if ($ exitExpression ->openParen !== null ) {
22032216 if ($ this ->isExpressionStart ($ this ->getCurrentToken ())) {
22042217 $ exitExpression ->expression = $ this ->parseExpression ($ exitExpression );
@@ -2360,8 +2373,8 @@ private function parseArgumentExpressionFn() {
23602373 return function ($ parentNode ) {
23612374 $ argumentExpression = new ArgumentExpression ();
23622375 $ argumentExpression ->parent = $ parentNode ;
2363- $ argumentExpression ->byRefToken = $ this ->eatOptional (TokenKind::AmpersandToken);
2364- $ argumentExpression ->dotDotDotToken = $ this ->eatOptional (TokenKind::DotDotDotToken);
2376+ $ argumentExpression ->byRefToken = $ this ->eatOptional1 (TokenKind::AmpersandToken);
2377+ $ argumentExpression ->dotDotDotToken = $ this ->eatOptional1 (TokenKind::DotDotDotToken);
23652378 $ argumentExpression ->expression = $ this ->parseExpression ($ argumentExpression );
23662379 return $ argumentExpression ;
23672380 };
@@ -2453,13 +2466,13 @@ private function parseObjectCreationExpression($parentNode) {
24532466 // TODO - add tests for this scenario
24542467 $ this ->isParsingObjectCreationExpression = true ;
24552468 $ objectCreationExpression ->classTypeDesignator =
2456- $ this ->eatOptional (TokenKind::ClassKeyword) ??
2457- $ this ->eatOptional (TokenKind::StaticKeyword) ??
2469+ $ this ->eatOptional1 (TokenKind::ClassKeyword) ??
2470+ $ this ->eatOptional1 (TokenKind::StaticKeyword) ??
24582471 $ this ->parseExpression ($ objectCreationExpression );
24592472
24602473 $ this ->isParsingObjectCreationExpression = false ;
24612474
2462- $ objectCreationExpression ->openParen = $ this ->eatOptional (TokenKind::OpenParenToken);
2475+ $ objectCreationExpression ->openParen = $ this ->eatOptional1 (TokenKind::OpenParenToken);
24632476 if ($ objectCreationExpression ->openParen !== null ) {
24642477 $ objectCreationExpression ->argumentExpressionList = $ this ->parseArgumentExpressionList ($ objectCreationExpression );
24652478 $ objectCreationExpression ->closeParen = $ this ->eat (TokenKind::CloseParenToken);
@@ -2501,7 +2514,7 @@ private function parseTernaryExpression($leftOperand, $questionToken):TernaryExp
25012514 private function parseClassInterfaceClause ($ parentNode ) {
25022515 $ classInterfaceClause = new ClassInterfaceClause ();
25032516 $ classInterfaceClause ->parent = $ parentNode ;
2504- $ classInterfaceClause ->implementsKeyword = $ this ->eatOptional (TokenKind::ImplementsKeyword);
2517+ $ classInterfaceClause ->implementsKeyword = $ this ->eatOptional1 (TokenKind::ImplementsKeyword);
25052518
25062519 if ($ classInterfaceClause ->implementsKeyword === null ) {
25072520 return null ;
@@ -2516,7 +2529,7 @@ private function parseClassBaseClause($parentNode) {
25162529 $ classBaseClause = new ClassBaseClause ();
25172530 $ classBaseClause ->parent = $ parentNode ;
25182531
2519- $ classBaseClause ->extendsKeyword = $ this ->eatOptional (TokenKind::ExtendsKeyword);
2532+ $ classBaseClause ->extendsKeyword = $ this ->eatOptional1 (TokenKind::ExtendsKeyword);
25202533 if ($ classBaseClause ->extendsKeyword === null ) {
25212534 return null ;
25222535 }
@@ -2623,7 +2636,7 @@ private function parseInterfaceBaseClause($parentNode) {
26232636 $ interfaceBaseClause = new InterfaceBaseClause ();
26242637 $ interfaceBaseClause ->parent = $ parentNode ;
26252638
2626- $ interfaceBaseClause ->extendsKeyword = $ this ->eatOptional (TokenKind::ExtendsKeyword);
2639+ $ interfaceBaseClause ->extendsKeyword = $ this ->eatOptional1 (TokenKind::ExtendsKeyword);
26272640 if (isset ($ interfaceBaseClause ->extendsKeyword )) {
26282641 $ interfaceBaseClause ->interfaceNameList = $ this ->parseQualifiedNameList ($ interfaceBaseClause );
26292642 } else {
@@ -2890,7 +2903,7 @@ private function parseStaticVariableDeclarationFn() {
28902903 $ staticVariableDeclaration = new StaticVariableDeclaration ();
28912904 $ staticVariableDeclaration ->parent = $ parentNode ;
28922905 $ staticVariableDeclaration ->variableName = $ this ->eat (TokenKind::VariableName);
2893- $ staticVariableDeclaration ->equalsToken = $ this ->eatOptional (TokenKind::EqualsToken);
2906+ $ staticVariableDeclaration ->equalsToken = $ this ->eatOptional1 (TokenKind::EqualsToken);
28942907 if ($ staticVariableDeclaration ->equalsToken !== null ) {
28952908 // TODO add post-parse rule that checks for invalid assignments
28962909 $ staticVariableDeclaration ->assignment = $ this ->parseExpression ($ staticVariableDeclaration );
@@ -2983,7 +2996,7 @@ private function parseAnonymousFunctionCreationExpression($parentNode) {
29832996 $ anonymousFunctionCreationExpression = new AnonymousFunctionCreationExpression ();
29842997 $ anonymousFunctionCreationExpression ->parent = $ parentNode ;
29852998
2986- $ anonymousFunctionCreationExpression ->staticModifier = $ this ->eatOptional (TokenKind::StaticKeyword);
2999+ $ anonymousFunctionCreationExpression ->staticModifier = $ this ->eatOptional1 (TokenKind::StaticKeyword);
29873000 $ this ->parseFunctionType ($ anonymousFunctionCreationExpression , false , true );
29883001
29893002 return $ anonymousFunctionCreationExpression ;
@@ -2993,7 +3006,7 @@ private function parseAnonymousFunctionUseClause($parentNode) {
29933006 $ anonymousFunctionUseClause = new AnonymousFunctionUseClause ();
29943007 $ anonymousFunctionUseClause ->parent = $ parentNode ;
29953008
2996- $ anonymousFunctionUseClause ->useKeyword = $ this ->eatOptional (TokenKind::UseKeyword);
3009+ $ anonymousFunctionUseClause ->useKeyword = $ this ->eatOptional1 (TokenKind::UseKeyword);
29973010 if ($ anonymousFunctionUseClause ->useKeyword === null ) {
29983011 return null ;
29993012 }
@@ -3007,7 +3020,7 @@ function ($token) {
30073020 function ($ parentNode ) {
30083021 $ useVariableName = new UseVariableName ();
30093022 $ useVariableName ->parent = $ parentNode ;
3010- $ useVariableName ->byRef = $ this ->eatOptional (TokenKind::AmpersandToken);
3023+ $ useVariableName ->byRef = $ this ->eatOptional1 (TokenKind::AmpersandToken);
30113024 $ useVariableName ->variableName = $ this ->eat (TokenKind::VariableName);
30123025 return $ useVariableName ;
30133026 },
@@ -3038,9 +3051,9 @@ private function eatSemicolonOrAbortStatement() {
30383051 private function parseInlineHtml ($ parentNode ) {
30393052 $ inlineHtml = new InlineHtml ();
30403053 $ inlineHtml ->parent = $ parentNode ;
3041- $ inlineHtml ->scriptSectionEndTag = $ this ->eatOptional (TokenKind::ScriptSectionEndTag);
3042- $ inlineHtml ->text = $ this ->eatOptional (TokenKind::InlineHtml);
3043- $ inlineHtml ->scriptSectionStartTag = $ this ->eatOptional (TokenKind::ScriptSectionStartTag);
3054+ $ inlineHtml ->scriptSectionEndTag = $ this ->eatOptional1 (TokenKind::ScriptSectionEndTag);
3055+ $ inlineHtml ->text = $ this ->eatOptional1 (TokenKind::InlineHtml);
3056+ $ inlineHtml ->scriptSectionStartTag = $ this ->eatOptional1 (TokenKind::ScriptSectionStartTag);
30443057
30453058 return $ inlineHtml ;
30463059 }
0 commit comments