AspParser.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. Console.WriteLine ("Error: " + tokenizer.location);
  21. Environment.Exit (-1); //FIXME
  22. }
  23. private void error (string msg)
  24. {
  25. Console.WriteLine ("Error: "+ msg + "\n" + tokenizer.location);
  26. Environment.Exit (-1); //FIXME
  27. }
  28. public AspParser (AspTokenizer tokenizer)
  29. {
  30. this.tokenizer = tokenizer;
  31. elements = new ArrayList ();
  32. }
  33. public AspParser (string filename, Stream input) :
  34. this (new AspTokenizer (filename, input))
  35. {
  36. }
  37. public ArrayList Elements
  38. {
  39. get { return elements; }
  40. }
  41. private bool Eat (int expected_token)
  42. {
  43. if (tokenizer.get_token () != expected_token) {
  44. tokenizer.put_back ();
  45. return false;
  46. }
  47. return true;
  48. }
  49. private void AddPlainText (string newText)
  50. {
  51. if (elements.Count > 0){
  52. Element element = (Element) elements [elements.Count - 1];
  53. if (element is PlainText){
  54. ((PlainText) element).Append (newText);
  55. return;
  56. }
  57. }
  58. elements.Add (new PlainText (newText));
  59. }
  60. public void Parse ()
  61. {
  62. int token;
  63. Element element;
  64. Tag tag_element;
  65. string tag = "";
  66. while ((token = tokenizer.get_token ()) != Token.EOF){
  67. if (tokenizer.Verbatim){
  68. string end_verbatim = "</" + tag + ">";
  69. string verbatim_text = GetVerbatim (token, end_verbatim);
  70. if (verbatim_text == null)
  71. error ("Unexpected EOF processing " + tag);
  72. AddPlainText (verbatim_text);
  73. elements.Add (new CloseTag (tag));
  74. tokenizer.Verbatim = false;
  75. }
  76. else if (token == '<'){
  77. element = GetTag ();
  78. if (element == null)
  79. error ();
  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. TagAttributes attributes;
  106. switch (token){
  107. case '%':
  108. if (Eat ('@')){
  109. id = (Eat (Token.DIRECTIVE) ? tokenizer.value : "Page");
  110. attributes = GetAttributes ();
  111. if (!Eat ('%') || !Eat ('>'))
  112. error ("expecting '%>'");
  113. return new Directive (id, attributes);
  114. }
  115. bool varname = Eat ('=');
  116. bool databinding = !varname && Eat ('#');
  117. tokenizer.Verbatim = true;
  118. string inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
  119. tokenizer.Verbatim = false;
  120. if (databinding)
  121. return new DataBindingTag (inside_tags);
  122. return new CodeRenderTag (varname, inside_tags);
  123. case '/':
  124. if (!Eat (Token.IDENTIFIER))
  125. error ("expecting TAGNAME");
  126. id = tokenizer.value;
  127. if (!Eat ('>'))
  128. error ("expecting '>'");
  129. return new CloseTag (id);
  130. case '!':
  131. bool double_dash = Eat (Token.DOUBLEDASH);
  132. if (double_dash)
  133. tokenizer.put_back ();
  134. tokenizer.Verbatim = true;
  135. string end = double_dash ? "-->" : ">";
  136. string comment = GetVerbatim (tokenizer.get_token (), end);
  137. tokenizer.Verbatim = false;
  138. if (comment == null)
  139. error ("Unfinished HTML comment/DTD");
  140. return new PlainText ("<!" + comment + end);
  141. case Token.IDENTIFIER:
  142. id = tokenizer.value;
  143. Tag tag = new Tag (id, GetAttributes (), Eat ('/'));
  144. if (!Eat ('>'))
  145. error ("expecting '>'");
  146. return tag;
  147. default:
  148. return null;
  149. }
  150. }
  151. private TagAttributes GetAttributes ()
  152. {
  153. int token;
  154. TagAttributes attributes;
  155. string id;
  156. attributes = new TagAttributes ();
  157. while ((token = tokenizer.get_token ()) != Token.EOF){
  158. if (token != Token.IDENTIFIER)
  159. break;
  160. id = tokenizer.value;
  161. if (Eat ('=')){
  162. if (Eat (Token.ATTVALUE)){
  163. attributes.Add (id, tokenizer.value);
  164. } else {
  165. //TODO: support data binding syntax without quotes
  166. error ("expected ATTVALUE");
  167. return null;
  168. }
  169. } else {
  170. attributes.Add (id, null);
  171. }
  172. }
  173. tokenizer.put_back ();
  174. if (attributes.Count == 0)
  175. return null;
  176. return attributes;
  177. }
  178. private string GetVerbatim (int token, string end)
  179. {
  180. StringBuilder vb_text = new StringBuilder ();
  181. int i = 0;
  182. if (tokenizer.value.Length > 1){
  183. // May be we have a put_back token that is not a single character
  184. vb_text.Append (tokenizer.value);
  185. token = tokenizer.get_token ();
  186. }
  187. while (token != Token.EOF){
  188. if (Char.ToUpper ((char) token) == end [i]){
  189. if (++i >= end.Length)
  190. break;
  191. token = tokenizer.get_token ();
  192. continue;
  193. }
  194. else {
  195. for (int j = 0; j < i; j++)
  196. vb_text.Append (end [j]);
  197. }
  198. i = 0;
  199. vb_text.Append ((char) token);
  200. token = tokenizer.get_token ();
  201. }
  202. if (token == Token.EOF)
  203. return null;
  204. return vb_text.ToString ();
  205. }
  206. }
  207. }