Esempio n. 1
0
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            yield return(this.TryCalculate(input.GetFirstPositionalArg(), out object result, out _)
                ? (result is IConvertible convertible ? convertible.ToString(CultureInfo.InvariantCulture) : result.ToString())
                : "0");
        }
Esempio n. 2
0
 /*********
 ** Public methods
 *********/
 /// <summary>Construct an instance.</summary>
 /// <param name="lexToken">The underlying lex token.</param>
 /// <param name="input">The parsed version of <see cref="Input"/>.</param>
 public TokenStringPart(ILexToken lexToken, TokenString?input)
 {
     this.LexToken  = lexToken;
     this.Input     = input;
     this.InputArgs = !string.IsNullOrWhiteSpace(input?.Raw)
         ? new InputArguments(input)
         : InputArguments.Empty;
 }
Esempio n. 3
0
        /// <inheritdoc />
        public override bool HasBoundedValues(IInputArguments input, out InvariantHashSet allowedValues)
        {
            allowedValues = !input.IsMutable
                ? new InvariantHashSet(input.PositionalArgs)
                : null;

            return(allowedValues != null);
        }
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            return(InvariantSets.FromValue(
                       this.GetPathExists(input.GetPositionalSegment())
                       ));
        }
Esempio n. 5
0
        /// <inheritdoc />
        public override bool HasBoundedValues(IInputArguments input, [NotNullWhen(true)] out IInvariantSet?allowedValues)
        {
            allowedValues = !input.IsMutable
                ? InvariantSets.From(input.PositionalArgs)
                : null;

            return(allowedValues != null);
        }
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            return(this.IsReady
                ? this.Values.ToArray()
                : Enumerable.Empty <string>());
        }
Esempio n. 7
0
 /// <inheritdoc />
 public override bool TryValidateInput(IInputArguments input, [NotNullWhen(false)] out string?error)
 {
     return
         (base.TryValidateInput(input, out error) &&
          (
              !input.HasPositionalArgs ||
              this.TryParseInput(input, null, out error)
          ));
 }
        /// <inheritdoc />
        public bool HasBoundedValues(IInputArguments input, out InvariantHashSet allowedValues)
        {
            bool bounded = this.Provider.HasBoundedValues(this.ToApiInput(input), out IEnumerable <string> values);

            allowedValues = bounded
                ? new InvariantHashSet(values)
                : null;
            return(bounded);
        }
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            yield return(this.Type switch
            {
                ConditionType.Lowercase => input.TokenString.Value.ToLowerInvariant(),
                ConditionType.Uppercase => input.TokenString.Value.ToUpperInvariant(),
                _ => throw new NotSupportedException($"Unimplemented letter case type '{this.Type}'.") // should never happen
            });
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            string?path = input.GetPositionalSegment();

            return(!string.IsNullOrWhiteSpace(path)
                ? InvariantSets.FromValue(this.GetInternalAssetKey(path).Name)
                : InvariantSets.Empty);
        }
Esempio n. 11
0
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            string?path = this.GetAbsolutePath(input.GetPositionalSegment());

            return(path != null
                ? InvariantSets.FromValue(path)
                : InvariantSets.Empty);
        }
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            return(InvariantSets.From(
                       input.PositionalArgs
                       .Where(this.GetPathExists)
                       .Take(1)
                       ));
        }
Esempio n. 13
0
        /// <inheritdoc />
        public virtual bool CanHaveMultipleValues(IInputArguments input)
        {
            // 'contains' limited to true/false
            if (input.ReservedArgs.ContainsKey(InputArguments.ContainsKey))
            {
                return(false);
            }

            // default logic
            return(this.Values.CanHaveMultipleValues(input));
        }
Esempio n. 14
0
        /// <inheritdoc />
        public virtual bool CanHaveMultipleValues(IInputArguments input)
        {
            // 'contains' and 'valueAt' filter to a single value
            if (input.ReservedArgs.ContainsKey(InputArguments.ContainsKey) || input.ReservedArgs.ContainsKey(InputArguments.ValueAtKey))
            {
                return(false);
            }

            // default logic
            return(this.Values.CanHaveMultipleValues(input));
        }
