예제 #1
0
        private static object ParseNumber(
            IRuleNode number,
            int factor)
        {
            var tokenType = GetSingleChildTokenType(number);
            if (tokenType == EsperEPL2GrammarLexer.IntegerLiteral)
            {
                return ParseIntLongByte(number.GetText(), factor);
            }

            if (tokenType == EsperEPL2GrammarLexer.FloatingPointLiteral)
            {
                var numberText = number.GetText();
                if (numberText.EndsWith("f") || numberText.EndsWith("F")) {
                    numberText = numberText.Substring(0, numberText.Length - 1);
                    return Single.Parse(numberText) * factor;
                } else if (numberText.EndsWith("m")) {
                    numberText = numberText.Substring(0, numberText.Length - 1);
                    return Decimal.Parse(numberText) * factor;
                } else if (numberText.EndsWith("d") || numberText.EndsWith("D")) {
                    numberText = numberText.Substring(0, numberText.Length - 1);
                }

                return Double.Parse(numberText) * factor;
            }

            throw ASTWalkException.From("Encountered unrecognized constant", number.GetText());
        }
예제 #2
0
 protected internal virtual void ExitRule(IParseTreeListener listener, IRuleNode r
     )
 {
     ParserRuleContext ctx = (ParserRuleContext)r.RuleContext;
     ctx.ExitRule(listener);
     listener.ExitEveryRule(ctx);
 }
예제 #3
0
        protected override void EnterRule(IParseTreeListener listener, IRuleNode r)
        {
            ParserRuleContext ctx = (ParserRuleContext)r.RuleContext;

            listener.EnterEveryRule(ctx);
            ctx.EnterRule(listener);
        }
예제 #4
0
        public static IToken GetStopSymbol(IParseTree context)
        {
            ITerminalNode node = GetStopNode(context);

            if (node != null)
            {
                return(node.Symbol);
            }

            IRuleNode ruleNode = context as IRuleNode;

            if (ruleNode == null)
            {
                return(null);
            }

            ParserRuleContext ruleContext = ruleNode.RuleContext as ParserRuleContext;

            if (ruleContext == null)
            {
                return(null);
            }

            return(ruleContext.Stop);
        }
예제 #5
0
        private static Object ParseNumber(IRuleNode number, int factor)
        {
            var tokenType = GetSingleChildTokenType(number);

            if (tokenType == EsperEPL2GrammarLexer.IntegerLiteral)
            {
                return(ParseIntLongByte(number.GetText(), factor));
            }

            else if (tokenType == EsperEPL2GrammarLexer.FloatingPointLiteral)
            {
                var numberText = number.GetText();
                if (numberText.EndsWith("m"))
                {
                    return(DecimalValue.ParseString(number.GetText()) * factor);
                }
                else if (numberText.EndsWith("f") || numberText.EndsWith("F"))
                {
                    return(FloatValue.ParseString(number.GetText()) * factor);
                }
                else
                {
                    return(DoubleValue.ParseString(number.GetText()) * factor);
                }
            }
            throw ASTWalkException.From("Encountered unrecognized constant", number.GetText());
        }
예제 #6
0
        public void DeserializationRequired()
        {
            XElement  element = XElement.Parse("<required />");
            IRuleNode rule    = XmlHelper.GetRuleNode(element);

            Assert.That(rule, Is.TypeOf <RequiredRuleNode>());
        }
예제 #7
0
        public override Expression VisitChildren(IRuleNode node)
        {
            writer($"{System.Reflection.MethodBase.GetCurrentMethod().Name} | Childs: {node.ChildCount} |{node.SourceInterval} | {node.GetText()}");
            Expression expression = base.VisitChildren(node);

            return(expression);
        }
예제 #8
0
        protected internal virtual void ExitRule(IParseTreeListener listener, IRuleNode r)
        {
            ParserRuleContext ctx = (ParserRuleContext)r.RuleContext;

            ctx.ExitRule(listener);
            listener.ExitEveryRule(ctx);
        }
예제 #9
0
        public virtual void Walk(IParseTreeListener listener, IParseTree t)
        {
            if (t is IErrorNode)
            {
                listener.VisitErrorNode((IErrorNode)t);
                return;
            }
            else
            {
                if (t is ITerminalNode)
                {
                    listener.VisitTerminal((ITerminalNode)t);
                    return;
                }
            }
            IRuleNode r = (IRuleNode)t;

            EnterRule(listener, r);
            int n = r.ChildCount;

            for (int i = 0; i < n; i++)
            {
                Walk(listener, r.GetChild(i));
            }
            ExitRule(listener, r);
        }
