Exemplo n.º 1
0
        protected virtual bool Parse(string pattern)
        {
            ArgTokens tokenParser             = ArgTokens.FromPattern(pattern);
            IEnumerable <ArgPattern> patterns = ParseExpression(tokenParser);

            m_requiredPattern = new ArgRequired(patterns.ToArray());
            return(true);
        }
Exemplo n.º 2
0
        private IEnumerable <ArgPattern> ParseAtom(ArgTokens tokenParser)
        {
            // atom ::= '(' expr ')' | '[' expr ']' | 'options'
            //  | long | shorts | argument | command ;

            var token  = tokenParser.Current();
            var result = new List <ArgPattern>();

            switch (token)
            {
            case "[":
            case "(":
            {
                tokenParser.Move();
                string matching;
                if (token == "(")
                {
                    matching = ")";
                    result.Add(new ArgRequired(ParseExpression(tokenParser).ToArray()));
                }
                else
                {
                    matching = "]";
                    result.Add(new ArgOptional(ParseExpression(tokenParser).ToArray()));
                }
                if (tokenParser.Move() != matching)
                {
                    throw new Exception("unmatched '" + token + "'");
                }
            }
            break;

            default:
                if ((token.StartsWith("<") && token.EndsWith(">")) || token.All(c => Char.IsUpper(c)))
                {
                    result.Add(new ArgArgument(tokenParser.Move()));
                }
                else if (token.StartsWith("-"))
                {
                    result.Add(ParseOption(tokenParser));
                    tokenParser.Move();
                }
                else
                {
                    result.Add(new ArgCommand(tokenParser.Move()));
                }
                break;
            }
            return(result);
        }
Exemplo n.º 3
0
        public ArgOption ParseOption(ArgTokens tokenParser)
        {
            var   token = tokenParser.Current();
            Match match = Regex.Match(token, @"(-.*)=\<(.*)\>");

            if (match.Success)
            {
                return(new ArgOption(match.Groups[1].ToString(), 1));
            }
            else
            {
                return(new ArgOption(token));
            }
        }
Exemplo n.º 4
0
        private IEnumerable <ArgPattern> ParseSequence(ArgTokens tokenParser)
        {
            var result = new List <ArgPattern>();

            while (!new[] { null, "]", ")", "|" }.Contains(tokenParser.Current()))
            {
                var atom = ParseAtom(tokenParser);

                if (tokenParser.Current() == "...")
                {
                    result.Add(new ArgOneOrMore(atom.ToArray()));
                    tokenParser.Move();
                    return(result);
                }
                result.AddRange(atom);
            }
            return(result);
        }
Exemplo n.º 5
0
        private IEnumerable <ArgPattern> ParseExpression(ArgTokens tokenParser)
        {
            var seq = ParseSequence(tokenParser);

            if (tokenParser.Current() != "|")
            {
                return(seq);
            }
            var result = new List <ArgPattern>();

            if (seq.Count() > 1)
            {
                result.Add(new ArgRequired(seq.ToArray()));
            }
            else
            {
                result.AddRange(seq);
            }
            while (tokenParser.Current() == "|")
            {
                tokenParser.Move();
                seq = ParseSequence(tokenParser);
                if (seq.Count() > 1)
                {
                    result.Add(new ArgRequired(seq.ToArray()));
                }
                else
                {
                    result.AddRange(seq);
                }
            }
            result = result.Distinct().ToList();
            if (result.Count > 1)
            {
                return new[] { new ArgEither(result.ToArray()) }
            }
            ;
            return(result);
        }