public ParameterToken SaveAnswerParameter([FromBody] ParameterToken token) { int assessmentId = Auth.AssessmentForUser(); RequirementsManager rm = new RequirementsManager(assessmentId); return(rm.SaveAnswerParameter(token.RequirementId, token.Id, token.AnswerId, token.Substitution)); }
public void Argument_given___OK(string expression) { Lexer sut = this.CreateSut(expression); ParameterToken actual = sut.ReadTokens() .Cast <ParameterToken>() .Single(); Assert.AreEqual(expression, actual.Parameter); }
internal ParameterBuilder( MethodBuilder methodBuilder, int sequence, ParameterAttributes attributes, string paramName) // can be NULL string { _position = sequence; _name = paramName; _methodBuilder = methodBuilder; _attributes = attributes; _token = new ParameterToken(TypeBuilder.SetParamInfo( _methodBuilder.GetModuleBuilder().GetNativeHandle(), _methodBuilder.GetToken().Token, sequence, attributes, paramName)); }
public override InputStream TryRead(InputStream stream, out LexicToken token) { token = null; foreach (var parameterName in parameterNames) { if (stream.Content.StartsWith(parameterName)) { ParameterToken paramToken = new ParameterToken(parameterName); token = paramToken; return(stream.Move(parameterName.Length)); } } return(stream); }
public override InputStream TryRead(InputStream stream, out LexicToken token) { token = null; foreach (var parameterName in parameterNames) { if (stream.Content.StartsWith(parameterName)) { ParameterToken paramToken = new ParameterToken(parameterName); token = paramToken; return stream.Move(parameterName.Length); } } return stream; }
//--------------------------------------------------------------------- public void Visit(ParameterToken parameter) { if (_lastToken is ValueToken) { this.Visit(Operation.Multiplication(Position.NotDefined)); } if (!_parameters.Contains(parameter.Parameter)) { _parameters.Add(parameter.Parameter); } var idxExpr = new IndexExpression(_parameters.IndexOf(parameter.Parameter)); var expr = new ArrayIndexExpression(parameter, _arrayParameter, idxExpr); _expressionStack.Push(expr); _lastToken = parameter; }
private static async Task <bool> CheckToken(ParameterToken token, ParameterInfo registration) { if (registration.Type == ParameterType.Regex) { token.Regex = new Regex(registration.Format, RegexOptions.Compiled | RegexOptions.IgnoreCase); } if (!(await token.IsType(registration.Type).ConfigureAwait(false))) { token.Regex = null; return(false); } if (!string.IsNullOrEmpty(registration.Format) && Regex.IsMatch(token.Raw, registration.Format, RegexOptions.Compiled | RegexOptions.IgnoreCase) == registration.Inverse) { token.Regex = null; return(false); } token.Regex = null; return(true); }
public void TestSameAPI(Type syncInterface, Type asyncInterface) { TestContext.Out.WriteLine($"Comparing '{GetCSharpTypeName(syncInterface)}' and '{GetCSharpTypeName(asyncInterface)}'..."); var actual = new List <string>(); foreach (var method in asyncInterface.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { var tok = new MethodToken(method); actual.Add(GetSignature(tok)); } var expected = new List <string>(); ParameterToken cancellationParameter = new ParameterToken("token", typeof(CancellationToken), ParameterAttributes.Optional); foreach (var method in syncInterface.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { AddExpected(method); } if (asyncInterface == typeof(IRedisSortedSetAsync) || asyncInterface == typeof(IRedisSetAsync) || asyncInterface == typeof(IRedisListAsync)) { AddFrom(typeof(ICollection <string>), nameof(ICollection <string> .Clear)); AddFrom(typeof(ICollection <string>), nameof(ICollection <string> .Add)); AddFrom(typeof(ICollection <string>), nameof(ICollection <string> .Remove)); AddFrom(typeof(ICollection <string>), nameof(ICollection <string> .Contains)); AddFrom(typeof(ICollection <string>), "get_" + nameof(ICollection <string> .Count), true); if (asyncInterface == typeof(IRedisListAsync)) { AddFrom(typeof(IList <string>), nameof(IList <string> .IndexOf)); AddFrom(typeof(IList <string>), nameof(IList <string> .RemoveAt)); AddFrom(typeof(IList <string>), "set_Item", true); AddFrom(typeof(IList <string>), "get_Item", true); } } else if (asyncInterface == typeof(IRedisSortedSetAsync <>) || asyncInterface == typeof(IRedisSetAsync <>) || asyncInterface == typeof(IRedisListAsync <>)) { AddFrom(typeof(ICollection <>), nameof(ICollection <string> .Clear)); AddFrom(typeof(ICollection <>), nameof(ICollection <string> .Add)); AddFrom(typeof(ICollection <>), nameof(ICollection <string> .Remove)); AddFrom(typeof(ICollection <>), nameof(ICollection <string> .Contains)); AddFrom(typeof(ICollection <>), "get_" + nameof(ICollection <string> .Count), true); if (asyncInterface == typeof(IRedisListAsync <>)) { AddFrom(typeof(IList <>), nameof(IList <string> .IndexOf)); AddFrom(typeof(IList <>), nameof(IList <string> .RemoveAt)); AddFrom(typeof(IList <>), "set_Item", true); AddFrom(typeof(IList <>), "get_Item", true); } } else if (asyncInterface == typeof(IRedisHashAsync <,>)) { AddFrom(typeof(ICollection <>).MakeGenericType(typeof(KeyValuePair <,>).MakeGenericType(asyncInterface.GetGenericArguments())), nameof(IDictionary <string, string> .Add)); AddFrom(typeof(IDictionary <,>), nameof(IDictionary <string, string> .Add)); AddFrom(typeof(ICollection <>), nameof(IDictionary <string, string> .Clear)); AddFrom(typeof(IDictionary <,>), nameof(IDictionary <string, string> .ContainsKey)); AddFrom(typeof(IDictionary <,>), nameof(IDictionary <string, string> .Remove)); AddFrom(typeof(ICollection <>), "get_" + nameof(IDictionary <string, string> .Count), true); } else if (asyncInterface == typeof(IRedisHashAsync)) { AddFrom(typeof(ICollection <KeyValuePair <string, string> >), nameof(IDictionary <string, string> .Add)); AddFrom(typeof(IDictionary <string, string>), nameof(IDictionary <string, string> .Add)); AddFrom(typeof(ICollection <string>), nameof(IDictionary <string, string> .Clear)); AddFrom(typeof(IDictionary <string, string>), nameof(IDictionary <string, string> .ContainsKey)); AddFrom(typeof(IDictionary <string, string>), nameof(IDictionary <string, string> .Remove)); AddFrom(typeof(ICollection <string>), "get_" + nameof(IDictionary <string, string> .Count), true); } else if (asyncInterface == typeof(IRedisNativeClientAsync)) { AddFrom(typeof(RedisClient), nameof(RedisClient.SlowlogReset)); AddFrom(typeof(RedisClient), nameof(RedisClient.BitCount)); AddFromTyped(typeof(RedisClient), nameof(RedisClient.ZCount), typeof(string), typeof(double), typeof(double)); // can't expose as SlowlogItem because of interface locations expected.Add("ValueTask<object[]> SlowlogGetAsync(int? top = default, CancellationToken token = default)"); // adding missing "exists" capability expected.Add("ValueTask<bool> SetAsync(string key, byte[] value, bool exists, long expirySeconds = 0, long expiryMilliseconds = 0, CancellationToken token = default)"); } else if (asyncInterface == typeof(IRedisClientAsync)) { expected.Add("ValueTask<SlowlogItem[]> GetSlowlogAsync(int? numberOfRecords = default, CancellationToken token = default)"); expected.Add("ValueTask SlowlogResetAsync(CancellationToken token = default)"); } else if (asyncInterface == typeof(ICacheClientAsync)) { AddFrom(typeof(ICacheClientExtended), nameof(ICacheClientExtended.GetKeysByPattern)); AddFrom(typeof(ICacheClientExtended), nameof(ICacheClientExtended.GetTimeToLive)); AddFrom(typeof(ICacheClientExtended), nameof(ICacheClientExtended.RemoveExpiredEntries)); } void AddFrom(Type syncInterface, string name, bool fromPropertyToMethod = false) => AddExpected(syncInterface.GetMethod(name), fromPropertyToMethod); void AddFromTyped(Type syncInterface, string name, params Type[] types)
//--------------------------------------------------------------------- public ParameterExpression(ParameterToken token) : base(token) => this.Parameter = token.Parameter;
public static bool op_Inequality(ParameterToken a, ParameterToken b) { }
public bool Equals(ParameterToken obj) {}
public bool Equals(ParameterToken obj) => obj.Token == Token;
public bool Equals(ParameterToken obj) { }
private async Task <(LastFmUserSettings settings, string name)> GetLastFmSettings(ParameterToken param, IGuildUser fallback = null) { var userOrName = await param.AsGuildUserOrName; if (!string.IsNullOrEmpty(userOrName?.Item2)) { // Off-discord username return(new LastFmUserSettings() { LastFmUsername = userOrName.Item2, Anonymous = false }, userOrName.Item2); } if (userOrName == null && fallback == null) { throw new ArgumentException(); } var user = param.HasValue ? userOrName.Item1 : fallback; return(await GetLastFmSettings(user, param.HasValue)); }
public static bool op_Inequality(ParameterToken a, ParameterToken b) {}
//--------------------------------------------------------------------- public void Visit(ParameterToken parameter) { this.CheckIntrinsic(parameter); _lastToken = parameter; }
private async Task <CommandParseResult> ParseParameters(string body, IEnumerable <ParameterToken> possibleTokens, IEnumerable <ParameterInfo> parameters, ICollection <ParameterToken> usedTokens, bool test = false) { var tokensQueue = new Queue <ParameterToken>(possibleTokens); int count = 0; foreach (var param in parameters) { count++; if (tokensQueue.Count <= 0) { if (param.Flags.HasFlag(ParameterFlags.Optional)) { continue; } else { return(new CommandParseResult(CommandParseResultType.NotEnoughParameters)); } } var token = tokensQueue.Peek(); // Extend the current token in case this parameter requires a remainder bool?remainderMatch = null; if (param.Flags.HasFlag(ParameterFlags.Remainder)) { string value = body.Substring(token.Begin); ParameterToken remainder; // Handle the case when a user surrounds the remainder with quotes (even though they don't have to) if (value.Length >= 2 && TextQualifiers.Contains(value.First()) && TextQualifiers.Contains(value.Last())) { remainder = new ParameterToken(new Token() { Begin = token.Begin + 1, End = body.Length - 1, Value = value.Substring(1, value.Length - 2) }, token.Guild, _userFetcher); } else { remainder = new ParameterToken(new Token() { Begin = token.Begin, End = body.Length, Value = value }, token.Guild, _userFetcher); } remainderMatch = await CheckToken(remainder, param); if (remainderMatch.Value) { token = remainder; } else if (param.Flags.HasFlag(ParameterFlags.Repeatable)) { remainderMatch = null; // Give it a second chance as a repeatable parameter } } // Check if the token fits the parameter description if (!(remainderMatch ?? await CheckToken(token, param))) { if (param.Flags.HasFlag(ParameterFlags.Optional)) { continue; } else { return(new InvalidParameterCommandParseResult(CommandParseResultType.InvalidParameterFormat, usedTokens.Count + 1)); } } // If the parameter is optional, peek forward to check if we aren't stealing it from a required parameter var lastParam = parameters.Count() == count; if (param.Flags.HasFlag(ParameterFlags.Optional) && !lastParam) { // Perform a testing run in the state we would be in if we accepted this token var remainingTokens = param.Flags.HasFlag(ParameterFlags.Remainder) ? Enumerable.Empty <ParameterToken>() : tokensQueue.Skip(1); if ((await ParseParameters(body, remainingTokens, parameters.Skip(count), usedTokens, test: true)).Type != CommandParseResultType.Success) { // The parsing would fail, so we can't take this token token.Regex = null; continue; } } // If this is a non-testing run, add the token to result if (!test) { token.Registration = param; token.Regex = param.Type == ParameterType.Regex ? new Regex(param.Format, RegexOptions.Compiled | RegexOptions.IgnoreCase) : null; usedTokens.Add(token); } // Remove the fitting token(s) from queue if (remainderMatch ?? false) { tokensQueue.Clear(); } else { tokensQueue.Dequeue(); } // If this is a repeatable (last) parameter, try to consume all remaining tokens if (lastParam && param.Flags.HasFlag(ParameterFlags.Repeatable)) { while (tokensQueue.Any()) { var remainingToken = tokensQueue.Peek(); if (!await CheckToken(remainingToken, param)) { break; } if (!test) { remainingToken.Regex = param.Type == ParameterType.Regex ? new Regex(param.Format, RegexOptions.Compiled | RegexOptions.IgnoreCase) : null; token.Repeats.Add(remainingToken); } tokensQueue.Dequeue(); } } } if (tokensQueue.Count > 0) { return(new CommandParseResult(CommandParseResultType.TooManyParameters)); } return(new CommandParseResult(CommandParseResultType.Success)); }
public ArrayIndexExpression(ParameterToken token, Expression left, Expression right) : base(token, left, right) { }