AspTokenizer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. namespace System.Web.Compilation
  34. {
  35. class Token
  36. {
  37. public const int EOF = 0x0200000;
  38. public const int IDENTIFIER = 0x0200001;
  39. public const int DIRECTIVE = 0x0200002;
  40. public const int ATTVALUE = 0x0200003;
  41. public const int TEXT = 0x0200004;
  42. public const int DOUBLEDASH = 0x0200005;
  43. public const int CLOSING = 0x0200006;
  44. }
  45. class AspTokenizer
  46. {
  47. class PutBackItem
  48. {
  49. public readonly string Value;
  50. public readonly int Position;
  51. public readonly int CurrentToken;
  52. public PutBackItem (string value, int position, int currentToken)
  53. {
  54. Value = value;
  55. Position = position;
  56. CurrentToken = currentToken;
  57. }
  58. }
  59. static char [] lfcr = new char [] { '\n', '\r' };
  60. TextReader sr;
  61. int current_token;
  62. StringBuilder sb, odds;
  63. int col, line;
  64. int begcol, begline;
  65. int position;
  66. bool inTag;
  67. bool expectAttrValue;
  68. bool alternatingQuotes;
  69. bool hasPutBack;
  70. bool verbatim;
  71. bool have_value;
  72. bool have_unget;
  73. int unget_value;
  74. string val;
  75. Stack putBackBuffer;
  76. public AspTokenizer (TextReader reader)
  77. {
  78. this.sr = reader;
  79. sb = new StringBuilder ();
  80. odds= new StringBuilder();
  81. col = line = 1;
  82. hasPutBack = inTag = false;
  83. }
  84. public bool Verbatim
  85. {
  86. get { return verbatim; }
  87. set { verbatim = value; }
  88. }
  89. public void put_back ()
  90. {
  91. if (hasPutBack && !inTag)
  92. throw new HttpException ("put_back called twice!");
  93. hasPutBack = true;
  94. if (putBackBuffer == null)
  95. putBackBuffer = new Stack ();
  96. string val = Value;
  97. putBackBuffer.Push (new PutBackItem (val, position, current_token));
  98. position -= val.Length;
  99. }
  100. public int get_token ()
  101. {
  102. if (hasPutBack) {
  103. PutBackItem pbi = putBackBuffer.Pop () as PutBackItem;
  104. hasPutBack = putBackBuffer.Count > 0;
  105. position = pbi.Position;
  106. have_value = false;
  107. val = null;
  108. sb = new StringBuilder (pbi.Value);
  109. current_token = pbi.CurrentToken;
  110. return current_token;
  111. }
  112. begline = line;
  113. begcol = col;
  114. have_value = false;
  115. current_token = NextToken ();
  116. return current_token;
  117. }
  118. bool is_identifier_start_character (char c)
  119. {
  120. return (Char.IsLetter (c) || c == '_' );
  121. }
  122. bool is_identifier_part_character (char c)
  123. {
  124. return (Char.IsLetterOrDigit (c) || c == '_' || c == '-');
  125. }
  126. void ungetc (int value)
  127. {
  128. have_unget = true;
  129. unget_value = value;
  130. // Only '/' passes through here now.
  131. // If we ever let \n here, update 'line'
  132. position--;
  133. col--;
  134. }
  135. int read_char ()
  136. {
  137. int c;
  138. if (have_unget) {
  139. c = unget_value;
  140. have_unget = false;
  141. } else {
  142. c = sr.Read ();
  143. }
  144. if (c == '\r' && sr.Peek () == '\n') {
  145. c = sr.Read ();
  146. position++;
  147. }
  148. if (c == '\n'){
  149. col = -1;
  150. line++;
  151. }
  152. if (c != -1) {
  153. col++;
  154. position++;
  155. }
  156. return c;
  157. }
  158. int ReadAttValue (int start)
  159. {
  160. int quoteChar = 0;
  161. bool quoted = false;
  162. if (start == '"' || start == '\'') {
  163. quoteChar = start;
  164. quoted = true;
  165. } else {
  166. sb.Append ((char) start);
  167. }
  168. int c;
  169. int last = 0;
  170. bool inServerTag = false;
  171. alternatingQuotes = true;
  172. while ((c = sr.Peek ()) != -1) {
  173. if (c == '%' && last == '<') {
  174. inServerTag = true;
  175. } else if (inServerTag && c == '>' && last == '%') {
  176. inServerTag = false;
  177. } else if (!inServerTag) {
  178. if (!quoted && c == '/') {
  179. read_char ();
  180. c = sr.Peek ();
  181. if (c == -1) {
  182. c = '/';
  183. } else if (c == '>') {
  184. ungetc ('/');
  185. break;
  186. }
  187. } else if (!quoted && (c == '>' || Char.IsWhiteSpace ((char) c))) {
  188. break;
  189. } else if (quoted && c == quoteChar && last != '\\') {
  190. read_char ();
  191. break;
  192. }
  193. } else if (quoted && c == quoteChar) {
  194. alternatingQuotes = false;
  195. }
  196. sb.Append ((char) c);
  197. read_char ();
  198. last = c;
  199. }
  200. return Token.ATTVALUE;
  201. }
  202. int NextToken ()
  203. {
  204. int c;
  205. sb.Length = 0;
  206. odds.Length=0;
  207. while ((c = read_char ()) != -1){
  208. if (verbatim){
  209. inTag = false;
  210. sb.Append ((char) c);
  211. return c;
  212. }
  213. if (inTag && expectAttrValue && (c == '"' || c == '\''))
  214. return ReadAttValue (c);
  215. if (c == '<'){
  216. inTag = true;
  217. sb.Append ((char) c);
  218. return c;
  219. }
  220. if (c == '>'){
  221. inTag = false;
  222. sb.Append ((char) c);
  223. return c;
  224. }
  225. if (current_token == '<' && "%/!".IndexOf ((char) c) != -1){
  226. sb.Append ((char) c);
  227. return c;
  228. }
  229. if (inTag && current_token == '%' && "@#=".IndexOf ((char) c) != -1){
  230. if (odds.Length == 0 || odds.ToString ().IndexOfAny (lfcr) < 0) {
  231. sb.Append ((char) c);
  232. return c;
  233. }
  234. sb.Append ((char) c);
  235. continue;
  236. }
  237. if (inTag && c == '-' && sr.Peek () == '-'){
  238. sb.Append ("--");
  239. read_char ();
  240. return Token.DOUBLEDASH;
  241. }
  242. if (!inTag){
  243. sb.Append ((char) c);
  244. while ((c = sr.Peek ()) != -1 && c != '<')
  245. sb.Append ((char) read_char ());
  246. return (c != -1 || sb.Length > 0) ? Token.TEXT : Token.EOF;
  247. }
  248. if (inTag && current_token == '=' && !Char.IsWhiteSpace ((char) c))
  249. return ReadAttValue (c);
  250. if (inTag && is_identifier_start_character ((char) c)){
  251. sb.Append ((char) c);
  252. while ((c = sr.Peek ()) != -1) {
  253. if (!is_identifier_part_character ((char) c) && c != ':')
  254. break;
  255. sb.Append ((char) read_char ());
  256. }
  257. if (current_token == '@' && Directive.IsDirective (sb.ToString ()))
  258. return Token.DIRECTIVE;
  259. return Token.IDENTIFIER;
  260. }
  261. if (!Char.IsWhiteSpace ((char) c)) {
  262. sb.Append ((char) c);
  263. return c;
  264. }
  265. // keep otherwise discarded characters in case we need.
  266. odds.Append((char) c);
  267. }
  268. return Token.EOF;
  269. }
  270. public string Value {
  271. get {
  272. if (have_value)
  273. return val;
  274. have_value = true;
  275. val = sb.ToString ();
  276. return val;
  277. }
  278. }
  279. public string Odds {
  280. get {
  281. return odds.ToString();
  282. }
  283. }
  284. public bool InTag {
  285. get { return inTag; }
  286. set { inTag = value; }
  287. }
  288. // Hack for preventing confusion with VB comments (see bug #63451)
  289. public bool ExpectAttrValue {
  290. get { return expectAttrValue; }
  291. set { expectAttrValue = value; }
  292. }
  293. public bool AlternatingQuotes {
  294. get { return alternatingQuotes; }
  295. }
  296. public int BeginLine {
  297. get { return begline; }
  298. }
  299. public int BeginColumn {
  300. get { return begcol; }
  301. }
  302. public int EndLine {
  303. get { return line; }
  304. }
  305. public int EndColumn {
  306. get { return col; }
  307. }
  308. public int Position {
  309. get { return position; }
  310. }
  311. }
  312. }