예제 #10
0
파일: Formatter.cs 프로젝트: hbqzq/luafmt
        public static void Format(ICharStream stream, IFormatWriter writer, FormatOptions options)
        {
            var lexer  = new LuaLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new LuaParser(tokens);

            tokens.Fill();

            var comments = tokens.GetTokens().Where(t => t.Channel == LuaLexer.Hidden);
            var spaces   = tokens.GetTokens().Where(t => t.Channel == 2);

            parser.BuildParseTree = true;
            parser.TrimParseTree  = false;

            IRuleNode root = parser.chunk();

            var ctx = new FormatContext(root, comments, spaces, writer, options);

            RuleFormatter.Format(root, ctx);

            ctx.WriteComments(int.MaxValue);

            var allTokens = tokens.GetTokens();

            if (allTokens.Count > 0)
            {
                var lastToken = allTokens[allTokens.Count - 1];
                while (ctx.line <= lastToken.Line)
                {
                    ctx.WriteLineBreak();
                }
            }

            tokens.Release(0);
        }
예제 #11
0
        internal ParseTreeNodeViewModel(IParseTree node)
        {
            this.node = node;
            Name      = "not set";
            TextColor = Brushes.Black;

            if (node is IErrorNode)
            {
                TextColor = Brushes.Red;
            }

            var terminalNode = node as ITerminalNode;

            if (terminalNode != null)
            {
                Name = terminalNode.Symbol.Text;
                return;
            }

            Name = node.GetType().Name;
            IRuleNode ruleNode = (IRuleNode)node;

            for (int i = 0; i < ruleNode.ChildCount; i++)
            {
                Children.Add(new ParseTreeNodeViewModel(ruleNode.GetChild(i)));
            }
        }
예제 #12
0
        public Ust VisitChildren(IRuleNode node)
        {
            Ust result;

            if (node.ChildCount == 0)
            {
                result = null;
            }
            else if (node.ChildCount == 1)
            {
                result = Visit(node.GetChild(0));
            }
            else
            {
                var exprs = new List <Expression>();
                for (int i = 0; i < node.ChildCount; i++)
                {
                    Ust child = Visit(node.GetChild(i));
                    if (child != null)
                    {
                        // Ignore null.
                        if (child is Expression childExpression)
                        {
                            exprs.Add(childExpression);
                        }
                        else
                        {
                            exprs.Add(new WrapperExpression(child));
                        }
                    }
                }
                result = new MultichildExpression(exprs);
            }
            return(result);
        }
예제 #13
0
            public virtual string Walk(IParseTreeListener listener, IParseTree t)
            {
                counter++;
                if (t is IErrorNode)
                {
                    listener.VisitErrorNode((IErrorNode)t);
                    txt = txt + t.ToString();
                    return(txt);
                }
                else
                {
                    if (t is ITerminalNode)
                    {
                        listener.VisitTerminal((ITerminalNode)t);
                        return(txt);
                    }
                }
                IRuleNode r          = (IRuleNode)t;
                string    parentname = r.GetText();

                //Console.WriteLine("##### PARENT " + counter + "####" + parentname + "#####");
                EnterRule(listener, r);
                int n = r.ChildCount;

                for (int i = 0; i < n; i++)
                {
                    Walk(listener, r.GetChild(i));

                    Console.WriteLine("##### CHILD " + i + " of " + parentname + "####" + r.GetChild(i).GetText() + "#####");
                }

                ExitRule(listener, r);
                return(txt);
            }
