Пример #1
0
            public override Tuple <IParseTree, int> VisitLexerAlt([NotNull] AbstractGrammarParser.LexerAltContext context)
            {
                Debug.Assert(context.lexerCommands() != null && ParseTrees.IsAncestorOf(context.lexerCommands(), targetElement));
                if (context.lexerElements() == null)
                {
                    return(null);
                }

                return(Tuple.Create <IParseTree, int>(context.lexerElements(), 0));
            }
Пример #2
0
        protected virtual int?GetIndent(KeyValuePair <RuleContext, CaretReachedException> parseTree, IParseTree firstNodeOnLine, SnapshotPoint lineStart)
        {
            for (IParseTree ancestor = firstNodeOnLine; ancestor != null; ancestor = ancestor.Parent)
            {
                AlignmentRequirements requirements = GetAlignmentRequirement(parseTree, firstNodeOnLine, ancestor);
                if ((requirements & AlignmentRequirements.UseAncestor) != 0)
                {
                    continue;
                }

                if ((requirements & AlignmentRequirements.IgnoreTree) != 0)
                {
                    return(null);
                }

                IList <IParseTree> siblings = null;
                if ((requirements & AlignmentRequirements.PriorSibling) != 0)
                {
                    int childCount = ancestor.ChildCount;
                    siblings = new List <IParseTree>(childCount);
                    for (int i = 0; i < childCount; i++)
                    {
                        IParseTree child = ancestor.GetChild(i);
                        siblings.Add(child);
                        if (ParseTrees.IsAncestorOf(child, firstNodeOnLine))
                        {
                            break;
                        }
                    }
                }

                Tuple <IParseTree, int> alignmentElement = GetAlignmentElement(parseTree, firstNodeOnLine, ancestor, siblings);
                if (alignmentElement == null)
                {
                    continue;
                }

                IToken startToken          = ParseTrees.GetStartSymbol(alignmentElement.Item1);
                string beginningOfLineText = startToken.TokenSource.InputStream.GetText(new Interval(startToken.StartIndex - startToken.Column, startToken.StartIndex - 1));
                int    elementIndent       = 0;
                for (int i = 0; i < beginningOfLineText.Length; i++)
                {
                    if (beginningOfLineText[i] == '\t')
                    {
                        elementIndent += TabSize;
                        elementIndent -= (elementIndent % TabSize);
                    }
                    else
                    {
                        elementIndent++;
                    }
                }

                var diagnosticsPane = DiagnosticsPane;
                if (diagnosticsPane != null)
                {
                    if (ParseTrees.GetStartSymbol(firstNodeOnLine) == startToken)
                    {
                        diagnosticsPane.WriteLine("Attempting to indent a line relative to an element on that line.");
                    }

                    diagnosticsPane.WriteLine(string.Format("Indent {0} relative to {1} (offset {2}) => {3}", firstNodeOnLine, alignmentElement.Item1, alignmentElement.Item2, elementIndent + alignmentElement.Item2));
                }

                return(elementIndent + alignmentElement.Item2);
            }

            return(null);
        }
