Refactor null checks
This commit is contained in:
		
							parent
							
								
									1bd8c0bb05
								
							
						
					
					
						commit
						e748f89fdf
					
				| @ -711,10 +711,6 @@ namespace Parser | |||||||
|             { |             { | ||||||
|                 commas.Add(Factory.Token(EatToken())); |                 commas.Add(Factory.Token(EatToken())); | ||||||
|             } |             } | ||||||
|             if (commas.Count == 0) |  | ||||||
|             { |  | ||||||
|                 commas = null; |  | ||||||
|             } |  | ||||||
|             var statementList = ParseStatements(); |             var statementList = ParseStatements(); | ||||||
|             return Factory.SwitchCase(Factory.Token(caseKeyword), caseId, statementList, commas); |             return Factory.SwitchCase(Factory.Token(caseKeyword), caseId, statementList, commas); | ||||||
|         } |         } | ||||||
| @ -728,10 +724,6 @@ namespace Parser | |||||||
|             { |             { | ||||||
|                 commas.Add(Factory.Token(EatToken())); |                 commas.Add(Factory.Token(EatToken())); | ||||||
|             } |             } | ||||||
|             if (commas.Count == 0) |  | ||||||
|             { |  | ||||||
|                 commas = null; |  | ||||||
|             } |  | ||||||
|             var casesList = new List<SwitchCaseNode>(); |             var casesList = new List<SwitchCaseNode>(); | ||||||
|             while (CurrentToken.Kind == TokenKind.Identifier |             while (CurrentToken.Kind == TokenKind.Identifier | ||||||
|                    && CurrentToken.PureToken.LiteralText == "case") |                    && CurrentToken.PureToken.LiteralText == "case") | ||||||
| @ -769,11 +761,6 @@ namespace Parser | |||||||
|             { |             { | ||||||
|                 commas.Add(Factory.Token(EatToken())); |                 commas.Add(Factory.Token(EatToken())); | ||||||
|             } |             } | ||||||
|             if (commas.Count == 0) |  | ||||||
|             { |  | ||||||
|                 commas = null; |  | ||||||
|             } |  | ||||||
|              |  | ||||||
|             var body = ParseStatements(); |             var body = ParseStatements(); | ||||||
|             var endKeyword = EatIdentifier("end"); |             var endKeyword = EatIdentifier("end"); | ||||||
|             return Factory.WhileStatement( |             return Factory.WhileStatement( | ||||||
| @ -809,10 +796,6 @@ namespace Parser | |||||||
|             { |             { | ||||||
|                 commas.Add(Factory.Token(EatToken())); |                 commas.Add(Factory.Token(EatToken())); | ||||||
|             } |             } | ||||||
|             if (commas.Count == 0) |  | ||||||
|             { |  | ||||||
|                 commas = null; |  | ||||||
|             } |  | ||||||
|             var body = ParseStatements(); |             var body = ParseStatements(); | ||||||
|             TokenNode elseKeyword = null; |             TokenNode elseKeyword = null; | ||||||
|             StatementListNode elseBody = null; |             StatementListNode elseBody = null; | ||||||
| @ -865,10 +848,6 @@ namespace Parser | |||||||
|             { |             { | ||||||
|                 commas.Add(Factory.Token(EatToken())); |                 commas.Add(Factory.Token(EatToken())); | ||||||
|             } |             } | ||||||
|             if (commas.Count == 0) |  | ||||||
|             { |  | ||||||
|                 commas = null; |  | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|             var body = ParseStatements(); |             var body = ParseStatements(); | ||||||
|             var endKeyword = Factory.Token(EatIdentifier("end")); |             var endKeyword = Factory.Token(EatIdentifier("end")); | ||||||
|  | |||||||
| @ -16,6 +16,11 @@ namespace Parser | |||||||
| 
 | 
 | ||||||
|             return parent; |             return parent; | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|  |         private static List<SyntaxNode> RemoveNulls(List<SyntaxNode> children) | ||||||
|  |         { | ||||||
|  |             return children.Where(x => x != null).ToList(); | ||||||
|  |         } | ||||||
|          |          | ||||||
|         public FunctionDeclarationNode FunctionDeclaration( |         public FunctionDeclarationNode FunctionDeclaration( | ||||||
|             TokenNode functionKeyword, |             TokenNode functionKeyword, | ||||||
| @ -26,29 +31,19 @@ namespace Parser | |||||||
|             TokenNode end, |             TokenNode end, | ||||||
|             TokenNode semicolonOrComma = null) |             TokenNode semicolonOrComma = null) | ||||||
|         { |         { | ||||||
|             var children = new List<SyntaxNode> {functionKeyword}; |             var children = new List<SyntaxNode> | ||||||
|             if (outputDescription != null) |  | ||||||
|             { |             { | ||||||
|                 children.Add(outputDescription); |                 functionKeyword, | ||||||
|             } |                 outputDescription, | ||||||
|             children.Add(name); |                 name, | ||||||
|             if (inputDescription != null) |                 inputDescription, | ||||||
|             { |                 body, | ||||||
|                 children.Add(inputDescription); |                 end, | ||||||
|             } |                 semicolonOrComma | ||||||
| 
 |             }; | ||||||
|             children.Add(body); |  | ||||||
|             if (end != null) |  | ||||||
|             { |  | ||||||
|                 children.Add(end); |  | ||||||
|             } |  | ||||||
|             if (semicolonOrComma != null) |  | ||||||
|             { |  | ||||||
|                 children.Add(semicolonOrComma); |  | ||||||
|             } |  | ||||||
|             var result = |             var result = | ||||||
|                 new FunctionDeclarationNode( |                 new FunctionDeclarationNode( | ||||||
|                     children, |                     RemoveNulls(children), | ||||||
|                     functionKeyword, |                     functionKeyword, | ||||||
|                     outputDescription, |                     outputDescription, | ||||||
|                     name, |                     name, | ||||||
| @ -65,12 +60,12 @@ namespace Parser | |||||||
|             TokenNode equalitySign) |             TokenNode equalitySign) | ||||||
|         { |         { | ||||||
|             var children = new List<SyntaxNode>(nodes); |             var children = new List<SyntaxNode>(nodes); | ||||||
|             nodes.Add(equalitySign); |             children.Add(equalitySign); | ||||||
|             var result = new FunctionOutputDescriptionNode( |             var result = new FunctionOutputDescriptionNode( | ||||||
|                 nodes, |                 children, | ||||||
|                 nodes |                 children | ||||||
|                     .Where(node => node is TokenNode && ((TokenNode) node).Token.Kind == TokenKind.Identifier) |                     .Where(node => node is TokenNode tokenNode && tokenNode.Token.Kind == TokenKind.Identifier) | ||||||
|                     .Select(node => node as TokenNode) |                     .Select(node => (TokenNode)node) | ||||||
|                     .ToList(), |                     .ToList(), | ||||||
|                 equalitySign |                 equalitySign | ||||||
|                 ); |                 ); | ||||||
| @ -84,8 +79,9 @@ namespace Parser | |||||||
|                 nodes, |                 nodes, | ||||||
|                 nodes |                 nodes | ||||||
|                     .Where( |                     .Where( | ||||||
|                         node => node is TokenNode && ((TokenNode) node).Token.Kind != TokenKind.Comma |                         node => node is TokenNode tokenNode && tokenNode.Token.Kind != TokenKind.Comma | ||||||
|                     ).ToList()); |                     ) | ||||||
|  |                     .ToList()); | ||||||
|             SetParent(result); |             SetParent(result); | ||||||
|             return result; |             return result; | ||||||
|         } |         } | ||||||
| @ -127,20 +123,13 @@ namespace Parser | |||||||
|             TokenNode semicolonOrComma = null) |             TokenNode semicolonOrComma = null) | ||||||
|         { |         { | ||||||
|             var children = new List<SyntaxNode> { switchKeyword, switchExpression }; |             var children = new List<SyntaxNode> { switchKeyword, switchExpression }; | ||||||
|             if (optionalCommasAfterExpression != null) |             children.AddRange(optionalCommasAfterExpression); | ||||||
|             { |  | ||||||
|                 children.AddRange(optionalCommasAfterExpression); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             children.AddRange(cases); |             children.AddRange(cases); | ||||||
|             children.Add(endKeyword); |             children.Add(endKeyword); | ||||||
|             if (semicolonOrComma != null) |             children.Add(semicolonOrComma); | ||||||
|             { |  | ||||||
|                 children.Add(semicolonOrComma); |  | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|             var result = new SwitchStatementNode( |             var result = new SwitchStatementNode( | ||||||
|                 children, |                 RemoveNulls(children), | ||||||
|                 switchKeyword, |                 switchKeyword, | ||||||
|                 switchExpression, |                 switchExpression, | ||||||
|                 cases, |                 cases, | ||||||
| @ -162,12 +151,14 @@ namespace Parser | |||||||
|                 caseKeyword, |                 caseKeyword, | ||||||
|                 caseIdentifier |                 caseIdentifier | ||||||
|             }; |             }; | ||||||
|             if (optionalCommasAfterIdentifier != null) |             children.AddRange(optionalCommasAfterIdentifier); | ||||||
|             { |  | ||||||
|                 children.AddRange(optionalCommasAfterIdentifier); |  | ||||||
|             } |  | ||||||
|             children.Add(statementList); |             children.Add(statementList); | ||||||
|             var result = new SwitchCaseNode(children, caseKeyword, caseIdentifier, statementList, optionalCommasAfterIdentifier); |             var result = new SwitchCaseNode( | ||||||
|  |                 RemoveNulls(children), | ||||||
|  |                 caseKeyword, | ||||||
|  |                 caseIdentifier, | ||||||
|  |                 statementList, | ||||||
|  |                 optionalCommasAfterIdentifier); | ||||||
|             SetParent(result); |             SetParent(result); | ||||||
|             return result; |             return result; | ||||||
|         } |         } | ||||||
| @ -429,19 +420,12 @@ namespace Parser | |||||||
|                 whileKeyword, |                 whileKeyword, | ||||||
|                 condition, |                 condition, | ||||||
|             }; |             }; | ||||||
|             if (optionalCommasAfterCondition != null) |             children.AddRange(optionalCommasAfterCondition); | ||||||
|             { |  | ||||||
|                 children.AddRange(optionalCommasAfterCondition); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             children.Add(body); |             children.Add(body); | ||||||
|             children.Add(end); |             children.Add(end); | ||||||
|             if (semicolonOrComma != null) |             children.Add(semicolonOrComma); | ||||||
|             { |  | ||||||
|                 children.Add(semicolonOrComma); |  | ||||||
|             } |  | ||||||
|             var result = new WhileStatementNode( |             var result = new WhileStatementNode( | ||||||
|                 children, |                 RemoveNulls(children), | ||||||
|                 whileKeyword, |                 whileKeyword, | ||||||
|                 condition, |                 condition, | ||||||
|                 optionalCommasAfterCondition, |                 optionalCommasAfterCondition, | ||||||
| @ -474,29 +458,14 @@ namespace Parser | |||||||
|                 ifKeyword, |                 ifKeyword, | ||||||
|                 condition |                 condition | ||||||
|             }; |             }; | ||||||
|             if (optionalCommasAfterCondition != null) |             children.AddRange(optionalCommasAfterCondition); | ||||||
|             { |  | ||||||
|                 children.AddRange(optionalCommasAfterCondition); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             children.Add(body); |             children.Add(body); | ||||||
|             if (elseKeyword != null) |             children.Add(elseKeyword); | ||||||
|             { |             children.Add(elseBody); | ||||||
|                 children.Add(elseKeyword); |             children.Add(endKeyword); | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             if (elseBody != null) |  | ||||||
|             { |  | ||||||
|                 children.Add(elseBody); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             if (endKeyword != null) |  | ||||||
|             { |  | ||||||
|                 children.Add(endKeyword); |  | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|             var result = new IfStatementNode( |             var result = new IfStatementNode( | ||||||
|                 children, |                 RemoveNulls(children), | ||||||
|                 ifKeyword, |                 ifKeyword, | ||||||
|                 condition, |                 condition, | ||||||
|                 optionalCommasAfterCondition, |                 optionalCommasAfterCondition, | ||||||
| @ -540,15 +509,11 @@ namespace Parser | |||||||
|                 forKeyword, |                 forKeyword, | ||||||
|                 forAssignment, |                 forAssignment, | ||||||
|             }; |             }; | ||||||
|             if (optionalCommasAfterAssignment != null) |             children.AddRange(optionalCommasAfterAssignment); | ||||||
|             { |  | ||||||
|                 children.AddRange(optionalCommasAfterAssignment); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             children.Add(body); |             children.Add(body); | ||||||
|             children.Add(endKeyword); |             children.Add(endKeyword); | ||||||
|             var result = new ForStatementNode( |             var result = new ForStatementNode( | ||||||
|                 children, |                 RemoveNulls(children), | ||||||
|                 forKeyword, |                 forKeyword, | ||||||
|                 forAssignment, |                 forAssignment, | ||||||
|                 body, |                 body, | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user