Esempio n. 1
0
    private bool HasPathDFS(TryNode node, string word, int index)
    {
        // If it gets to the end of the word
        if (index == word.Length)
        {
            return(node.isCompleteWord);
        }

        // If the children contains the char I'm looking for, get one level deep
        if (node.children.ContainsKey(word[index]) && HasPathDFS(node.children[word[index]], word, index + 1))
        {
            return(true);
        }

        // In case the word contains '.' it means we can use any child as the next step
        if (word[index] == '.')
        {
            foreach (TryNode child in node.children.Values)
            {
                if (HasPathDFS(child, word, index + 1))
                {
                    return(true);
                }
            }
        }

        // If we get to this point, there is no path
        return(false);
    }
Esempio n. 2
0
        public virtual void Visit(TryNode node)
        {
            if (node != null)
            {
                if (node.TryBlock != null)
                {
                    node.TryBlock.Accept(this);
                }

                if (node.CatchParameter != null)
                {
                    node.CatchParameter.Accept(this);
                }

                if (node.CatchBlock != null)
                {
                    node.CatchBlock.Accept(this);
                }

                if (node.FinallyBlock != null)
                {
                    node.FinallyBlock.Accept(this);
                }
            }
        }
Esempio n. 3
0
        private IEnumerable <Block> GetTryNodeBlocks(TryNode tryNode)
        {
            List <Block> blocks = new List <Block>();

            blocks.Add(tryNode.Block);
            List <Block> tryNodeBlocks = GetBlocks(tryNode.Block.Statements);

            blocks.AddRange(tryNodeBlocks);
            return(blocks);
        }
Esempio n. 4
0
 public override bool Walk(TryNode node)
 {
     if (CheckBlock(node.TryBlock) ||
         CheckBlock(node.FinallyBlock) ||
         CheckBlock(node.CatchBlock))
     {
         Span = GetTargetStatement(node).GetSpan(_tree.LocationResolver);
         return(false);
     }
     return(base.Walk(node));
 }
Esempio n. 5
0
        public TryBuilder(TryNode trynode)
        {
            node          = trynode;
            DotDefinition = new List <IDotElement>();

            CreateNode();
            CreateEndNode();
            CreateCatchEdge();
            CreateEdgeToFirstChildren();
            CreateEdgeToNextSibling();
        }
Esempio n. 6
0
 public object Process(Parser parser, SortedDictionary <string, object> parameters)
 {
     if (parser.CurrenToken.Type == TokenType.KwCatch)
     {
         parser.NextToken();
         if (parser.CurrenToken.Type == TokenType.PmLeftParent)
         {
             parser.NextToken();
             if (parser.CurrenToken.Type == TokenType.Id)
             {
                 string _idName = parser.CurrenToken.LexemeVal;
                 parser.NextToken();
                 if (parser.CurrenToken.Type == TokenType.PmRightParent)
                 {
                     parser.NextToken();
                     var _codeCatch = (List <StatementNode>) new CompoundStatement().Process(parser, parameters);
                     var _id        = new IdNode {
                         Name = _idName
                     };
                     var _catch = new CatchNode {
                         Id = _id, CodeCatch = _codeCatch
                     };
                     var _try = new TryNode {
                         ExceptionID = _id, CatchBlockCode = _catch
                     };
                     return(_try);
                 }
                 else
                 {
                     throw new ParserException("This was expected ) in the catch block, Received: [" +
                                               parser.CurrenToken.LexemeVal + "], Row: " + parser.CurrenToken.Row
                                               + ", Column: " + parser.CurrenToken.Column);
                 }
             }
             else
             {
                 throw  new ParserException("This was expected a Identifier in the catch block, Received: [" +
                                            parser.CurrenToken.LexemeVal + "], Row: " + parser.CurrenToken.Row
                                            + ", Column: " + parser.CurrenToken.Column);
             }
         }
         else
         {
             throw new ParserException("This was expected ( in the catch block, Received: [" +
                                       parser.CurrenToken.LexemeVal + "], Row: " + parser.CurrenToken.Row
                                       + ", Column: " + parser.CurrenToken.Column);
         }
     }
     else
     {
         return(new TryNode());
     }
 }
