internal SingletonPredictionContext(PredictionContext parent, int returnState)
     : base(CalculateHashCode(parent, returnState))
 {
     System.Diagnostics.Debug.Assert(returnState != EmptyFullStateKey && returnState != EmptyLocalStateKey);
     this.parent = parent;
     this.returnState = returnState;
 }
Ejemplo n.º 2
0
 protected internal ATNConfig(ATNState state, int alt, PredictionContext context)
 {
     System.Diagnostics.Debug.Assert((alt & unchecked((int)(0xFFFFFF))) == alt);
     this.state = state;
     this.altAndOuterContextDepth = alt & unchecked((int)(0x7FFFFFFF));
     this.context = context;
 }
Ejemplo n.º 3
0
 public static Antlr4.Runtime.Atn.ATNConfig Create(ATNState state, int alt, PredictionContext context, Antlr4.Runtime.Atn.SemanticContext semanticContext, LexerActionExecutor lexerActionExecutor)
 {
     if (semanticContext != Antlr4.Runtime.Atn.SemanticContext.None)
     {
         if (lexerActionExecutor != null)
         {
             return new ATNConfig.ActionSemanticContextATNConfig(lexerActionExecutor, semanticContext, state, alt, context, false);
         }
         else
         {
             return new ATNConfig.SemanticContextATNConfig(semanticContext, state, alt, context);
         }
     }
     else
     {
         if (lexerActionExecutor != null)
         {
             return new ATNConfig.ActionATNConfig(lexerActionExecutor, state, alt, context, false);
         }
         else
         {
             return new Antlr4.Runtime.Atn.ATNConfig(state, alt, context);
         }
     }
 }
Ejemplo n.º 4
0
 protected internal ATNConfig(Antlr4.Runtime.Atn.ATNConfig c, ATNState state, PredictionContext
      context)
 {
     this.state = state;
     this.altAndOuterContextDepth = c.altAndOuterContextDepth & unchecked((int)(0x7FFFFFFF
         ));
     this.context = context;
 }
Ejemplo n.º 5
0
 internal ArrayPredictionContext(PredictionContext[] parents, int[] returnStates, int hashCode)
     : base(hashCode)
 {
     System.Diagnostics.Debug.Assert(parents.Length == returnStates.Length);
     System.Diagnostics.Debug.Assert(returnStates.Length > 1 || returnStates[0] != EmptyFullStateKey, "Should be using PredictionContext.EMPTY instead.");
     this.parents = parents;
     this.returnStates = returnStates;
 }
Ejemplo n.º 6
0
		public static PredictionContext Create(PredictionContext parent, int returnState)
		{
			if (returnState == EMPTY_RETURN_STATE && parent == null)
			{
				// someone can pass in the bits of an array ctx that mean $
				return PredictionContext.EMPTY;
			}
			return new SingletonPredictionContext(parent, returnState);
		}
Ejemplo n.º 7
0
 public void Put(PredictionContext a, PredictionContext b, PredictionContext value)
 {
     Dictionary<PredictionContext, PredictionContext> first;
     if (!data.TryGetValue(a, out first))
     {
         first = new Dictionary<PredictionContext, PredictionContext>();
         data[a] = first;
     }
     first[b] = value;
 }
Ejemplo n.º 8
0
 public PredictionContext Get(PredictionContext a, PredictionContext b)
 {
     Dictionary<PredictionContext, PredictionContext> first;
     if (!data.TryGetValue(a, out first))
         return null;
     PredictionContext value;
     if (first.TryGetValue(b, out value))
         return value;
     else
         return null;
 }
Ejemplo n.º 9
0
 /** Add a context to the cache and return it. If the context already exists,
  *  return that one instead and do not add a new context to the cache.
  *  Protect shared cache from unsafe thread access.
  */
 public PredictionContext Add(PredictionContext ctx)
 {
     if (ctx == PredictionContext.EMPTY)
         return PredictionContext.EMPTY;
     PredictionContext existing = cache.Get(ctx);
     if (existing != null)
     {
         return existing;
     }
     cache.Put(ctx, ctx);
     return ctx;
 }
Ejemplo n.º 10
0
        public PredictionContext getCachedContext(PredictionContext context)
        {
            if (sharedContextCache == null) return context;

            lock (sharedContextCache)
            {
                PredictionContext.IdentityHashMap visited =
                    new PredictionContext.IdentityHashMap();
                return PredictionContext.GetCachedContext(context,
                                                          sharedContextCache,
                                                          visited);
            }
        }
Ejemplo n.º 11
0
 public virtual PredictionContext GetAsCached(PredictionContext context)
 {
     if (!enableCache)
     {
         return context;
     }
     PredictionContext result;
     if (!contexts.TryGetValue(context, out result))
     {
         result = context;
         contexts[context] = context;
     }
     return result;
 }
Ejemplo n.º 12
0
        public void Test_a_b()
        {
            PredictionContext r = _contextCache.Join(A(false), B(false));

            Console.WriteLine(ToDotString(r));
            String expecting =
                "digraph G {" + Environment.NewLine +
                "rankdir=LR;" + Environment.NewLine +
                "  s0[shape=record, label=\"<p0>|<p1>\"];" + Environment.NewLine +
                "  s1[label=\"*\"];" + Environment.NewLine +
                "  s0:p0->s1[label=\"1\"];" + Environment.NewLine +
                "  s0:p1->s1[label=\"2\"];" + Environment.NewLine +
                "}" + Environment.NewLine;

            Assert.AreEqual(expecting, ToDotString(r));
        }
Ejemplo n.º 13
0
 public virtual PredictionContext GetChild(PredictionContext context, int invokingState)
 {
     if (!enableCache)
     {
         return context.GetChild(invokingState);
     }
     PredictionContextCache.PredictionContextAndInt operands = new PredictionContextCache.PredictionContextAndInt(context, invokingState);
     PredictionContext result;
     if (!childContexts.TryGetValue(operands, out result))
     {
         result = context.GetChild(invokingState);
         result = GetAsCached(result);
         childContexts[operands] = result;
     }
     return result;
 }
Ejemplo n.º 14
0
        public void VerifyNoPrediction(string userInput)
        {
            var predictionContext = PredictionContext.Create(userInput);
            var actual            = this._service.GetSuggestion(predictionContext, 1, 1, CancellationToken.None);

            Assert.Equal(0, actual.Count);

            actual = this._noFallbackPredictorService.GetSuggestion(predictionContext, 1, 1, CancellationToken.None);
            Assert.Equal(0, actual.Count);

            actual = this._noCommandBasedPredictorService.GetSuggestion(predictionContext, 1, 1, CancellationToken.None);
            Assert.Equal(0, actual.Count);

            actual = this._noPredictorService.GetSuggestion(predictionContext, 1, 1, CancellationToken.None);
            Assert.Null(actual);
        }
