/// <summary> /// Creates a state machine representing this expression /// </summary> /// <typeparam name="TAccept">The type of accept symbol to use for this expression</typeparam> /// <param name="accept">The accept symbol to use for this expression</param> /// <returns>A new <see cref="CharFA{TAccept}"/> finite state machine representing this expression</returns> public override CharFA <TAccept> ToFA <TAccept>(TAccept accept) { var ranges = new List <CharRange>(); for (int ic = Entries.Count, i = 0; i < ic; ++i) { var entry = Entries[i]; var crc = entry as RegexCharsetCharEntry; if (null != crc) { ranges.Add(new CharRange(crc.Value, crc.Value)); } var crr = entry as RegexCharsetRangeEntry; if (null != crr) { ranges.Add(new CharRange(crr.First, crr.Last)); } var crcl = entry as RegexCharsetClassEntry; if (null != crcl) { ranges.AddRange(CharFA <TAccept> .CharacterClasses[crcl.Name]); } } if (HasNegatedRanges) { return(CharFA <TAccept> .Set(CharRange.NotRanges(ranges), accept)); } return(CharFA <TAccept> .Set(ranges, accept)); }
// build the character classes static IDictionary <string, IList <CharRange> > _GetCharacterClasses() { var result = new Dictionary <string, IList <CharRange> >(); result.Add("alnum", new List <CharRange>( new CharRange[] { new CharRange('A', 'Z'), new CharRange('a', 'z'), new CharRange('0', '9') })); result.Add("alpha", new List <CharRange>( new CharRange[] { new CharRange('A', 'Z'), new CharRange('a', 'z') })); result.Add("ascii", new List <CharRange>( new CharRange[] { new CharRange('\0', '\x7F') })); result.Add("blank", new List <CharRange>( new CharRange[] { new CharRange(' ', ' '), new CharRange('\t', '\t') })); result.Add("cntrl", new List <CharRange>( new CharRange[] { new CharRange('\0', '\x1F'), new CharRange('\x7F', '\x7F') })); result.Add("digit", new List <CharRange>( new CharRange[] { new CharRange('0', '9') })); result.Add("^digit", new List <CharRange>(CharRange.NotRanges(result["digit"]))); result.Add("graph", new List <CharRange>( new CharRange[] { new CharRange('\x21', '\x7E') })); result.Add("lower", new List <CharRange>( new CharRange[] { new CharRange('a', 'z') })); result.Add("print", new List <CharRange>( new CharRange[] { new CharRange('\x20', '\x7E') })); // [!"\#$%&'()*+,\-./:;<=>?@\[\\\]^_`{|}~] result.Add("punct", new List <CharRange>( CharRange.GetRanges("!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~") )); //[ \t\r\n\v\f] result.Add("space", new List <CharRange>( CharRange.GetRanges(" \t\r\n\v\f") )); result.Add("^space", new List <CharRange>(CharRange.NotRanges(result["space"]))); result.Add("upper", new List <CharRange>( new CharRange[] { new CharRange('A', 'Z') })); result.Add("word", new List <CharRange>( new CharRange[] { new CharRange('0', '9'), new CharRange('A', 'Z'), new CharRange('_', '_'), new CharRange('a', 'z') })); result.Add("^word", new List <CharRange>(CharRange.NotRanges(result["word"]))); result.Add("xdigit", new List <CharRange>( new CharRange[] { new CharRange('0', '9'), new CharRange('A', 'F'), new CharRange('a', 'f') })); return(result); }