Пример #1
0
        private static void CompleteToken(ParseData data)
        {
            bool isVariadic = false;

            if (data.Token.EndsWith("..."))
            {
                data.Token = data.Token.Substring(0, data.Token.Length - 3);
                isVariadic = true;
            }

            if (data.Token.Length > 0 || isVariadic)
            {
                if (data.Token.Length > 0)
                {
                    data.Tokens.Add(data.Token);
                }

                if (data.IsParsingValue && data.Token.Length > 0)
                {
                    CommandParam p;
                    if (data.IsParsingDefaultValue)
                    {
                        CommandReferenceParam refParam = data.RefParamStack
                                                         .Peek().GetChildren().ElementAt(data.ValParamStack.Peek().ChildCount);
                        p = new CommandParam(refParam);
                        p.SetValueByParse(data.Tokens[0]);
                    }
                    else
                    {
                        p = new CommandParam(data.Tokens[0]);
                    }
                    data.ValParamStack.Peek().AddChild(p);
                    data.Tokens.Clear();
                }
                if (!data.IsParsingValue)
                {
                    if (data.Token.Length > 0 && data.Tokens.Count == 2)
                    {
                        data.RefParamStack.Peek().AddChild(new CommandReferenceParam()
                        {
                            Type       = ParseCommandParamType(data.Tokens[0]),
                            Name       = data.Tokens[1],
                            IsVariadic = isVariadic
                        });
                        data.Tokens.Clear();
                    }
                    else if (isVariadic)
                    {
                        data.RefParamStack.Peek().GetChildren().Last().IsVariadic = true;
                    }
                }

                data.Token = "";
            }
        }
Пример #2
0
        //-----------------------------------------------------------------------------
        // Constructor
        //-----------------------------------------------------------------------------

        // Recursively check whether the parameters are matching, outputting the new parameters.
        private static bool AreParametersMatching(CommandReferenceParam reference, CommandParam userParams, out CommandParam newParameters)
        {
            newParameters = null;

            if (reference == null)
            {
                newParameters = new CommandParam(userParams);
                return(true);
            }
            if (!userParams.IsValidType(reference.Type))
            {
                newParameters = null;
                return(false);
            }

            // Make sure arrays are matching.
            if (reference.Type == CommandParamType.Array)
            {
                newParameters      = new CommandParam(CommandParamType.Array);
                newParameters.Name = reference.Name;

                // Find the child index of the first parameter with a default value.
                int defaultIndex = 0;
                for (CommandReferenceParam p = reference.Children; p != null; p = p.NextParam)
                {
                    if (p.DefaultValue != null)
                    {
                        break;
                    }
                    defaultIndex++;
                }

                // Check if the reference is variadic on the last parameter.
                CommandReferenceParam lastRefChild = reference.GetChildren().LastOrDefault();
                bool isVariadic = false;
                if (lastRefChild != null && lastRefChild.IsVariadic)
                {
                    isVariadic = true;
                }

                // Verify the user parameter's child count is within the valid range.
                if (userParams.ChildCount < defaultIndex ||
                    (userParams.ChildCount > reference.ChildCount && !isVariadic))
                {
                    newParameters = null;
                    return(false);
                }

                // Verify each child paremeter matches the reference.
                CommandReferenceParam referenceChild = reference.Children;
                CommandParam          userChild      = userParams.Children;
                int count = reference.ChildCount;
                if (isVariadic)
                {
                    count = Math.Max(count, userParams.ChildCount);
                }
                for (int i = 0; i < count; i++)
                {
                    CommandParam newChild;
                    if (i < userParams.ChildCount)
                    {
                        if (!AreParametersMatching(referenceChild, userChild, out newChild))
                        {
                            newParameters = null;
                            return(false);
                        }
                        userChild = userChild.NextParam;
                    }
                    else
                    {
                        newChild = new CommandParam(referenceChild.DefaultValue);
                    }
                    newParameters.AddChild(newChild);
                    if (referenceChild.NextParam != null)
                    {
                        referenceChild = referenceChild.NextParam;
                    }
                }
            }
            else
            {
                if (userParams.Type == CommandParamType.Array)
                {
                    newParameters = new CommandParam(CommandParamType.Array);
                    foreach (CommandParam userChild in userParams.GetChildren())
                    {
                        newParameters.AddChild(new CommandParam(userChild));
                    }
                }
                else
                {
                    newParameters = new CommandParam(reference);
                    newParameters.SetValueByParse(userParams.StringValue);
                }
            }

            return(true);
        }
        private static void CompleteToken(ParseData data)
        {
            bool isVariadic = false;

            if (data.Token.EndsWith("...")) {
                data.Token = data.Token.Substring(0, data.Token.Length - 3);
                isVariadic = true;
            }

            if (data.Token.Length > 0 || isVariadic) {
                if (data.Token.Length > 0)
                    data.Tokens.Add(data.Token);

                if (data.IsParsingValue && data.Token.Length > 0) {
                    CommandParam p;
                    if (data.IsParsingDefaultValue) {
                        CommandReferenceParam refParam = data.RefParamStack
                            .Peek().GetChildren().ElementAt(data.ValParamStack.Peek().ChildCount);
                        p = new CommandParam(refParam);
                        p.SetValueByParse(data.Tokens[0]);
                    }
                    else {
                        p = new CommandParam(data.Tokens[0]);
                    }
                    data.ValParamStack.Peek().AddChild(p);
                    data.Tokens.Clear();
                }
                if (!data.IsParsingValue) {
                    if (data.Token.Length > 0 && data.Tokens.Count == 2) {
                        data.RefParamStack.Peek().AddChild(new CommandReferenceParam() {
                            Type		= ParseCommandParamType(data.Tokens[0]),
                            Name		= data.Tokens[1],
                            IsVariadic	= isVariadic
                        });
                        data.Tokens.Clear();
                    }
                    else if (isVariadic) {
                        data.RefParamStack.Peek().GetChildren().Last().IsVariadic = true;
                    }
                }

                data.Token = "";
            }
        }