Esempio n. 7
0
        public void MethodGraph_TryCatchFinally_FinallyBlockIsParsed()
        {
            Method  sampleMethod          = TestHelper.GetSample <MethodGraph_ClassSample> ("TryCatchFinally");
            TryNode outerTryNode          = (TryNode)sampleMethod.Body.Statements[1];
            Block   finallyBlock          = outerTryNode.Finally.Block;
            int     finallyBlockUniqueKey = finallyBlock.UniqueKey;

            IMethodGraph methodGraph       = BuildMethodGraph(sampleMethod);
            BasicBlock   finallyBasicBlock = methodGraph.GetBasicBlockById(finallyBlockUniqueKey);

            Assert.That(finallyBasicBlock, Is.Not.Null);
        }
Esempio n. 8
0
        private Statement ProcessTryCatchFinallyStatement(TryNode node)
        {
            Debug.Assert(node.CatchClauses.Count < 2);
            Debug.Assert(node.CatchClauses.Count == 1 || node.FinallyClause != null);

            TryCatchFinallyStatement statement = new TryCatchFinallyStatement();

            Statement body = BuildStatement((StatementNode)node.Body);

            statement.AddBody(body);

            if (node.CatchClauses.Count == 1)
            {
                CatchNode catchNode = (CatchNode)node.CatchClauses[0];

                VariableSymbol exceptionVariableSymbol = null;

                if (catchNode.Name != null)
                {
                    TypeSymbol exceptionVariableType =
                        symbolSet.ResolveType(catchNode.Type, symbolTable, memberContext);
                    Debug.Assert(exceptionVariableType != null);

                    exceptionVariableSymbol =
                        new VariableSymbol(catchNode.Name.Name, memberContext, exceptionVariableType);
                }
                else
                {
                    TypeSymbol exceptionVariableType =
                        symbolSet.ResolveIntrinsicType(IntrinsicType.Exception);
                    Debug.Assert(exceptionVariableType != null);

                    exceptionVariableSymbol =
                        new VariableSymbol(symbolTable.CreateSymbolName("e"), memberContext, exceptionVariableType);
                }

                symbolTable.PushScope();
                symbolTable.AddSymbol(exceptionVariableSymbol);

                Statement catchStatement = BuildStatement((StatementNode)catchNode.Body);
                statement.AddCatch(exceptionVariableSymbol, catchStatement);

                symbolTable.PopScope();
            }

            if (node.FinallyClause != null)
            {
                Statement finallyStatement = BuildStatement((StatementNode)node.FinallyClause);
                statement.AddFinally(finallyStatement);
            }

            return(statement);
        }
Esempio n. 9
0
        bool IParseNodeValidator.Validate(ParseNode node, CompilerOptions options, IErrorHandler errorHandler)
        {
            TryNode tryNode = (TryNode)node;

            if (tryNode.CatchClauses != null && tryNode.CatchClauses.Count > 1)
            {
                errorHandler.ReportNodeValidationError(DSharpStringResources.NODE_VALIDATION_ERROR_TRY_CATCH, tryNode);

                return(false);
            }

            return(true);
        }
Esempio n. 10
0
        bool IParseNodeValidator.Validate(ParseNode node, CompilerOptions options, IErrorHandler errorHandler)
        {
            TryNode tryNode = (TryNode)node;

            if ((tryNode.CatchClauses != null) && (tryNode.CatchClauses.Count > 1))
            {
                errorHandler.ReportError("Try/Catch statements are limited to a single catch clause.",
                                         tryNode.Token.Location);
                return(false);
            }

            return(true);
        }
Esempio n. 11
0
        public void MethodGraph_TryCatchFinally_CatchBlockIsParsed()
        {
            Method    sampleMethod        = TestHelper.GetSample <MethodGraph_ClassSample> ("TryCatchFinally");
            TryNode   outerTryNode        = (TryNode)sampleMethod.Body.Statements[1];
            TryNode   innerTryNode        = (TryNode)outerTryNode.Block.Statements[0];
            CatchNode catchNode           = innerTryNode.Catchers[0];
            int       catchBlockUniqueKey = catchNode.Block.UniqueKey;

            IMethodGraph methodGraph     = BuildMethodGraph(sampleMethod);
            BasicBlock   catchBasicBlock = methodGraph.GetBasicBlockById(catchBlockUniqueKey);

            Assert.That(catchBasicBlock, Is.Not.Null);
        }
Esempio n. 12
0
 /** Adds a word into the data structure. */
 public void AddWord(string word)
 {
     pointer = tryHead;
     foreach (char c in word)
     {
         if (!pointer.children.ContainsKey(c))
         {
             TryNode child = new TryNode(c);
             pointer.children[c] = child;
         }
         pointer = pointer.children[c];
     }
     pointer.isCompleteWord = true;
 }
