/// <summary> /// Reads the next for-statement from the file and returns it. /// </summary> /// <param name="parentReference">The parent code unit.</param> /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param> /// <returns>Returns the statement.</returns> private ForStatement ParseForStatement(Reference<ICodePart> parentReference, bool unsafeCode) { Param.AssertNotNull(parentReference, "parentReference"); Param.Ignore(unsafeCode); var statementReference = new Reference<ICodePart>(); // Add the for keyword. CsToken firstToken = this.GetToken(CsTokenType.For, SymbolType.For, parentReference, statementReference); Node<CsToken> firstTokenNode = this.tokens.InsertLast(firstToken); // Get the opening parenthesis. Bracket openParenthesis = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis, statementReference); Node<CsToken> openParenthesisNode = this.tokens.InsertLast(openParenthesis); // Get each of the initializers. List<Expression> initializers = this.ParseForStatementInitializers(statementReference, unsafeCode); // Get the condition expression. Expression condition = this.ParseForStatementCondition(statementReference, unsafeCode); // Get the iterators. List<Expression> iterators = this.ParseForStatementIterators(statementReference, unsafeCode, openParenthesis, openParenthesisNode); // Get the embedded statement. Statement childStatement = this.GetNextStatement(statementReference, unsafeCode); if (childStatement == null || childStatement.Tokens.First == null) { throw new SyntaxException(this.document.SourceCode, firstToken.LineNumber); } // Create the token list for the statement. CsTokenList partialTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last); // Create and return the for-statement. ForStatement statement = new ForStatement( partialTokens, initializers.ToArray(), condition, iterators.ToArray()); statement.EmbeddedStatement = childStatement; statementReference.Target = statement; // Add the variables declared in the statement. foreach (Expression initializer in initializers) { VariableDeclarationExpression variableDeclaration = initializer as VariableDeclarationExpression; if (variableDeclaration != null) { var initializerReference = new Reference<ICodePart>(initializer); foreach (VariableDeclaratorExpression declarator in variableDeclaration.Declarators) { Variable variable = new Variable( variableDeclaration.Type, declarator.Identifier.Token.Text, VariableModifiers.None, CodeLocation.Join(variableDeclaration.Type.Location, declarator.Identifier.Token.Location), initializerReference, variableDeclaration.Type.Generated || declarator.Identifier.Token.Generated); // If there is already a variable in this scope with the same name, ignore this one. if (!statement.Variables.Contains(declarator.Identifier.Token.Text)) { statement.Variables.Add(variable); } } } } return statement; }
private ForStatement ParseForStatement(bool unsafeCode) { CsToken item = this.GetToken(CsTokenType.For, SymbolType.For); Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(item); Bracket bracketToken = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis); Microsoft.StyleCop.Node<CsToken> openParenthesisNode = this.tokens.InsertLast(bracketToken); List<Expression> list = this.ParseForStatementInitializers(unsafeCode); Expression condition = this.ParseForStatementCondition(unsafeCode); List<Expression> list2 = this.ParseForStatementIterators(unsafeCode, bracketToken, openParenthesisNode); Statement nextStatement = this.GetNextStatement(unsafeCode); if ((nextStatement == null) || (nextStatement.Tokens.First == null)) { throw new SyntaxException(this.document.SourceCode, item.LineNumber); } CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last); ForStatement statement2 = new ForStatement(tokens, list.ToArray(), condition, list2.ToArray()); statement2.EmbeddedStatement = nextStatement; foreach (Expression expression2 in list) { VariableDeclarationExpression expression3 = expression2 as VariableDeclarationExpression; if (expression3 != null) { foreach (VariableDeclaratorExpression expression4 in expression3.Declarators) { Variable variable = new Variable(expression3.Type, expression4.Identifier.Token.Text, VariableModifiers.None, expression4.Tokens.First.Value.Location.StartPoint, expression3.Type.Generated || expression4.Identifier.Token.Generated); if (!statement2.Variables.Contains(expression4.Identifier.Token.Text)) { statement2.Variables.Add(variable); } } continue; } } return statement2; }