Example #1
0
        //-----------------------------------------------------------------------------
        // Array Methods
        //-----------------------------------------------------------------------------

        // Get an enumerable list of an array paraneter's children.
        public IEnumerable <CommandReferenceParam> GetChildren()
        {
            for (CommandReferenceParam child = children; child != null; child = child.nextParam)
            {
                yield return(child);
            }
        }
Example #2
0
        //-----------------------------------------------------------------------------
        // Command Parameter ToString
        //-----------------------------------------------------------------------------

        // Returns a string representation of a reference command parameter.
        public static string ToString(CommandReferenceParam param)
        {
            string result = "";

            if (param.Type == CommandParamType.Array)
            {
                result += "(";
                CommandReferenceParam child = param.Children;
                while (child != null)
                {
                    result += ToString(child);
                    if (child.NextParam != null)
                    {
                        result += ", ";
                    }
                    child = child.NextParam;
                }
                result += ")";
            }
            else
            {
                if (param.Type == CommandParamType.String)
                {
                    result += "string";
                }
                else if (param.Type == CommandParamType.Integer)
                {
                    result += "int";
                }
                else if (param.Type == CommandParamType.Float)
                {
                    result += "float";
                }
                else if (param.Type == CommandParamType.Boolean)
                {
                    result += "bool";
                }
                else if (param.Type == CommandParamType.Any)
                {
                    result += "var";
                }
                result += " " + param.Name;
            }

            // Show the variadic ellipses.
            if (param.IsVariadic)
            {
                result += "...";
            }

            // Append the default value.
            if (param.DefaultValue != null)
            {
                result += " = " + ToString(param.DefaultValue);
            }

            return(result);
        }
Example #3
0
 public ScriptCommand(string name, string[] parameterOverloads, Action <CommandParam> action) :
     this(name, action)
 {
     for (int i = 0; i < parameterOverloads.Length; i++)
     {
         CommandReferenceParam p = CommandParamParser.ParseReferenceParams(parameterOverloads[i]);
         p.Name = parameterOverloads[i];
         this.parameterOverloads.Add(p);
     }
 }
Example #4
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 = "";
            }
        }
 public CommandReferenceParam(CommandParamType type)
 {
     this.type			= type;
     this.nextParam		= null;
     this.parent			= null;
     this.children		= null;
     this.defaultValue	= null;
     this.value			= null;
     this.name			= "";
     if (type == CommandParamType.Array)
         value = (int) 0;
 }
Example #6
0
 public CommandReferenceParam(CommandParamType type)
 {
     this.type         = type;
     this.nextParam    = null;
     this.parent       = null;
     this.children     = null;
     this.defaultValue = null;
     this.value        = null;
     this.name         = "";
     if (type == CommandParamType.Array)
     {
         value = (int)0;
     }
 }
Example #7
0
        public CommandParam(CommandReferenceParam referenceParam) :
            this()
        {
            this.name = referenceParam.Name;
            this.type = referenceParam.Type;

            // Copy array children.
            this.count = 0;
            if (type == CommandParamType.Array)
            {
                CommandReferenceParam copyChild = referenceParam.Children;
                while (copyChild != null)
                {
                    AddChild(new CommandParam(copyChild));
                    copyChild = copyChild.NextParam;
                }
            }
        }
        //-----------------------------------------------------------------------------
        // Command Parameter ToString
        //-----------------------------------------------------------------------------
        // Returns a string representation of a reference command parameter.
        public static string ToString(CommandReferenceParam param)
        {
            string result = "";

            if (param.Type == CommandParamType.Array) {
                result += "(";
                CommandReferenceParam child = param.Children;
                while (child != null) {
                    result += ToString(child);
                    if (child.NextParam != null)
                        result += ", ";
                    child = child.NextParam;
                }
                result += ")";
            }
            else {
                if (param.Type == CommandParamType.String)
                    result += "string";
                else if (param.Type == CommandParamType.Integer)
                    result += "int";
                else if (param.Type == CommandParamType.Float)
                    result += "float";
                else if (param.Type == CommandParamType.Boolean)
                    result += "bool";
                else if (param.Type == CommandParamType.Any)
                    result += "var";
                result += " " + param.Name;
            }

            // Show the variadic ellipses.
            if (param.IsVariadic)
                result += "...";

            // Append the default value.
            if (param.DefaultValue != null) {
                result += " = " + ToString(param.DefaultValue);
            }

            return result;
        }