Ejemplo n.º 15
0
        public List <PredictiveSuggestion> GetSuggestion(PredictionContext context, CancellationToken cancellationToken)
        {
            if (_delay)
            {
                // The delay is exaggerated to make the test reliable.
                // xUnit must spin up a lot tasks, which makes the test unreliable when the time difference between 'delay' and 'timeout' is small.
                Thread.Sleep(2000);
            }

            // You can get the user input from the AST.
            var userInput = context.InputAst.Extent.Text;

            return(new List <PredictiveSuggestion> {
                new PredictiveSuggestion($"{userInput} TEST-1 from {Name}"),
                new PredictiveSuggestion($"{userInput} TeSt-2 from {Name}"),
            });
        }
Ejemplo n.º 16
0
 public virtual PredictionContext Join(PredictionContext x, PredictionContext y)
 {
     if (!enableCache)
     {
         return PredictionContext.Join(x, y, this);
     }
     PredictionContextCache.IdentityCommutativePredictionContextOperands operands = new PredictionContextCache.IdentityCommutativePredictionContextOperands(x, y);
     PredictionContext result;
     if (joinContexts.TryGetValue(operands, out result))
     {
         return result;
     }
     result = PredictionContext.Join(x, y, this);
     result = GetAsCached(result);
     joinContexts[operands] = result;
     return result;
 }
Ejemplo n.º 17
0
        public void VerifySuggestionOnIncompleteCommand()
        {
            // We need to get the suggestions for more than one. So we create a local version az predictor.
            var localAzPredictor = new AzPredictor(this._service, this._telemetryClient, new Settings()
            {
                SuggestionCount = 7,
            },
                                                   null);

            var userInput = "New-AzResourceGroup -Name 'ResourceGroup01' -Location 'Central US' -WhatIf -";
            var expected  = "New-AzResourceGroup -Name 'ResourceGroup01' -Location 'Central US' -WhatIf -Verbose ***";

            var predictionContext = PredictionContext.Create(userInput);
            var actual            = localAzPredictor.GetSuggestion(predictionContext, CancellationToken.None);

            Assert.Equal(expected, actual.First().SuggestionText);
        }
        public void VerifySuggestionOnIncompleteCommand()
        {
            // We need to get the suggestions for more than one. So we create a local version az predictor.
            using var localAzPredictor = new AzPredictor(_service, _telemetryClient, new Settings()
            {
                SuggestionCount            = 7,
                MaxAllowedCommandDuplicate = 1,
            },
                                                         null);

            var userInput = "New-AzResourceGroup -Name 'ResourceGroup01' -Location 'Central US' -WhatIf -";
            var expected  = "New-AzResourceGroup -Name 'ResourceGroup01' -Location 'Central US' -WhatIf -Tag value1";

            var predictionContext = PredictionContext.Create(userInput);
            var actual            = localAzPredictor.GetSuggestion(MockObjects.PredictionClient, predictionContext, CancellationToken.None);

            Assert.Equal(expected, actual.SuggestionEntries.First().SuggestionText);
        }
Ejemplo n.º 19
0
        public void Test_axdollar_adollar()
        {
            PredictionContext x  = X(false);
            PredictionContext a1 = CreateSingleton(x, 1);
            PredictionContext a2 = A(false);
            PredictionContext r  = _contextCache.Join(a1, a2);

            Console.WriteLine(ToDotString(r));
            String expecting =
                "digraph G {" + Environment.NewLine +
                "rankdir=LR;" + Environment.NewLine +
                "  s0[label=\"0\"];" + Environment.NewLine +
                "  s1[label=\"*\"];" + Environment.NewLine +
                "  s0->s1[label=\"1\"];" + Environment.NewLine +
                "}" + Environment.NewLine;

            Assert.AreEqual(expecting, ToDotString(r));
        }
        [InlineData("Get-AzContext Name")] // a wrong command
        public void GetNoPredictionWithCommandNameParameters(string userInput)
        {
            var predictionContext = PredictionContext.Create(userInput);
            var commandAst        = predictionContext.InputAst.FindAll(p => p is CommandAst, true).LastOrDefault() as CommandAst;
            var commandName       = (commandAst?.CommandElements?.FirstOrDefault() as StringConstantExpressionAst)?.Value;
            var inputParameterSet = new ParameterSet(commandAst, _azContext);
            var rawUserInput      = predictionContext.InputAst.Extent.Text;
            var presentCommands   = new Dictionary <string, int>();
            var result            = this._predictor.GetSuggestion(commandName,
                                                                  inputParameterSet,
                                                                  rawUserInput,
                                                                  presentCommands,
                                                                  1,
                                                                  1,
                                                                  CancellationToken.None);

            Assert.Equal(0, result.Count);
        }
        public void VerifyPredictionForCommand()
        {
            var predictionContext = PredictionContext.Create("Connect-AzAccount");
            var commandAst        = predictionContext.InputAst.FindAll(p => p is CommandAst, true).LastOrDefault() as CommandAst;
            var commandName       = (commandAst?.CommandElements?.FirstOrDefault() as StringConstantExpressionAst)?.Value;
            var inputParameterSet = new ParameterSet(commandAst, _azContext);
            var rawUserInput      = predictionContext.InputAst.Extent.Text;
            var presentCommands   = new Dictionary <string, int>();
            var result            = this._predictor.GetSuggestion(commandName,
                                                                  inputParameterSet,
                                                                  rawUserInput,
                                                                  presentCommands,
                                                                  1,
                                                                  1,
                                                                  CancellationToken.None);

            Assert.Equal("Connect-AzAccount -Identity", result.PredictiveSuggestions.First().SuggestionText);
        }
        public void VerifyPredictionForCommandAndNamedParameters()
        {
            var predictionContext = PredictionContext.Create("GET-AZSTORAGEACCOUNTKEY -NAME");
            var commandAst        = predictionContext.InputAst.FindAll(p => p is CommandAst, true).LastOrDefault() as CommandAst;
            var commandName       = (commandAst?.CommandElements?.FirstOrDefault() as StringConstantExpressionAst)?.Value;
            var inputParameterSet = new ParameterSet(commandAst, _azContext);
            var rawUserInput      = predictionContext.InputAst.Extent.Text;
            var presentCommands   = new Dictionary <string, int>();
            var result            = this._predictor.GetSuggestion(commandName,
                                                                  inputParameterSet,
                                                                  rawUserInput,
                                                                  presentCommands,
                                                                  1,
                                                                  1,
                                                                  CancellationToken.None);

            Assert.Equal("Get-AzStorageAccountKey -Name 'myStorageAccount' -ResourceGroupName 'ContosoGroup02'", result.PredictiveSuggestions.First().SuggestionText);
        }
