Пример #1
0
            public override Tuple <IParseTree, int> VisitActionBlock([NotNull] AbstractGrammarParser.ActionBlockContext context)
            {
                if (context.ChildCount == 0 || targetElement == context.GetChild(0))
                {
                    return(null);
                }

                if (ParseTrees.GetTerminalNodeType(targetElement) == GrammarParser.RBRACE)
                {
                    return(Tuple.Create <IParseTree, int>(context, 0));
                }

                // align to the previous element
                for (int i = priorSiblings.Count - 2; i >= 0; i--)
                {
                    IParseTree sibling = priorSiblings[i];
                    // stop at the first id rule, index 0 is the BEGIN_ACTION terminal itself
                    if (i == 1 || ParseTrees.ElementStartsLine(sibling))
                    {
                        return(Tuple.Create(sibling, 0));
                    }
                }

                return(Tuple.Create <IParseTree, int>(context, _indentSize));
            }
Пример #2
0
            private Tuple <IParseTree, int> VisitElements()
            {
                // the non-terminals under these rules are straightforward
                int firstElementIndex = -1;

                for (int i = 0; i < priorSiblings.Count; i++)
                {
                    IParseTree sibling = priorSiblings[i];
                    if (sibling is IRuleNode)
                    {
                        firstElementIndex = i;
                        break;
                    }
                }

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

                    if (i == firstElementIndex || ParseTrees.ElementStartsLine(sibling))
                    {
                        return(Tuple.Create(sibling, 0));
                    }
                }

                // handle at the parent
                return(null);
            }
Пример #3
0
            public override Tuple <IParseTree, int> VisitRules([NotNull] AbstractGrammarParser.RulesContext context)
            {
                for (int i = priorSiblings.Count - 2; i >= 0; i--)
                {
                    IParseTree sibling = priorSiblings[i];
                    if (i == 0 || ParseTrees.ElementStartsLine(sibling))
                    {
                        return(Tuple.Create(sibling, 0));
                    }
                }

                return(null);
            }
Пример #4
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));
            }
Пример #5
0
            private Tuple <IParseTree, int> VisitGenericBlock(ParserRuleContext container)
            {
                if (targetElement == ParseTrees.GetStartNode(container))
                {
                    return(null);
                }

                if (ParseTrees.GetTerminalNodeType(targetElement) == GrammarParser.RPAREN)
                {
                    return(Tuple.Create <IParseTree, int>(container, 0));
                }

                // OR lines up with previous OR
                bool orNode = ParseTrees.GetTerminalNodeType(targetElement) == GrammarParser.OR;

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

                        if (i == 0 || ParseTrees.ElementStartsLine(sibling))
                        {
                            return(Tuple.Create <IParseTree, int>(sibling, 0));
                        }
                    }

                    if (ParseTrees.GetTerminalNodeType(ParseTrees.GetStartNode(container)) != GrammarParser.LPAREN)
                    {
                        // handle at the parent so it aligns at the (
                        return(null);
                    }

                    return(Tuple.Create <IParseTree, int>(container, 0));
                }

                // the non-terminals under these rules are straightforward
                int firstRuleIndex = -1;

                for (int i = 0; i < priorSiblings.Count; i++)
                {
                    IParseTree sibling = priorSiblings[i];
                    if (sibling is IRuleNode)
                    {
                        firstRuleIndex = i;
                        break;
                    }
                }

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

                    if (i == firstRuleIndex || ParseTrees.ElementStartsLine(sibling))
                    {
                        return(Tuple.Create <IParseTree, int>(sibling, 0));
                    }
                }

                // handle at the parent
                return(null);
            }