Ejemplo n.º 1
0
 public override void EnterLocal_variable_declaration([NotNull] CSharpParser.Local_variable_declarationContext context)
 {
     //sua lai
     if (context.GetChild(1).GetChild(0).GetText().Equals(commandVar))
     {
         if (!listLine.Contains(context.Start.Line))
         {
             listLine.Add(context.Start.Line);
         }
     }
 }
Ejemplo n.º 2
0
        public override void EnterLocal_variable_declaration([NotNull] CSharpParser.Local_variable_declarationContext context)
        {
            //for (int i = 0; i < context.ChildCount; i++)
            //{
            //    Console.WriteLine("Child " + i + " : " + context.GetChild(i).GetText());
            //    for (int j = 0; j < context.GetChild(i).ChildCount; j++)
            //    {
            //        Console.WriteLine("Child " + i + "." + j + " : " + context.GetChild(i).GetChild(j).GetText());
            //    }
            //    Console.WriteLine("====================");
            //}

            /*
             * string type = "";
             * string value = "";
             * string varId = "";
             *
             * for (int i = 0; i < context.ChildCount; i++)
             * {
             *  IParseTree childTree = context.GetChild(i);
             *  if (childTree is CSharpParser.TypeContext)
             *  {
             *      type = childTree.GetText();
             *  }
             *  else if (childTree is CSharpParser.Local_variable_declaratorContext)
             *  {
             *
             *      varId = childTree.GetChild(0).GetText();
             *      if (!varId.Equals(varName))
             *      {
             *          return;
             *      }
             *      //Console.WriteLine(childTree.Parent.Parent.Parent.Parent.Parent.Parent.GetText());
             *      if (childTree.ChildCount >= 3)
             *      {
             *          value = childTree.GetChild(2).GetText();
             *          int child = childTree.GetChild(2).ChildCount;
             *          Console.WriteLine(child);
             *          Console.WriteLine(childTree.GetChild(2).GetType().ToString());
             *          Console.WriteLine("type: " + type + " | varId: " + varId + " | value: " + value);
             *      }
             *  }
             *
             * }
             */


            // Console.WriteLine(context.GetText());
        }
Ejemplo n.º 3
0
 public override void EnterLocal_variable_declaration([NotNull] CSharpParser.Local_variable_declarationContext context)
 {
     if (queryVar == null)
     {
         return;
     }
     foreach (var item in queryVar)
     {
         if (context.GetChild(1).GetChild(0).GetText().Equals(item))
         {
             if (!listExpressLine.Contains(context.Start.Line))
             {
                 listExpressLine.Add(context.Start.Line);
             }
         }
     }
 }
Ejemplo n.º 4
0
        public override void EnterLocal_variable_declaration([NotNull] CSharpParser.Local_variable_declarationContext context)
        {
            string type  = "";
            string value = "";
            string varId = "";

            for (int i = 0; i < context.ChildCount; i++)
            {
                IParseTree childTree = context.GetChild(i);
                if (childTree is CSharpParser.TypeContext)
                {
                    type = childTree.GetText();
                }
                else if (childTree is CSharpParser.Local_variable_declaratorContext)
                {
                    varId = childTree.GetChild(0).GetText();
                    //Console.WriteLine(childTree.Parent.Parent.Parent.Parent.Parent.Parent.GetText());
                    if (childTree.ChildCount >= 3)
                    {
                        value = childTree.GetChild(2).GetText();
                    }
                }
            }
            VariableDefine newVar = new VariableDefine(type, varId, value);

            if (!isString(newVar.value))
            {
                if (listLocalVariable != null)
                {
                    if (!listLocalVariable.Any(x => x.name == varId))
                    {
                        listLocalVariable.Add(newVar);
                    }
                }
                else
                {
                    listLocalVariable = new List <VariableDefine>();
                    listLocalVariable.Add(newVar);
                }
            }
        }