Ejemplo n.º 23
0
        /// <inhericdoc />
        public SuggestionPackage GetSuggestion(string clientId, PredictionContext context, CancellationToken cancellationToken)
        {
            var localSuggestionSessionId = _suggestionSessionId++;

            if (_settings.SuggestionCount.Value <= 0)
            {
                return(CreateResult(null));
            }

            Exception             exception   = null;
            CommandLineSuggestion suggestions = null;

            try
            {
                var localCancellationToken = Settings.ContinueOnTimeout ? CancellationToken.None : cancellationToken;

                suggestions = _service.GetSuggestion(context, _settings.SuggestionCount.Value, _settings.MaxAllowedCommandDuplicate.Value, localCancellationToken);

                var returnedValue = suggestions?.PredictiveSuggestions?.ToList();
                return(CreateResult(returnedValue));
            }
            catch (Exception e) when(!(e is OperationCanceledException))
            {
                exception = e;
                return(CreateResult(null));
            }
            finally
            {
                _telemetryClient.OnGetSuggestion(new GetSuggestionTelemetryData(clientId, localSuggestionSessionId, context.InputAst,
                                                                                suggestions,
                                                                                cancellationToken.IsCancellationRequested,
                                                                                exception));
            }

            SuggestionPackage CreateResult(List <PredictiveSuggestion> suggestions)
            {
                if ((suggestions == null) || (suggestions.Count == 0))
                {
                    return(default(SuggestionPackage));
                }

                return(new SuggestionPackage(localSuggestionSessionId, suggestions));
            }
        }
Ejemplo n.º 24
0
        public void VerifySuggestionSessionIdChanged()
        {
            var expectedTelemetryCount = 2;

            var(azPredictor, telemetryClient) = CreateTestObjects(throwException: false, expectedTelemetryCount + 1);

            var predictionContext      = PredictionContext.Create("New-AzResourceGroup -Name 'ResourceGroup01' -Location 'Central US' -WhatIf");
            var firstSuggestionPackage = azPredictor.GetSuggestion(AzPredictorTelemetryTests.AzPredictorClient, predictionContext, CancellationToken.None);
            var firstGetSuggestionData = telemetryClient.GetSuggestionData;

            var secondSuggestionPackage = azPredictor.GetSuggestion(AzPredictorTelemetryTests.AzPredictorClient, predictionContext, CancellationToken.None);
            var secondGetSuggestionData = telemetryClient.GetSuggestionData;

            Assert.NotEqual(secondSuggestionPackage.Session, firstSuggestionPackage.Session);
            AzPredictorTelemetryTests.EnsureSameCorrelationId(secondGetSuggestionData, firstGetSuggestionData);
            AzPredictorTelemetryTests.EnsureSameSessionId(secondGetSuggestionData, firstGetSuggestionData);

            VerifyTelemetryRecordCount(expectedTelemetryCount, telemetryClient);
        }
Ejemplo n.º 25
0
        public void Test_Aa_Aa()
        {
            PredictionContext a1 = A(false);
            PredictionContext a2 = A(false);
            PredictionContext A1 = Array(a1);
            PredictionContext A2 = Array(a2);
            PredictionContext r  = _contextCache.Join(A1, A2);

            Console.WriteLine(ToDotString(r));
            String expecting =
                "digraph G {" + Environment.NewLine +
                "rankdir=LR;" + Environment.NewLine +
                "  s0[label=\"0\"];" + Environment.NewLine +
                "  s1[label=\"*\"];" + Environment.NewLine +
                "  s0->s1[label=\"1\"];" + Environment.NewLine +
                "}" + Environment.NewLine;

            Assert.AreEqual(expecting, ToDotString(r));
        }
Ejemplo n.º 26
0
        public void VerifyExceptionInGetSuggestion()
        {
            var expectedTelemetryCount = 1;

            var(azPredictor, telemetryClient) = CreateTestObjects(throwException: true, expectedTelemetryCount + 1);

            var predictionContext = PredictionContext.Create("New-AzResourceGroup -Name 'ResourceGroup01' -Location 'Central US' -WhatIf");
            var suggestionPackage = azPredictor.GetSuggestion(AzPredictorTelemetryTests.AzPredictorClient, predictionContext, CancellationToken.None);

            Assert.IsType <MockTestException>(telemetryClient.GetSuggestionData.Exception);
            Assert.Equal(AzPredictorTelemetryTests.AzPredictorClient, telemetryClient.GetSuggestionData.ClientId);

            VerifyTelemetryRecordCount(expectedTelemetryCount, telemetryClient);

            Assert.EndsWith("GetSuggestion", telemetryClient.RecordedTelemetry[0].EventName);
            Assert.Equal(AzPredictorTelemetryTests.AzPredictorClient, telemetryClient.RecordedTelemetry[0].Properties["ClientId"]);
            Assert.Equal("New-AzResourceGroup -Location *** -Name *** -WhatIf ***", telemetryClient.RecordedTelemetry[0].Properties["UserInput"]);
            Assert.StartsWith($"Type: {typeof(MockTestException)}\nStack Trace: ", telemetryClient.RecordedTelemetry[0].Properties["Exception"]);
        }
