AspTokenizer.cs 6.7 KB

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