예제 #14
0
 private object VisitChildrenWithNewScope(IHasScope decl, IRuleNode context)
 {
     using (scope.NewContext(CurrentScope.MakeChildScope()))
     {
         decl.Scope = CurrentScope;
         return(VisitChildren(context));
     }
 }
 /// <summary>
 /// This method is called after visiting each child in
 ///             <see cref="M:Antlr4.Runtime.Tree.AbstractParseTreeVisitor`1.VisitChildren(Antlr4.Runtime.Tree.IRuleNode)"/>
 ///             . This method is first called before the first
 ///             child is visited; at that point
 /// <code>
 /// currentResult
 /// </code>
 ///             will be the initial
 ///             value (in the default implementation, the initial value is returned by a
 ///             call to
 ///             <see cref="P:Antlr4.Runtime.Tree.AbstractParseTreeVisitor`1.DefaultResult"/>
 ///             . This method is not called after the last
 ///             child is visited.
 ///             <p>The default implementation always returns
 /// <code>
 /// true
 /// </code>
 ///             , indicating that
 /// <code>
 /// visitChildren
 /// </code>
 ///             should only return after all children are visited.
 ///             One reason to override this method is to provide a "short circuit"
 ///             evaluation option for situations where the result of visiting a single
 ///             child has the potential to determine the result of the visit operation as
 ///             a whole.</p>
 /// </summary>
 /// <param name="node">The
 ///             <see cref="T:Antlr4.Runtime.Tree.IRuleNode"/>
 ///             whose children are currently being
 ///             visited.
 ///             </param><param name="currentResult">The current aggregate result of the children visited
 ///             to the current point.
 ///             </param>
 /// <returns>
 /// <code>
 /// true
 /// </code>
 ///             to continue visiting children. Otherwise return
 /// <code>
 /// false
 /// </code>
 ///             to stop visiting children and immediately return the
 ///             current aggregate result from
 ///             <see cref="M:Antlr4.Runtime.Tree.AbstractParseTreeVisitor`1.VisitChildren(Antlr4.Runtime.Tree.IRuleNode)"/>
 ///             .
 /// </returns>
 protected override bool ShouldVisitNextChild(IRuleNode node, IFilterExpression currentResult)
 {
     if (currentResult != null)
     {
         return(false);
     }
     return(base.ShouldVisitNextChild(node, currentResult));
 }
예제 #16
0
        public override object VisitChildren(IRuleNode node)
        {
            IncrementIndent();
            WriteConsoleText(node);
            var returnValue = base.VisitChildren(node);

            DecrementIndent();
            return(returnValue);
        }
예제 #17
0
            public override Unit VisitChildren(IRuleNode node)
            {
                _builder.ResetCurrentLexeme(node.SourceInterval.a, node.SourceInterval.a);
                var mark = _builder.Mark();

                base.VisitChildren(node);
                _builder.Done(mark, SpringNodeElement.NODE_TYPE, node.RuleContext);
                return(Unit.Instance);
            }
예제 #18
0
        public TerminalRule(Interval ruleInterval, string text, IRuleNode parent) : base(ruleInterval, null, text)
        {
            IRuleNode tmpParent = parent;

            while (tmpParent.RuleContext.ChildCount < 2)
            {
                _branchRulesTypes.Add(tmpParent.RuleContext.GetType().ToString().ToLower().Replace("context", "").Replace("mysqlparser+", ""));
                tmpParent = tmpParent.Parent;
            }
        }
예제 #19
0
        public override string VisitChildren([NotNull] IRuleNode node)
        {
            string s = string.Empty;

            for (var i = 0; i < node.ChildCount; i++)
            {
                s += Visit(node.GetChild(i));
            }
            return(s);
        }
예제 #20
0
        public void OrChain(IRuleNode orNode)
        {
            if (_lastRule.Count == 0)
            {
                _lastRule.Push(new OrRule(orNode));
            }
            var leftSide = _lastRule.Pop();

            _lastRule.Push(new OrRule(leftSide, orNode));
        }
예제 #21
0
        public void AndChain(IRuleNode andNode)
        {
            if (_lastRule.Count == 0)
            {
                _lastRule.Push(new AndRule(andNode));
            }
            var leftSide = _lastRule.Pop();

            _lastRule.Push(new AndRule(leftSide, andNode));
        }
예제 #22
0
 // use as generic error handler
 protected override bool ShouldVisitNextChild(IRuleNode node, object?currentResult)
 {
     if (node is ParserRuleContext context &&
         context.exception is not null)
     {
         _builderContext.CompilerContext.SyntaxError(context);
         // usually pointless to continue
         return(false);
     }
     return(true);
 }
예제 #23
0
 public virtual IParseTreeVisitorResults VisitChildren(IRuleNode node)
 {
     if (node is ParserRuleContext context)
     {
         foreach (var child in context.children)
         {
             Visit(child);
         }
     }
     return(_contextValues);
 }
예제 #24
0
        public override List <ITerminalNode> VisitChildren([NotNull] IRuleNode node)
        {
            if (string.IsNullOrEmpty(RuleName) == false && ParseTreeUtility.IsMatchedContext(RuleName, node) == true)
            {
                return(new List <ITerminalNode>()
                {
                    new TerminalVisitor().Visit(node).FirstOrDefault()
                });
            }

            return(base.VisitChildren(node));
        }
예제 #25
0
                public override void Walk(IParseTreeListener listener, IParseTree t)
                {
                    IRuleNode ruleNode = t as IRuleNode;

                    if (ruleNode != null)
                    {
                        EnterRule(listener, ruleNode);
                        ExitRule(listener, ruleNode);
                        return;
                    }

                    base.Walk(listener, t);
                }
