AspParser.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //
  2. // System.Web.Compilation.AspParser
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 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. delegate void ParseErrorHandler (ILocation location, string message);
  16. delegate void TextParsedHandler (ILocation location, string text);
  17. delegate void TagParsedHandler (ILocation location, TagType tagtype, string id, TagAttributes attributes);
  18. class AspParser : ILocation
  19. {
  20. AspTokenizer tokenizer;
  21. int beginLine, endLine;
  22. int beginColumn, endColumn;
  23. int beginPosition, endPosition;
  24. string filename;
  25. string fileText;
  26. string verbatimID;
  27. public AspParser (string filename, TextReader input)
  28. {
  29. this.filename = filename;
  30. fileText = input.ReadToEnd ();
  31. StringReader reader = new StringReader (fileText);
  32. tokenizer = new AspTokenizer (reader);
  33. }
  34. public int BeginLine {
  35. get { return beginLine; }
  36. }
  37. public int BeginColumn {
  38. get { return beginColumn; }
  39. }
  40. public int EndLine {
  41. get { return endLine; }
  42. }
  43. public int EndColumn {
  44. get { return endColumn; }
  45. }
  46. public string PlainText {
  47. get {
  48. if (beginPosition >= endPosition)
  49. return null;
  50. return fileText.Substring (beginPosition, endPosition - beginPosition);
  51. }
  52. }
  53. public string Filename {
  54. get { return filename; }
  55. }
  56. public string VerbatimID {
  57. set {
  58. tokenizer.Verbatim = true;
  59. verbatimID = value.ToUpper ();
  60. }
  61. }
  62. bool Eat (int expected_token)
  63. {
  64. if (tokenizer.get_token () != expected_token) {
  65. tokenizer.put_back ();
  66. return false;
  67. }
  68. endLine = tokenizer.EndLine;
  69. endColumn = tokenizer.EndColumn;
  70. return true;
  71. }
  72. void BeginElement ()
  73. {
  74. beginLine = tokenizer.BeginLine;
  75. beginColumn = tokenizer.BeginColumn;
  76. beginPosition = tokenizer.Position - 1;
  77. }
  78. void EndElement ()
  79. {
  80. endLine = tokenizer.EndLine;
  81. endColumn = tokenizer.EndColumn;
  82. endPosition = tokenizer.Position;
  83. }
  84. public void Parse ()
  85. {
  86. int token;
  87. string id;
  88. TagAttributes attributes;
  89. TagType tagtype;
  90. StringBuilder text = new StringBuilder ();
  91. while ((token = tokenizer.get_token ()) != Token.EOF) {
  92. BeginElement ();
  93. if (tokenizer.Verbatim){
  94. string end_verbatim = "</" + verbatimID + ">";
  95. string verbatim_text = GetVerbatim (token, end_verbatim);
  96. if (verbatim_text == null)
  97. OnError ("Unexpected EOF processing " + verbatimID);
  98. tokenizer.Verbatim = false;
  99. EndElement ();
  100. endPosition -= end_verbatim.Length;
  101. OnTextParsed (verbatim_text);
  102. beginPosition = endPosition;
  103. endPosition += end_verbatim.Length;
  104. OnTagParsed (TagType.Close, verbatimID, null);
  105. continue;
  106. }
  107. if (token == '<') {
  108. GetTag (out tagtype, out id, out attributes);
  109. EndElement ();
  110. if (tagtype == TagType.ServerComment)
  111. continue;
  112. if (tagtype == TagType.Text)
  113. OnTextParsed (id);
  114. else
  115. OnTagParsed (tagtype, id, attributes);
  116. continue;
  117. }
  118. text.Length = 0;
  119. do {
  120. text.Append (tokenizer.Value);
  121. token = tokenizer.get_token ();
  122. } while (token != '<' && token != Token.EOF);
  123. tokenizer.put_back ();
  124. EndElement ();
  125. OnTextParsed (text.ToString ());
  126. }
  127. }
  128. void GetTag (out TagType tagtype, out string id, out TagAttributes attributes)
  129. {
  130. int token = tokenizer.get_token ();
  131. tagtype = TagType.ServerComment;
  132. id = null;
  133. attributes = null;
  134. switch (token){
  135. case '%':
  136. GetServerTag (out tagtype, out id, out attributes);
  137. break;
  138. case '/':
  139. if (!Eat (Token.IDENTIFIER))
  140. OnError ("expecting TAGNAME");
  141. id = tokenizer.Value;
  142. if (!Eat ('>'))
  143. OnError ("expecting '>'");
  144. tagtype = TagType.Close;
  145. break;
  146. case '!':
  147. bool double_dash = Eat (Token.DOUBLEDASH);
  148. if (double_dash)
  149. tokenizer.put_back ();
  150. tokenizer.Verbatim = true;
  151. string end = double_dash ? "-->" : ">";
  152. string comment = GetVerbatim (tokenizer.get_token (), end);
  153. tokenizer.Verbatim = false;
  154. if (comment == null)
  155. OnError ("Unfinished HTML comment/DTD");
  156. tagtype = TagType.Text;
  157. id = "<!" + comment + end;
  158. break;
  159. case Token.IDENTIFIER:
  160. id = tokenizer.Value;
  161. attributes = GetAttributes ();
  162. tagtype = TagType.Tag;
  163. if (Eat ('/') && Eat ('>'))
  164. tagtype = TagType.SelfClosing;
  165. else if (!Eat ('>'))
  166. OnError ("expecting '>'");
  167. break;
  168. default:
  169. tagtype = TagType.Text;
  170. id = "<" + tokenizer.Value;
  171. break;
  172. }
  173. }
  174. TagAttributes GetAttributes ()
  175. {
  176. int token;
  177. TagAttributes attributes;
  178. string id;
  179. attributes = new TagAttributes ();
  180. while ((token = tokenizer.get_token ()) != Token.EOF){
  181. if (token != Token.IDENTIFIER)
  182. break;
  183. id = tokenizer.Value;
  184. if (Eat ('=')){
  185. if (Eat (Token.ATTVALUE)){
  186. attributes.Add (id, tokenizer.Value);
  187. } else {
  188. //TODO: support data binding syntax without quotes
  189. OnError ("expected ATTVALUE");
  190. return null;
  191. }
  192. } else {
  193. attributes.Add (id, null);
  194. }
  195. }
  196. tokenizer.put_back ();
  197. if (attributes.Count == 0)
  198. return null;
  199. return attributes;
  200. }
  201. string GetVerbatim (int token, string end)
  202. {
  203. StringBuilder vb_text = new StringBuilder ();
  204. int i = 0;
  205. if (tokenizer.Value.Length > 1){
  206. // May be we have a put_back token that is not a single character
  207. vb_text.Append (tokenizer.Value);
  208. token = tokenizer.get_token ();
  209. }
  210. while (token != Token.EOF){
  211. if (Char.ToUpper ((char) token) == end [i]){
  212. if (++i >= end.Length)
  213. break;
  214. token = tokenizer.get_token ();
  215. continue;
  216. } else if (i > 0) {
  217. for (int j = 0; j < i; j++)
  218. vb_text.Append (end [j]);
  219. i = 0;
  220. }
  221. vb_text.Append ((char) token);
  222. token = tokenizer.get_token ();
  223. }
  224. return RemoveComments (vb_text.ToString ());
  225. }
  226. string RemoveComments (string text)
  227. {
  228. int end;
  229. int start = text.IndexOf ("<%--");
  230. while (start != -1) {
  231. end = text.IndexOf ("--%>");
  232. if (end == -1 || end <= start + 1)
  233. break;
  234. text = text.Remove (start, end - start + 4);
  235. start = text.IndexOf ("<%--");
  236. }
  237. return text;
  238. }
  239. void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
  240. {
  241. string inside_tags;
  242. if (Eat ('@')){
  243. tagtype = TagType.Directive;
  244. id = "";
  245. if (Eat (Token.DIRECTIVE))
  246. id = tokenizer.Value;
  247. attributes = GetAttributes ();
  248. if (!Eat ('%') || !Eat ('>'))
  249. OnError ("expecting '%>'");
  250. return;
  251. }
  252. if (Eat (Token.DOUBLEDASH)) {
  253. tokenizer.Verbatim = true;
  254. inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
  255. tokenizer.Verbatim = false;
  256. id = null;
  257. attributes = null;
  258. tagtype = TagType.ServerComment;
  259. return;
  260. }
  261. bool varname;
  262. bool databinding;
  263. varname = Eat ('=');
  264. databinding = !varname && Eat ('#');
  265. tokenizer.Verbatim = true;
  266. inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
  267. tokenizer.Verbatim = false;
  268. id = inside_tags;
  269. attributes = null;
  270. tagtype = (databinding ? TagType.DataBinding :
  271. (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
  272. }
  273. public event ParseErrorHandler Error;
  274. public event TagParsedHandler TagParsed;
  275. public event TextParsedHandler TextParsed;
  276. void OnError (string msg)
  277. {
  278. if (Error != null)
  279. Error (this, msg);
  280. }
  281. void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
  282. {
  283. if (TagParsed != null)
  284. TagParsed (this, tagtype, id, attributes);
  285. }
  286. void OnTextParsed (string text)
  287. {
  288. if (TextParsed != null)
  289. TextParsed (this, text);
  290. }
  291. }
  292. }