Esempio n. 1
0
        // TryStmt
        public override bool Walk(TryStatement node)
        {
            BitArray save = _bits;

            _bits = new BitArray(_bits);

            // Flow the body
            node.Body.Walk(this);

            if (node.Else != null)
            {
                // Else is flown only after completion of Try with same bits
                node.Else.Walk(this);
            }


            if (node.Handlers != null)
            {
                foreach (TryStatementHandler tsh in node.Handlers)
                {
                    // Restore to saved state
                    _bits.SetAll(false);
                    _bits.Or(save);

                    // Flow the test
                    if (tsh.Test != null)
                    {
                        tsh.Test.Walk(this);
                    }

                    // Define the target
                    if (tsh.Target != null)
                    {
                        tsh.Target.Walk(_fdef);
                    }

                    // Flow the body
                    tsh.Body.Walk(this);
                }
            }

            _bits = save;

            if (node.Finally != null)
            {
                // Flow finally - this executes no matter what
                node.Finally.Walk(this);
            }

            return(false);
        }
Esempio n. 2
0
        //try_stmt: ('try' ':' suite (except_clause ':' suite)+
        //    ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite)
        //# NB compile.c makes sure that the default except clause is last

        // Python 2.5 grammar
        //try_stmt: 'try' ':' suite
        //          (
        //            (except_clause ':' suite)+
        //            ['else' ':' suite]
        //            ['finally' ':' suite]
        //          |
        //            'finally' : suite
        //          )


        private Statement ParseTryStatement() {
            Eat(TokenKind.KeywordTry);
            string tryWhiteSpace = _tokenWhiteSpace;
            var start = GetStart();
            var mid = GetEnd();
            Statement body = ParseSuite();
            Statement finallySuite = null;
            Statement elseSuite = null;
            Statement ret;
            int end;

            string finallyWhiteSpace = null, elseWhiteSpace = null;
            if (MaybeEat(TokenKind.KeywordFinally)) {
                finallyWhiteSpace = _tokenWhiteSpace;
                finallySuite = ParseFinallySuite(finallySuite);
                end = finallySuite.EndIndex;
                TryStatement tfs = new TryStatement(body, null, elseSuite, finallySuite);
                tfs.HeaderIndex = mid;
                ret = tfs;
            } else {
                List<TryStatementHandler> handlers = new List<TryStatementHandler>();
                TryStatementHandler dh = null;
                end = GetEnd();
                while (true) {
                    if (!MaybeEat(TokenKind.KeywordExcept)) {
                        break;
                    }
                    TryStatementHandler handler = ParseTryStmtHandler();

                    end = handler.EndIndex;
                    handlers.Add(handler);

                    if (dh != null) {
                        ReportSyntaxError(dh.StartIndex, dh.HeaderIndex, "default 'except' must be last");
                    }
                    if (handler.Test == null) {
                        dh = handler;
                    }
                } 

                if (MaybeEat(TokenKind.KeywordElse)) {
                    elseWhiteSpace = _tokenWhiteSpace;
                    elseSuite = ParseSuite();
                    end = elseSuite.EndIndex;
                }

                if (MaybeEat(TokenKind.KeywordFinally)) {
                    // If this function has an except block, then it can set the current exception.
                    finallyWhiteSpace = _tokenWhiteSpace;
                    finallySuite = ParseFinallySuite(finallySuite);
                    end = finallySuite.EndIndex;
                }

                TryStatement ts = new TryStatement(body, handlers.ToArray(), elseSuite, finallySuite);
                ts.HeaderIndex = mid;
                ret = ts;
            }
            if (_verbatim) {
                AddPreceedingWhiteSpace(ret, tryWhiteSpace);
                if (elseWhiteSpace != null) {
                    AddSecondPreceedingWhiteSpace(ret, elseWhiteSpace);
                }
                if (finallyWhiteSpace != null) {
                    AddThirdPreceedingWhiteSpace(ret, finallyWhiteSpace);
                }
            }
            ret.SetLoc(start, end);
            return ret;
        }
            public override bool Walk(TryStatement node) {
                if (ShouldWalkNormally(node)) {
                    return base.Walk(node);
                }

                // Don't walk 'try' body, but walk everything else
                if (node.Handlers != null) {
                    foreach (var handler in node.Handlers) {
                        handler.Walk(this);
                    }
                }
                if (node.Else != null) {
                    node.Else.Walk(this);
                }
                if (node.Finally != null) {
                    node.Finally.Walk(this);
                }

                return false;
            }
            private static bool ShouldWalkNormally(TryStatement node) {
                if (node.Handlers == null) {
                    return true;
                }

                foreach (var handler in node.Handlers) {
                    if (handler.Test == null || IsImportError(handler.Test)) {
                        return false;
                    }
                }

                return true;
            }