Пример #3
0
        protected override AlignmentRequirements GetAlignmentRequirement(KeyValuePair <RuleContext, CaretReachedException> parseTree, IParseTree targetElement, IParseTree ancestor)
        {
            if (ancestor == targetElement && !(targetElement is ITerminalNode))
            {
                // special handling for predicted tokens that don't actually exist yet
                CaretReachedException ex = parseTree.Value;
                if (ex != null && ex.Transitions != null)
                {
                    bool validTransition = false;
                    bool selfTransition  = false;

                    // examine transitions for predictions that don't actually exist yet
                    foreach (KeyValuePair <ATNConfig, IList <Transition> > entry in ex.Transitions)
                    {
                        if (entry.Value == null)
                        {
                            continue;
                        }

                        foreach (Transition transition in entry.Value)
                        {
                            IntervalSet label = transition.Label;
                            if (label == null)
                            {
                                continue;
                            }

                            bool containsInvalid =
                                label.Contains(GrammarParser.OR) ||
                                label.Contains(GrammarParser.RPAREN) ||
                                label.Contains(GrammarParser.RBRACE) ||
                                label.Contains(GrammarParser.END_ACTION) ||
                                label.Contains(GrammarParser.END_ARG_ACTION) ||
                                label.Contains(GrammarParser.OPTIONS) ||
                                label.Contains(GrammarParser.AT) ||
                                label.Contains(GrammarParser.ASSIGN) ||
                                label.Contains(GrammarParser.SEMI) ||
                                label.Contains(GrammarParser.COMMA) ||
                                label.Contains(GrammarParser.MODE) ||
                                label.Contains(GrammarParser.RARROW) ||
                                label.Contains(GrammarParser.POUND);
                            bool containsInvalidSelf =
                                label.Contains(GrammarParser.LPAREN) ||
                                label.Contains(GrammarParser.BEGIN_ACTION) ||
                                label.Contains(GrammarParser.BEGIN_ARG_ACTION);

                            if (transition is NotSetTransition)
                            {
                                containsInvalid     = !containsInvalid;
                                containsInvalidSelf = !containsInvalidSelf;
                            }

                            validTransition |= !containsInvalid;
                            selfTransition  |= !containsInvalidSelf;
                        }
                    }

                    if (!validTransition)
                    {
                        return(AlignmentRequirements.IgnoreTree);
                    }
                    else if (!selfTransition)
                    {
                        return(AlignmentRequirements.UseAncestor);
                    }
                }
            }

            IRuleNode ruleNode = ancestor as IRuleNode;

            if (ruleNode == null)
            {
                return(AlignmentRequirements.UseAncestor);
            }

            RuleContext ruleContext = ruleNode.RuleContext;

            switch (ruleContext.RuleIndex)
            {
            case GrammarParser.RULE_parserRuleSpec:
                if (((GrammarParser.ParserRuleSpecContext)ruleContext).RULE_REF() == null)
                {
                    return(AlignmentRequirements.UseAncestor);
                }

                return(AlignmentRequirements.PriorSibling);

            case GrammarParser.RULE_lexerRule:
                if (((GrammarParser.LexerRuleContext)ruleContext).TOKEN_REF() == null)
                {
                    return(AlignmentRequirements.UseAncestor);
                }

                return(AlignmentRequirements.PriorSibling);

            case GrammarParser.RULE_ruleAltList:
            case GrammarParser.RULE_lexerAltList:
            case GrammarParser.RULE_altList:
            case GrammarParser.RULE_blockSet:
            case GrammarParser.RULE_lexerBlock:
            case GrammarParser.RULE_block:
            case GrammarParser.RULE_optionsSpec:
            case GrammarParser.RULE_tokensSpec:
            case GrammarParser.RULE_channelsSpec:
            case GrammarParser.RULE_modeSpec:
            case GrammarParser.RULE_delegateGrammars:
            case GrammarParser.RULE_actionBlock:
            case GrammarParser.RULE_elements:
            case GrammarParser.RULE_lexerElements:
            case GrammarParser.RULE_rules:
                //case GrammarParser.RULE_lexerCommands:
                return(AlignmentRequirements.PriorSibling);

            case GrammarParser.RULE_lexerAlt:
                GrammarParser.LexerAltContext lexerAltContext = ParseTrees.GetTypedRuleContext <GrammarParser.LexerAltContext>(ancestor);
                if (lexerAltContext != null && lexerAltContext.lexerCommands() != null && ParseTrees.IsAncestorOf(lexerAltContext.lexerCommands(), targetElement))
                {
                    return(AlignmentRequirements.PriorSibling);
                }
                else
                {
                    return(AlignmentRequirements.UseAncestor);
                }

            case GrammarParser.RULE_labeledAlt:
                if (ParseTrees.GetTerminalNodeType(targetElement) == GrammarParser.POUND)
                {
                    return(AlignmentRequirements.PriorSibling);
                }
                else
                {
                    return(AlignmentRequirements.UseAncestor);
                }

            case GrammarParser.RULE_delegateGrammar:
                return(AlignmentRequirements.None);

            default:
                return(AlignmentRequirements.UseAncestor);
            }
        }