OtherBlockStmtType() static private method

static private OtherBlockStmtType ( LNode _n, Pedantics p ) : Symbol
_n LNode
p Pedantics
return Symbol
示例#1
0
        private bool PrintBracedBlockOrStmt(LNode stmt, Ambiguity flags, NewlineOpt beforeBrace = NewlineOpt.BeforeExecutableBrace)
        {
            var name = stmt.Name;

            if (name == S.Braces && !HasPAttrs(stmt) && HasSimpleHeadWPA(stmt))
            {
                PrintBracedBlock(stmt, beforeBrace);
                return(true);
            }
            // Detect "else if (...)", and suppress newline/indent between "else" and "if".
            if (name == S.If && (flags & Ambiguity.ElseClause) != 0)
            {
                using (With(stmt))
                    if (EcsValidators.OtherBlockStmtType(_n, Pedantics) == S.If)
                    {
                        PrintStmt(flags & (Ambiguity.FinalStmt | Ambiguity.ElseClause));
                        return(false);
                    }
            }
            using (Indented)
            {
                Newline(NewlineOpt.BeforeSingleSubstmt);
                PrintStmt(stmt, flags & (Ambiguity.FinalStmt | Ambiguity.NoIfWithoutElse));
                return(false);
            }
        }
示例#2
0
        // Prints a child statement that could be a braced block, or not
        private bool PrintBracedBlockOrStmt(LNode child, NewlineOpt beforeBrace = NewlineOpt.BeforeExecutableBrace)
        {
            var name = child.Name;

            if (name == S.Braces && !HasPAttrs(child) && HasSimpleHeadWPA(child))
            {
                PrintBracedBlock(child, beforeBrace);
                return(true);
            }
            // Detect "else if (...)", and suppress newline/indent between "else" and "if".
            if (name == S.If && Flagged(Ambiguity.ElseClause))
            {
                if (EcsValidators.OtherBlockStmtType(_n, Pedantics) == S.If)
                {
                    PrintStmt(child, _flags & (Ambiguity.FinalStmt | Ambiguity.ElseClause | Ambiguity.OneLiner));
                    return(false);
                }
            }
            using (Indented)
            {
                PrintStmt(child, _flags & (Ambiguity.FinalStmt | Ambiguity.NoIfWithoutElse | Ambiguity.OneLiner) | Ambiguity.NewlineBeforeChildStmt);
                return(false);
            }
        }
示例#3
0
        public SPResult AutoPrintOtherBlockStmt(Ambiguity flags)
        {
            // S.If, S.For, S.ForEach, S.Checked, S.Unchecked, S.Try
            var type = EcsValidators.OtherBlockStmtType(_n, Pedantics);

            if (type == null)
            {
                return(SPResult.Fail);
            }

            if (type == S.If)
            {
                var  @else          = _n.Args[2, null];
                bool needCloseBrace = false;
                if (@else == null && (flags & Ambiguity.NoIfWithoutElse) != 0)
                {
                    if (AllowExtraBraceForIfElseAmbig)
                    {
                        _out.Write('{', true);
                        needCloseBrace = true;
                    }
                    else
                    {
                        return(SPResult.Fail);
                    }
                }

                // Note: the "if" statement in particular cannot have "word" attributes
                //       because they would create ambiguity with property declarations
                G.Verify(0 == PrintAttrs(StartStmt, AttrStyle.AllowKeywordAttrs, flags));

                _out.Write("if", true);
                PrintWithinParens(ParenFor.KeywordCall, _n.Args[0]);

                var thenFlags = flags & ~(Ambiguity.ElseClause);
                if (@else != null)
                {
                    thenFlags |= Ambiguity.NoIfWithoutElse;
                }
                bool braces = PrintBracedBlockOrStmt(_n.Args[1], thenFlags);

                if (@else != null)
                {
                    if (!Newline(braces ? NewlineOpt.BeforeExecutableBrace : NewlineOpt.Default))
                    {
                        Space(SpaceOpt.Default);
                    }
                    _out.Write("else", true);
                    PrintBracedBlockOrStmt(@else, flags | Ambiguity.ElseClause);
                }

                if (needCloseBrace)
                {
                    _out.Write('}', true);
                }
                return(SPResult.NeedSuffixTrivia);
            }

            G.Verify(0 == PrintAttrs(StartStmt, AttrStyle.AllowWordAttrs, flags));

            if (type == S.For)
            {
                _out.Write("for", true);
                PrintArgList(_n.Args.First(3), ParenFor.KeywordCall, flags, true, ';');
                PrintBracedBlockOrStmt(_n.Args[3], flags);
            }
            else if (type == S.ForEach)
            {
                _out.Write("foreach", true);
                WriteOpenParen(ParenFor.KeywordCall);
                PrintExpr(_n.Args[0], EP.Equals.LeftContext(StartStmt), Ambiguity.AllowUnassignedVarDecl | Ambiguity.ForEachInitializer);
                _out.Space();
                _out.Write("in", true);
                _out.Space();
                PrintExpr(_n.Args[1], ContinueExpr, flags);
                WriteCloseParen(ParenFor.KeywordCall);

                PrintBracedBlockOrStmt(_n.Args[2], flags);
            }
            else if (type == S.Try)
            {
                _out.Write("try", true);
                bool braces = PrintBracedBlockOrStmt(_n.Args[0], flags, NewlineOpt.BeforeSimpleStmtBrace);
                for (int i = 1, c = _n.ArgCount; i < c; i++)
                {
                    if (!Newline(braces ? NewlineOpt.BeforeExecutableBrace : NewlineOpt.Default))
                    {
                        Space(SpaceOpt.Default);
                    }
                    var   clause = _n.Args[i];
                    LNode first = clause.Args[0], second = clause.Args[1, null];

                    WriteOperatorName(clause.Name);
                    if (clause.Name == S.Finally)
                    {
                        braces = PrintBracedBlockOrStmt(clause.Args[0], flags);
                    }
                    else                       // catch
                    {
                        var eVar = clause.Args[0];
                        if (!eVar.IsIdNamed(S.Missing))
                        {
                            PrintWithinParens(ParenFor.KeywordCall, eVar, Ambiguity.AllowUnassignedVarDecl);
                        }
                        var when = clause.Args[1];
                        if (!when.IsIdNamed(S.Missing))
                        {
                            Space(SpaceOpt.Default);
                            _out.Write("when", true);
                            PrintWithinParens(ParenFor.KeywordCall, when);
                        }
                        braces = PrintBracedBlockOrStmt(clause.Args[2], flags);
                    }
                }
            }
            else if (type == S.Checked)             // includes S.Unchecked
            {
                WriteOperatorName(_n.Name);
                PrintBracedBlockOrStmt(_n.Args[0], flags, NewlineOpt.BeforeSimpleStmtBrace);
            }

            return(SPResult.NeedSuffixTrivia);
        }