Lexer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // Lexer.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Globalization;
  29. namespace System.Net.Http.Headers
  30. {
  31. struct Token
  32. {
  33. public enum Type
  34. {
  35. Error,
  36. End,
  37. Token,
  38. QuotedString,
  39. SeparatorEqual,
  40. SeparatorSemicolon,
  41. SeparatorSlash,
  42. SeparatorDash,
  43. SeparatorComma,
  44. OpenParens,
  45. }
  46. readonly Type type;
  47. public Token (Type type, int startPosition, int endPosition)
  48. : this ()
  49. {
  50. this.type = type;
  51. StartPosition = startPosition;
  52. EndPosition = endPosition;
  53. }
  54. public int StartPosition { get; private set; }
  55. public int EndPosition { get; private set; }
  56. public Type Kind {
  57. get {
  58. return type;
  59. }
  60. }
  61. public static implicit operator Token.Type (Token token)
  62. {
  63. return token.type;
  64. }
  65. public override string ToString ()
  66. {
  67. return type.ToString ();
  68. }
  69. }
  70. class Lexer
  71. {
  72. // any CHAR except CTLs or separators
  73. static readonly bool[] token_chars = {
  74. /*0*/ false, false, false, false, false, false, false, false, false, false,
  75. /*10*/ false, false, false, false, false, false, false, false, false, false,
  76. /*20*/ false, false, false, false, false, false, false, false, false, false,
  77. /*30*/ false, false, false, true, false, true, true, true, true, true,
  78. /*40*/ false, false, true, true, false, true, true, false, true, true,
  79. /*50*/ true, true, true, true, true, true, true, true, false, false,
  80. /*60*/ false, false, false, false, false, true, true, true, true, true,
  81. /*70*/ true, true, true, true, true, true, true, true, true, true,
  82. /*80*/ true, true, true, true, true, true, true, true, true, true,
  83. /*90*/ true, false, false, false, true, true, true, true, true, true,
  84. /*100*/ true, true, true, true, true, true, true, true, true, true,
  85. /*110*/ true, true, true, true, true, true, true, true, true, true,
  86. /*120*/ true, true, true, false, true, false
  87. };
  88. static readonly int last_token_char = token_chars.Length;
  89. static readonly string[] dt_formats = new[] {
  90. "r",
  91. "dddd, dd'-'MMM'-'yy HH:mm:ss 'GMT'",
  92. "ddd MMM d HH:mm:ss yyyy",
  93. "d MMM yy H:m:s",
  94. "ddd, d MMM yyyy H:m:s zzz"
  95. };
  96. readonly string s;
  97. int pos;
  98. public Lexer (string stream)
  99. {
  100. this.s = stream;
  101. }
  102. public string GetStringValue (Token token)
  103. {
  104. return s.Substring (token.StartPosition, token.EndPosition - token.StartPosition);
  105. }
  106. public string GetStringValue (Token start, Token end)
  107. {
  108. return s.Substring (start.StartPosition, end.EndPosition - start.StartPosition);
  109. }
  110. public string GetQuotedStringValue (Token start)
  111. {
  112. return s.Substring (start.StartPosition + 1, start.EndPosition - start.StartPosition - 2);
  113. }
  114. public string GetRemainingStringValue (int position)
  115. {
  116. return position > s.Length ? null : s.Substring (position);
  117. }
  118. public bool IsStarStringValue (Token token)
  119. {
  120. return (token.EndPosition - token.StartPosition) == 1 && s[token.StartPosition] == '*';
  121. }
  122. public bool TryGetNumericValue (Token token, out int value)
  123. {
  124. return int.TryParse (GetStringValue (token), NumberStyles.None, CultureInfo.InvariantCulture, out value);
  125. }
  126. public TimeSpan? TryGetTimeSpanValue (Token token)
  127. {
  128. int seconds;
  129. if (TryGetNumericValue (token, out seconds)) {
  130. return TimeSpan.FromSeconds (seconds);
  131. }
  132. return null;
  133. }
  134. public bool TryGetDateValue (Token token, out DateTimeOffset value)
  135. {
  136. string text = token == Token.Type.QuotedString ?
  137. s.Substring (token.StartPosition + 1, token.EndPosition - token.StartPosition - 2) :
  138. GetStringValue (token);
  139. return TryGetDateValue (text, out value);
  140. }
  141. public static bool TryGetDateValue (string text, out DateTimeOffset value)
  142. {
  143. const DateTimeStyles DefaultStyles = DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces;
  144. return DateTimeOffset.TryParseExact (text, dt_formats, DateTimeFormatInfo.InvariantInfo, DefaultStyles, out value);
  145. }
  146. public bool TryGetDoubleValue (Token token, out double value)
  147. {
  148. string s = GetStringValue (token);
  149. return double.TryParse (s, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out value);
  150. }
  151. public static bool IsValidToken (string input)
  152. {
  153. int i = 0;
  154. //
  155. // any CHAR except CTLs or separator
  156. //
  157. for (; i < input.Length; ++i) {
  158. char s = input[i];
  159. if (s > last_token_char || !token_chars[s])
  160. return false;
  161. }
  162. return i > 0;
  163. }
  164. public void EatChar ()
  165. {
  166. ++pos;
  167. }
  168. public int PeekChar ()
  169. {
  170. return pos < s.Length ? s[pos] : -1;
  171. }
  172. public bool ScanCommentOptional (out string value)
  173. {
  174. Token t;
  175. if (ScanCommentOptional (out value, out t))
  176. return true;
  177. return t == Token.Type.End;
  178. }
  179. public bool ScanCommentOptional (out string value, out Token readToken)
  180. {
  181. readToken = Scan ();
  182. if (readToken != Token.Type.OpenParens) {
  183. value = null;
  184. return false;
  185. }
  186. while (pos < s.Length) {
  187. var ch = s[pos];
  188. if (ch == ')') {
  189. ++pos;
  190. var start = readToken.StartPosition;
  191. value = s.Substring (start, pos - start);
  192. return true;
  193. }
  194. // any OCTET except CTLs, but including LWS
  195. if (ch < 32 || ch > 126)
  196. break;
  197. ++pos;
  198. }
  199. value = null;
  200. return false;
  201. }
  202. public Token Scan ()
  203. {
  204. int start = pos;
  205. if (s == null)
  206. return new Token (Token.Type.Error, 0, 0);
  207. Token.Type ttype;
  208. if (pos >= s.Length) {
  209. ttype = Token.Type.End;
  210. } else {
  211. ttype = Token.Type.Error;
  212. start:
  213. char ch = s[pos++];
  214. switch (ch) {
  215. case ' ':
  216. case '\t':
  217. if (pos == s.Length) {
  218. ttype = Token.Type.End;
  219. break;
  220. }
  221. goto start;
  222. case '=':
  223. ttype = Token.Type.SeparatorEqual;
  224. break;
  225. case ';':
  226. ttype = Token.Type.SeparatorSemicolon;
  227. break;
  228. case '/':
  229. ttype = Token.Type.SeparatorSlash;
  230. break;
  231. case '-':
  232. ttype = Token.Type.SeparatorDash;
  233. break;
  234. case ',':
  235. ttype = Token.Type.SeparatorComma;
  236. break;
  237. case '"':
  238. // Quoted string
  239. start = pos - 1;
  240. while (pos < s.Length) {
  241. ch = s[pos];
  242. if (ch == '"') {
  243. ++pos;
  244. ttype = Token.Type.QuotedString;
  245. break;
  246. }
  247. // any OCTET except CTLs, but including LWS
  248. if (ch < 32 || ch > 126)
  249. break;
  250. ++pos;
  251. }
  252. break;
  253. case '(':
  254. start = pos - 1;
  255. ttype = Token.Type.OpenParens;
  256. break;
  257. default:
  258. if (ch <= last_token_char && token_chars[ch]) {
  259. start = pos - 1;
  260. ttype = Token.Type.Token;
  261. while (pos < s.Length) {
  262. ch = s[pos];
  263. if (ch > last_token_char || !token_chars[ch]) {
  264. break;
  265. }
  266. ++pos;
  267. }
  268. }
  269. break;
  270. }
  271. }
  272. return new Token (ttype, start, pos);
  273. }
  274. }
  275. }