Lexer.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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, true
  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 bool TryGetNumericValue (Token token, out long value)
  136. {
  137. return long.TryParse (GetStringValue (token), NumberStyles.None, CultureInfo.InvariantCulture, out value);
  138. }
  139. public TimeSpan? TryGetTimeSpanValue (Token token)
  140. {
  141. int seconds;
  142. if (TryGetNumericValue (token, out seconds)) {
  143. return TimeSpan.FromSeconds (seconds);
  144. }
  145. return null;
  146. }
  147. public bool TryGetDateValue (Token token, out DateTimeOffset value)
  148. {
  149. string text = token == Token.Type.QuotedString ?
  150. s.Substring (token.StartPosition + 1, token.EndPosition - token.StartPosition - 2) :
  151. GetStringValue (token);
  152. return TryGetDateValue (text, out value);
  153. }
  154. public static bool TryGetDateValue (string text, out DateTimeOffset value)
  155. {
  156. const DateTimeStyles DefaultStyles = DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces;
  157. return DateTimeOffset.TryParseExact (text, dt_formats, DateTimeFormatInfo.InvariantInfo, DefaultStyles, out value);
  158. }
  159. public bool TryGetDoubleValue (Token token, out double value)
  160. {
  161. string s = GetStringValue (token);
  162. return double.TryParse (s, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out value);
  163. }
  164. public static bool IsValidToken (string input)
  165. {
  166. int i = 0;
  167. //
  168. // any CHAR except CTLs or separator
  169. //
  170. for (; i < input.Length; ++i) {
  171. char s = input[i];
  172. if (!IsValidCharacter (s))
  173. return false;
  174. }
  175. return i > 0;
  176. }
  177. public static bool IsValidCharacter (char input)
  178. {
  179. return input < last_token_char && token_chars[input];
  180. }
  181. public void EatChar ()
  182. {
  183. ++pos;
  184. }
  185. public int PeekChar ()
  186. {
  187. return pos < s.Length ? s[pos] : -1;
  188. }
  189. public bool ScanCommentOptional (out string value)
  190. {
  191. Token t;
  192. if (ScanCommentOptional (out value, out t))
  193. return true;
  194. return t == Token.Type.End;
  195. }
  196. public bool ScanCommentOptional (out string value, out Token readToken)
  197. {
  198. readToken = Scan ();
  199. if (readToken != Token.Type.OpenParens) {
  200. value = null;
  201. return false;
  202. }
  203. int parens = 1;
  204. while (pos < s.Length) {
  205. var ch = s[pos];
  206. if (ch == '(') {
  207. ++parens;
  208. ++pos;
  209. continue;
  210. }
  211. if (ch == ')') {
  212. ++pos;
  213. if (--parens > 0)
  214. continue;
  215. var start = readToken.StartPosition;
  216. value = s.Substring (start, pos - start);
  217. return true;
  218. }
  219. // any OCTET except CTLs, but including LWS
  220. if (ch < 32 || ch > 126)
  221. break;
  222. ++pos;
  223. }
  224. value = null;
  225. return false;
  226. }
  227. public Token Scan (bool recognizeDash = false)
  228. {
  229. int start = pos;
  230. if (s == null)
  231. return new Token (Token.Type.Error, 0, 0);
  232. Token.Type ttype;
  233. if (pos >= s.Length) {
  234. ttype = Token.Type.End;
  235. } else {
  236. ttype = Token.Type.Error;
  237. start:
  238. char ch = s[pos++];
  239. switch (ch) {
  240. case ' ':
  241. case '\t':
  242. if (pos == s.Length) {
  243. ttype = Token.Type.End;
  244. break;
  245. }
  246. goto start;
  247. case '=':
  248. ttype = Token.Type.SeparatorEqual;
  249. break;
  250. case ';':
  251. ttype = Token.Type.SeparatorSemicolon;
  252. break;
  253. case '/':
  254. ttype = Token.Type.SeparatorSlash;
  255. break;
  256. case '-':
  257. if (recognizeDash) {
  258. ttype = Token.Type.SeparatorDash;
  259. break;
  260. }
  261. goto default;
  262. case ',':
  263. ttype = Token.Type.SeparatorComma;
  264. break;
  265. case '"':
  266. // Quoted string
  267. start = pos - 1;
  268. while (pos < s.Length) {
  269. ch = s [pos++];
  270. //
  271. // The backslash character ("\") MAY be used as a single-character
  272. // quoting mechanism only within quoted-string
  273. //
  274. if (ch == '\\') {
  275. if (pos + 1 < s.Length) {
  276. ++pos;
  277. continue;
  278. }
  279. break;
  280. }
  281. if (ch == '"') {
  282. ttype = Token.Type.QuotedString;
  283. break;
  284. }
  285. }
  286. break;
  287. case '(':
  288. start = pos - 1;
  289. ttype = Token.Type.OpenParens;
  290. break;
  291. default:
  292. if (ch < last_token_char && token_chars[ch]) {
  293. start = pos - 1;
  294. ttype = Token.Type.Token;
  295. while (pos < s.Length) {
  296. ch = s[pos];
  297. if (ch >= last_token_char || !token_chars[ch]) {
  298. break;
  299. }
  300. ++pos;
  301. }
  302. }
  303. break;
  304. }
  305. }
  306. return new Token (ttype, start, pos);
  307. }
  308. }
  309. }