Esempio n. 13
0
        public override bool Walk(TryNode node)
        {
            node.TryBlock.Walk(this);
            if (node.CatchBlock != null)
            {
                node.CatchBlock.Walk(this);
            }
#if FALSE
            if (node.Handlers != null)
            {
                foreach (var handler in node.Handlers)
                {
                    var test = AnalysisSet.Empty;
                    if (handler.Test != null)
                    {
                        var testTypes = _eval.Evaluate(handler.Test);

                        if (handler.Target != null)
                        {
                            foreach (var type in testTypes)
                            {
                                ClassInfo klass = type as ClassInfo;
                                if (klass != null)
                                {
                                    test = test.Union(klass.Instance.SelfSet);
                                }

                                BuiltinClassInfo builtinClass = type as BuiltinClassInfo;
                                if (builtinClass != null)
                                {
                                    test = test.Union(builtinClass.Instance.SelfSet);
                                }
                            }

                            _eval.AssignTo(handler, handler.Target, test);
                        }
                    }

                    handler.Body.Walk(this);
                }
            }
#endif

            if (node.FinallyBlock != null)
            {
                node.FinallyBlock.Walk(this);
            }
            return(false);
        }
Esempio n. 14
0
        private List <Block> GetBlocks(StatementCollection statements)
        {
            List <Block> blocks = new List <Block>();

            foreach (var statement in statements)
            {
                if (statement is Block)
                {
                    blocks.Add((Block)statement);
                }
                else if (statement is TryNode)
                {
                    TryNode tryNode = (TryNode)statement;
                    blocks.AddRange(GetTryNodeBlocks(tryNode));
                    blocks.AddRange(GetCatcherBlocks(tryNode.Catchers));
                    if (tryNode.Finally != null)
                    {
                        blocks.AddRange(GetFinallyBlocks(tryNode.Finally));
                    }
                }
            }
            return(blocks);
        }
Esempio n. 15
0
        public virtual void Visit(TryNode node)
        {
            if (node != null)
            {
                if (node.TryBlock != null)
                {
                    node.TryBlock.Accept(this);
                }

                if (node.CatchBlock != null)
                {
                    node.CatchBlock.Accept(this);
                }

                if (node.FinallyBlock != null)
                {
                    node.FinallyBlock.Accept(this);
                }
            }
        }
Esempio n. 16
0
 public override bool Walk(TryNode node)
 {
     UpdateChildRanges(node);
     return(base.Walk(node));
 }
Esempio n. 17
0
 public virtual void PostWalk(TryNode node)
 {
 }
Esempio n. 18
0
 public override bool Walk(TryNode node)
 {
     AddNode(node); return(true);
 }
Esempio n. 19
0
 public virtual void Visit(TryNode node)
 {
     if (node != null)
     {
          AcceptChildren(node);
     }
 }
Esempio n. 20
0
 public virtual bool Walk(TryNode node)
 {
     return(true);
 }