Example #9
0
 // Add a parameter child.
 public CommandReferenceParam AddChild(CommandReferenceParam child)
 {
     if (type == CommandParamType.Array)
     {
         if (children == null)
         {
             children = child;
             value    = (int)1;
         }
         else
         {
             CommandReferenceParam lastChild = children;
             while (lastChild.NextParam != null)
             {
                 lastChild = lastChild.NextParam;
             }
             lastChild.nextParam = child;
             value = ((int)value + 1);
         }
     }
     return(child);
 }
Example #10
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 ParseData ParseParameters(string format, bool parseValue)
        {
            ParseData parseData = new ParseData();
            parseData.RefParamStack		= new Stack<CommandReferenceParam>();
            parseData.ValParamStack		= new Stack<CommandParam>();
            parseData.Tokens			= new List<string>();
            parseData.Token				= "";
            parseData.IsParsingValue	= parseValue;

            // Create the root parameter.
            if (parseData.IsParsingValue)
                parseData.ValParamStack.Push(new CommandParam(CommandParamType.Array));
            else
                parseData.RefParamStack.Push(new CommandReferenceParam(CommandParamType.Array));

            // Parse the format, character by character.
            bool quotes = false;
            for (int i = 0; i < format.Length; i++) {
                char c = format[i];

                // Parse strings between quotes.
                if (quotes) {
                    if (c == '"') {
                        quotes = false;
                        CompleteToken(parseData);
                    }
                    else
                        parseData.Token += c;
                }
                else if (c == '"')
                    quotes = true;

                // Whitespace.
                else if (c == ' ' || c == '\t')
                    CompleteToken(parseData);

                // Parameter delimiter (comma)
                else if (c == ',') {
                    CompleteToken(parseData);
                    CompleteDefaultValue(parseData);
                }

                // Parameter array begin
                else if (c == '(') {
                    if (parseData.IsParsingValue) {
                        if (parseData.IsParsingDefaultValue) {
                            parseData.RefParamStack.Push(parseData.RefParamStack.Peek()
                                .GetChildren().ElementAt(parseData.ValParamStack.Peek().ChildCount));
                        }
                        parseData.ValParamStack.Push(parseData.ValParamStack.Peek()
                            .AddChild(new CommandParam(CommandParamType.Array)));
                    }
                    else {
                        parseData.RefParamStack.Push(parseData.RefParamStack.Peek()
                            .AddChild(new CommandReferenceParam(CommandParamType.Array)));
                    }
                }

                // Parameter array end.
                else if (c == ')') {
                    CompleteToken(parseData);
                    if (parseData.IsParsingValue)
                        parseData.ValParamStack.Pop();
                    if (!parseData.IsParsingValue || (parseData.IsParsingValue && parseData.IsParsingDefaultValue))
                        parseData.RefParamStack.Pop();
                }

                // Default parameter value.
                else if (c == '=') {
                    parseData.IsParsingValue = true;
                    parseData.IsParsingDefaultValue = true;
                    CommandReferenceParam refParent = new CommandReferenceParam(CommandParamType.Array);
                    refParent.AddChild(parseData.RefParamStack.Peek().GetChildren().Last());
                    parseData.RefParamStack.Push(refParent);
                    parseData.ValParamStack.Push(new CommandParam(CommandParamType.Array));
                }
                //else if (c == '.') { TODO: variadic parameters.
                //}
                else {
                    parseData.Token += c;
                }
            }

            CompleteToken(parseData);
            CompleteDefaultValue(parseData);

            return parseData;
        }
