Lexer.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 (s > last_token_char || !token_chars[s])
  169. return false;
  170. }
  171. return i > 0;
  172. }
  173. public void EatChar ()
  174. {
  175. ++pos;
  176. }
  177. public int PeekChar ()
  178. {
  179. return pos < s.Length ? s[pos] : -1;
  180. }
  181. public bool ScanCommentOptional (out string value)
  182. {
  183. Token t;
  184. if (ScanCommentOptional (out value, out t))
  185. return true;
  186. return t == Token.Type.End;
  187. }
  188. public bool ScanCommentOptional (out string value, out Token readToken)
  189. {
  190. readToken = Scan ();
  191. if (readToken != Token.Type.OpenParens) {
  192. value = null;
  193. return false;
  194. }
  195. while (pos < s.Length) {
  196. var ch = s[pos];
  197. if (ch == ')') {
  198. ++pos;
  199. var start = readToken.StartPosition;
  200. value = s.Substring (start, pos - start);
  201. return true;
  202. }
  203. // any OCTET except CTLs, but including LWS
  204. if (ch < 32 || ch > 126)
  205. break;
  206. ++pos;
  207. }
  208. value = null;
  209. return false;
  210. }
  211. public Token Scan (bool recognizeDash = false)
  212. {
  213. int start = pos;
  214. if (s == null)
  215. return new Token (Token.Type.Error, 0, 0);
  216. Token.Type ttype;
  217. if (pos >= s.Length) {
  218. ttype = Token.Type.End;
  219. } else {
  220. ttype = Token.Type.Error;
  221. start:
  222. char ch = s[pos++];
  223. switch (ch) {
  224. case ' ':
  225. case '\t':
  226. if (pos == s.Length) {
  227. ttype = Token.Type.End;
  228. break;
  229. }
  230. goto start;
  231. case '=':
  232. ttype = Token.Type.SeparatorEqual;
  233. break;
  234. case ';':
  235. ttype = Token.Type.SeparatorSemicolon;
  236. break;
  237. case '/':
  238. ttype = Token.Type.SeparatorSlash;
  239. break;
  240. case '-':
  241. if (recognizeDash) {
  242. ttype = Token.Type.SeparatorDash;
  243. break;
  244. }
  245. goto default;
  246. case ',':
  247. ttype = Token.Type.SeparatorComma;
  248. break;
  249. case '"':
  250. // Quoted string
  251. start = pos - 1;
  252. while (pos < s.Length) {
  253. ch = s[pos];
  254. if (ch == '"') {
  255. ++pos;
  256. ttype = Token.Type.QuotedString;
  257. break;
  258. }
  259. // any OCTET except CTLs, but including LWS
  260. if (ch < 32 || ch > 126)
  261. break;
  262. ++pos;
  263. }
  264. break;
  265. case '(':
  266. start = pos - 1;
  267. ttype = Token.Type.OpenParens;
  268. break;
  269. default:
  270. if (ch <= last_token_char && token_chars[ch]) {
  271. start = pos - 1;
  272. ttype = Token.Type.Token;
  273. while (pos < s.Length) {
  274. ch = s[pos];
  275. if (ch > last_token_char || !token_chars[ch]) {
  276. break;
  277. }
  278. ++pos;
  279. }
  280. }
  281. break;
  282. }
  283. }
  284. return new Token (ttype, start, pos);
  285. }
  286. }
  287. }