Beispiel #1
0
        public override MacroToken VisitConcatenate(MacroParser.ConcatenateContext context)
        {
            var firstParam    = context.SHARPSHARP(0).Symbol.Column > context.primarySource(0).stop.Column ? Visit(context.primarySource(0)) : new TextToken(String.Empty);
            int index         = firstParam.Text.Equals(String.Empty) ? 0 : 1;
            var concatenation = new ConcatenationToken(firstParam, Visit(context.primarySource(index)));

            for (var i = index + 1; i < context.primarySource().Length; i++)
            {
                concatenation = new ConcatenationToken(concatenation, Visit(context.primarySource(i)));
            }
            return(concatenation);
        }
Beispiel #2
0
            public static Token Parse(string s, CultureInfo cultureInfo)
            {
                if (string.IsNullOrEmpty(s))
                {
                    return(new NullToken());
                }

                int length = s.Length;

                int startingPos = 0;

                return(ParseUnion(ref startingPos, false).Optimize());

                Token ParseUnion(ref int pos, bool mustClose)
                {
                    UnionToken         unionToken         = new UnionToken();
                    ConcatenationToken concatenationToken = null;

                    int  startpos = pos;
                    bool closed   = false;

                    while (pos < length)
                    {
                        Token token = null;

                        if (TryMatchCharacter(ref pos, Token.GroupBegin))
                        {
                            // parse group
                            token = ParseUnion(ref pos, true);
                        }
                        else if (mustClose && TryMatchCharacter(ref pos, Token.GroupEnd))
                        {
                            closed = true;
                            break;
                        }
                        else if (TryMatchCharacter(ref pos, Token.GroupSeparator))
                        {
                            unionToken.Tokens.Add(concatenationToken as Token ?? new NullToken());
                            concatenationToken = null;
                            continue;
                        }
                        else if (TryMatchCharacter(ref pos, Token.RangeBegin))
                        {
                            // parse range
                            token = ParseRange(ref pos);
                        }
                        else if (TryParseCharacterClass(ref pos, out string characters))
                        {
                            token = new CharsetToken(characters);
                        }

                        int savedpos = pos;
                        if (TryParseQuantifier(ref pos, out int minlen, out int maxlen))
                        {
                            if (token == null)
                            {
                                throw new ArgumentException(string.Format(
                                                                CultureInfo.InvariantCulture,
                                                                Properties.Resources.E_InvalidRegex_TokenNotQuantifiable,
                                                                s,
                                                                savedpos));
                            }

                            token.MinCount = minlen;
                            token.MaxCount = maxlen;
                        }