AspTokenizer.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. string val;
  38. public AspTokenizer (TextReader reader)
  39. {
  40. this.sr = reader;
  41. sb = new StringBuilder ();
  42. odds= new StringBuilder();
  43. col = line = 1;
  44. hasPutBack = inTag = false;
  45. }
  46. public bool Verbatim
  47. {
  48. get { return verbatim; }
  49. set { verbatim = value; }
  50. }
  51. public void put_back ()
  52. {
  53. if (hasPutBack)
  54. throw new HttpException ("put_back called twice!");
  55. hasPutBack = true;
  56. position -= Value.Length;
  57. }
  58. public int get_token ()
  59. {
  60. if (hasPutBack){
  61. hasPutBack = false;
  62. position += Value.Length;
  63. return current_token;
  64. }
  65. begline = line;
  66. begcol = col;
  67. have_value = false;
  68. current_token = NextToken ();
  69. return current_token;
  70. }
  71. bool is_identifier_start_character (char c)
  72. {
  73. return (Char.IsLetter (c) || c == '_' );
  74. }
  75. bool is_identifier_part_character (char c)
  76. {
  77. return (Char.IsLetterOrDigit (c) || c == '_' || c == '-');
  78. }
  79. int read_char ()
  80. {
  81. int c = sr.Read ();
  82. if (c == '\r' && sr.Peek () == '\n') {
  83. c = sr.Read ();
  84. position++;
  85. }
  86. if (c == '\n'){
  87. col = -1;
  88. line++;
  89. }
  90. if (c != -1) {
  91. col++;
  92. position++;
  93. }
  94. return c;
  95. }
  96. int ReadAttValue (int start)
  97. {
  98. int quoteChar = 0;
  99. bool quoted = false;
  100. if (start == '"' || start == '\'') {
  101. quoteChar = start;
  102. quoted = true;
  103. } else {
  104. sb.Append ((char) start);
  105. }
  106. int c;
  107. int last = 0;
  108. bool inServerTag = false;
  109. while ((c = sr.Peek ()) != -1) {
  110. if (c == '%' && last == '<') {
  111. inServerTag = true;
  112. } else if (inServerTag && c == '>' && last == '%') {
  113. inServerTag = false;
  114. } else if (!inServerTag) {
  115. if (!quoted && (c == '/' || c == '>' || Char.IsWhiteSpace ((char) c))) {
  116. break;
  117. } else if (quoted && c == quoteChar && last != '\\') {
  118. read_char ();
  119. break;
  120. }
  121. }
  122. sb.Append ((char) c);
  123. read_char ();
  124. last = c;
  125. }
  126. return Token.ATTVALUE;
  127. }
  128. int NextToken ()
  129. {
  130. int c;
  131. sb.Length = 0;
  132. odds.Length=0;
  133. while ((c = read_char ()) != -1){
  134. if (verbatim){
  135. inTag = false;
  136. sb.Append ((char) c);
  137. return c;
  138. }
  139. if (inTag && (c == '"' || c == '\''))
  140. return ReadAttValue (c);
  141. if (c == '<'){
  142. inTag = true;
  143. sb.Append ((char) c);
  144. return c;
  145. }
  146. if (c == '>'){
  147. inTag = false;
  148. sb.Append ((char) c);
  149. return c;
  150. }
  151. if (current_token == '<' && "%/!".IndexOf ((char) c) != -1){
  152. sb.Append ((char) c);
  153. return c;
  154. }
  155. if (inTag && current_token == '%' && "@#=".IndexOf ((char) c) != -1){
  156. sb.Append ((char) c);
  157. return c;
  158. }
  159. if (inTag && c == '-' && sr.Peek () == '-'){
  160. sb.Append ("--");
  161. read_char ();
  162. return Token.DOUBLEDASH;
  163. }
  164. if (!inTag){
  165. sb.Append ((char) c);
  166. while ((c = sr.Peek ()) != -1 && c != '<')
  167. sb.Append ((char) read_char ());
  168. return (c != -1 || sb.Length > 0) ? Token.TEXT : Token.EOF;
  169. }
  170. if (inTag && current_token == '=' && !Char.IsWhiteSpace ((char) c))
  171. return ReadAttValue (c);
  172. if (inTag && is_identifier_start_character ((char) c)){
  173. sb.Append ((char) c);
  174. while ((c = sr.Peek ()) != -1) {
  175. if (!is_identifier_part_character ((char) c) && c != ':')
  176. break;
  177. sb.Append ((char) read_char ());
  178. }
  179. if (current_token == '@' && Directive.IsDirective (sb.ToString ()))
  180. return Token.DIRECTIVE;
  181. return Token.IDENTIFIER;
  182. }
  183. if (!Char.IsWhiteSpace ((char) c)) {
  184. sb.Append ((char) c);
  185. return c;
  186. }
  187. // keep otherwise discarded characters in case we need.
  188. odds.Append((char) c);
  189. }
  190. return Token.EOF;
  191. }
  192. public string Value {
  193. get {
  194. if (have_value)
  195. return val;
  196. have_value = true;
  197. val = sb.ToString ();
  198. return val;
  199. }
  200. }
  201. public string Odds {
  202. get {
  203. return odds.ToString();
  204. }
  205. }
  206. public bool InTag {
  207. get { return inTag; }
  208. set { inTag = value; }
  209. }
  210. public int BeginLine {
  211. get { return begline; }
  212. }
  213. public int BeginColumn {
  214. get { return begcol; }
  215. }
  216. public int EndLine {
  217. get { return line; }
  218. }
  219. public int EndColumn {
  220. get { return col; }
  221. }
  222. public int Position {
  223. get { return position; }
  224. }
  225. }
  226. }