Lexer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. namespace System.Net.Http.Headers
  29. {
  30. struct Token
  31. {
  32. public enum Type
  33. {
  34. Error,
  35. End,
  36. Token,
  37. QuotedString,
  38. SeparatorEqual,
  39. SeparatorSemicolon,
  40. SeparatorSlash,
  41. }
  42. readonly Type type;
  43. public Token (Type type, int startPosition, int endPosition)
  44. : this ()
  45. {
  46. this.type = type;
  47. StartPosition = startPosition;
  48. EndPosition = endPosition;
  49. }
  50. public int StartPosition { get; private set; }
  51. public int EndPosition { get; private set; }
  52. public Type Kind {
  53. get {
  54. return type;
  55. }
  56. }
  57. public static implicit operator Token.Type (Token token)
  58. {
  59. return token.type;
  60. }
  61. public override string ToString ()
  62. {
  63. return type.ToString ();
  64. }
  65. }
  66. class Lexer
  67. {
  68. static readonly bool[] token_chars = {
  69. false, false, false, false, false, false, false, false, false, false, false, false, false, false,
  70. false, false, false, false, false, false, false, false, false, false, false, false, false, false,
  71. false, false, false, false, false, true, false, true, true, true, true, false, false, false, true,
  72. true, false, true, true, false, true, true, true, true, true, true, true, true, true, true, false,
  73. false, false, false, false, false, false, true, true, true, true, true, true, true, true, true,
  74. true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
  75. false, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true,
  76. true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
  77. false, true, false
  78. };
  79. static readonly int last_token_char = token_chars.Length;
  80. readonly string s;
  81. int pos;
  82. public Lexer (string stream)
  83. {
  84. this.s = stream;
  85. }
  86. public string GetStringValue (Token token)
  87. {
  88. return s.Substring (token.StartPosition, token.EndPosition - token.StartPosition);
  89. }
  90. public static bool IsValidToken (string input)
  91. {
  92. int i = 0;
  93. //
  94. // any CHAR except CTLs or separator
  95. //
  96. for (; i < input.Length; ++i) {
  97. char s = input[i];
  98. if (s > last_token_char || !token_chars[s])
  99. return false;
  100. }
  101. return i > 0;
  102. }
  103. public Token Scan ()
  104. {
  105. int start = pos;
  106. if (s == null)
  107. return new Token (Token.Type.Error, 0, 0);
  108. Token.Type ttype;
  109. if (pos >= s.Length) {
  110. ttype = Token.Type.End;
  111. } else {
  112. ttype = Token.Type.Error;
  113. start:
  114. char ch = s[pos++];
  115. switch (ch) {
  116. case ' ':
  117. case '\t':
  118. if (pos == s.Length) {
  119. ttype = Token.Type.End;
  120. break;
  121. }
  122. goto start;
  123. case '=':
  124. ttype = Token.Type.SeparatorEqual;
  125. break;
  126. case ';':
  127. ttype = Token.Type.SeparatorSemicolon;
  128. break;
  129. case '/':
  130. ttype = Token.Type.SeparatorSlash;
  131. break;
  132. case '"':
  133. // Quoted string
  134. start = pos - 1;
  135. while (pos < s.Length) {
  136. ch = s[pos];
  137. if (ch == '"') {
  138. ++pos;
  139. ttype = Token.Type.QuotedString;
  140. break;
  141. }
  142. // any OCTET except CTLs, but including LWS
  143. if (ch < 32 || ch > 126)
  144. break;
  145. ++pos;
  146. }
  147. break;
  148. case '(':
  149. break;
  150. default:
  151. if (ch <= last_token_char && token_chars[ch]) {
  152. start = pos - 1;
  153. ttype = Token.Type.Token;
  154. while (pos < s.Length) {
  155. ch = s[pos];
  156. if (ch > last_token_char || !token_chars[ch]) {
  157. break;
  158. }
  159. ++pos;
  160. }
  161. }
  162. break;
  163. }
  164. }
  165. return new Token (ttype, start, pos);
  166. }
  167. }
  168. }