Ejemplo n.º 1
0
        /// <summary>
        /// Reads the next do-while-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 DoWhileStatement ParseDoWhileStatement(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            Reference<ICodePart> statementReference = new Reference<ICodePart>();

            // Add the do keyword.
            CsToken firstToken = this.GetToken(CsTokenType.Do, SymbolType.Do, parentReference, statementReference);
            Node<CsToken> firstTokenNode = this.tokens.InsertLast(firstToken);

            // Get the attached statement.
            Statement childStatement = this.GetNextStatement(statementReference, unsafeCode);
            if (childStatement == null || childStatement.Tokens.First == null)
            {
                throw new SyntaxException(this.document.SourceCode, firstToken.LineNumber);
            }

            // Get the while keyword and add it.
            this.tokens.Add(this.GetToken(CsTokenType.WhileDo, SymbolType.While, statementReference));

            // Get the opening parenthesis and add it.
            Bracket openParenthesis = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis, statementReference);
            Node<CsToken> openParenthesisNode = this.tokens.InsertLast(openParenthesis);

            // Get the expression within the parenthesis.
            Expression expression = this.GetNextExpression(ExpressionPrecedence.None, statementReference, unsafeCode);
            if (expression == null)
            {
                throw this.CreateSyntaxException();
            }

            // Get the closing parenthesis.
            Bracket closeParenthesis = this.GetBracketToken(CsTokenType.CloseParenthesis, SymbolType.CloseParenthesis, statementReference);
            Node<CsToken> closeParenthesisNode = this.tokens.InsertLast(closeParenthesis);

            openParenthesis.MatchingBracketNode = closeParenthesisNode;
            closeParenthesis.MatchingBracketNode = openParenthesisNode;

            // Get the closing semicolon.
            this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon, statementReference));

            // Create the token list for the statement.
            CsTokenList partialTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last);

            // Create and return the do-while-statement.
            DoWhileStatement statement = new DoWhileStatement(partialTokens, expression, childStatement);
            statementReference.Target = statement;

            return statement;
        }
        /// <summary>
        /// The save.
        /// </summary>
        /// <param name="doWhileStatement">
        /// The do while statement.
        /// </param>
        private void Save(DoWhileStatement doWhileStatement)
        {
            this.cppWriter.Write("do");

            this.Save(doWhileStatement.EmbeddedStatement);

            this.cppWriter.Write("while (");
            @switch(doWhileStatement.ConditionalExpression);
            this.cppWriter.WriteLine(")");
        }