Ejemplo n.º 27
0
        /// <summary>
        /// This method implements the single-token insertion inline error recovery
        /// strategy.
        /// </summary>
        /// <remarks>
        /// This method implements the single-token insertion inline error recovery
        /// strategy. It is called by
        /// <see cref="RecoverInline(Parser)"/>
        /// if the single-token
        /// deletion strategy fails to recover from the mismatched input. If this
        /// method returns
        /// <see langword="true"/>
        /// ,
        /// <paramref name="recognizer"/>
        /// will be in error recovery
        /// mode.
        /// <p>This method determines whether or not single-token insertion is viable by
        /// checking if the
        /// <c>LA(1)</c>
        /// input symbol could be successfully matched
        /// if it were instead the
        /// <c>LA(2)</c>
        /// symbol. If this method returns
        /// <see langword="true"/>
        /// , the caller is responsible for creating and inserting a
        /// token with the correct type to produce this behavior.</p>
        /// </remarks>
        /// <param name="recognizer">the parser instance</param>
        /// <returns>
        ///
        /// <see langword="true"/>
        /// if single-token insertion is a viable recovery
        /// strategy for the current mismatched input, otherwise
        /// <see langword="false"/>
        /// </returns>
        protected internal virtual bool SingleTokenInsertion(Parser recognizer)
        {
            int currentSymbolType = ((ITokenStream)recognizer.InputStream).La(1);
            // if current token is consistent with what could come after current
            // ATN state, then we know we're missing a token; error recovery
            // is free to conjure up and insert the missing token
            ATNState    currentState   = recognizer.Interpreter.atn.states[recognizer.State];
            ATNState    next           = currentState.Transition(0).target;
            ATN         atn            = recognizer.Interpreter.atn;
            IntervalSet expectingAtLL2 = atn.NextTokens(next, PredictionContext.FromRuleContext(atn, recognizer.RuleContext));

            //		System.out.println("LT(2) set="+expectingAtLL2.toString(recognizer.getTokenNames()));
            if (expectingAtLL2.Contains(currentSymbolType))
            {
                ReportMissingToken(recognizer);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 28
0
        public void Test_axdollar_adollar_fullctx()
        {
            PredictionContext x  = X(true);
            PredictionContext a1 = CreateSingleton(x, 1);
            PredictionContext a2 = A(true);
            PredictionContext r  = _contextCache.Join(a1, a2);

            Console.WriteLine(ToDotString(r));
            String expecting =
                "digraph G {" + Environment.NewLine +
                "rankdir=LR;" + Environment.NewLine +
                "  s0[label=\"0\"];" + Environment.NewLine +
                "  s1[shape=record, label=\"<p0>|<p1>$\"];" + Environment.NewLine +
                "  s2[label=\"$\"];" + Environment.NewLine +
                "  s0->s1[label=\"1\"];" + Environment.NewLine +
                "  s1:p0->s2[label=\"9\"];" + Environment.NewLine +
                "}" + Environment.NewLine;

            Assert.AreEqual(expecting, ToDotString(r));
        }
Ejemplo n.º 29
0
        public void Test_adollar_bx()
        {
            PredictionContext x2 = X(false);
            PredictionContext a  = A(false);
            PredictionContext b  = CreateSingleton(x2, 2);
            PredictionContext r  = _contextCache.Join(a, b);

            Console.WriteLine(ToDotString(r));
            String expecting =
                "digraph G {" + Environment.NewLine +
                "rankdir=LR;" + Environment.NewLine +
                "  s0[shape=record, label=\"<p0>|<p1>\"];" + Environment.NewLine +
                "  s2[label=\"2\"];" + Environment.NewLine +
                "  s1[label=\"*\"];" + Environment.NewLine +
                "  s0:p0->s1[label=\"1\"];" + Environment.NewLine +
                "  s0:p1->s2[label=\"2\"];" + Environment.NewLine +
                "  s2->s1[label=\"9\"];" + Environment.NewLine +
                "}" + Environment.NewLine;

            Assert.AreEqual(expecting, ToDotString(r));
        }
Ejemplo n.º 30
0
        public void Test_aadollar_adollar_dollar_fullCtx()
        {
            PredictionContext empty  = PredictionContext.EmptyFull;
            PredictionContext child1 = CreateSingleton(empty, 8);
            PredictionContext right  = _contextCache.Join(empty, child1);
            PredictionContext left   = CreateSingleton(right, 8);
            PredictionContext merged = _contextCache.Join(left, right);
            String            actual = ToDotString(merged);

            Console.WriteLine(actual);
            String expecting =
                "digraph G {" + Environment.NewLine +
                "rankdir=LR;" + Environment.NewLine +
                "  s0[shape=record, label=\"<p0>|<p1>$\"];" + Environment.NewLine +
                "  s1[shape=record, label=\"<p0>|<p1>$\"];" + Environment.NewLine +
                "  s2[label=\"$\"];" + Environment.NewLine +
                "  s0:p0->s1[label=\"8\"];" + Environment.NewLine +
                "  s1:p0->s2[label=\"8\"];" + Environment.NewLine +
                "}" + Environment.NewLine;

            Assert.AreEqual(expecting, actual);
        }
Ejemplo n.º 31
0
        public void Test_Aac_Ab()
        { // a,c + b
            PredictionContext a  = A(false);
            PredictionContext b  = B(false);
            PredictionContext c  = C(false);
            PredictionContext A1 = Array(a, c);
            PredictionContext A2 = Array(b);
            PredictionContext r  = _contextCache.Join(A1, A2);

            Console.WriteLine(ToDotString(r));
            String expecting =
                "digraph G {" + Environment.NewLine +
                "rankdir=LR;" + Environment.NewLine +
                "  s0[shape=record, label=\"<p0>|<p1>|<p2>\"];" + Environment.NewLine +
                "  s1[label=\"*\"];" + Environment.NewLine +
                "  s0:p0->s1[label=\"1\"];" + Environment.NewLine +
                "  s0:p1->s1[label=\"2\"];" + Environment.NewLine +
                "  s0:p2->s1[label=\"3\"];" + Environment.NewLine +
                "}" + Environment.NewLine;

            Assert.AreEqual(expecting, ToDotString(r));
        }
Ejemplo n.º 32
0
        public void Test_Aax_Aay()
        { // ax + ay -> merged singleton a, array parent
            PredictionContext a1 = CreateSingleton(X(false), 1);
            PredictionContext a2 = CreateSingleton(Y(false), 1);
            PredictionContext A1 = Array(a1);
            PredictionContext A2 = Array(a2);
            PredictionContext r  = _contextCache.Join(A1, A2);

            Console.WriteLine(ToDotString(r));
            String expecting =
                "digraph G {" + Environment.NewLine +
                "rankdir=LR;" + Environment.NewLine +
                "  s0[label=\"0\"];" + Environment.NewLine +
                "  s1[shape=record, label=\"<p0>|<p1>\"];" + Environment.NewLine +
                "  s2[label=\"*\"];" + Environment.NewLine +
                "  s0->s1[label=\"1\"];" + Environment.NewLine +
                "  s1:p0->s2[label=\"9\"];" + Environment.NewLine +
                "  s1:p1->s2[label=\"10\"];" + Environment.NewLine +
                "}" + Environment.NewLine;

            Assert.AreEqual(expecting, ToDotString(r));
        }
        public void VerifyUsingFallbackPredictor(string userInput)
        {
            var predictionContext = PredictionContext.Create(userInput);
            var commandAst        = predictionContext.RelatedAsts.OfType <CommandAst>().LastOrDefault();
            var commandName       = (commandAst?.CommandElements?.FirstOrDefault() as StringConstantExpressionAst)?.Value;
            var inputParameterSet = new ParameterSet(commandAst, _azContext);
            var rawUserInput      = predictionContext.InputAst.Extent.Text;
            var presentCommands   = new Dictionary <string, int>();
            var expected          = this._fallbackPredictor.GetSuggestion(commandName,
                                                                          inputParameterSet,
                                                                          rawUserInput,
                                                                          presentCommands,
                                                                          1,
                                                                          1,
                                                                          CancellationToken.None);

            var actual = this._service.GetSuggestion(predictionContext, 1, 1, CancellationToken.None);

            Assert.NotNull(actual);
            Assert.True(actual.Count > 0);
            Assert.NotNull(actual.PredictiveSuggestions.First());
            Assert.NotNull(actual.PredictiveSuggestions.First().SuggestionText);
            Assert.Equal(expected.Count, actual.Count);
            Assert.Equal <PredictiveSuggestion>(expected.PredictiveSuggestions, actual.PredictiveSuggestions, new PredictiveSuggestionComparer());
            Assert.Equal <string>(expected.SourceTexts, actual.SourceTexts);
            Assert.All <SuggestionSource>(actual.SuggestionSources, (source) => Assert.Equal(SuggestionSource.StaticCommands, source));

            actual = this._noCommandBasedPredictorService.GetSuggestion(predictionContext, 1, 1, CancellationToken.None);
            Assert.NotNull(actual);
            Assert.True(actual.Count > 0);
            Assert.NotNull(actual.PredictiveSuggestions.First());
            Assert.NotNull(actual.PredictiveSuggestions.First().SuggestionText);
            Assert.Equal(expected.Count, actual.Count);
            Assert.Equal <PredictiveSuggestion>(expected.PredictiveSuggestions, actual.PredictiveSuggestions, new PredictiveSuggestionComparer());
            Assert.Equal <string>(expected.SourceTexts, actual.SourceTexts);
            Assert.All <SuggestionSource>(actual.SuggestionSources, (source) => Assert.Equal(SuggestionSource.StaticCommands, source));
        }
        public void VerifyPredictionForCommandAndTwoPositionalParameters()
        {
            var predictionContext = PredictionContext.Create("Get-AzStorageAccount test test"); // Two positional parameters with the same value.
            var commandAst        = predictionContext.InputAst.FindAll(p => p is CommandAst, true).LastOrDefault() as CommandAst;
            var commandName       = (commandAst?.CommandElements?.FirstOrDefault() as StringConstantExpressionAst)?.Value;
            var inputParameterSet = new ParameterSet(commandAst, _azContext);
            var rawUserInput      = predictionContext.InputAst.Extent.Text;
            var presentCommands   = new Dictionary <string, int>();
            var result            = this._predictor.GetSuggestion(commandName,
                                                                  inputParameterSet,
                                                                  rawUserInput,
                                                                  presentCommands,
                                                                  3,
                                                                  1,
                                                                  CancellationToken.None);

            var expected = new PredictiveSuggestion[]
            {
                new PredictiveSuggestion("Get-AzStorageAccount test test -DefaultProfile {IAzureContextContainer}"),
                new PredictiveSuggestion("Get-AzStorageAccount test test -IncludeGeoReplicationStats"),
            };

            Assert.Equal(expected.Select(e => e.SuggestionText), result.PredictiveSuggestions.Select(r => r.SuggestionText));
        }
Ejemplo n.º 35
0
        public void Test_Aax_Aby()
        { // ax + by but in arrays
            PredictionContext a  = CreateSingleton(X(false), 1);
            PredictionContext b  = CreateSingleton(Y(false), 2);
            PredictionContext A1 = Array(a);
            PredictionContext A2 = Array(b);
            PredictionContext r  = _contextCache.Join(A1, A2);

            Console.WriteLine(ToDotString(r));
            String expecting =
                "digraph G {" + Environment.NewLine +
                "rankdir=LR;" + Environment.NewLine +
                "  s0[shape=record, label=\"<p0>|<p1>\"];" + Environment.NewLine +
                "  s2[label=\"2\"];" + Environment.NewLine +
                "  s3[label=\"*\"];" + Environment.NewLine +
                "  s1[label=\"1\"];" + Environment.NewLine +
                "  s0:p0->s1[label=\"1\"];" + Environment.NewLine +
                "  s0:p1->s2[label=\"2\"];" + Environment.NewLine +
                "  s2->s3[label=\"10\"];" + Environment.NewLine +
                "  s1->s3[label=\"9\"];" + Environment.NewLine +
                "}" + Environment.NewLine;

            Assert.AreEqual(expecting, ToDotString(r));
        }
Ejemplo n.º 36
0
 public PredictionsController(PredictionContext context)
 {
     _context = context;
 }
Ejemplo n.º 37
0
 protected internal override PredictionContext AddEmptyContext()
 {
     PredictionContext[] parents = new PredictionContext[] { parent, EmptyFull };
     int[] returnStates = new int[] { returnState, EmptyFullStateKey };
     return new ArrayPredictionContext(parents, returnStates);
 }
Ejemplo n.º 38
0
 protected internal ActionATNConfig(LexerActionExecutor lexerActionExecutor, ATNConfig c, ATNState state, PredictionContext context, bool passedThroughNonGreedyDecision)
     : base(c, state, context)
 {
     if (c.SemanticContext != SemanticContext.None)
     {
         throw new NotSupportedException();
     }
     this.lexerActionExecutor = lexerActionExecutor;
     this.passedThroughNonGreedyDecision = passedThroughNonGreedyDecision;
 }
Ejemplo n.º 39
0
 public virtual IntervalSet NextTokens(ATNState s, PredictionContext ctx)
 {
     Args.NotNull("ctx", ctx);
     LL1Analyzer anal = new LL1Analyzer(this);
     IntervalSet next = anal.Look(s, ctx);
     return next;
 }
Ejemplo n.º 40
0
 public RandomPredictionController(PredictionContext context)
 {
     _context = context;
 }
Ejemplo n.º 41
0
        public PredictionContext CreateSingleton(PredictionContext parent, int payload)
        {
            PredictionContext a = _contextCache.GetChild(parent, payload);

            return(a);
        }
        public void VerifyParameterValues()
        {
            var predictionContext = PredictionContext.Create("Get-AzContext");
            var commandAst        = predictionContext.InputAst.FindAll(p => p is CommandAst, true).LastOrDefault() as CommandAst;
            var commandName       = (commandAst?.CommandElements?.FirstOrDefault() as StringConstantExpressionAst)?.Value;
            var inputParameterSet = new ParameterSet(commandAst, _azContext);
            var rawUserInput      = predictionContext.InputAst.Extent.Text;
            var presentCommands   = new Dictionary <string, int>();

            Action actual = () => this._predictor.GetSuggestion(null,
                                                                inputParameterSet,
                                                                rawUserInput,
                                                                presentCommands,
                                                                1,
                                                                1,
                                                                CancellationToken.None);

            Assert.Throws <ArgumentException>(actual);

            actual = () => this._predictor.GetSuggestion(commandName,
                                                         null,
                                                         rawUserInput,
                                                         presentCommands,
                                                         1,
                                                         1,
                                                         CancellationToken.None);
            Assert.Throws <ArgumentNullException>(actual);

            actual = () => this._predictor.GetSuggestion(commandName,
                                                         inputParameterSet,
                                                         null,
                                                         presentCommands,
                                                         1,
                                                         1,
                                                         CancellationToken.None);
            Assert.Throws <ArgumentException>(actual);

            actual = () => this._predictor.GetSuggestion(commandName,
                                                         inputParameterSet,
                                                         rawUserInput,
                                                         null,
                                                         1,
                                                         1,
                                                         CancellationToken.None);
            Assert.Throws <ArgumentNullException>(actual);

            actual = () => this._predictor.GetSuggestion(commandName,
                                                         inputParameterSet,
                                                         rawUserInput,
                                                         presentCommands,
                                                         0,
                                                         1,
                                                         CancellationToken.None);
            Assert.Throws <ArgumentOutOfRangeException>(actual);

            actual = () => this._predictor.GetSuggestion(commandName,
                                                         inputParameterSet,
                                                         rawUserInput,
                                                         presentCommands,
                                                         1,
                                                         0,
                                                         CancellationToken.None);
            Assert.Throws <ArgumentOutOfRangeException>(actual);
        }
Ejemplo n.º 43
0
 public virtual IntervalSet Look(ATNState s, PredictionContext ctx)
 {
     return Look(s, s.atn.ruleToStopState[s.ruleIndex], ctx);
 }
Ejemplo n.º 44
0
 public PredictionContext Get(PredictionContext ctx)
 {
     return cache.Get(ctx);
 }
Ejemplo n.º 45
0
 public virtual IntervalSet Look(ATNState s, PredictionContext ctx)
 {
     return Look(s, null, ctx);
 }
Ejemplo n.º 46
0
 public override string[] ToStrings(IRecognizer recognizer, PredictionContext stop, int currentState)
 {
     return new string[] { "[]" };
 }
        public void VerifyFailToParseUserInput(string userInput)
        {
            var predictionContext = PredictionContext.Create(userInput);

            Assert.Throws <CommandLineException>(() => _service.GetSuggestion(predictionContext, 1, 1, CancellationToken.None));
        }
Ejemplo n.º 48
0
 public ActionSemanticContextATNConfig(LexerActionExecutor lexerActionExecutor, SemanticContext semanticContext, ATNConfig c, ATNState state, PredictionContext context, bool passedThroughNonGreedyDecision)
     : base(semanticContext, c, state, context)
 {
     this.lexerActionExecutor = lexerActionExecutor;
     this.passedThroughNonGreedyDecision = passedThroughNonGreedyDecision;
 }
Ejemplo n.º 49
0
        private static String ToDotString(PredictionContext context)
        {
            StringBuilder nodes = new StringBuilder();
            StringBuilder edges = new StringBuilder();
            IDictionary <PredictionContext, PredictionContext> visited = new IdentityHashMap <PredictionContext, PredictionContext>();
            IDictionary <PredictionContext, int> contextIds            = new IdentityHashMap <PredictionContext, int>();
            Stack <PredictionContext>            workList = new Stack <PredictionContext>();

            visited[context]    = context;
            contextIds[context] = contextIds.Count;
            workList.Push(context);
            while (workList.Count > 0)
            {
                PredictionContext current = workList.Pop();
                nodes.Append("  s").Append(contextIds[current]).Append('[');

                if (current.Size > 1)
                {
                    nodes.Append("shape=record, ");
                }

                nodes.Append("label=\"");

                if (current.IsEmpty)
                {
                    nodes.Append(PredictionContext.IsEmptyLocal(current) ? '*' : '$');
                }
                else if (current.Size > 1)
                {
                    for (int i = 0; i < current.Size; i++)
                    {
                        if (i > 0)
                        {
                            nodes.Append('|');
                        }

                        nodes.Append("<p").Append(i).Append('>');
                        if (current.GetReturnState(i) == PredictionContext.EmptyFullStateKey)
                        {
                            nodes.Append('$');
                        }
                        else if (current.GetReturnState(i) == PredictionContext.EmptyLocalStateKey)
                        {
                            nodes.Append('*');
                        }
                    }
                }
                else
                {
                    nodes.Append(contextIds[current]);
                }

                nodes.AppendLine("\"];");

                for (int i = 0; i < current.Size; i++)
                {
                    if (current.GetReturnState(i) == PredictionContext.EmptyFullStateKey ||
                        current.GetReturnState(i) == PredictionContext.EmptyLocalStateKey)
                    {
                        continue;
                    }

                    if (!visited.ContainsKey(current.GetParent(i)))
                    {
                        visited[current.GetParent(i)]    = current.GetParent(i);
                        contextIds[current.GetParent(i)] = contextIds.Count;
                        workList.Push(current.GetParent(i));
                    }

                    edges.Append("  s").Append(contextIds[current]);
                    if (current.Size > 1)
                    {
                        edges.Append(":p").Append(i);
                    }

                    edges.Append("->");
                    edges.Append('s').Append(contextIds[current.GetParent(i)]);
                    edges.Append("[label=\"").Append(current.GetReturnState(i)).Append("\"]");
                    edges.AppendLine(";");
                }
            }

            StringBuilder builder = new StringBuilder();

            builder.AppendLine("digraph G {");
            builder.AppendLine("rankdir=LR;");
            builder.Append(nodes);
            builder.Append(edges);
            builder.AppendLine("}");
            return(builder.ToString());
        }
Ejemplo n.º 50
0
 public override PredictionContext AppendContext(PredictionContext suffix, PredictionContextCache contextCache)
 {
     return AppendContext(this, suffix, new PredictionContext.IdentityHashMap());
 }
Ejemplo n.º 51
0
 public virtual PredictionContext GetCachedContext(PredictionContext context)
 {
     return PredictionContext.GetCachedContext(context, contextCache, new PredictionContext.IdentityHashMap());
 }
Ejemplo n.º 52
0
 public PredictionContextAndInt(PredictionContext obj, int value)
 {
     this.obj = obj;
     this.value = value;
 }
Ejemplo n.º 53
0
 /// <summary>
 /// Compute set of tokens that can follow
 /// <code>s</code>
 /// in the ATN in the
 /// specified
 /// <code>ctx</code>
 /// .
 /// <p/>
 /// If
 /// <code>ctx</code>
 /// is
 /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see>
 /// and
 /// <code>stopState</code>
 /// or the end of the rule containing
 /// <code>s</code>
 /// is reached,
 /// <see cref="TokenConstants.Epsilon"/>
 /// is added to the result set. If
 /// <code>ctx</code>
 /// is not
 /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see>
 /// and
 /// <code>addEOF</code>
 /// is
 /// <code>true</code>
 /// and
 /// <code>stopState</code>
 /// or the end of the outermost rule is reached,
 /// <see cref="TokenConstants.Eof"/>
 /// is added to the result set.
 /// </summary>
 /// <param name="s">the ATN state.</param>
 /// <param name="stopState">
 /// the ATN state to stop at. This can be a
 /// <see cref="BlockEndState">BlockEndState</see>
 /// to detect epsilon paths through a closure.
 /// </param>
 /// <param name="ctx">
 /// The outer context, or
 /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see>
 /// if
 /// the outer context should not be used.
 /// </param>
 /// <param name="look">The result lookahead set.</param>
 /// <param name="lookBusy">
 /// A set used for preventing epsilon closures in the ATN
 /// from causing a stack overflow. Outside code should pass
 /// <code>new HashSet&lt;ATNConfig&gt;</code>
 /// for this argument.
 /// </param>
 /// <param name="calledRuleStack">
 /// A set used for preventing left recursion in the
 /// ATN from causing a stack overflow. Outside code should pass
 /// <code>new BitSet()</code>
 /// for this argument.
 /// </param>
 /// <param name="seeThruPreds">
 /// 
 /// <code>true</code>
 /// to true semantic predicates as
 /// implicitly
 /// <code>true</code>
 /// and "see through them", otherwise
 /// <code>false</code>
 /// to treat semantic predicates as opaque and add
 /// <see cref="HitPred">HitPred</see>
 /// to the
 /// result if one is encountered.
 /// </param>
 /// <param name="addEOF">
 /// Add
 /// <see cref="TokenConstants.Eof"/>
 /// to the result if the end of the
 /// outermost context is reached. This parameter has no effect if
 /// <code>ctx</code>
 /// is
 /// <see cref="PredictionContext.EmptyLocal">PredictionContext.EmptyLocal</see>
 /// .
 /// </param>
 protected internal virtual void Look(ATNState s, ATNState stopState, PredictionContext
      ctx, IntervalSet look, HashSet<ATNConfig> lookBusy, BitSet calledRuleStack, 
     bool seeThruPreds, bool addEOF)
 {
     //		System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx);
     ATNConfig c = ATNConfig.Create(s, 0, ctx);
     if (!lookBusy.Add(c))
     {
         return;
     }
     if (s == stopState)
     {
         if (PredictionContext.IsEmptyLocal(ctx))
         {
             look.Add(TokenConstants.Epsilon);
             return;
         }
         else
         {
             if (ctx.IsEmpty && addEOF)
             {
                 look.Add(TokenConstants.Eof);
                 return;
             }
         }
     }
     if (s is RuleStopState)
     {
         if (PredictionContext.IsEmptyLocal(ctx))
         {
             look.Add(TokenConstants.Epsilon);
             return;
         }
         else
         {
             if (ctx.IsEmpty && addEOF)
             {
                 look.Add(TokenConstants.Eof);
                 return;
             }
         }
         for (int i = 0; i < ctx.Size; i++)
         {
             if (ctx.GetReturnState(i) != PredictionContext.EmptyFullStateKey)
             {
                 ATNState returnState = atn.states[ctx.GetReturnState(i)];
                 //					System.out.println("popping back to "+retState);
                 for (int j = 0; j < ctx.Size; j++)
                 {
                     bool removed = calledRuleStack.Get(returnState.ruleIndex);
                     try
                     {
                         calledRuleStack.Clear(returnState.ruleIndex);
                         Look(returnState, stopState, ctx.GetParent(j), look, lookBusy, calledRuleStack, seeThruPreds
                             , addEOF);
                     }
                     finally
                     {
                         if (removed)
                         {
                             calledRuleStack.Set(returnState.ruleIndex);
                         }
                     }
                 }
                 return;
             }
         }
     }
     int n = s.NumberOfTransitions;
     for (int i_1 = 0; i_1 < n; i_1++)
     {
         Transition t = s.Transition(i_1);
         if (t.GetType() == typeof(RuleTransition))
         {
             if (calledRuleStack.Get(((RuleTransition)t).target.ruleIndex))
             {
                 continue;
             }
             PredictionContext newContext = ctx.GetChild(((RuleTransition)t).followState.stateNumber
                 );
             try
             {
                 calledRuleStack.Set(((RuleTransition)t).target.ruleIndex);
                 Look(t.target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds
                     , addEOF);
             }
             finally
             {
                 calledRuleStack.Clear(((RuleTransition)t).target.ruleIndex);
             }
         }
         else
         {
             if (t is AbstractPredicateTransition)
             {
                 if (seeThruPreds)
                 {
                     Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF
                         );
                 }
                 else
                 {
                     look.Add(HitPred);
                 }
             }
             else
             {
                 if (t.IsEpsilon)
                 {
                     Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF
                         );
                 }
                 else
                 {
                     if (t.GetType() == typeof(WildcardTransition))
                     {
                         look.AddAll(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType));
                     }
                     else
                     {
                         //				System.out.println("adding "+ t);
                         IntervalSet set = t.Label;
                         if (set != null)
                         {
                             if (t is NotSetTransition)
                             {
                                 set = set.Complement(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType
                                     ));
                             }
                             look.AddAll(set);
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 54
0
 public override PredictionContext AppendContext(PredictionContext suffix, PredictionContextCache
      contextCache)
 {
     return suffix;
 }
Ejemplo n.º 55
0
        /// <inheritdoc/>
        /// <remarks>
        /// Tries to get the suggestions for the user input from the command history. If that doesn't find
        /// <paramref name="suggestionCount"/> suggestions, it'll fallback to find the suggestion regardless of command history.
        /// </remarks>
        public CommandLineSuggestion GetSuggestion(PredictionContext context, int suggestionCount, int maxAllowedCommandDuplicate, CancellationToken cancellationToken)
        {
            Validation.CheckArgument(context, $"{nameof(context)} cannot be null");
            Validation.CheckArgument <ArgumentOutOfRangeException>(suggestionCount > 0, $"{nameof(suggestionCount)} must be larger than 0.");
            Validation.CheckArgument <ArgumentOutOfRangeException>(maxAllowedCommandDuplicate > 0, $"{nameof(maxAllowedCommandDuplicate)} must be larger than 0.");

            var        relatedAsts = context.RelatedAsts;
            CommandAst commandAst  = null;

            for (var i = relatedAsts.Count - 1; i >= 0; --i)
            {
                if (relatedAsts[i] is CommandAst c)
                {
                    commandAst = c;
                    break;
                }
            }

            var commandName = commandAst?.GetCommandName();

            if (string.IsNullOrWhiteSpace(commandName))
            {
                return(null);
            }

            ParameterSet inputParameterSet = null;

            try
            {
                inputParameterSet = new ParameterSet(commandAst);
            }
            catch when(!IsSupportedCommand(commandName))
            {
                // We only ignore the exception when the command name is not supported.
                // For the supported ones, this most likely happens when positional parameters are used.
                // We want to collect the telemetry about the exception how common a positional parameter is used.
                return(null);
            }

            var rawUserInput               = context.InputAst.ToString();
            var presentCommands            = new Dictionary <string, int>();
            var commandBasedPredictor      = _commandBasedPredictor;
            var commandToRequestPrediction = _commandToRequestPrediction;

            var result = commandBasedPredictor?.Item2?.GetSuggestion(commandName,
                                                                     inputParameterSet,
                                                                     rawUserInput,
                                                                     presentCommands,
                                                                     suggestionCount,
                                                                     maxAllowedCommandDuplicate,
                                                                     cancellationToken);

            if ((result != null) && (result.Count > 0))
            {
                var suggestionSource = SuggestionSource.PreviousCommand;

                if (string.Equals(commandToRequestPrediction, commandBasedPredictor?.Item1, StringComparison.Ordinal))
                {
                    suggestionSource = SuggestionSource.CurrentCommand;
                }

                for (var i = 0; i < result.Count; ++i)
                {
                    result.UpdateSuggestionSource(i, suggestionSource);
                }
            }

            if ((result == null) || (result.Count < suggestionCount))
            {
                var fallbackPredictor        = _fallbackPredictor;
                var suggestionCountToRequest = (result == null) ? suggestionCount : suggestionCount - result.Count;
                var resultsFromFallback      = fallbackPredictor?.GetSuggestion(commandName,
                                                                                inputParameterSet,
                                                                                rawUserInput,
                                                                                presentCommands,
                                                                                suggestionCountToRequest,
                                                                                maxAllowedCommandDuplicate,
                                                                                cancellationToken);

                if ((result == null) && (resultsFromFallback != null))
                {
                    result = resultsFromFallback;

                    for (var i = 0; i < result.Count; ++i)
                    {
                        result.UpdateSuggestionSource(i, SuggestionSource.StaticCommands);
                    }
                }
                else if ((resultsFromFallback != null) && (resultsFromFallback.Count > 0))
                {
                    for (var i = 0; i < resultsFromFallback.Count; ++i)
                    {
                        if (result.SourceTexts.Contains(resultsFromFallback.SourceTexts[i]))
                        {
                            continue;
                        }

                        result.AddSuggestion(resultsFromFallback.PredictiveSuggestions[i], resultsFromFallback.SourceTexts[i], SuggestionSource.StaticCommands);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 56
0
        public override IToken RecoverInline(Parser recognizer)
        {
            if (recognizer is ICodeCompletionParser &&
                ((ITokenStream)recognizer.InputStream).Lt(1) is ICaretToken)
            {
                ICodeCompletionParser parser = (ICodeCompletionParser)recognizer;
                ICaretToken           token  = (ICaretToken)((ITokenStream)recognizer.InputStream).Lt(1);

                CompletionParserATNSimulator interp = parser.Interpreter;
                int      stateNumber = recognizer.State;
                ATNState state       = interp.atn.states[stateNumber];

                PredictionContext context      = PredictionContext.FromRuleContext(interp.atn, recognizer.Context, false);
                ATNConfigSet      intermediate = new ATNConfigSet();
                ATNConfigSet      closure      = new ATNConfigSet();
                for (int i = 0; i < state.NumberOfTransitions; i++)
                {
                    Transition transition = state.Transition(i);
                    if (transition.IsEpsilon)
                    {
                        ATNState  target = transition.target;
                        ATNConfig config = ATNConfig.Create(target, i + 1, context);
                        intermediate.Add(config);
                    }
                }

                bool collectPredicates = false;
                bool hasMoreContext    = true;
                interp.ClosureHelper(intermediate, closure, collectPredicates, hasMoreContext, PredictionContextCache.Uncached, false);

                if (!state.OnlyHasEpsilonTransitions)
                {
                    for (int i = 0; i < state.NumberOfTransitions; i++)
                    {
                        closure.Add(ATNConfig.Create(state, i + 1, PredictionContext.FromRuleContext(interp.atn, recognizer.Context)));
                    }
                }

                Dictionary <ATNConfig, IList <Transition> > transitions = null;
                int ncl = closure.Count;
                // TODO: foreach
                for (int ci = 0; ci < ncl; ci++)
                {
                    ATNConfig c = closure[ci];

                    List <Transition> configTransitions = null;

                    int n = c.State.NumberOfTransitions;
                    for (int ti = 0; ti < n; ti++)
                    {               // for each transition
                        Transition trans  = c.State.Transition(ti);
                        ATNState   target = interp.GetReachableTargetHelper(c, trans, CaretToken.CaretTokenType);
                        if (target != null)
                        {
                            if (transitions == null)
                            {
                                transitions = new Dictionary <ATNConfig, IList <Transition> >();
                            }

                            if (configTransitions == null)
                            {
                                configTransitions = new List <Transition>();
                                transitions[c]    = configTransitions;
                            }

                            configTransitions.Add(trans);
                        }
                    }
                }

                /*
                 * This should be null if the intended token is not "wordlike", and
                 * should be a single transition from the current state.
                 */
                if (transitions != null)
                {
                    Debug.Assert(transitions.Count == 1);
                    Debug.Assert(transitions.Values.First().Count == 1);
                    Debug.Assert(state.NumberOfTransitions == 1);
                    Debug.Assert(transitions.Values.First()[0] == state.Transition(0));
                }

                throw new CaretReachedException(parser.Context, token, transitions, null);
            }

            return(base.RecoverInline(recognizer));
        }
Ejemplo n.º 57
0
 public override PredictionContext AppendContext(PredictionContext suffix, PredictionContextCache
      contextCache)
 {
     return contextCache.GetChild(parent.AppendContext(suffix, contextCache), returnState
         );
 }
Ejemplo n.º 58
0
 public virtual IntervalSet Look(ATNState s, ATNState stopState, PredictionContext
      ctx)
 {
     IntervalSet r = new IntervalSet();
     bool seeThruPreds = true;
     // ignore preds; get all lookahead
     Look(s, stopState, ctx, r, new HashSet<ATNConfig>(), new BitSet(), seeThruPreds, 
         true);
     return r;
 }
Ejemplo n.º 59
0
 private static PredictionContext AppendContext(PredictionContext context, PredictionContext suffix, PredictionContext.IdentityHashMap visited)
 {
     if (suffix.IsEmpty)
     {
         if (IsEmptyLocal(suffix))
         {
             if (context.HasEmpty)
             {
                 return EmptyLocal;
             }
             throw new NotSupportedException("what to do here?");
         }
         return context;
     }
     if (suffix.Size != 1)
     {
         throw new NotSupportedException("Appending a tree suffix is not yet supported.");
     }
     PredictionContext result;
     if (!visited.TryGetValue(context, out result))
     {
         if (context.IsEmpty)
         {
             result = suffix;
         }
         else
         {
             int parentCount = context.Size;
             if (context.HasEmpty)
             {
                 parentCount--;
             }
             PredictionContext[] updatedParents = new PredictionContext[parentCount];
             int[] updatedReturnStates = new int[parentCount];
             for (int i = 0; i < parentCount; i++)
             {
                 updatedReturnStates[i] = context.GetReturnState(i);
             }
             for (int i_1 = 0; i_1 < parentCount; i_1++)
             {
                 updatedParents[i_1] = AppendContext(context.GetParent(i_1), suffix, visited);
             }
             if (updatedParents.Length == 1)
             {
                 result = new SingletonPredictionContext(updatedParents[0], updatedReturnStates[0]);
             }
             else
             {
                 System.Diagnostics.Debug.Assert(updatedParents.Length > 1);
                 result = new Antlr4.Runtime.Atn.ArrayPredictionContext(updatedParents, updatedReturnStates);
             }
             if (context.HasEmpty)
             {
                 result = PredictionContext.Join(result, suffix);
             }
         }
         visited[context] = result;
     }
     return result;
 }
Ejemplo n.º 60
0
 public IdentityCommutativePredictionContextOperands(PredictionContext x, PredictionContext y)
 {
     this.x = x;
     this.y = y;
 }