Example #12
0
        public CommandParam(CommandReferenceParam referenceParam)
            : this()
        {
            this.name = referenceParam.Name;
            this.type = referenceParam.Type;

            // Copy array children.
            this.count = 0;
            if (type == CommandParamType.Array) {
                CommandReferenceParam copyChild = referenceParam.Children;
                while (copyChild != null) {
                    AddChild(new CommandParam(copyChild));
                    copyChild = copyChild.NextParam;
                }
            }
        }
        //-----------------------------------------------------------------------------
        // 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;
        }
 // Add a parameter child.
 public CommandReferenceParam AddChild(CommandReferenceParam child)
 {
     if (type == CommandParamType.Array) {
         if (children == null) {
             children = child;
             value = (int) 1;
         }
         else {
             CommandReferenceParam lastChild = children;
             while (lastChild.NextParam != null)
                 lastChild = lastChild.NextParam;
             lastChild.nextParam = child;
             value = ((int) value + 1);
         }
     }
     return child;
 }
Example #15
0
        private static ParseData ParseParameters(string format, bool parseValue)
        {
            ParseData parseData = new ParseData();

            parseData.RefParamStack  = new Stack <CommandReferenceParam>();
            parseData.ValParamStack  = new Stack <CommandParam>();
            parseData.Tokens         = new List <string>();
            parseData.Token          = "";
            parseData.IsParsingValue = parseValue;

            // Create the root parameter.
            if (parseData.IsParsingValue)
            {
                parseData.ValParamStack.Push(new CommandParam(CommandParamType.Array));
            }
            else
            {
                parseData.RefParamStack.Push(new CommandReferenceParam(CommandParamType.Array));
            }

            // Parse the format, character by character.
            bool quotes = false;

            for (int i = 0; i < format.Length; i++)
            {
                char c = format[i];

                // Parse strings between quotes.
                if (quotes)
                {
                    if (c == '"')
                    {
                        quotes = false;
                        CompleteToken(parseData);
                    }
                    else
                    {
                        parseData.Token += c;
                    }
                }
                else if (c == '"')
                {
                    quotes = true;
                }

                // Whitespace.
                else if (c == ' ' || c == '\t')
                {
                    CompleteToken(parseData);
                }

                // Parameter delimiter (comma)
                else if (c == ',')
                {
                    CompleteToken(parseData);
                    CompleteDefaultValue(parseData);
                }

                // Parameter array begin
                else if (c == '(')
                {
                    if (parseData.IsParsingValue)
                    {
                        if (parseData.IsParsingDefaultValue)
                        {
                            parseData.RefParamStack.Push(parseData.RefParamStack.Peek()
                                                         .GetChildren().ElementAt(parseData.ValParamStack.Peek().ChildCount));
                        }
                        parseData.ValParamStack.Push(parseData.ValParamStack.Peek()
                                                     .AddChild(new CommandParam(CommandParamType.Array)));
                    }
                    else
                    {
                        parseData.RefParamStack.Push(parseData.RefParamStack.Peek()
                                                     .AddChild(new CommandReferenceParam(CommandParamType.Array)));
                    }
                }

                // Parameter array end.
                else if (c == ')')
                {
                    CompleteToken(parseData);
                    if (parseData.IsParsingValue)
                    {
                        parseData.ValParamStack.Pop();
                    }
                    if (!parseData.IsParsingValue || (parseData.IsParsingValue && parseData.IsParsingDefaultValue))
                    {
                        parseData.RefParamStack.Pop();
                    }
                }

                // Default parameter value.
                else if (c == '=')
                {
                    parseData.IsParsingValue        = true;
                    parseData.IsParsingDefaultValue = true;
                    CommandReferenceParam refParent = new CommandReferenceParam(CommandParamType.Array);
                    refParent.AddChild(parseData.RefParamStack.Peek().GetChildren().Last());
                    parseData.RefParamStack.Push(refParent);
                    parseData.ValParamStack.Push(new CommandParam(CommandParamType.Array));
                }
                //else if (c == '.') { TODO: variadic parameters.
                //}
                else
                {
                    parseData.Token += c;
                }
            }

            CompleteToken(parseData);
            CompleteDefaultValue(parseData);

            return(parseData);
        }