Lexer.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 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. int parens = 1;
  200. while (pos < s.Length) {
  201. var ch = s[pos];
  202. if (ch == '(') {
  203. ++parens;
  204. ++pos;
  205. continue;
  206. }
  207. if (ch == ')') {
  208. ++pos;
  209. if (--parens > 0)
  210. continue;
  211. var start = readToken.StartPosition;
  212. value = s.Substring (start, pos - start);
  213. return true;
  214. }
  215. // any OCTET except CTLs, but including LWS
  216. if (ch < 32 || ch > 126)
  217. break;
  218. ++pos;
  219. }
  220. value = null;
  221. return false;
  222. }
  223. public Token Scan (bool recognizeDash = false)
  224. {
  225. int start = pos;
  226. if (s == null)
  227. return new Token (Token.Type.Error, 0, 0);
  228. Token.Type ttype;
  229. if (pos >= s.Length) {
  230. ttype = Token.Type.End;
  231. } else {
  232. ttype = Token.Type.Error;
  233. start:
  234. char ch = s[pos++];
  235. switch (ch) {
  236. case ' ':
  237. case '\t':
  238. if (pos == s.Length) {
  239. ttype = Token.Type.End;
  240. break;
  241. }
  242. goto start;
  243. case '=':
  244. ttype = Token.Type.SeparatorEqual;
  245. break;
  246. case ';':
  247. ttype = Token.Type.SeparatorSemicolon;
  248. break;
  249. case '/':
  250. ttype = Token.Type.SeparatorSlash;
  251. break;
  252. case '-':
  253. if (recognizeDash) {
  254. ttype = Token.Type.SeparatorDash;
  255. break;
  256. }
  257. goto default;
  258. case ',':
  259. ttype = Token.Type.SeparatorComma;
  260. break;
  261. case '"':
  262. // Quoted string
  263. start = pos - 1;
  264. while (pos < s.Length) {
  265. ch = s [pos++];
  266. if (ch == '"') {
  267. ttype = Token.Type.QuotedString;
  268. break;
  269. }
  270. }
  271. break;
  272. case '(':
  273. start = pos - 1;
  274. ttype = Token.Type.OpenParens;
  275. break;
  276. default:
  277. if (ch < last_token_char && token_chars[ch]) {
  278. start = pos - 1;
  279. ttype = Token.Type.Token;
  280. while (pos < s.Length) {
  281. ch = s[pos];
  282. if (ch >= last_token_char || !token_chars[ch]) {
  283. break;
  284. }
  285. ++pos;
  286. }
  287. }
  288. break;
  289. }
  290. }
  291. return new Token (ttype, start, pos);
  292. }
  293. }
  294. }