Example #1
0
        private NFA parseParenthesis(string str, ref int loc)
        {
            //str[loc] == '('
            loc++;
            NFA totalNFA = null;
            NFA builtNFA = null;

            while (str[loc] != ')')
            {
                if (loc >= str.Length)
                {
                    throw new RegexException("Regex has a ( without a matching )");
                }
                if (str[loc] == '|')
                {
                    if (totalNFA == null)
                    {
                        totalNFA = builtNFA;
                    }
                    else
                    {
                        totalNFA = totalNFA.or(builtNFA);
                    }
                    builtNFA = null;
                    loc++;
                }
                NFA nextNFA = parseOne(str, ref loc);
                if (builtNFA == null)
                {
                    builtNFA = nextNFA;
                }
                else
                {
                    builtNFA = builtNFA.conc(nextNFA);
                }
            }
            if (totalNFA == null)
            {
                totalNFA = builtNFA;
            }
            else
            {
                totalNFA = totalNFA.or(builtNFA);
            }
            loc++;
            //loc is first character after )
            return(totalNFA);
        }
Example #2
0
        private NFA parseAlternates(String str, ref int loc)
        {
            //str[loc] == '['
            loc++;
            bool exclusion = false;

            if (str[loc] == '^')
            {
                exclusion = true;
                loc++;
            }
            NFA builtNFA = null;

            while (str[loc] != ']')
            {
                if (loc >= str.Length)
                {
                    throw new RegexException("Regex has a [ without a matching ]");
                }
                NFA nextNFA;
                if (str[loc] == '\\')
                {
                    nextNFA = parseSpecial(str, ref loc);
                }
                else if (loc < str.Length - 1 && str[loc + 1] == '-')
                {
                    char start = str[loc];
                    loc += 2;
                    char           end   = str[loc];
                    HashSet <char> chars = new HashSet <char>();
                    for (char ch = start; ch <= end; ch++)
                    {
                        chars.Add(ch);
                    }
                    nextNFA = new NFA(chars);
                    loc++;
                }
                else
                {
                    nextNFA = new NFA(str.Substring(loc, 1));
                    loc++;
                }
                if (builtNFA == null)
                {
                    builtNFA = nextNFA;
                }
                else
                {
                    builtNFA = builtNFA.or(nextNFA);
                }
            }
            if (exclusion)
            {
                builtNFA = builtNFA.complement();
            }
            builtNFA = builtNFA.complement().or(new NFA(1).complement()).complement();
            loc++;
            //loc is first character after ]
            return(builtNFA);
        }