예제 #26
0
        private injectionParser.SubrutineContext GetSubrutine(IRuleNode node)
        {
            while (node != null)
            {
                if (node is injectionParser.SubrutineContext subrutine)
                {
                    return(subrutine);
                }

                node = node.Parent;
            }

            return(null);
        }
예제 #27
0
        public static T GetTypedRuleContext <T>(IParseTree node)
            where T : ParserRuleContext
        {
            IRuleNode ruleNode = node as IRuleNode;

            if (ruleNode == null)
            {
                return(null);
            }

            RuleContext ruleContext = ruleNode.RuleContext;

            return(ruleContext as T);
        }
예제 #28
0
        public string VisitChildren(IRuleNode node)
        {
            var result = new StringBuilder();

            for (int i = 0; i < node.ChildCount; i++)
            {
                result.Append(Visit(node.GetChild(i)));
                if (i != node.ChildCount - 1)
                {
                    result.Append(Delimeter);
                }
            }
            return(result.ToString());
        }
예제 #29
0
            /// <summary>
            /// Получить родительский блок предложений.
            /// </summary>
            /// <param name="ruleNode">Узел правил.</param>
            /// <returns>Блок предложений.</returns>
            private static IsblParser.StatementBlockContext GetParentStatementBlock(IRuleNode ruleNode)
            {
                var parent = ruleNode.Parent;

                while (parent != null)
                {
                    if (parent is IsblParser.StatementBlockContext)
                    {
                        break;
                    }
                    parent = parent.Parent;
                }
                return(parent as IsblParser.StatementBlockContext);
            }
예제 #30
0
            public override Tuple <IParseTree, int> VisitOptionsSpec([NotNull] AbstractGrammarParser.OptionsSpecContext context)
            {
                // use previous option if any, otherwise use the block.
                // special handling for closing }
                if (targetElement == context.RBRACE())
                {
                    return(Tuple.Create <IParseTree, int>(context, 0));
                }

                int firstOptionIndex = -1;

                for (int i = 0; i < priorSiblings.Count; i++)
                {
                    IRuleNode sibling = priorSiblings[i] as IRuleNode;
                    if (sibling == null)
                    {
                        continue;
                    }

                    if (sibling.RuleContext.RuleIndex == GrammarParser.RULE_option)
                    {
                        firstOptionIndex = i;
                        break;
                    }
                }

                bool semi = ParseTrees.GetTerminalNodeType(targetElement) == GrammarParser.SEMI;

                for (int i = priorSiblings.Count - 2; i >= 0; i--)
                {
                    IRuleNode sibling = priorSiblings[i] as IRuleNode;
                    if (sibling == null)
                    {
                        continue;
                    }

                    RuleContext ruleContext = sibling.RuleContext;
                    if (ruleContext.RuleIndex == GrammarParser.RULE_option)
                    {
                        if (i == firstOptionIndex || ParseTrees.ElementStartsLine(sibling))
                        {
                            return(Tuple.Create <IParseTree, int>(sibling, semi ? _indentSize : 0));
                        }
                    }
                }

                return(Tuple.Create <IParseTree, int>(context, _indentSize));
            }
예제 #31
0
 protected override bool ShouldVisitNextChild([NotNull] IRuleNode node, MurMurVariable currentResult)
 {
     if (@return != null)
     {
         return(false);
     }
     if (currentResult.Halt != null)
     {
         currentStack.Push(currentResult.Halt);
         return(false);
     }
     else
     {
         return(base.ShouldVisitNextChild(node, currentResult));
     }
 }
예제 #32
0
 private static PathSegment GetModIdent(IRuleNode modNode)
 {
     var firstNode = modNode.GetChild(0) as IRuleNode;
     int currentChild = 0;
     if (firstNode != null)
     {
         currentChild = 1;
         ITree attrRoot = modNode.GetChild(0);
         for(int i = 0; i < attrRoot.ChildCount; i++)
         {
             IRuleNode attrNode = (IRuleNode)attrRoot.GetChild(i);
             if(attrNode.GetChild(2).GetText() == "path")
             {
                 string rawString = EscapeRustString(attrNode.GetChild(4).GetText());
                 return new PathSegment(rawString.Substring(1, rawString.Length - 2), true);
             }
         }
     }
     var pubOrMod = (ITerminalNode)modNode.GetChild(currentChild);
     IParseTree identNode = modNode.GetChild(pubOrMod.Symbol.Type == ModuleParser.PUB ? currentChild + 2 : currentChild + 1);
     return new PathSegment(identNode.GetText(), false);
 }