Esempio n. 21
0
    override protected void ParseExceptionHandlerEntry(bool smallSection) {
      int dataSize = this.reader.tables.GetByte();
      int n = (int)(ushort)this.reader.tables.GetInt16();
      if (smallSection)
        n = dataSize / 12;
      else
        n = (dataSize + (n << 8)) / 24;
      for (int i = 0; i < n; i++) {
        int flags, tryOffset, tryLength, handlerOffset, handlerLength, tokenOrOffset;
        if (smallSection) {
          flags = this.reader.tables.GetInt16();
          tryOffset = this.reader.tables.GetUInt16();
          tryLength = this.reader.tables.GetByte();
          handlerOffset = this.reader.tables.GetUInt16();
          handlerLength = this.reader.tables.GetByte();
        }
        else {
          flags = this.reader.tables.GetInt32();
          tryOffset = this.reader.tables.GetInt32();
          tryLength = this.reader.tables.GetInt32();
          handlerOffset = this.reader.tables.GetInt32();
          handlerLength = this.reader.tables.GetInt32();
        }
        tokenOrOffset = this.reader.tables.GetInt32();
        Block tryStartBlock = Reader.GetOrCreateBlock(this.blockMap, tryOffset);
        Block blockAfterTryEnd = Reader.GetOrCreateBlock(this.blockMap, tryOffset + tryLength);
        Block handlerStartBlock = Reader.GetOrCreateBlock(this.blockMap, handlerOffset);
        Block blockAfterHandlerEnd = Reader.GetOrCreateBlock(this.blockMap, handlerOffset + handlerLength);
        List<TryNode> tryList = null;
        if (!this.tryMap.TryGetValue(tryStartBlock, out tryList)) {
          this.tryMap[tryStartBlock] = tryList = new List<TryNode>();
        }
        TryNode currentTry = null;
        int tryEnd = tryOffset + tryLength;
        foreach (TryNode t in tryList) {
          if (t.tryEnd == tryEnd) {
            currentTry = t;
            break;
          }
        }
        if (currentTry == null) {
          currentTry = new TryNode();
          currentTry.tryEnd = tryEnd;
          tryList.Add(currentTry);
        }
        int handlerEnd = handlerOffset + handlerLength;
        if (currentTry.handlersEnd < handlerEnd)
          currentTry.handlersEnd = handlerEnd;

        Debug.Assert((int)flags != 3);
        Debug.Assert((int)flags < 5);

        switch (flags) {
          case 0x00:
            // for a catch handler, tokenOrOffset represents
            // the metadata token of the handler type. handlerOffset
            // is the literal offset for the catch block
            int pos = this.reader.tables.GetCurrentPosition();
            TypeNode filterType = (TypeNode)this.reader.GetMemberFromToken(tokenOrOffset);
            this.reader.tables.SetCurrentPosition(pos);
            string variableName = "$exception" + this.handlerMap.Count.ToString(CultureInfo.InvariantCulture);
            StackVariable exception = new StackVariable(filterType, variableName);
            CatchNode c = new CatchNode(handlerStartBlock, exception, filterType);
            c.handlerEnd = handlerEnd;
            currentTry.Catchers.Add(c);
            this.handlerMap[handlerOffset] = exception;
            break;
          case 0x01:
            // for a filter, tokenOrOffset represents the IL offset
            // of the filter block. handlerOffset represents
            // the IL offset of the associated catch handler
            Block filterExpression = Reader.GetOrCreateBlock(blockMap, tokenOrOffset);
            variableName = "$exception" + this.handlerMap.Count.ToString(CultureInfo.InvariantCulture);
            exception = new StackVariable(CoreSystemTypes.Object, variableName);
            Filter filter = new Filter(filterExpression, exception);
            filter.handlerEnd = handlerOffset;
            c = new CatchNode(handlerStartBlock, exception, null, filter);
            c.handlerEnd = handlerEnd;
            currentTry.Catchers.Add(c);
            // note that handlerOffset would not be correct here!
            this.handlerMap[tokenOrOffset] = exception;
            break;
          case 0x02:
            FinallyNode f = new FinallyNode(handlerStartBlock);
            f.handlerEnd = handlerEnd;
            currentTry.Finally = f;
            break;
          case 0x04:
            FaultHandler fh = new FaultHandler(handlerStartBlock);
            fh.handlerEnd = handlerEnd;
            currentTry.FaultHandler = fh;
            break;
        }
      }
    }
Esempio n. 22
0
 public virtual void PostWalk(TryNode node) { }
Esempio n. 23
0
 public virtual bool Walk(TryNode node) { return true; }
Esempio n. 24
0
 public TryCompiler(TryNode node)
 {
     this.node = node;
 }
 public void Visit(TryNode node)
 {
     // not applicable; terminate
 }
Esempio n. 26
0
 private void GetHandlerEnd(TryNode t, Block block, out int handlerEnd, out Node handlerNode) {
   handlerEnd = Int32.MaxValue;
   handlerNode = null;
   int startPos = block.ILOffset;
   if (t.Finally != null
     && t.Finally.handlerEnd > startPos
     && t.Finally.handlerEnd < handlerEnd) {
     handlerEnd = t.Finally.handlerEnd;
     handlerNode = t.Finally;
   }
   foreach (CatchNode c in t.Catchers) {
     if (c.handlerEnd > startPos && c.handlerEnd < handlerEnd) {
       handlerEnd = c.handlerEnd;
       handlerNode = c;
     }
     if (c.Filter != null && c.Filter.handlerEnd > startPos && c.Filter.handlerEnd < handlerEnd) {
       handlerEnd = c.Filter.handlerEnd;
       handlerNode = c.Filter;
     }
   }
   if (t.FaultHandler != null
     && t.FaultHandler.handlerEnd > startPos
     && t.FaultHandler.handlerEnd < handlerEnd) {
     handlerEnd = t.FaultHandler.handlerEnd;
     handlerNode = t.FaultHandler;
   }
 }
Esempio n. 27
0
 /** Initialize your data structure here. */
 public WordDictionary()
 {
     tryHead = new TryNode();
 }