Ejemplo n.º 1
0
        private void AddMethod(string methodName, string returnType, MethodType type, ModifiersEnum modifiers, ParserRuleContext context, FormalParametersContext parameterContext)
        {
            List <Variable> parameters = new List <Variable>();

            FormalParameterListContext parameterListContext = parameterContext.formalParameterList();

            if (parameterListContext != null)
            {
                foreach (var parameter in parameterListContext.formalParameter())
                {
                    parameters.Add(new Variable(parameter.variableDeclaratorId().GetText(), parameter.typeType().GetText(), parameter.variableDeclaratorId()));
                }
            }

            Method method = new Method(Class, Class.PopMethodId(), methodName, modifiers, returnType,
                                       parameters, context);


            StatementListener listener = new StatementListener(method);

            foreach (var methodRule in context.GetRuleContexts <ParserRuleContext>())
            {
                methodRule.EnterRule(listener);
            }

            method.Statements = listener.GetResult();

            Class.Methods.Add(method.Name, method);
        }
Ejemplo n.º 2
0
        public virtual void EnterEveryRule(ParserRuleContext ctx)
        {
            // Find sibling lists that are children of this parent node
            ISet <Type> completed = new HashSet <Type>(); // only count sibling list for each subtree type once

            for (int i = 0; i < ctx.ChildCount; i++)
            {
                ParseTree child = ctx.GetChild(i);

                if (completed.Contains(child.GetType()))
                {
                    continue; // avoid counting repeatedly
                }
                completed.Add(child.GetType());
                if (child is TerminalNode)
                {
                    continue; // tokens are separators at most not siblings
                }

                // found subtree child
                //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
                //ORIGINAL LINE: java.util.List<? extends org.antlr.v4.runtime.ParserRuleContext> siblings = ctx.getRuleContexts(((org.antlr.v4.runtime.ParserRuleContext) child).getClass());

                var s = ctx.GetRuleContexts <ParserRuleContext>();
                IList <ParserRuleContext> siblings = s.Where((n) => n.GetType() == child.GetType()).ToList();

                //IList<ParserRuleContext> siblings = ctx.GetRuleContexts(((ParserRuleContext) child).GetType());
                if (siblings.Count > 1)
                { // we found a list
                  // check for separator by looking between first two siblings (assume all are same)
                    ParserRuleContext first    = siblings[0];
                    ParserRuleContext second   = siblings[1];
                    IList <ITree>     children = Trees.GetChildren(ctx);

                    int firstIndex  = children.IndexOf(first);
                    int secondIndex = children.IndexOf(second);

                    if (firstIndex + 1 == secondIndex)
                    {
                        continue; // nothing between first and second so no separator
                    }

                    ParseTree between = ctx.GetChild(firstIndex + 1);
                    if (between is TerminalNode)
                    { // is it a token?
                        Token separator = ((TerminalNode)between).Symbol;
                        visitNonSingletonWithSeparator(ctx, siblings, separator);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public static TChild FindDescendantOfType <TChild>(this ParserRuleContext ctx) where TChild : ParserRuleContext
        {
            if (ctx.ChildCount == 0)
            {
                return(null);
            }


            foreach (var child in ctx.GetRuleContexts <ParserRuleContext>())
            {
                if (child is TChild childOfType)
                {
                    return(childOfType);
                }

                return(child.FindDescendantOfType <TChild>());
            }

            return(null);
        }
Ejemplo n.º 4
0
 static public IEnumerable <ParserRuleContext> GetRules(this ParserRuleContext context)
 {
     return(context.GetRuleContexts <ParserRuleContext>());
 }