static IDictionary <string, IList <CharRange> > _GetCharacterClasses() { var result = new Dictionary <string, IList <CharRange> >(); result.Add("letter", new List <CharRange>(CharRange.GetRanges(CharUtility.Letter))); result.Add("digit", new List <CharRange>(CharRange.GetRanges(CharUtility.Digit))); return(result); }
/// <summary> /// Creates an FA that will match any one of a set of a characters /// </summary> /// <param name="ranges">The set ranges of characters that will be matched</param> /// <param name="accept">The symbol to accept</param> /// <returns>An FA that will match the specified set</returns> public static CharFA <TAccept> Set(IEnumerable <CharRange> ranges, TAccept accept = default(TAccept)) { var result = new CharFA <TAccept>(); var final = new CharFA <TAccept>(true, accept); foreach (var ch in CharRange.ExpandRanges(ranges)) { result.Transitions.Add(ch, final); } return(result); }
public new CharDfaEntry[] ToArray(IList <TAccept> symbolTable = null) { var dfa = ToDfa(); var closure = dfa.FillClosure(); var symbolLookup = new ListDictionary <TAccept, int>(); if (null == symbolTable) { var i = 0; for (int jc = closure.Count, j = 0; j < jc; ++j) { var fa = closure[j]; if (fa.IsAccepting && !symbolLookup.ContainsKey(fa.AcceptSymbol)) { symbolLookup.Add(fa.AcceptSymbol, i); ++i; } } } else { for (int ic = symbolTable.Count, i = 0; i < ic; ++i) { if (null != symbolTable[i]) { symbolLookup.Add(symbolTable[i], i); } } } var result = new CharDfaEntry[closure.Count]; for (var i = 0; i < result.Length; i++) { var fa = closure[i]; var trgs = ((CharFA <TAccept>)fa).FillInputTransitionRangesGroupedByState(); var trns = new KeyValuePair <string, int> [trgs.Count]; var j = 0; foreach (var trg in trgs) { trns[j] = new KeyValuePair <string, int>( CharRange.ToPackedString(trg.Value), closure.IndexOf(trg.Key)); ++j; } result[i] = new CharDfaEntry( fa.IsAccepting ? symbolLookup[fa.AcceptSymbol] : -1, trns); } return(result); }
static IEnumerable <CharRange> _ParseRanges(IEnumerable <char> charRanges, bool normalize) { if (!normalize) { return(_ParseRanges(charRanges)); } else { var result = new List <CharRange>(_ParseRanges(charRanges)); CharRange.NormalizeRangeList(result); return(result); } }
/// <summary> /// Returns a <see cref="IDictionary{FA,IList{KeyValuePair{Char,Char}}}"/>, keyed by state, that contains all of the outgoing local input transitions, expressed as a series of ranges /// </summary> /// <param name="result">The <see cref="IDictionary{FA,IList{CharRange}}"/> to fill, or null to create one.</param> /// <returns>A <see cref="IDictionary{FA,IList{CharRange}}"/> containing the result of the query</returns> public IDictionary <FA <char, TAccept>, IList <CharRange> > FillInputTransitionRangesGroupedByState(IDictionary <FA <char, TAccept>, IList <CharRange> > result = null) { if (null == result) { result = new Dictionary <FA <char, TAccept>, IList <CharRange> >(); } // using the optimized dictionary we have little to do here. foreach (var trns in (IDictionary <FA <char, TAccept>, ICollection <char> >)Transitions) { result.Add(trns.Key, new List <CharRange>(CharRange.GetRanges(trns.Value))); } return(result); }
public static void _AppendRangeTo(StringBuilder builder, CharRange range) { _AppendRangeCharTo(builder, range.First); if (0 == range.Last.CompareTo(range.First)) { return; } if (range.Last == range.First + 1) // spit out 1 length ranges as two chars { _AppendRangeCharTo(builder, range.Last); return; } builder.Append('-'); _AppendRangeCharTo(builder, range.Last); }