Esempio n. 15
0
 /// <inheritdoc />
 public override bool CanHaveMultipleValues(IInputArguments input)
 {
     try
     {
         return(base.CanHaveMultipleValues(input));
     }
     catch (Exception ex)
     {
         this.Log(ex);
         return(false);
     }
 }
Esempio n. 16
0
        /// <inheritdoc />
        public virtual bool HasBoundedValues(IInputArguments input, out InvariantHashSet allowedValues)
        {
            // 'contains' limited to true/false
            if (input.ReservedArgs.ContainsKey(InputArguments.ContainsKey))
            {
                allowedValues = InvariantHashSet.Boolean();
                return(true);
            }

            // default logic
            return(this.Values.HasBoundedValues(input, out allowedValues));
        }
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            if (!this.TryGetPart(input.PositionalArgs[0], input.PositionalArgs[1], out string?part, out string?error))
            {
                throw new InvalidOperationException(error); // shouldn't happen since we check the input in TryValidateInput
            }
            return(part != null
                ? InvariantSets.FromValue(part)
                : InvariantSets.Empty);
        }
Esempio n. 18
0
 /// <inheritdoc />
 public override bool TryValidateInput(IInputArguments input, out string error)
 {
     try
     {
         return(base.TryValidateInput(input, out error));
     }
     catch (Exception ex)
     {
         this.Log(ex);
         error = null;
         return(true);
     }
 }
Esempio n. 19
0
        /// <inheritdoc />
        public virtual bool HasBoundedRangeValues(IInputArguments input, out int min, out int max)
        {
            // 'contains' limited to true/false
            if (input.ReservedArgs.ContainsKey(InputArguments.ContainsKey))
            {
                min = -1;
                max = -1;
                return(false);
            }

            // default logic
            return(this.Values.HasBoundedRangeValues(input, out min, out max));
        }
Esempio n. 20
0
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            var values = this
                         .GetContextsFor(input)
                         .Select(context => this.Values.TryGetValue(context, out Weather weather)
                    ? weather.ToString()
                    : Weather.Sun.ToString() // the game treats an invalid context (e.g. MAX) as always sunny
                                 );

            return(InvariantSets.From(values));
        }
Esempio n. 21
0
 /// <inheritdoc />
 public override bool TryValidateValues(IInputArguments input, InvariantHashSet values, IContext context, out string error)
 {
     try
     {
         return(base.TryValidateValues(input, values, context, out error));
     }
     catch (Exception ex)
     {
         this.Log(ex);
         error = null;
         return(true);
     }
 }
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            string?output = this.Type switch
            {
                ConditionType.Lowercase => input.TokenString?.Value?.ToLowerInvariant(),
                ConditionType.Uppercase => input.TokenString?.Value?.ToUpperInvariant(),
                _ => throw new NotSupportedException($"Unimplemented letter case type '{this.Type}'.") // should never happen
            };

            return(InvariantSets.FromValue(output ?? string.Empty));
        }
    }
        /*********
        ** Private methods
        *********/
        /// <summary>Parse the numeric min/max values from a range specifier if it's valid.</summary>
        /// <param name="input">The input arguments containing the range specifier.</param>
        /// <param name="min">The parsed min value, if valid.</param>
        /// <param name="max">The parsed max value, if valid.</param>
        /// <param name="error">The error indicating why the range is invalid, if applicable.</param>
        private bool TryParseRange(IInputArguments input, out int min, out int max, out string error)
        {
            min = 0;
            max = 0;
            string errorPrefix = $"invalid input ('{input.TokenString}')";

            // check if input provided
            if (!input.HasPositionalArgs)
            {
                error = $"{errorPrefix}, token {this.Name} requires input arguments.";
                return(false);
            }

            // validate length
            if (input.PositionalArgs.Length != 2)
            {
                error = $"{errorPrefix}, must specify a minimum and maximum value like {{{{{this.Name}:0,20}}}}.";
                return(false);
            }

            // parse min/max values
            if (!int.TryParse(input.PositionalArgs[0], out min))
            {
                error = $"{errorPrefix}, can't parse min value '{input.PositionalArgs[0]}' as an integer.";
                return(false);
            }
            if (!int.TryParse(input.PositionalArgs[1], out max))
            {
                error = $"{errorPrefix}, can't parse max value '{input.PositionalArgs[1]}' as an integer.";
                return(false);
            }

            // validate range
            if (min > max)
            {
                error = $"{errorPrefix}, min value '{min}' can't be greater than max value '{max}'.";
                return(false);
            }

            int count = (max - min) + 1;

            if (count > RangeValueProvider.MaxCount)
            {
                error = $"{errorPrefix}, range can't exceed {RangeValueProvider.MaxCount} numbers (specified range would contain {count} numbers).";
                return(false);
            }

            error = null;
            return(true);
        }
