예제 #1
0
        public object[] Parse(string source)
        {
            Dictionary <string, object> argumentValue = new Dictionary <string, object>();
            string         name  = null;
            string         value = null;
            List <object>  positionalArguments = new List <object>();
            int            position            = 0;
            int            length = source.Length;
            CommandLineLex lex    = new CommandLineLex();
            Token <CommandLineLex.TokenType> token = lex.ReadToken(source, ref position);

ExpectingNewArgument:

            switch (token.Type)
            {
            case TokenType.EOF:
                goto End;

            case TokenType.ArgumentName:

                ConfigurationPropertyInfo cpi = GetConfigurationPropertyInfo(token.Value);
                name = cpi.Name;

                if (cpi is ConfigurationSwitchPropertyInfo)
                {
                    token = lex.ReadToken(source, ref position);

                    if (token.Type == TokenType.Colon)
                    {
                        token = lex.ReadToken(source, ref position);
                        goto ExpectingArgumentValue;
                    }
                    argumentValue[name] = true;
                    token = lex.ReadToken(source, ref position);
                    goto ExpectingNewArgument;
                }
                token = lex.ReadToken(source, ref position);
                goto ExpectingArgumentValue;

            case TokenType.Colon:
                throw new ArgumentException(GetParseError("NewArgument", token.Type, token.Value));

            case TokenType.String:
                positionalArguments.Add(token.Value);
                token = lex.ReadToken(source, ref position);
                goto ExpectingNewArgument;
            }

ExpectingArgumentValue:

            switch (token.Type)
            {
            case TokenType.ArgumentName:
                throw new ArgumentException(GetParseError("ArgumentValue", token.Type, token.Value));

            case TokenType.Colon:
                token = lex.ReadToken(source, ref position);
                if (token.Type == TokenType.String)
                {
                    value = token.Value;
                    argumentValue[name] = value;
                    token = lex.ReadToken(source, ref position);
                    goto ExpectingNewArgument;
                }
                else
                {
                    throw new ArgumentException(GetParseError("ArgumentValue", token.Type, token.Value));
                }

            case TokenType.EOF:
                throw new ArgumentException(GetParseError("ArgumentValue", token.Type, token.Value));;

            case TokenType.String:
                value = token.Value;
                argumentValue[name] = value;
                token = lex.ReadToken(source, ref position);
                goto ExpectingNewArgument;
            }

End:

            List <object> values = new List <object>();
            int positionalIndex  = 0;

            foreach (var pi in configurationInfo.Definitions)
            {
                if (pi.Name != null && argumentValue.ContainsKey(pi.Name))
                {
                    values.Add(argumentValue[pi.Name]);
                }
                else if (positionalIndex < positionalArguments.Count)
                {
                    values.Add(positionalArguments[positionalIndex]);
                    ++positionalIndex;
                }
                else
                {
                    values.Add(pi.DefaultValue);
                }
            }

            return(values.ToArray());
        }
        public object[] Parse(string source)
        {
            Dictionary<string, object> argumentValue = new Dictionary<string, object>();
            string name = null;
            string value = null;
            List<object> positionalArguments = new List<object>();
            int position = 0;
            int length = source.Length;
            CommandLineLex lex = new CommandLineLex();
            Token<CommandLineLex.TokenType> token = lex.ReadToken(source, ref position);

            ExpectingNewArgument:

            switch (token.Type)
            {
                case TokenType.EOF:
                    goto End;
                case TokenType.ArgumentName:

                    ConfigurationPropertyInfo cpi = GetConfigurationPropertyInfo(token.Value);
                    name = cpi.Name;

                    if (cpi is ConfigurationSwitchPropertyInfo)
                    {
                        token = lex.ReadToken(source, ref position);

                        if (token.Type == TokenType.Colon)
                        {
                            token = lex.ReadToken(source, ref position);
                            goto ExpectingArgumentValue;
                        }
                        argumentValue[name] = true;
                        token = lex.ReadToken(source, ref position);
                        goto ExpectingNewArgument;
                    }
                    token = lex.ReadToken(source, ref position);
                    goto ExpectingArgumentValue;

                case TokenType.Colon:
                    throw new ArgumentException(GetParseError("NewArgument", token.Type, token.Value));
                case TokenType.String:
                    positionalArguments.Add(token.Value);
                    token = lex.ReadToken(source, ref position);
                    goto ExpectingNewArgument;
            }

            ExpectingArgumentValue:

            switch (token.Type)
            {
                case TokenType.ArgumentName:
                    throw new ArgumentException(GetParseError("ArgumentValue", token.Type, token.Value));
                case TokenType.Colon:
                    token = lex.ReadToken(source, ref position);
                    if (token.Type == TokenType.String)
                    {
                        value = token.Value;
                        argumentValue[name] = value;
                        token = lex.ReadToken(source, ref position);
                        goto ExpectingNewArgument;
                    }
                    else
                    {
                        throw new ArgumentException(GetParseError("ArgumentValue", token.Type, token.Value));
                    }
                case TokenType.EOF:
                    throw new ArgumentException(GetParseError("ArgumentValue", token.Type, token.Value));;
                case TokenType.String:
                    value = token.Value;
                    argumentValue[name] = value;
                    token = lex.ReadToken(source, ref position);
                    goto ExpectingNewArgument;
            }

            End:

            List<object> values = new List<object>();
            int positionalIndex = 0;
            foreach (var pi in configurationInfo.Definitions)
            {
                if (pi.Name != null && argumentValue.ContainsKey(pi.Name))
                {
                    values.Add(argumentValue[pi.Name]);
                }
                else if (positionalIndex < positionalArguments.Count)
                {
                    values.Add(positionalArguments[positionalIndex]);
                    ++positionalIndex;
                }
                else
                {
                    values.Add(pi.DefaultValue);
                }
            }

            return values.ToArray();
        }