AspTokenizer.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //
  2. // System.Web.Compilation.AspTokenizer
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Text;
  33. using System.Security.Cryptography;
  34. namespace System.Web.Compilation
  35. {
  36. class Token
  37. {
  38. public const int EOF = 0x0200000;
  39. public const int IDENTIFIER = 0x0200001;
  40. public const int DIRECTIVE = 0x0200002;
  41. public const int ATTVALUE = 0x0200003;
  42. public const int TEXT = 0x0200004;
  43. public const int DOUBLEDASH = 0x0200005;
  44. public const int CLOSING = 0x0200006;
  45. }
  46. class AspTokenizer
  47. {
  48. #if NET_2_0
  49. const int CHECKSUM_BUF_SIZE = 8192;
  50. #endif
  51. class PutBackItem
  52. {
  53. public readonly string Value;
  54. public readonly int Position;
  55. public readonly int CurrentToken;
  56. public PutBackItem (string value, int position, int currentToken)
  57. {
  58. Value = value;
  59. Position = position;
  60. CurrentToken = currentToken;
  61. }
  62. }
  63. static char [] lfcr = new char [] { '\n', '\r' };
  64. TextReader sr;
  65. int current_token;
  66. StringBuilder sb, odds;
  67. int col, line;
  68. int begcol, begline;
  69. int position;
  70. bool inTag;
  71. bool expectAttrValue;
  72. bool alternatingQuotes;
  73. bool hasPutBack;
  74. bool verbatim;
  75. bool have_value;
  76. bool have_unget;
  77. int unget_value;
  78. string val;
  79. Stack putBackBuffer;
  80. #if NET_2_0
  81. MD5 checksum;
  82. char[] checksum_buf = new char [CHECKSUM_BUF_SIZE];
  83. int checksum_buf_pos = -1;
  84. public MD5 Checksum {
  85. get { return checksum; }
  86. }
  87. #endif
  88. public AspTokenizer (TextReader reader)
  89. {
  90. this.sr = reader;
  91. sb = new StringBuilder ();
  92. odds= new StringBuilder();
  93. col = line = 1;
  94. hasPutBack = inTag = false;
  95. }
  96. public bool Verbatim
  97. {
  98. get { return verbatim; }
  99. set { verbatim = value; }
  100. }
  101. public void put_back ()
  102. {
  103. if (hasPutBack && !inTag)
  104. throw new HttpException ("put_back called twice!");
  105. hasPutBack = true;
  106. if (putBackBuffer == null)
  107. putBackBuffer = new Stack ();
  108. string val = Value;
  109. putBackBuffer.Push (new PutBackItem (val, position, current_token));
  110. position -= val.Length;
  111. }
  112. public int get_token ()
  113. {
  114. if (hasPutBack) {
  115. PutBackItem pbi = putBackBuffer.Pop () as PutBackItem;
  116. hasPutBack = putBackBuffer.Count > 0;
  117. position = pbi.Position;
  118. have_value = false;
  119. val = null;
  120. sb = new StringBuilder (pbi.Value);
  121. current_token = pbi.CurrentToken;
  122. return current_token;
  123. }
  124. begline = line;
  125. begcol = col;
  126. have_value = false;
  127. current_token = NextToken ();
  128. return current_token;
  129. }
  130. bool is_identifier_start_character (char c)
  131. {
  132. return (Char.IsLetter (c) || c == '_' );
  133. }
  134. bool is_identifier_part_character (char c)
  135. {
  136. return (Char.IsLetterOrDigit (c) || c == '_' || c == '-');
  137. }
  138. void ungetc (int value)
  139. {
  140. have_unget = true;
  141. unget_value = value;
  142. // Only '/' passes through here now.
  143. // If we ever let \n here, update 'line'
  144. position--;
  145. col--;
  146. }
  147. #if NET_2_0
  148. void TransformNextBlock (int count, bool final)
  149. {
  150. byte[] input = Encoding.UTF8.GetBytes (checksum_buf, 0, count);
  151. if (checksum == null)
  152. checksum = MD5.Create ();
  153. if (final)
  154. checksum.TransformFinalBlock (input, 0, input.Length);
  155. else
  156. checksum.TransformBlock (input, 0, input.Length, input, 0);
  157. input = null;
  158. checksum_buf_pos = -1;
  159. }
  160. void UpdateChecksum (int c)
  161. {
  162. bool final = c == -1;
  163. if (!final) {
  164. if (checksum_buf_pos >= CHECKSUM_BUF_SIZE)
  165. TransformNextBlock (checksum_buf_pos + 1, false);
  166. checksum_buf [++checksum_buf_pos] = (char)c;
  167. } else
  168. TransformNextBlock (checksum_buf_pos + 1, true);
  169. }
  170. #endif
  171. int read_char ()
  172. {
  173. int c;
  174. if (have_unget) {
  175. c = unget_value;
  176. have_unget = false;
  177. } else {
  178. c = sr.Read ();
  179. #if NET_2_0
  180. UpdateChecksum (c);
  181. #endif
  182. }
  183. if (c == '\r' && sr.Peek () == '\n') {
  184. c = sr.Read ();
  185. #if NET_2_0
  186. UpdateChecksum (c);
  187. #endif
  188. position++;
  189. }
  190. if (c == '\n'){
  191. col = -1;
  192. line++;
  193. }
  194. if (c != -1) {
  195. col++;
  196. position++;
  197. }
  198. return c;
  199. }
  200. int ReadAttValue (int start)
  201. {
  202. int quoteChar = 0;
  203. bool quoted = false;
  204. if (start == '"' || start == '\'') {
  205. quoteChar = start;
  206. quoted = true;
  207. } else {
  208. sb.Append ((char) start);
  209. }
  210. int c;
  211. int last = 0;
  212. bool inServerTag = false;
  213. alternatingQuotes = true;
  214. while ((c = sr.Peek ()) != -1) {
  215. if (c == '%' && last == '<') {
  216. inServerTag = true;
  217. } else if (inServerTag && c == '>' && last == '%') {
  218. inServerTag = false;
  219. } else if (!inServerTag) {
  220. if (!quoted && c == '/') {
  221. read_char ();
  222. c = sr.Peek ();
  223. if (c == -1) {
  224. c = '/';
  225. } else if (c == '>') {
  226. ungetc ('/');
  227. break;
  228. }
  229. } else if (!quoted && (c == '>' || Char.IsWhiteSpace ((char) c))) {
  230. break;
  231. } else if (quoted && c == quoteChar && last != '\\') {
  232. read_char ();
  233. break;
  234. }
  235. } else if (quoted && c == quoteChar) {
  236. alternatingQuotes = false;
  237. }
  238. sb.Append ((char) c);
  239. read_char ();
  240. last = c;
  241. }
  242. return Token.ATTVALUE;
  243. }
  244. int NextToken ()
  245. {
  246. int c;
  247. sb.Length = 0;
  248. odds.Length=0;
  249. while ((c = read_char ()) != -1){
  250. if (verbatim){
  251. inTag = false;
  252. sb.Append ((char) c);
  253. return c;
  254. }
  255. if (inTag && expectAttrValue && (c == '"' || c == '\''))
  256. return ReadAttValue (c);
  257. if (c == '<'){
  258. inTag = true;
  259. sb.Append ((char) c);
  260. return c;
  261. }
  262. if (c == '>'){
  263. inTag = false;
  264. sb.Append ((char) c);
  265. return c;
  266. }
  267. if (current_token == '<' && "%/!".IndexOf ((char) c) != -1){
  268. sb.Append ((char) c);
  269. return c;
  270. }
  271. if (inTag && current_token == '%' && "@#=".IndexOf ((char) c) != -1){
  272. if (odds.Length == 0 || odds.ToString ().IndexOfAny (lfcr) < 0) {
  273. sb.Append ((char) c);
  274. return c;
  275. }
  276. sb.Append ((char) c);
  277. continue;
  278. }
  279. if (inTag && c == '-' && sr.Peek () == '-'){
  280. sb.Append ("--");
  281. read_char ();
  282. return Token.DOUBLEDASH;
  283. }
  284. if (!inTag){
  285. sb.Append ((char) c);
  286. while ((c = sr.Peek ()) != -1 && c != '<')
  287. sb.Append ((char) read_char ());
  288. return (c != -1 || sb.Length > 0) ? Token.TEXT : Token.EOF;
  289. }
  290. if (inTag && current_token == '=' && !Char.IsWhiteSpace ((char) c))
  291. return ReadAttValue (c);
  292. if (inTag && is_identifier_start_character ((char) c)){
  293. sb.Append ((char) c);
  294. while ((c = sr.Peek ()) != -1) {
  295. if (!is_identifier_part_character ((char) c) && c != ':')
  296. break;
  297. sb.Append ((char) read_char ());
  298. }
  299. if (current_token == '@' && Directive.IsDirective (sb.ToString ()))
  300. return Token.DIRECTIVE;
  301. return Token.IDENTIFIER;
  302. }
  303. if (!Char.IsWhiteSpace ((char) c)) {
  304. sb.Append ((char) c);
  305. return c;
  306. }
  307. // keep otherwise discarded characters in case we need.
  308. odds.Append((char) c);
  309. }
  310. return Token.EOF;
  311. }
  312. public string Value {
  313. get {
  314. if (have_value)
  315. return val;
  316. have_value = true;
  317. val = sb.ToString ();
  318. return val;
  319. }
  320. }
  321. public string Odds {
  322. get {
  323. return odds.ToString();
  324. }
  325. }
  326. public bool InTag {
  327. get { return inTag; }
  328. set { inTag = value; }
  329. }
  330. // Hack for preventing confusion with VB comments (see bug #63451)
  331. public bool ExpectAttrValue {
  332. get { return expectAttrValue; }
  333. set { expectAttrValue = value; }
  334. }
  335. public bool AlternatingQuotes {
  336. get { return alternatingQuotes; }
  337. }
  338. public int BeginLine {
  339. get { return begline; }
  340. }
  341. public int BeginColumn {
  342. get { return begcol; }
  343. }
  344. public int EndLine {
  345. get { return line; }
  346. }
  347. public int EndColumn {
  348. get { return col; }
  349. }
  350. public int Position {
  351. get { return position; }
  352. }
  353. }
  354. }