Esempio n. 24
0
        /// <inheritdoc />
        public override bool TryValidateInput(IInputArguments input, out string error)
        {
            if (!base.TryValidateInput(input, out error))
            {
                return(false);
            }

            if (input.IsReady)
            {
                return(this.TryCalculate(input.GetFirstPositionalArg(), out _, out error));
            }

            return(true);
        }
Esempio n. 25
0
        /// <inheritdoc />
        public virtual IEnumerable <string> GetValues(IInputArguments input)
        {
            // get values
            var values = this.Values.GetValues(input);

            // apply contains
            if (input.ReservedArgs.TryGetValue(InputArguments.ContainsKey, out IInputArgumentValue rawSearch))
            {
                InvariantHashSet search = new InvariantHashSet(rawSearch.Parsed.Select(this.NormalizeValue));
                bool             match  = search.Any() && values.Any(value => search.Contains(value));
                values = new[] { match.ToString() };
            }

            return(values);
        }
Esempio n. 26
0
        /// <inheritdoc />
        public virtual bool TryValidateInput(IInputArguments input, [NotNullWhen(false)] out string?error)
        {
            // validate 'valueAt'
            foreach ((string name, IInputArgumentValue value) in input.ReservedArgsList)
            {
                if (InputArguments.ValueAtKey.EqualsIgnoreCase(name) && !int.TryParse(value.Raw, out _))
                {
                    error = $"invalid '{InputArguments.ValueAtKey}' index '{value.Raw}', must be a numeric value.";
                    return(false);
                }
            }

            // default logic
            return(this.Values.TryValidateInput(input, out error));
        }
Esempio n. 27
0
        /// <inheritdoc />
        public virtual bool TryValidateInput(IInputArguments input, out string error)
        {
            // validate 'valueAt'
            foreach (var arg in input.ReservedArgsList)
            {
                if (InputArguments.ValueAtKey.EqualsIgnoreCase(arg.Key) && !int.TryParse(arg.Value.Raw, out _))
                {
                    error = $"invalid '{InputArguments.ValueAtKey}' index '{arg.Value.Raw}', must be a numeric value.";
                    return(false);
                }
            }

            // default logic
            return(this.Values.TryValidateInput(input, out error));
        }
Esempio n. 28
0
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            if (!input.HasPositionalArgs)
            {
                return(this.GetValuesFor(null));
            }

            var playerIds = new HashSet <long>();

            return(this.TryParseInput(input, playerIds, out _)
                ? this.GetValuesFor(playerIds)
                : InvariantSets.Empty);
        }
        /// <inheritdoc />
        public override IEnumerable <string> GetValues(IInputArguments input)
        {
            this.AssertInput(input);

            Translation translation = this.TranslationHelper.Get(input.GetFirstPositionalArg());

            if (input.NamedArgs.TryGetValue("default", out IInputArgumentValue defaultValue))
            {
                translation = translation
                              .Default(string.Join(", ", defaultValue.Parsed))
                              .UsePlaceholder(false); // allow setting a blank default
            }

            yield return(translation);
        }
        /// <inheritdoc />
        public override bool TryValidateInput(IInputArguments input, [NotNullWhen(false)] out string?error)
        {
            if (!base.TryValidateInput(input, out error))
            {
                return(false);
            }

            if (input.IsReady)
            {
                return
                    (base.TryValidateInput(input, out error) &&
                     this.TryParse(input, out _, out _, out _, out error));
            }

            return(true);
        }