Exemplo n.º 1
0
        private static RTree ParseValueReference(RevgexStringReader inp, GroupSet groups)
        {
            inp.Read(); // '$'
            var c = inp.Peek();

            if (c >= '0' && c <= '9')
            {
                var groupId = inp.Read() - '0';
                c = inp.Peek();
                if (c >= '0' && c <= '9')
                {
                    groupId = groupId * 10 + (inp.Read() - '0');
                }
                if (groupId == 0)
                {
                    throw Error(inp, "Value backreference to the entire patternt (zero-th group) is not allowed.");
                }
                if (groups.Get(groupId) == null)
                {
                    throw Error(inp, "Invalid group backreference.");
                }
                return(new RGroupValueRef(groupId, LookupQuantifier(inp)));
            }
            switch (c)
            {
            case -1:
                throw Error(inp, "Incomplete group value backreference.");

            case ':':
                var groupName = ParseGroupName(inp);
                if (groups.Get(groupName) == null)
                {
                    throw Error(inp, "Invalid named group value backreference.");
                }
                return(new RNamedGroupValueRef(groupName, LookupQuantifier(inp)));

            case '(':
                inp.Read();
                var groupId = 0;
                c = inp.Peek();
                do
                {
                    if (c < '0' || c > '9')
                    {
                        throw Error(inp, "Invalid group value backreference.");
                    }
                    groupId = groupId * 10 + (inp.Read() - '0');
                } while ((c = inp.Peek()) != ')');
                inp.Read();     // ')'
                if (groups.Get(groupId) == null)
                {
                    throw Error(inp, "Invalid group value backreference.");
                }
                return(new RGroupValueRef(groupId, LookupQuantifier(inp)));

            default: throw Error(inp, "Invalid group value backreference.");
            }
        }
Exemplo n.º 2
0
 public Revgex(string str, bool ignoreLineEndings = false, bool ignoreWhitespace = false, bool allowPredefinedGroups = true)
 {
     groups = str.Length == 0
         ? new GroupSet()
         : Parser.Parse(str.Replace("\r\n", "\n").Replace('\r', '\n'), ignoreLineEndings, ignoreWhitespace, allowPredefinedGroups);
     tree = groups.Get(0) ?? REmpty.Instance;
 }
Exemplo n.º 3
0
        private static RTree ParseEscapeSequence(RevgexStringReader inp, GroupSet groups)
        {
            inp.Read(); // '\'
            var c = inp.Peek();

            if (c >= '0' && c <= '9')
            {
                var groupId = inp.Read() - '0';
                c = inp.Peek();
                if (c >= '0' && c <= '9')
                {
                    groupId = groupId * 10 + (inp.Read() - '0');
                }
                if (groupId == 0)
                {
                    throw Error(inp, "Backreference to the entire patternt (zero-th group) is not allowed.");
                }
                if (groups.Get(groupId) == null)
                {
                    throw Error(inp, "Invalid group backreference.");
                }
                return(new RGroupRef(groupId, LookupQuantifier(inp)));
            }
            switch (c)
            {
            case -1:
                throw Error(inp, "Incomplete escape sequence.");

            case ':':
                var groupName = ParseGroupName(inp);
                if (groups.Get(groupName) == null)
                {
                    throw Error(inp, "Invalid named group backreference.");
                }
                return(new RNamedGroupRef(groupName, LookupQuantifier(inp)));

            case '(':
                inp.Read();
                var groupId = 0;
                c = inp.Peek();
                do
                {
                    if (c < '0' || c > '9')
                    {
                        throw Error(inp, "Invalid group backreference.");
                    }
                    groupId = groupId * 10 + (inp.Read() - '0');
                } while ((c = inp.Peek()) != ')');
                inp.Read();     // ')'
                if (groups.Get(groupId) == null)
                {
                    throw Error(inp, "Invalid group backreference.");
                }
                return(new RGroupRef(groupId, LookupQuantifier(inp)));

            case 'w':
                inp.Read();
                return(RChar.w(LookupQuantifier(inp)));

            case 'W':
                inp.Read();
                return(RChar.W(LookupQuantifier(inp)));

            case 's':
                inp.Read();
                return(RChar.s(LookupQuantifier(inp)));

            case 'S':
                inp.Read();
                return(RChar.S(LookupQuantifier(inp)));

            case 'd':
                inp.Read();
                return(RChar.d(LookupQuantifier(inp)));

            case 'D':
                inp.Read();
                return(RChar.D(LookupQuantifier(inp)));

            default:
                return(new RFixedString(inp.Read().ToString(), LookupQuantifier(inp)));
            }
        }