AspTokenizer.cs 6.9 KB

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