示例#1
0
        private void Parse(GitCmdTokens tokens)
        {
            m_exe = tokens.Dequeue(GitCmdTokenType.Literal).Value;

            var token = tokens.Dequeue();

            if (token.Type == GitCmdTokenType.EndOfStram)
            {
                return;
            }

            if (token.Type != GitCmdTokenType.Literal)
            {
                tokens.ParseError();
            }
            m_name = token.Value;

            while (tokens.Any())
            {
                token = tokens.Dequeue();

                if (token.Type == GitCmdTokenType.Dash)
                {
                    ParseShortSwitch(tokens);
                }

                else if (token.Type == GitCmdTokenType.DoubleDash)
                {
                    ParseLongSwitch(tokens);
                }

                else if (token.Type == GitCmdTokenType.Literal)
                {
                    m_arguments.Add(token.Value);
                }

                else if (token.Type == GitCmdTokenType.EndOfStram)
                {
                    break;
                }

                else
                {
                    tokens.ParseError();
                }
            }
        }
示例#2
0
        private static IEnumerable <GitCmdToken> GenerateTokens(string commandLine)
        {
            var match = Regex.Match(commandLine, TokenPattern);

            if (!match.Success)
            {
                throw new GitCmdException(
                          $"Failed to parse command line '{commandLine}' into tokens using pattern: {TokenPattern}");
            }

            var tokens = new GitCmdTokens(
                from pair in TokenPatterns
                let token = pair.Key
                            from Capture capture in match.Groups[$"{token}"].Captures
                            orderby capture.Index
                            select new GitCmdToken(token, capture.Value, capture.Index)
                );

            // unescape quoted string into literal
            while (tokens.Any())
            {
                var token = tokens.Dequeue();
                var type  = token.Type;

                if (type == GitCmdTokenType.WhiteSpace)
                {
                    continue;
                }

                if (type == GitCmdTokenType.Quote ||
                    type == GitCmdTokenType.Backslash ||
                    type == GitCmdTokenType.Literal)
                {
                    yield return(ParseLiteral(tokens));
                }

                else
                {
                    yield return(token);
                }
            }
        }