Ejemplo n.º 5
0
        public override void EnterLocal_variable_declaration([NotNull] CSharpParser.Local_variable_declarationContext context)
        {
            //if (listVal == null)
            //{
            //    listVal = new List<string>();
            //}
            //string varId = "";
            //for (int i = 0; i < context.ChildCount; i++)
            //{
            //    IParseTree childTree = context.GetChild(i);
            //    if (childTree is CSharpParser.Local_variable_declaratorContext)
            //    {
            //        varId = childTree.GetChild(0).GetText();

            //        if (varId.Equals(varName))
            //        {
            //            listVal.Add(childTree.GetChild(2).GetText());
            //            isContain = true;
            //            break;
            //        }
            //    }
            //}
        }
        private void TreatForStatement(CSharpParser.ForStatementContext statement, Node parentNode)
        {
            // Creating node indicating it's a for statement:
            IToken forToken = statement.FOR().Symbol;
            Node   forNode  = new Node(forToken, Node.Kind.ForStatement, null);

            ast.AddNode(forNode);
            int forIndex = ast.NodeIndex(forNode);

            parentNode.AddChildIndex(forIndex);

            symbolTable.EnterScope(forIndex);

            // Craeting sub tree for the initializer expression:
            CSharpParser.For_initializerContext initContext = statement.for_initializer();
            if (initContext != null)
            {
                CSharpParser.Local_variable_declarationContext declContext = initContext.local_variable_declaration();
                if (declContext != null)
                {
                    // Getting the local variables modifiers:
                    Symbol.ModifierFlag modFlags = Symbol.ModifierFlag.None;
                    if (declContext.REF() != null)
                    {
                        modFlags |= Symbol.ModifierFlag.Ref;
                    }
                    if (declContext.READONLY() != null)
                    {
                        modFlags |= Symbol.ModifierFlag.ReadOnly;
                    }

                    // Creating the local variables symbols:
                    IToken typeToken = declContext.local_variable_type().Start;
                    Type   t         = symbolTable.FindType(typeToken);
                    if (t == null)
                    {
                        Console.WriteLine("FATAL ERROR: Unidentified type found.");
                        Environment.Exit(1);
                    }
                    MethodSymbol ownerMethod = (MethodSymbol)symbolTable.FindSymbol(parentNode.Token, ast);
                    CSharpParser.Local_variable_declaratorContext[] declarators =
                        declContext.local_variable_declarator();
                    foreach (CSharpParser.Local_variable_declaratorContext declarator in declarators)
                    {
                        // Creating the variable symbol:
                        CSharpParser.IdentifierContext identifier = declarator.identifier();
                        IToken         variableID     = identifier.Start;
                        VariableSymbol variableSymbol = new VariableSymbol(modFlags, ownerMethod);
                        symbolTable.AddSymbol(variableID, variableSymbol);

                        // Creating the variable node and adding it to the AST:
                        Node variableNode = new Node(variableID, Node.Kind.MethodVariableDeclaration, t);
                        ast.AddNode(variableNode);
                        int varDeclIndex = ast.NodeIndex(variableNode);
                        parentNode.AddChildIndex(varDeclIndex);

                        // Treating the variable initialization:
                        TreatVariableInitializer(declarator.local_variable_initializer(), variableNode);
                    }
                }
                else
                {
                    CSharpParser.ExpressionContext[] initExpressions = initContext.expression();
                    foreach (CSharpParser.ExpressionContext expression in initExpressions)
                    {
                        beginTreatExpression(expression, forNode);
                    }
                }
            }

            // Creating sub tree for the condition expression:
            beginTreatExpression(statement.expression(), forNode);

            // Creating sub tree for the iterator:
            CSharpParser.For_iteratorContext iteratorContext = statement.for_iterator();
            if (iteratorContext != null)
            {
                CSharpParser.ExpressionContext[] expressions = iteratorContext.expression();
                foreach (CSharpParser.ExpressionContext expression in expressions)
                {
                    beginTreatExpression(expression, forNode);
                }
            }

            // Craeting sub tree and scope for the list of statements:
            CSharpParser.Embedded_statementContext        embedded        = statement.embedded_statement();
            CSharpParser.Simple_embedded_statementContext simpleStatement = embedded.simple_embedded_statement();
            if (simpleStatement != null)
            {
                TreatSimpleEmbeddedStatement(simpleStatement, forNode);
            }
            else
            {
                TreatBlock(embedded.block(), forNode);
            }

            symbolTable.ExitScope();
        }
        private void TreatStatement(CSharpParser.StatementContext statement, Node parentNode)
        {
            CSharpParser.Labeled_StatementContext labeledStatement = statement.labeled_Statement();
            if (labeledStatement != null)
            {
                // Getting the label token:
                CSharpParser.IdentifierContext label = labeledStatement.identifier();
                IToken labelToken = label.Start;

                // Adding the label to the symbol table:
                Symbol labelSymbol = new Symbol(Symbol.ModifierFlag.None);
                symbolTable.AddSymbol(labelToken, labelSymbol);

                // Creating the label node and adding it to the AST:
                Node labelNode = new Node(labelToken, Node.Kind.Label, null);
                ast.AddNode(labelNode);
                int labelNodeIndex = ast.NodeIndex(labelNode);

                parentNode.AddChildIndex(labelNodeIndex);

                CSharpParser.StatementContext innerStatement = labeledStatement.statement();

                // Treating the labeled statement:
                TreatStatement(innerStatement, labelNode);
            }
            else
            {
                CSharpParser.DeclarationStatementContext declaration = statement.declarationStatement();
                if (declaration != null)
                {
                    CSharpParser.Local_variable_declarationContext localVariable = declaration.local_variable_declaration();
                    if (localVariable != null)
                    {
                        // Getting the local variables modifiers:
                        Symbol.ModifierFlag modFlags = Symbol.ModifierFlag.None;
                        if (localVariable.REF() != null)
                        {
                            modFlags |= Symbol.ModifierFlag.Ref;
                        }
                        if (localVariable.READONLY() != null)
                        {
                            modFlags |= Symbol.ModifierFlag.ReadOnly;
                        }

                        // Creating the local variables symbols:
                        IToken typeToken = localVariable.local_variable_type().Start;
                        Type   t         = symbolTable.FindType(typeToken);
                        if (t == null)
                        {
                            Console.WriteLine("FATAL ERROR: Unidentified type found.");
                            Environment.Exit(1);
                        }
                        MethodSymbol ownerMethod = (MethodSymbol)symbolTable.FindSymbol(parentNode.Token, ast);
                        CSharpParser.Local_variable_declaratorContext[] declarators =
                            localVariable.local_variable_declarator();
                        foreach (CSharpParser.Local_variable_declaratorContext declarator in declarators)
                        {
                            // Creating the variable symbol:
                            CSharpParser.IdentifierContext identifier = declarator.identifier();
                            IToken         variableID     = identifier.Start;
                            VariableSymbol variableSymbol = new VariableSymbol(modFlags, ownerMethod);
                            symbolTable.AddSymbol(variableID, variableSymbol);

                            // Creating the variable node and adding it to the AST:
                            Node variableNode = new Node(variableID, Node.Kind.MethodVariableDeclaration, t);
                            ast.AddNode(variableNode);
                            int varDeclIndex = ast.NodeIndex(variableNode);
                            parentNode.AddChildIndex(varDeclIndex);

                            // Treating the variable initialization:
                            TreatVariableInitializer(declarator.local_variable_initializer(), variableNode);
                        }
                    }
                }
                else
                {
                    CSharpParser.Embedded_statementContext        embedded        = statement.embedded_statement();
                    CSharpParser.Simple_embedded_statementContext simpleStatement = embedded.simple_embedded_statement();
                    if (simpleStatement != null)
                    {
                        TreatSimpleEmbeddedStatement(simpleStatement, parentNode);
                    }
                    else
                    {
                        TreatBlock(embedded.block(), parentNode);
                    }
                }
            }
        }