Token.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #region Copyright and License
  2. //
  3. // Fizzler - CSS Selector Engine for Microsoft .NET Framework
  4. // Copyright (c) 2009 Atif Aziz, Colin Ramsay. All rights reserved.
  5. //
  6. // This library is free software; you can redistribute it and/or modify it under
  7. // the terms of the GNU Lesser General Public License as published by the Free
  8. // Software Foundation; either version 3 of the License, or (at your option)
  9. // any later version.
  10. //
  11. // This library is distributed in the hope that it will be useful, but WITHOUT
  12. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. // details.
  15. //
  16. // You should have received a copy of the GNU Lesser General Public License
  17. // along with this library; if not, write to the Free Software Foundation, Inc.,
  18. // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. //
  20. #endregion
  21. #pragma warning disable
  22. namespace Fizzler
  23. {
  24. using System;
  25. /// <summary>
  26. /// Represent a token and optionally any text associated with it.
  27. /// </summary>
  28. public struct Token : IEquatable<Token>
  29. {
  30. /// <summary>
  31. /// Gets the kind/type/class of the token.
  32. /// </summary>
  33. public TokenKind Kind { get; private set; }
  34. /// <summary>
  35. /// Gets text, if any, associated with the token.
  36. /// </summary>
  37. public string Text { get; private set; }
  38. private Token(TokenKind kind) : this(kind, null) { }
  39. private Token(TokenKind kind, string text) : this()
  40. {
  41. Kind = kind;
  42. Text = text;
  43. }
  44. /// <summary>
  45. /// Creates an end-of-input token.
  46. /// </summary>
  47. public static Token Eoi()
  48. {
  49. return new Token(TokenKind.Eoi);
  50. }
  51. private static readonly Token _star = Char('*');
  52. private static readonly Token _dot = Char('.');
  53. private static readonly Token _colon = Char(':');
  54. private static readonly Token _comma = Char(',');
  55. private static readonly Token _rightParenthesis = Char(')');
  56. private static readonly Token _equals = Char('=');
  57. private static readonly Token _pipe = Char('|');
  58. private static readonly Token _leftBracket = Char('[');
  59. private static readonly Token _rightBracket = Char(']');
  60. /// <summary>
  61. /// Creates a star token.
  62. /// </summary>
  63. public static Token Star()
  64. {
  65. return _star;
  66. }
  67. /// <summary>
  68. /// Creates a dot token.
  69. /// </summary>
  70. public static Token Dot()
  71. {
  72. return _dot;
  73. }
  74. /// <summary>
  75. /// Creates a colon token.
  76. /// </summary>
  77. public static Token Colon()
  78. {
  79. return _colon;
  80. }
  81. /// <summary>
  82. /// Creates a comma token.
  83. /// </summary>
  84. public static Token Comma()
  85. {
  86. return _comma;
  87. }
  88. /// <summary>
  89. /// Creates a right parenthesis token.
  90. /// </summary>
  91. public static Token RightParenthesis()
  92. {
  93. return _rightParenthesis;
  94. }
  95. /// <summary>
  96. /// Creates an equals token.
  97. /// </summary>
  98. public static Token Equals()
  99. {
  100. return _equals;
  101. }
  102. /// <summary>
  103. /// Creates a left bracket token.
  104. /// </summary>
  105. public static Token LeftBracket()
  106. {
  107. return _leftBracket;
  108. }
  109. /// <summary>
  110. /// Creates a right bracket token.
  111. /// </summary>
  112. public static Token RightBracket()
  113. {
  114. return _rightBracket;
  115. }
  116. /// <summary>
  117. /// Creates a pipe (vertical line) token.
  118. /// </summary>
  119. public static Token Pipe()
  120. {
  121. return _pipe;
  122. }
  123. /// <summary>
  124. /// Creates a plus token.
  125. /// </summary>
  126. public static Token Plus()
  127. {
  128. return new Token(TokenKind.Plus);
  129. }
  130. /// <summary>
  131. /// Creates a greater token.
  132. /// </summary>
  133. public static Token Greater()
  134. {
  135. return new Token(TokenKind.Greater);
  136. }
  137. /// <summary>
  138. /// Creates an includes token.
  139. /// </summary>
  140. public static Token Includes()
  141. {
  142. return new Token(TokenKind.Includes);
  143. }
  144. /// <summary>
  145. /// Creates a dash-match token.
  146. /// </summary>
  147. public static Token DashMatch()
  148. {
  149. return new Token(TokenKind.DashMatch);
  150. }
  151. /// <summary>
  152. /// Creates a prefix-match token.
  153. /// </summary>
  154. public static Token PrefixMatch()
  155. {
  156. return new Token(TokenKind.PrefixMatch);
  157. }
  158. /// <summary>
  159. /// Creates a suffix-match token.
  160. /// </summary>
  161. public static Token SuffixMatch()
  162. {
  163. return new Token(TokenKind.SuffixMatch);
  164. }
  165. /// <summary>
  166. /// Creates a substring-match token.
  167. /// </summary>
  168. public static Token SubstringMatch()
  169. {
  170. return new Token(TokenKind.SubstringMatch);
  171. }
  172. /// <summary>
  173. /// Creates a general sibling token.
  174. /// </summary>
  175. public static Token Tilde()
  176. {
  177. return new Token(TokenKind.Tilde);
  178. }
  179. /// <summary>
  180. /// Creates an identifier token.
  181. /// </summary>
  182. public static Token Ident(string text)
  183. {
  184. ValidateTextArgument(text);
  185. return new Token(TokenKind.Ident, text);
  186. }
  187. /// <summary>
  188. /// Creates an integer token.
  189. /// </summary>
  190. public static Token Integer(string text)
  191. {
  192. ValidateTextArgument(text);
  193. return new Token(TokenKind.Integer, text);
  194. }
  195. /// <summary>
  196. /// Creates a hash-name token.
  197. /// </summary>
  198. public static Token Hash(string text)
  199. {
  200. ValidateTextArgument(text);
  201. return new Token(TokenKind.Hash, text);
  202. }
  203. /// <summary>
  204. /// Creates a white-space token.
  205. /// </summary>
  206. public static Token WhiteSpace(string space)
  207. {
  208. ValidateTextArgument(space);
  209. return new Token(TokenKind.WhiteSpace, space);
  210. }
  211. /// <summary>
  212. /// Creates a string token.
  213. /// </summary>
  214. public static Token String(string text)
  215. {
  216. return new Token(TokenKind.String, text ?? string.Empty);
  217. }
  218. /// <summary>
  219. /// Creates a function token.
  220. /// </summary>
  221. public static Token Function(string text)
  222. {
  223. ValidateTextArgument(text);
  224. return new Token(TokenKind.Function, text);
  225. }
  226. /// <summary>
  227. /// Creates an arbitrary character token.
  228. /// </summary>
  229. public static Token Char(char ch)
  230. {
  231. return new Token(TokenKind.Char, ch.ToString());
  232. }
  233. /// <summary>
  234. /// Indicates whether this instance and a specified object are equal.
  235. /// </summary>
  236. public override bool Equals(object obj)
  237. {
  238. return obj != null && obj is Token && Equals((Token)obj);
  239. }
  240. /// <summary>
  241. /// Returns the hash code for this instance.
  242. /// </summary>
  243. public override int GetHashCode()
  244. {
  245. return Text == null ? Kind.GetHashCode() : Kind.GetHashCode() ^ Text.GetHashCode();
  246. }
  247. /// <summary>
  248. /// Indicates whether the current object is equal to another object of the same type.
  249. /// </summary>
  250. public bool Equals(Token other)
  251. {
  252. return Kind == other.Kind && Text == other.Text;
  253. }
  254. /// <summary>
  255. /// Gets a string representation of the token.
  256. /// </summary>
  257. public override string ToString()
  258. {
  259. return Text == null ? Kind.ToString() : Kind + ": " + Text;
  260. }
  261. /// <summary>
  262. /// Performs a logical comparison of the two tokens to determine
  263. /// whether they are equal.
  264. /// </summary>
  265. public static bool operator ==(Token a, Token b)
  266. {
  267. return a.Equals(b);
  268. }
  269. /// <summary>
  270. /// Performs a logical comparison of the two tokens to determine
  271. /// whether they are inequal.
  272. /// </summary>
  273. public static bool operator !=(Token a, Token b)
  274. {
  275. return !a.Equals(b);
  276. }
  277. private static void ValidateTextArgument(string text)
  278. {
  279. if (text == null) throw new ArgumentNullException("text");
  280. if (text.Length == 0) throw new ArgumentException(null, "text");
  281. }
  282. }
  283. }
  284. #pragma warning restore