Пример #4
0
        //-----------------------------------------------------------------------------
        // Constructor
        //-----------------------------------------------------------------------------
        // Recursively check whether the parameters are matching, outputting the new parameters.
        private static bool AreParametersMatching(CommandReferenceParam reference, CommandParam userParams, out CommandParam newParameters)
        {
            newParameters = null;

            if (reference == null) {
                newParameters = new CommandParam(userParams);
                return true;
            }
            if (!userParams.IsValidType(reference.Type)) {
                newParameters = null;
                return false;
            }

            // Make sure arrays are matching.
            if (reference.Type == CommandParamType.Array) {
                newParameters = new CommandParam(CommandParamType.Array);
                newParameters.Name = reference.Name;

                // Find the child index of the first parameter with a default value.
                int defaultIndex = 0;
                for (CommandReferenceParam p = reference.Children; p != null; p = p.NextParam) {
                    if (p.DefaultValue != null)
                        break;
                    defaultIndex++;
                }

                // Check if the reference is variadic on the last parameter.
                CommandReferenceParam lastRefChild = reference.GetChildren().LastOrDefault();
                bool isVariadic = false;
                if (lastRefChild != null && lastRefChild.IsVariadic)
                    isVariadic = true;

                // Verify the user parameter's child count is within the valid range.
                if (userParams.ChildCount < defaultIndex ||
                    (userParams.ChildCount > reference.ChildCount && !isVariadic))
                {
                    newParameters = null;
                    return false;
                }

                // Verify each child paremeter matches the reference.
                CommandReferenceParam referenceChild = reference.Children;
                CommandParam userChild = userParams.Children;
                int count = reference.ChildCount;
                if (isVariadic)
                    count = Math.Max(count, userParams.ChildCount);
                for (int i = 0; i < count; i++) {
                    CommandParam newChild;
                    if (i < userParams.ChildCount) {
                        if (!AreParametersMatching(referenceChild, userChild, out newChild)) {
                            newParameters = null;
                            return false;
                        }
                        userChild = userChild.NextParam;
                    }
                    else {
                        newChild = new CommandParam(referenceChild.DefaultValue);
                    }
                    newParameters.AddChild(newChild);
                    if (referenceChild.NextParam != null)
                        referenceChild = referenceChild.NextParam;
                }
            }
            else {
                if (userParams.Type == CommandParamType.Array) {
                    newParameters = new CommandParam(CommandParamType.Array);
                    foreach (CommandParam userChild in userParams.GetChildren())
                        newParameters.AddChild(new CommandParam(userChild));
                }
                else {
                    newParameters = new CommandParam(reference);
                    newParameters.SetValueByParse(userParams.StringValue);
                }
            }

            return true;
        }