Example #1
0
        private static int?NameUsedForPositional(AnalyzedArguments arguments, ParameterMap argsToParameters)
        {
            // Was there a named argument used for a previously-supplied positional argument?

            // If the map is trivial then clearly not.
            if (argsToParameters.IsTrivial)
            {
                return(null);
            }

            // PERFORMANCE: This is an O(n-squared) algorithm, but n will typically be small.  We could rewrite this
            // PERFORMANCE: as a linear algorithm if we wanted to allocate more memory.

            for (int argumentPosition = 0; argumentPosition < argsToParameters.Length; ++argumentPosition)
            {
                if (arguments.Name(argumentPosition) != null)
                {
                    for (int i = 0; i < argumentPosition; ++i)
                    {
                        if (arguments.Name(i) == null)
                        {
                            if (argsToParameters[argumentPosition] == argsToParameters[i])
                            {
                                // Error; we've got a named argument that corresponds to
                                // a previously-given positional argument.
                                return(argumentPosition);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Example #2
0
        private static int?CheckForDuplicateNamedArgument(AnalyzedArguments arguments)
        {
            if (arguments.Names.IsEmpty())
            {
                // No checks if there are no named arguments
                return(null);
            }

            var alreadyDefined = PooledHashSet <string> .GetInstance();

            for (int i = 0; i < arguments.Names.Count; ++i)
            {
                string name = arguments.Name(i);

                if (name is null)
                {
                    // Skip unnamed arguments
                    continue;
                }

                if (!alreadyDefined.Add(name))
                {
                    alreadyDefined.Free();
                    return(i);
                }
            }

            alreadyDefined.Free();
            return(null);
        }
        private static int?CheckForBadNonTrailingNamedArgument(
            AnalyzedArguments arguments,
            ParameterMap argsToParameters,
            ImmutableArray <ParameterSymbol> parameters
            )
        {
            // Is there any named argument used out-of-position and followed by unnamed arguments?

            // If the map is trivial then clearly not.
            if (argsToParameters.IsTrivial)
            {
                return(null);
            }

            // Find the first named argument which is used out-of-position
            int foundPosition = -1;
            int length        = arguments.Arguments.Count;

            for (int i = 0; i < length; i++)
            {
                int parameter = argsToParameters[i];
                if (parameter != -1 && parameter != i && arguments.Name(i) != null)
                {
                    foundPosition = i;
                    break;
                }
            }

            if (foundPosition != -1)
            {
                // Verify that all the following arguments are named
                for (int i = foundPosition + 1; i < length; i++)
                {
                    if (arguments.Name(i) == null)
                    {
                        return(foundPosition);
                    }
                }
            }

            return(null);
        }
        private static int? NameUsedForPositional(AnalyzedArguments arguments, ParameterMap argsToParameters)
        {
            // Was there a named argument used for a previously-supplied positional argument?

            // If the map is trivial then clearly not. 
            if (argsToParameters.IsTrivial)
            {
                return null;
            }

            // PERFORMANCE: This is an O(n-squared) algorithm, but n will typically be small.  We could rewrite this
            // PERFORMANCE: as a linear algorithm if we wanted to allocate more memory.

            for (int argumentPosition = 0; argumentPosition < argsToParameters.Length; ++argumentPosition)
            {
                if (arguments.Name(argumentPosition) != null)
                {
                    for (int i = 0; i < argumentPosition; ++i)
                    {
                        if (arguments.Name(i) == null)
                        {
                            if (argsToParameters[argumentPosition] == argsToParameters[i])
                            {
                                // Error; we've got a named argument that corresponds to 
                                // a previously-given positional argument.
                                return argumentPosition;
                            }
                        }
                    }
                }
            }

            return null;
        }