AspTokenizer.cs 6.5 KB

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