Пример #1
0
        /// <summary>
        /// Checks whether the ATNState is compatiple with the given stack.
        /// </summary>
        /// <param name="state"></param>
        /// <param name="parserStack"></param>
        /// <returns></returns>
        public static bool IsCompatibleWith(ATNState state, ParserStack parserStack)
        {
            var res = parserStack.Process(state);

            if (!res.Item1)
            {
                return(false);
            }
            if (state.epsilonOnlyTransitions)
            {
                return(state.Transitions.Any(it => IsCompatibleWith(it.target, res.Item2)));
            }
            else
            {
                return(true);
            }
        }
Пример #2
0
        private void AddSymbol(ICollection <Suggestion> collector, ParserStack parserStack, int currentTokenType, IToken token)
        {
            switch (currentTokenType)
            {
            case CQLLexer.ID:
                var ruleId       = parserStack.Top.ruleIndex;
                var allVariables = new List <IVariable <object> >();
                var currentScope = context;
                while (currentScope != null)
                {
                    allVariables.AddRange(currentScope);
                    currentScope = currentScope.Parent;
                }
                var nameables = allVariables.Where(v => v.Name.ToUpper().StartsWith(token.Text.ToUpper()))
                                .Where(lookupPredicateByRuleId[ruleId])
                                .OrderBy(n => n.Name)
                                .ToArray();
                var type   = lookupSuggestionByRuleId[ruleId];
                var length = token.Type < 0 ? 0 : token.Text.Length;
                if (type == SuggestionType.Function)
                {
                    foreach (var nameable in nameables.OfType <IVariable <object> >())
                    {
                        collector.Add(new Suggestion(type, token.Column, length, nameable.Name + "(???)", ""));
                    }
                }
                else
                {
                    foreach (var nameable in nameables)
                    {
                        collector.Add(new Suggestion(type, token.Column, length, nameable.Name, ""));
                    }
                }
                break;

            default:
                if (suggestionsByTokenType.ContainsKey(currentTokenType))
                {
                    foreach (var suggestion in suggestionsByTokenType[currentTokenType])
                    {
                        collector.Add(new Suggestion(SuggestionType.Token, token.Column, 0, suggestion.Name, suggestion.Usage));
                    }
                }
                break;
            }
        }
Пример #3
0
        private void Process(ATNState state, MyTokenStream tokens, ICollection <Suggestion> collector, ParserStack parserStack, HashSet <int> alreadyPassed)
        {
            var atCaret  = tokens.AtCaret();
            var stackRes = parserStack.Process(state);

            if (!stackRes.Item1)
            {
                return;
            }

            foreach (var it in state.Transitions)
            {
                if (it.IsEpsilon)
                {
                    var stateNo = it.target.StateNumber;
                    if (!alreadyPassed.Contains(stateNo))
                    {
                        Process(it.target, tokens, collector, stackRes.Item2, alreadyPassed);
                        alreadyPassed.Add(stateNo);
                    }
                }
                else if (it is AtomTransition)
                {
                    var atomTransition = it as AtomTransition;
                    var nextToken      = tokens.Next();
                    if (atCaret)
                    {
                        if (ParserStack.IsCompatibleWith(it.target, parserStack))
                        {
                            AddSymbol(collector, parserStack, atomTransition.label, nextToken);
                        }
                    }
                    else
                    {
                        if (nextToken.Type == atomTransition.label)
                        {
                            Process(it.target, tokens.Move(), collector, stackRes.Item2, new HashSet <int>());
                        }
                    }
                }
                else if (it is SetTransition)
                {
                    var setTransition = it as SetTransition;
                    var nextToken     = tokens.Next();
                    foreach (var sym in setTransition.Label.ToList())
                    {
                        if (atCaret)
                        {
                            if (ParserStack.IsCompatibleWith(it.target, parserStack))
                            {
                                AddSymbol(collector, parserStack, sym, nextToken);
                            }
                        }
                        else
                        {
                            if (nextToken.Type == sym)
                            {
                                Process(it.target, tokens.Move(), collector, stackRes.Item2, new HashSet <int>());
                            }
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException(it.GetType().FullName);
                }
            }
        }