using System; using sharp.extensions; namespace sharp.parser { public class CharGroup { public static readonly CharGroup digit = new CharGroup('0', '9'); public static readonly CharGroup zero = new CharGroup('0'); public static readonly CharGroup digit19 = new CharGroup('1', '9'); public static readonly CharGroup plusminus = new CharGroup(new char[] { '+', '-' }); public static readonly CharGroup minus = new CharGroup(new char[] { '+', '-' }); public static readonly CharGroup plus = new CharGroup(new char[] { '+' }); public static readonly CharGroup az = new CharGroup('a','z'); public static readonly CharGroup AZ = new CharGroup('A','Z'); public static readonly CharGroup aAzZ = az + AZ; public static readonly CharGroup LF = new CharGroup((char)0x0A); public static readonly CharGroup CR = new CharGroup((char)0x0D); public static readonly CharGroup HTAB = new CharGroup((char)0x09); public static readonly CharGroup WS = new CharGroup(new char[] { (char)0x09, (char)0x0A, (char)0x0B, (char)0x0C, (char)0x0D, (char)0x20}); public static readonly CharGroup hexdigits = new CharGroup('a','f') + new CharGroup('A','F') + digit; char[] chars; public CharGroup(char ch) { chars = new char[] { ch }; } public CharGroup(char[] chars) { this.chars = chars.Segment(0); } public CharGroup(char first,char last) { int l = (int)last - (int)first; this.chars = new char[l+1]; for (int n = 0; n <= l;n++){ this.chars[n] = (char)(first + n); } } public bool Contains(char ch){ foreach (char c in chars){ if (c == ch){ return true; } } return false; } public bool Intersects(CharGroup other){ foreach (char ch in chars){ if (other.Contains(ch)){ return true; } } return false; } public static CharGroup operator +(CharGroup cg1, CharGroup cg2) { return new CharGroup(cg1.chars.Combine(cg2.chars)); } public static CharGroup operator -(CharGroup cg1, CharGroup cg2) { return new CharGroup(cg1.chars.Remove(cg2.chars)); } public override string ToString() { return string.Format("[CharGroup '{0}']",new string(this.chars)); } } }