/// <summary>Get the text representation of a token's values.</summary> /// <param name="context">Provides access to contextual tokens.</param> /// <param name="part">The token string part whose value to fetch.</param> /// <param name="unavailableTokens">A list of unavailable or unready token names to update if needed.</param> /// <param name="errors">The errors which occurred (if any).</param> /// <param name="text">The text representation, if available.</param> /// <returns>Returns true if the token is ready and <paramref name="text"/> was set, else false.</returns> private bool TryGetTokenText(IContext context, TokenStringPart part, InvariantHashSet unavailableTokens, InvariantHashSet errors, out string text) { switch (part.LexToken) { case LexTokenToken lexToken: { // get token IToken token = context.GetToken(lexToken.Name, enforceContext: true); if (token == null || !token.IsReady) { unavailableTokens.Add(lexToken.Name); text = null; return(false); } // get token input if (part.Input != null) { // update input part.Input.UpdateContext(context); // check for unavailable tokens string[] unavailableInputTokens = part.Input .GetTokensUsed() .Where(name => context.GetToken(name, enforceContext: true)?.IsReady != true) .ToArray(); if (unavailableInputTokens.Any()) { foreach (string tokenName in unavailableInputTokens) { unavailableTokens.Add(tokenName); } text = null; return(false); } } // validate input if (!token.TryValidateInput(part.InputArgs, out string error)) { errors.Add(error); text = null; return(false); } // get text representation string[] values = token.GetValues(part.InputArgs).ToArray(); text = string.Join(", ", values); return(true); } default: text = part.LexToken.ToString(); return(true); } }
/// <summary>Get the text representation of a token's values.</summary> /// <param name="context">Provides access to contextual tokens.</param> /// <param name="part">The token string part whose value to fetch.</param> /// <param name="state">The context state to update with errors, unavailable tokens, etc.</param> /// <param name="text">The text representation, if available.</param> /// <returns>Returns true if the token is ready and <paramref name="text"/> was set, else false.</returns> private bool TryGetTokenText(IContext context, TokenStringPart part, ContextualState state, out string text) { switch (part.LexToken) { case LexTokenToken lexToken: { // get token IToken token = context.GetToken(lexToken.Name, enforceContext: false); if (token == null) { state.AddInvalidTokens(lexToken.Name); text = null; return(false); } if (!token.IsReady) { this.State.AddUnreadyTokens(lexToken.Name); text = null; return(false); } // get token input if (part.Input != null) { part.Input.UpdateContext(context); if (!part.Input.IsReady) { state.MergeFrom(part.Input.GetDiagnosticState()); text = null; return(false); } } // validate input if (!token.TryValidateInput(part.InputArgs, out string error)) { state.AddErrors(error); text = null; return(false); } // get text representation string[] values = token.GetValues(part.InputArgs).ToArray(); text = string.Join(", ", values); return(true); } default: text = part.LexToken.ToString(); return(true); } }