AspTokenizer.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Text;
  13. namespace System.Web.Compilation
  14. {
  15. class Token
  16. {
  17. public const int EOF = 0x0200000;
  18. public const int IDENTIFIER = 0x0200001;
  19. public const int DIRECTIVE = 0x0200002;
  20. public const int ATTVALUE = 0x0200003;
  21. public const int TEXT = 0x0200004;
  22. public const int DOUBLEDASH = 0x0200005;
  23. public const int CLOSING = 0x0200006;
  24. }
  25. class AspTokenizer
  26. {
  27. TextReader sr;
  28. int current_token;
  29. StringBuilder sb, odds;
  30. int col, line;
  31. int begcol, begline;
  32. int position;
  33. bool inTag;
  34. bool hasPutBack;
  35. bool verbatim;
  36. bool have_value;
  37. bool have_unget;
  38. int unget_value;
  39. string val;
  40. public AspTokenizer (TextReader reader)
  41. {
  42. this.sr = reader;
  43. sb = new StringBuilder ();
  44. odds= new StringBuilder();
  45. col = line = 1;
  46. hasPutBack = inTag = false;
  47. }
  48. public bool Verbatim
  49. {
  50. get { return verbatim; }
  51. set { verbatim = value; }
  52. }
  53. public void put_back ()
  54. {
  55. if (hasPutBack)
  56. throw new HttpException ("put_back called twice!");
  57. hasPutBack = true;
  58. position -= Value.Length;
  59. }
  60. public int get_token ()
  61. {
  62. if (hasPutBack){
  63. hasPutBack = false;
  64. position += Value.Length;
  65. return current_token;
  66. }
  67. begline = line;
  68. begcol = col;
  69. have_value = false;
  70. current_token = NextToken ();
  71. return current_token;
  72. }
  73. bool is_identifier_start_character (char c)
  74. {
  75. return (Char.IsLetter (c) || c == '_' );
  76. }
  77. bool is_identifier_part_character (char c)
  78. {
  79. return (Char.IsLetterOrDigit (c) || c == '_' || c == '-');
  80. }
  81. void ungetc (int value)
  82. {
  83. have_unget = true;
  84. unget_value = value;
  85. // Only '/' passes through here now.
  86. // If we ever let \n here, update 'line'
  87. position--;
  88. col--;
  89. }
  90. int read_char ()
  91. {
  92. int c;
  93. if (have_unget) {
  94. c = unget_value;
  95. have_unget = false;
  96. } else {
  97. c = sr.Read ();
  98. }
  99. if (c == '\r' && sr.Peek () == '\n') {
  100. c = sr.Read ();
  101. position++;
  102. }
  103. if (c == '\n'){
  104. col = -1;
  105. line++;
  106. }
  107. if (c != -1) {
  108. col++;
  109. position++;
  110. }
  111. return c;
  112. }
  113. int ReadAttValue (int start)
  114. {
  115. int quoteChar = 0;
  116. bool quoted = false;
  117. if (start == '"' || start == '\'') {
  118. quoteChar = start;
  119. quoted = true;
  120. } else {
  121. sb.Append ((char) start);
  122. }
  123. int c;
  124. int last = 0;
  125. bool inServerTag = false;
  126. while ((c = sr.Peek ()) != -1) {
  127. if (c == '%' && last == '<') {
  128. inServerTag = true;
  129. } else if (inServerTag && c == '>' && last == '%') {
  130. inServerTag = false;
  131. } else if (!inServerTag) {
  132. if (!quoted && c == '/') {
  133. read_char ();
  134. c = sr.Peek ();
  135. if (c == -1) {
  136. c = '/';
  137. } else if (c == '>') {
  138. ungetc ('/');
  139. break;
  140. }
  141. } else if (!quoted && (c == '>' || Char.IsWhiteSpace ((char) c))) {
  142. break;
  143. } else if (quoted && c == quoteChar && last != '\\') {
  144. read_char ();
  145. break;
  146. }
  147. }
  148. sb.Append ((char) c);
  149. read_char ();
  150. last = c;
  151. }
  152. return Token.ATTVALUE;
  153. }
  154. int NextToken ()
  155. {
  156. int c;
  157. sb.Length = 0;
  158. odds.Length=0;
  159. while ((c = read_char ()) != -1){
  160. if (verbatim){
  161. inTag = false;
  162. sb.Append ((char) c);
  163. return c;
  164. }
  165. if (inTag && (c == '"' || c == '\''))
  166. return ReadAttValue (c);
  167. if (c == '<'){
  168. inTag = true;
  169. sb.Append ((char) c);
  170. return c;
  171. }
  172. if (c == '>'){
  173. inTag = false;
  174. sb.Append ((char) c);
  175. return c;
  176. }
  177. if (current_token == '<' && "%/!".IndexOf ((char) c) != -1){
  178. sb.Append ((char) c);
  179. return c;
  180. }
  181. if (inTag && current_token == '%' && "@#=".IndexOf ((char) c) != -1){
  182. sb.Append ((char) c);
  183. return c;
  184. }
  185. if (inTag && c == '-' && sr.Peek () == '-'){
  186. sb.Append ("--");
  187. read_char ();
  188. return Token.DOUBLEDASH;
  189. }
  190. if (!inTag){
  191. sb.Append ((char) c);
  192. while ((c = sr.Peek ()) != -1 && c != '<')
  193. sb.Append ((char) read_char ());
  194. return (c != -1 || sb.Length > 0) ? Token.TEXT : Token.EOF;
  195. }
  196. if (inTag && current_token == '=' && !Char.IsWhiteSpace ((char) c))
  197. return ReadAttValue (c);
  198. if (inTag && is_identifier_start_character ((char) c)){
  199. sb.Append ((char) c);
  200. while ((c = sr.Peek ()) != -1) {
  201. if (!is_identifier_part_character ((char) c) && c != ':')
  202. break;
  203. sb.Append ((char) read_char ());
  204. }
  205. if (current_token == '@' && Directive.IsDirective (sb.ToString ()))
  206. return Token.DIRECTIVE;
  207. return Token.IDENTIFIER;
  208. }
  209. if (!Char.IsWhiteSpace ((char) c)) {
  210. sb.Append ((char) c);
  211. return c;
  212. }
  213. // keep otherwise discarded characters in case we need.
  214. odds.Append((char) c);
  215. }
  216. return Token.EOF;
  217. }
  218. public string Value {
  219. get {
  220. if (have_value)
  221. return val;
  222. have_value = true;
  223. val = sb.ToString ();
  224. return val;
  225. }
  226. }
  227. public string Odds {
  228. get {
  229. return odds.ToString();
  230. }
  231. }
  232. public bool InTag {
  233. get { return inTag; }
  234. set { inTag = value; }
  235. }
  236. public int BeginLine {
  237. get { return begline; }
  238. }
  239. public int BeginColumn {
  240. get { return begcol; }
  241. }
  242. public int EndLine {
  243. get { return line; }
  244. }
  245. public int EndColumn {
  246. get { return col; }
  247. }
  248. public int Position {
  249. get { return position; }
  250. }
  251. }
  252. }