Lexer.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. public static readonly Token Empty = new Token (Type.Token, 0, 0);
  47. readonly Type type;
  48. public Token (Type type, int startPosition, int endPosition)
  49. : this ()
  50. {
  51. this.type = type;
  52. StartPosition = startPosition;
  53. EndPosition = endPosition;
  54. }
  55. public int StartPosition { get; private set; }
  56. public int EndPosition { get; private set; }
  57. public Type Kind {
  58. get {
  59. return type;
  60. }
  61. }
  62. public static implicit operator Token.Type (Token token)
  63. {
  64. return token.type;
  65. }
  66. public override string ToString ()
  67. {
  68. return type.ToString ();
  69. }
  70. }
  71. class Lexer
  72. {
  73. // any CHAR except CTLs or separators
  74. static readonly bool[] token_chars = {
  75. /*0*/ false, false, false, false, false, false, false, false, false, false,
  76. /*10*/ false, false, false, false, false, false, false, false, false, false,
  77. /*20*/ false, false, false, false, false, false, false, false, false, false,
  78. /*30*/ false, false, false, true, false, true, true, true, true, true,
  79. /*40*/ false, false, true, true, false, true, true, false, true, true,
  80. /*50*/ true, true, true, true, true, true, true, true, false, false,
  81. /*60*/ false, false, false, false, false, true, true, true, true, true,
  82. /*70*/ true, true, true, true, true, true, true, true, true, true,
  83. /*80*/ true, true, true, true, true, true, true, true, true, true,
  84. /*90*/ true, false, false, false, true, true, true, true, true, true,
  85. /*100*/ true, true, true, true, true, true, true, true, true, true,
  86. /*110*/ true, true, true, true, true, true, true, true, true, true,
  87. /*120*/ true, true, true, false, true, false
  88. };
  89. static readonly int last_token_char = token_chars.Length;
  90. static readonly string[] dt_formats = new[] {
  91. "r",
  92. "dddd, dd'-'MMM'-'yy HH:mm:ss 'GMT'",
  93. "ddd MMM d HH:mm:ss yyyy",
  94. "d MMM yy H:m:s",
  95. "ddd, d MMM yyyy H:m:s zzz"
  96. };
  97. readonly string s;
  98. int pos;
  99. public Lexer (string stream)
  100. {
  101. this.s = stream;
  102. }
  103. public int Position {
  104. get {
  105. return pos;
  106. }
  107. set {
  108. pos = value;
  109. }
  110. }
  111. public string GetStringValue (Token token)
  112. {
  113. return s.Substring (token.StartPosition, token.EndPosition - token.StartPosition);
  114. }
  115. public string GetStringValue (Token start, Token end)
  116. {
  117. return s.Substring (start.StartPosition, end.EndPosition - start.StartPosition);
  118. }
  119. public string GetQuotedStringValue (Token start)
  120. {
  121. return s.Substring (start.StartPosition + 1, start.EndPosition - start.StartPosition - 2);
  122. }
  123. public string GetRemainingStringValue (int position)
  124. {
  125. return position > s.Length ? null : s.Substring (position);
  126. }
  127. public bool IsStarStringValue (Token token)
  128. {
  129. return (token.EndPosition - token.StartPosition) == 1 && s[token.StartPosition] == '*';
  130. }
  131. public bool TryGetNumericValue (Token token, out int value)
  132. {
  133. return int.TryParse (GetStringValue (token), NumberStyles.None, CultureInfo.InvariantCulture, out value);
  134. }
  135. public TimeSpan? TryGetTimeSpanValue (Token token)
  136. {
  137. int seconds;
  138. if (TryGetNumericValue (token, out seconds)) {
  139. return TimeSpan.FromSeconds (seconds);
  140. }
  141. return null;
  142. }
  143. public bool TryGetDateValue (Token token, out DateTimeOffset value)
  144. {
  145. string text = token == Token.Type.QuotedString ?
  146. s.Substring (token.StartPosition + 1, token.EndPosition - token.StartPosition - 2) :
  147. GetStringValue (token);
  148. return TryGetDateValue (text, out value);
  149. }
  150. public static bool TryGetDateValue (string text, out DateTimeOffset value)
  151. {
  152. const DateTimeStyles DefaultStyles = DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces;
  153. return DateTimeOffset.TryParseExact (text, dt_formats, DateTimeFormatInfo.InvariantInfo, DefaultStyles, out value);
  154. }
  155. public bool TryGetDoubleValue (Token token, out double value)
  156. {
  157. string s = GetStringValue (token);
  158. return double.TryParse (s, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out value);
  159. }
  160. public static bool IsValidToken (string input)
  161. {
  162. int i = 0;
  163. //
  164. // any CHAR except CTLs or separator
  165. //
  166. for (; i < input.Length; ++i) {
  167. char s = input[i];
  168. if (!IsValidCharacter (s))
  169. return false;
  170. }
  171. return i > 0;
  172. }
  173. public static bool IsValidCharacter (char input)
  174. {
  175. return input <= last_token_char && token_chars[input];
  176. }
  177. public void EatChar ()
  178. {
  179. ++pos;
  180. }
  181. public int PeekChar ()
  182. {
  183. return pos < s.Length ? s[pos] : -1;
  184. }
  185. public bool ScanCommentOptional (out string value)
  186. {
  187. Token t;
  188. if (ScanCommentOptional (out value, out t))
  189. return true;
  190. return t == Token.Type.End;
  191. }
  192. public bool ScanCommentOptional (out string value, out Token readToken)
  193. {
  194. readToken = Scan ();
  195. if (readToken != Token.Type.OpenParens) {
  196. value = null;
  197. return false;
  198. }
  199. while (pos < s.Length) {
  200. var ch = s[pos];
  201. if (ch == ')') {
  202. ++pos;
  203. var start = readToken.StartPosition;
  204. value = s.Substring (start, pos - start);
  205. return true;
  206. }
  207. // any OCTET except CTLs, but including LWS
  208. if (ch < 32 || ch > 126)
  209. break;
  210. ++pos;
  211. }
  212. value = null;
  213. return false;
  214. }
  215. public Token Scan (bool recognizeDash = false)
  216. {
  217. int start = pos;
  218. if (s == null)
  219. return new Token (Token.Type.Error, 0, 0);
  220. Token.Type ttype;
  221. if (pos >= s.Length) {
  222. ttype = Token.Type.End;
  223. } else {
  224. ttype = Token.Type.Error;
  225. start:
  226. char ch = s[pos++];
  227. switch (ch) {
  228. case ' ':
  229. case '\t':
  230. if (pos == s.Length) {
  231. ttype = Token.Type.End;
  232. break;
  233. }
  234. goto start;
  235. case '=':
  236. ttype = Token.Type.SeparatorEqual;
  237. break;
  238. case ';':
  239. ttype = Token.Type.SeparatorSemicolon;
  240. break;
  241. case '/':
  242. ttype = Token.Type.SeparatorSlash;
  243. break;
  244. case '-':
  245. if (recognizeDash) {
  246. ttype = Token.Type.SeparatorDash;
  247. break;
  248. }
  249. goto default;
  250. case ',':
  251. ttype = Token.Type.SeparatorComma;
  252. break;
  253. case '"':
  254. // Quoted string
  255. start = pos - 1;
  256. while (pos < s.Length) {
  257. ch = s[pos];
  258. if (ch == '"') {
  259. ++pos;
  260. ttype = Token.Type.QuotedString;
  261. break;
  262. }
  263. // any OCTET except CTLs, but including LWS
  264. if (ch < 32 || ch > 126)
  265. break;
  266. ++pos;
  267. }
  268. break;
  269. case '(':
  270. start = pos - 1;
  271. ttype = Token.Type.OpenParens;
  272. break;
  273. default:
  274. if (ch <= last_token_char && token_chars[ch]) {
  275. start = pos - 1;
  276. ttype = Token.Type.Token;
  277. while (pos < s.Length) {
  278. ch = s[pos];
  279. if (ch > last_token_char || !token_chars[ch]) {
  280. break;
  281. }
  282. ++pos;
  283. }
  284. }
  285. break;
  286. }
  287. }
  288. return new Token (ttype, start, pos);
  289. }
  290. }
  291. }