AspParser.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 Tag)){
  79. AddPlainText (((PlainText) element).Text);
  80. continue;
  81. }
  82. elements.Add (element);
  83. tag_element = element as Tag;
  84. tag = tag_element.TagID.ToUpper ();
  85. if (!tag_element.SelfClosing && (tag == "SCRIPT" || tag == "PRE"))
  86. tokenizer.Verbatim = true;
  87. }
  88. else {
  89. StringBuilder text = new StringBuilder ();
  90. do {
  91. text.Append (tokenizer.value);
  92. token = tokenizer.get_token ();
  93. } while (token != '<' && token != Token.EOF);
  94. tokenizer.put_back ();
  95. AddPlainText (text.ToString ());
  96. }
  97. }
  98. }
  99. private Element GetTag ()
  100. {
  101. int token = tokenizer.get_token ();
  102. string id;
  103. TagAttributes attributes;
  104. switch (token){
  105. case '%':
  106. if (Eat ('@')){
  107. id = (Eat (Token.DIRECTIVE) ? tokenizer.value : "Page");
  108. attributes = GetAttributes ();
  109. if (!Eat ('%') || !Eat ('>'))
  110. error ("expecting '%>'");
  111. return new Directive (id, attributes);
  112. }
  113. bool varname = Eat ('=');
  114. bool databinding = !varname && Eat ('#');
  115. tokenizer.Verbatim = true;
  116. string inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
  117. tokenizer.Verbatim = false;
  118. if (databinding)
  119. return new DataBindingTag (inside_tags);
  120. return new CodeRenderTag (varname, inside_tags);
  121. case '/':
  122. if (!Eat (Token.IDENTIFIER))
  123. error ("expecting TAGNAME");
  124. id = tokenizer.value;
  125. if (!Eat ('>'))
  126. error ("expecting '>'");
  127. return new CloseTag (id);
  128. case '!':
  129. bool double_dash = Eat (Token.DOUBLEDASH);
  130. if (double_dash)
  131. tokenizer.put_back ();
  132. tokenizer.Verbatim = true;
  133. string end = double_dash ? "-->" : ">";
  134. string comment = GetVerbatim (tokenizer.get_token (), end);
  135. tokenizer.Verbatim = false;
  136. if (comment == null)
  137. error ("Unfinished HTML comment/DTD");
  138. return new PlainText ("<!" + comment + end);
  139. case Token.IDENTIFIER:
  140. id = tokenizer.value;
  141. Tag tag = new Tag (id, GetAttributes (), Eat ('/'));
  142. if (!Eat ('>'))
  143. error ("expecting '>'");
  144. return tag;
  145. default:
  146. return null;
  147. }
  148. }
  149. private TagAttributes GetAttributes ()
  150. {
  151. int token;
  152. TagAttributes attributes;
  153. string id;
  154. attributes = new TagAttributes ();
  155. while ((token = tokenizer.get_token ()) != Token.EOF){
  156. if (token != Token.IDENTIFIER)
  157. break;
  158. id = tokenizer.value;
  159. if (Eat ('=')){
  160. if (Eat (Token.ATTVALUE)){
  161. attributes.Add (id, tokenizer.value);
  162. } else {
  163. //TODO: support data binding syntax without quotes
  164. error ("expected ATTVALUE");
  165. return null;
  166. }
  167. } else {
  168. attributes.Add (id, null);
  169. }
  170. }
  171. tokenizer.put_back ();
  172. if (attributes.Count == 0)
  173. return null;
  174. return attributes;
  175. }
  176. private string GetVerbatim (int token, string end)
  177. {
  178. StringBuilder vb_text = new StringBuilder ();
  179. int i = 0;
  180. if (tokenizer.value.Length > 1){
  181. // May be we have a put_back token that is not a single character
  182. vb_text.Append (tokenizer.value);
  183. token = tokenizer.get_token ();
  184. }
  185. while (token != Token.EOF){
  186. if (Char.ToUpper ((char) token) == end [i]){
  187. if (++i >= end.Length)
  188. break;
  189. token = tokenizer.get_token ();
  190. continue;
  191. }
  192. else {
  193. for (int j = 0; j < i; j++)
  194. vb_text.Append (end [j]);
  195. }
  196. i = 0;
  197. vb_text.Append ((char) token);
  198. token = tokenizer.get_token ();
  199. }
  200. if (token == Token.EOF)
  201. return null;
  202. return vb_text.ToString ();
  203. }
  204. }
  205. }