示例#1
0
        /// <summary>
        /// Reads the next switch-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 SwitchStatement ParseSwitchStatement(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

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

            // Move past the switch keyword.
            CsToken firstToken = this.GetToken(CsTokenType.Switch, SymbolType.Switch, 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 the inner expression.
            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 opening curly bracket.
            Bracket openingBracket = this.GetBracketToken(CsTokenType.OpenCurlyBracket, SymbolType.OpenCurlyBracket, statementReference);
            Node<CsToken> openingBracketNode = this.tokens.InsertLast(openingBracket);

            // Get the case and default statements.
            SwitchDefaultStatement defaultStatement;
            List<SwitchCaseStatement> caseStatements = this.ParseSwitchStatementCaseStatements(statementReference, unsafeCode, out defaultStatement);

            // Get the closing curly bracket.
            Bracket closingBracket = this.GetBracketToken(CsTokenType.CloseCurlyBracket, SymbolType.CloseCurlyBracket, statementReference);
            Node<CsToken> closingBracketNode = this.tokens.InsertLast(closingBracket);

            openingBracket.MatchingBracketNode = closingBracketNode;
            closingBracket.MatchingBracketNode = openingBracketNode;

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

            // Create and return the switch-statement.
            SwitchStatement statement = new SwitchStatement(partialTokens, expression, caseStatements.ToArray(), defaultStatement);
            statementReference.Target = statement;

            return statement;
        }
        /// <summary>
        /// The save.
        /// </summary>
        /// <param name="switchStatement">
        /// The switch statement.
        /// </param>
        private void Save(SwitchStatement switchStatement)
        {
            this.cppWriter.Write("switch (");
            @switch(switchStatement.SwitchItem);
            this.cppWriter.WriteLine(")");

            this.SetMarkBeginOfBlock();

            this.cppWriter.WriteLine("{");

            @switch(switchStatement.CaseStatements);

            this.cppWriter.Indent++;
            this.cppWriter.WriteLine();
            @switch(switchStatement.DefaultStatement);
            this.cppWriter.Indent--;

            this.cppWriter.WriteLine("}");

            this.SetMarkEndOfBlock();
        }