Esempio n. 1
0
 static void _ParseCharClassEscape(ParseContext pc, string cls, List <RegexCharsetEntry> result, ref RegexCharsetEntry next, ref bool readDash)
 {
     if (null != next)
     {
         result.Add(next);
         if (readDash)
         {
             result.Add(new RegexCharsetCharEntry('-'));
         }
         result.Add(new RegexCharsetCharEntry('-'));
     }
     pc.Advance();
     result.Add(new RegexCharsetClassEntry(cls));
     next     = null;
     readDash = false;
 }
Esempio n. 2
0
        static IList <RegexCharsetEntry> _ParseRanges(ParseContext pc)
        {
            pc.EnsureStarted();
            var result             = new List <RegexCharsetEntry>();
            RegexCharsetEntry next = null;
            bool readDash          = false;

            while (-1 != pc.Current && ']' != pc.Current)
            {
                switch (pc.Current)
                {
                case '[':                         // char class
                    if (null != next)
                    {
                        result.Add(next);
                        if (readDash)
                        {
                            result.Add(new RegexCharsetCharEntry('-'));
                        }
                        result.Add(new RegexCharsetCharEntry('-'));
                    }
                    pc.Advance();
                    pc.Expecting(':');
                    pc.Advance();
                    var l = pc.CaptureBuffer.Length;
                    pc.TryReadUntil(':', false);
                    var n = pc.GetCapture(l);
                    pc.Advance();
                    pc.Expecting(']');
                    pc.Advance();
                    result.Add(new RegexCharsetClassEntry(n));
                    readDash = false;
                    next     = null;
                    break;

                case '\\':
                    pc.Advance();
                    pc.Expecting();
                    switch (pc.Current)
                    {
                    case 'h':
                        _ParseCharClassEscape(pc, "space", result, ref next, ref readDash);
                        break;

                    case 'd':
                        _ParseCharClassEscape(pc, "digit", result, ref next, ref readDash);
                        break;

                    case 'D':
                        _ParseCharClassEscape(pc, "^digit", result, ref next, ref readDash);
                        break;

                    case 'l':
                        _ParseCharClassEscape(pc, "lower", result, ref next, ref readDash);
                        break;

                    case 's':
                        _ParseCharClassEscape(pc, "space", result, ref next, ref readDash);
                        break;

                    case 'S':
                        _ParseCharClassEscape(pc, "^space", result, ref next, ref readDash);
                        break;

                    case 'u':
                        _ParseCharClassEscape(pc, "upper", result, ref next, ref readDash);
                        break;

                    case 'w':
                        _ParseCharClassEscape(pc, "word", result, ref next, ref readDash);
                        break;

                    case 'W':
                        _ParseCharClassEscape(pc, "^word", result, ref next, ref readDash);
                        break;

                    default:
                        var ch = (char)_ParseRangeEscapePart(pc);
                        if (null == next)
                        {
                            next = new RegexCharsetCharEntry(ch);
                        }
                        else if (readDash)
                        {
                            result.Add(new RegexCharsetRangeEntry(((RegexCharsetCharEntry)next).Value, ch));
                            next     = null;
                            readDash = false;
                        }
                        else
                        {
                            result.Add(next);
                            next = new RegexCharsetCharEntry(ch);
                        }

                        break;
                    }

                    break;

                case '-':
                    pc.Advance();
                    if (null == next)
                    {
                        next     = new RegexCharsetCharEntry('-');
                        readDash = false;
                    }
                    else
                    {
                        if (readDash)
                        {
                            result.Add(next);
                        }
                        readDash = true;
                    }
                    break;

                default:
                    if (null == next)
                    {
                        next = new RegexCharsetCharEntry((char)pc.Current);
                    }
                    else
                    {
                        if (readDash)
                        {
                            result.Add(new RegexCharsetRangeEntry(((RegexCharsetCharEntry)next).Value, (char)pc.Current));
                            next     = null;
                            readDash = false;
                        }
                        else
                        {
                            result.Add(next);
                            next = new RegexCharsetCharEntry((char)pc.Current);
                        }
                    }
                    pc.Advance();
                    break;
                }
            }
            if (null != next)
            {
                result.Add(next);
                if (readDash)
                {
                    next = new RegexCharsetCharEntry('-');
                    result.Add(next);
                }
            }
            return(result);
        }