AspParser.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // System.Web.Compilation.AspParser
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 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 AspParser {
  16. private AspTokenizer tokenizer;
  17. private ArrayList elements; // List of processed elements in the HTML page.
  18. private void error ()
  19. {
  20. throw new HttpException ("Error parsing: " + tokenizer.location);
  21. }
  22. private void error (string msg)
  23. {
  24. throw new Exception ("Error: "+ msg + "\n" + tokenizer.location);
  25. }
  26. public AspParser (AspTokenizer tokenizer)
  27. {
  28. this.tokenizer = tokenizer;
  29. elements = new ArrayList ();
  30. }
  31. public AspParser (string filename, Stream input) :
  32. this (new AspTokenizer (filename, input))
  33. {
  34. }
  35. public ArrayList Elements
  36. {
  37. get { return elements; }
  38. }
  39. private bool Eat (int expected_token)
  40. {
  41. if (tokenizer.get_token () != expected_token) {
  42. tokenizer.put_back ();
  43. return false;
  44. }
  45. return true;
  46. }
  47. private void AddPlainText (string newText)
  48. {
  49. if (elements.Count > 0){
  50. Element element = (Element) elements [elements.Count - 1];
  51. if (element is PlainText){
  52. ((PlainText) element).Append (newText);
  53. return;
  54. }
  55. }
  56. elements.Add (new PlainText (newText));
  57. }
  58. public void Parse ()
  59. {
  60. int token;
  61. Element element;
  62. Tag tag_element;
  63. string tag = "";
  64. while ((token = tokenizer.get_token ()) != Token.EOF){
  65. if (tokenizer.Verbatim){
  66. string end_verbatim = "</" + tag + ">";
  67. string verbatim_text = GetVerbatim (token, end_verbatim);
  68. if (verbatim_text == null)
  69. error ("Unexpected EOF processing " + tag);
  70. AddPlainText (verbatim_text);
  71. elements.Add (new CloseTag (tag));
  72. tokenizer.Verbatim = false;
  73. }
  74. else if (token == '<') {
  75. element = GetTag ();
  76. if (element == null)
  77. error ();
  78. if (element is ServerComment)
  79. continue;
  80. if (!(element is Tag)){
  81. AddPlainText (((PlainText) element).Text);
  82. continue;
  83. }
  84. elements.Add (element);
  85. tag_element = element as Tag;
  86. tag = tag_element.TagID.ToUpper ();
  87. if (!tag_element.SelfClosing && (tag == "SCRIPT" || tag == "PRE"))
  88. tokenizer.Verbatim = true;
  89. }
  90. else {
  91. StringBuilder text = new StringBuilder ();
  92. do {
  93. text.Append (tokenizer.value);
  94. token = tokenizer.get_token ();
  95. } while (token != '<' && token != Token.EOF);
  96. tokenizer.put_back ();
  97. AddPlainText (text.ToString ());
  98. }
  99. }
  100. }
  101. private Element GetTag ()
  102. {
  103. int token = tokenizer.get_token ();
  104. string id;
  105. switch (token){
  106. case '%':
  107. return GetServerTag ();
  108. case '/':
  109. if (!Eat (Token.IDENTIFIER))
  110. error ("expecting TAGNAME");
  111. id = tokenizer.value;
  112. if (!Eat ('>'))
  113. error ("expecting '>'");
  114. return new CloseTag (id);
  115. case '!':
  116. bool double_dash = Eat (Token.DOUBLEDASH);
  117. if (double_dash)
  118. tokenizer.put_back ();
  119. tokenizer.Verbatim = true;
  120. string end = double_dash ? "-->" : ">";
  121. string comment = GetVerbatim (tokenizer.get_token (), end);
  122. tokenizer.Verbatim = false;
  123. if (comment == null)
  124. error ("Unfinished HTML comment/DTD");
  125. return new PlainText ("<!" + comment + end);
  126. case Token.IDENTIFIER:
  127. id = tokenizer.value;
  128. Tag tag = new Tag (id, GetAttributes (), Eat ('/'));
  129. if (!Eat ('>'))
  130. error ("expecting '>'");
  131. return tag;
  132. default:
  133. return null;
  134. }
  135. }
  136. private TagAttributes GetAttributes ()
  137. {
  138. int token;
  139. TagAttributes attributes;
  140. string id;
  141. attributes = new TagAttributes ();
  142. while ((token = tokenizer.get_token ()) != Token.EOF){
  143. if (token != Token.IDENTIFIER)
  144. break;
  145. id = tokenizer.value;
  146. if (Eat ('=')){
  147. if (Eat (Token.ATTVALUE)){
  148. attributes.Add (id, tokenizer.value);
  149. } else {
  150. //TODO: support data binding syntax without quotes
  151. error ("expected ATTVALUE");
  152. return null;
  153. }
  154. } else {
  155. attributes.Add (id, null);
  156. }
  157. }
  158. tokenizer.put_back ();
  159. if (attributes.Count == 0)
  160. return null;
  161. return attributes;
  162. }
  163. private string GetVerbatim (int token, string end)
  164. {
  165. StringBuilder vb_text = new StringBuilder ();
  166. int i = 0;
  167. if (tokenizer.value.Length > 1){
  168. // May be we have a put_back token that is not a single character
  169. vb_text.Append (tokenizer.value);
  170. token = tokenizer.get_token ();
  171. }
  172. while (token != Token.EOF){
  173. if (Char.ToUpper ((char) token) == end [i]){
  174. if (++i >= end.Length)
  175. break;
  176. token = tokenizer.get_token ();
  177. continue;
  178. }
  179. else {
  180. for (int j = 0; j < i; j++)
  181. vb_text.Append (end [j]);
  182. }
  183. i = 0;
  184. vb_text.Append ((char) token);
  185. token = tokenizer.get_token ();
  186. }
  187. if (token == Token.EOF)
  188. return null;
  189. return RemoveComments (vb_text.ToString ());
  190. }
  191. private string RemoveComments (string text)
  192. {
  193. int end;
  194. int start = text.IndexOf ("<%--");
  195. while (start != -1) {
  196. end = text.IndexOf ("--%>");
  197. if (end == -1 || end <= start + 1)
  198. break;
  199. text = text.Remove (start, end - start + 4);
  200. start = text.IndexOf ("<%--");
  201. }
  202. return text;
  203. }
  204. private Element GetServerTag ()
  205. {
  206. string id;
  207. string inside_tags;
  208. TagAttributes attributes;
  209. if (Eat ('@')){
  210. id = (Eat (Token.DIRECTIVE) ? tokenizer.value : "Page");
  211. attributes = GetAttributes ();
  212. if (!Eat ('%') || !Eat ('>'))
  213. error ("expecting '%>'");
  214. return new Directive (id, attributes);
  215. } else if (Eat (Token.DOUBLEDASH)) {
  216. tokenizer.Verbatim = true;
  217. inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
  218. tokenizer.Verbatim = false;
  219. return new ServerComment ("<%--" + inside_tags + "--%>");
  220. }
  221. bool varname;
  222. bool databinding;
  223. varname = Eat ('=');
  224. databinding = !varname && Eat ('#');
  225. tokenizer.Verbatim = true;
  226. inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
  227. tokenizer.Verbatim = false;
  228. if (databinding)
  229. return new DataBindingTag (inside_tags);
  230. return new CodeRenderTag (varname, inside_tags);
  231. }
  232. }
  233. }