Esempio n. 5
0
 public override bool Walk(TryStatement node) {
     _statements.Add(node);
     return base.Walk(node);
 }
Esempio n. 6
0
        // TryStatement
        public override bool Walk(TryStatement node) {
            // we manually walk the TryStatement so we can track finally blocks.
            _currentScope.ContainsExceptionHandling = true;

            node.Body.Walk(this);

            if (node.Handlers != null) {
                foreach (TryStatementHandler tsh in node.Handlers) {
                    if (tsh.Target != null) {
                        tsh.Target.Walk(_define);
                    }
                    tsh.Walk(this);
                }
            }

            if (node.Else != null) {
                node.Else.Walk(this);
            }

            if (node.Finally != null) {
                _finallyCount[_finallyCount.Count - 1]++;
                node.Finally.Walk(this);
                _finallyCount[_finallyCount.Count - 1]--;
            }

            return false;
        }
Esempio n. 7
0
        public override bool Walk(TryStatement node) {
            UpdateLineInfo(node);

            return base.Walk(node);
        }
Esempio n. 8
0
 public override bool Walk(TryStatement node) {
     UpdateChildRanges(node);
     return base.Walk(node);
 }
Esempio n. 9
0
            public override bool Walk(TryStatement node) {
                node.Body.Walk(this);

                if (node.Handlers != null && node.Handlers.Count > 0) {
                    // treat any exceptions from the body as handled, any exceptions
                    // from the handlers/else are not handled.
                    _raises = false;

                    foreach (var handler in node.Handlers) {
                        handler.Walk(this);
                    }
                }

                if (node.Finally != null) {
                    node.Finally.Walk(this);
                }

                if (node.Else != null) {
                    node.Else.Walk(this);
                }

                return false;
            }
Esempio n. 10
0
        // TryStmt
        public override bool Walk(TryStatement node) {
            BitArray save = _bits;
            _bits = new BitArray(_bits);

            // Flow the body
            node.Body.Walk(this);

            if (node.Else != null) {
                // Else is flown only after completion of Try with same bits
                node.Else.Walk(this);
            }


            if (node.Handlers != null) {
                foreach (TryStatementHandler tsh in node.Handlers) {
                    // Restore to saved state
                    _bits.SetAll(false);
                    _bits.Or(save);

                    // Flow the test
                    if (tsh.Test != null) {
                        tsh.Test.Walk(this);
                    }

                    // Define the target
                    if (tsh.Target != null) {
                        tsh.Target.Walk(_fdef);
                    }

                    // Flow the body
                    tsh.Body.Walk(this);
                }
            }

            _bits = save;

            if (node.Finally != null) {
                // Flow finally - this executes no matter what
                node.Finally.Walk(this);
            }

            return false;
        }
Esempio n. 11
0
 public override void PostWalk(TryStatement node) { PostWalkWorker(node); }
Esempio n. 12
0
 // TryStatement
 public override bool Walk(TryStatement node) { return ShouldWalkWorker(node); }
Esempio n. 13
0
        public override bool Walk(TryStatement node) {
            if (node.Body != null) {
                AddTagIfNecessary(node.StartIndex, node.Body.EndIndex, node.HeaderIndex);
            }
            if (node.Handlers != null) {
                foreach (var h in node.Handlers) {
                    AddTagIfNecessary(h, h.HeaderIndex);
                }
            }
            if (node.Finally != null) {
                AddTagIfNecessary(node.FinallyIndex, node.Finally.EndIndex, node.FinallyIndex);
            }
            if (node.Else != null) {
                AddTagIfNecessary(node.ElseIndex, node.Else.EndIndex, node.ElseIndex);
            }

            return base.Walk(node);
        }