AspTokenizer.cs 4.7 KB

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