AspParser.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. bool GetInclude (string str, out string pathType, out string filename)
  129. {
  130. pathType = null;
  131. filename = null;
  132. str = str.Substring (2).Trim ();
  133. int len = str.Length;
  134. int lastQuote = str.LastIndexOf ('"');
  135. if (len < 10 || lastQuote != len - 1 || !str.StartsWith ("#include "))
  136. return false;
  137. str = str.Substring (9).Trim ();
  138. bool isfile = (str.StartsWith ("file"));
  139. if (!isfile && !str.StartsWith ("virtual"))
  140. return false;
  141. pathType = (isfile) ? "file" : "virtual";
  142. if (str.Length < pathType.Length + 3)
  143. return false;
  144. str = str.Substring (pathType.Length).Trim ();
  145. if (str.Length < 3 || str [0] != '=')
  146. return false;
  147. int index = 1;
  148. for (; index < str.Length; index++) {
  149. if (Char.IsWhiteSpace (str [index]))
  150. index++;
  151. else if (str [index] == '"')
  152. break;
  153. }
  154. if (index == str.Length || index == lastQuote)
  155. return false;
  156. str = str.Substring (index);
  157. if (str.Length == 2) { // only quotes
  158. OnError ("Empty file name.");
  159. return false;
  160. }
  161. filename = str.Trim ().Substring (index, str.Length - 2);
  162. if (filename.LastIndexOf ('"') != -1)
  163. return false; // file=""" -> no error
  164. return true;
  165. }
  166. void GetTag (out TagType tagtype, out string id, out TagAttributes attributes)
  167. {
  168. int token = tokenizer.get_token ();
  169. tagtype = TagType.ServerComment;
  170. id = null;
  171. attributes = null;
  172. switch (token){
  173. case '%':
  174. GetServerTag (out tagtype, out id, out attributes);
  175. break;
  176. case '/':
  177. if (!Eat (Token.IDENTIFIER))
  178. OnError ("expecting TAGNAME");
  179. id = tokenizer.Value;
  180. if (!Eat ('>'))
  181. OnError ("expecting '>'");
  182. tagtype = TagType.Close;
  183. break;
  184. case '!':
  185. bool double_dash = Eat (Token.DOUBLEDASH);
  186. if (double_dash)
  187. tokenizer.put_back ();
  188. tokenizer.Verbatim = true;
  189. string end = double_dash ? "-->" : ">";
  190. string comment = GetVerbatim (tokenizer.get_token (), end);
  191. tokenizer.Verbatim = false;
  192. if (comment == null)
  193. OnError ("Unfinished HTML comment/DTD");
  194. string pathType, filename;
  195. if (double_dash && GetInclude (comment, out pathType, out filename)) {
  196. tagtype = TagType.Include;
  197. attributes = new TagAttributes ();
  198. attributes.Add (pathType, filename);
  199. } else {
  200. tagtype = TagType.Text;
  201. id = "<!" + comment + end;
  202. }
  203. break;
  204. case Token.IDENTIFIER:
  205. id = tokenizer.Value;
  206. attributes = GetAttributes ();
  207. tagtype = TagType.Tag;
  208. if (Eat ('/') && Eat ('>'))
  209. tagtype = TagType.SelfClosing;
  210. else if (!Eat ('>'))
  211. OnError ("expecting '>'");
  212. break;
  213. default:
  214. tagtype = TagType.Text;
  215. id = "<" + tokenizer.Value;
  216. break;
  217. }
  218. }
  219. TagAttributes GetAttributes ()
  220. {
  221. int token;
  222. TagAttributes attributes;
  223. string id;
  224. attributes = new TagAttributes ();
  225. while ((token = tokenizer.get_token ()) != Token.EOF){
  226. if (token != Token.IDENTIFIER)
  227. break;
  228. id = tokenizer.Value;
  229. if (Eat ('=')){
  230. if (Eat (Token.ATTVALUE)){
  231. attributes.Add (id, tokenizer.Value);
  232. } else {
  233. //TODO: support data binding syntax without quotes
  234. OnError ("expected ATTVALUE");
  235. return null;
  236. }
  237. } else {
  238. attributes.Add (id, null);
  239. }
  240. }
  241. tokenizer.put_back ();
  242. if (attributes.Count == 0)
  243. return null;
  244. return attributes;
  245. }
  246. string GetVerbatim (int token, string end)
  247. {
  248. StringBuilder vb_text = new StringBuilder ();
  249. int i = 0;
  250. if (tokenizer.Value.Length > 1){
  251. // May be we have a put_back token that is not a single character
  252. vb_text.Append (tokenizer.Value);
  253. token = tokenizer.get_token ();
  254. }
  255. while (token != Token.EOF){
  256. if (Char.ToUpper ((char) token) == end [i]){
  257. if (++i >= end.Length)
  258. break;
  259. token = tokenizer.get_token ();
  260. continue;
  261. } else if (i > 0) {
  262. for (int j = 0; j < i; j++)
  263. vb_text.Append (end [j]);
  264. i = 0;
  265. }
  266. vb_text.Append ((char) token);
  267. token = tokenizer.get_token ();
  268. }
  269. return RemoveComments (vb_text.ToString ());
  270. }
  271. string RemoveComments (string text)
  272. {
  273. int end;
  274. int start = text.IndexOf ("<%--");
  275. while (start != -1) {
  276. end = text.IndexOf ("--%>");
  277. if (end == -1 || end <= start + 1)
  278. break;
  279. text = text.Remove (start, end - start + 4);
  280. start = text.IndexOf ("<%--");
  281. }
  282. return text;
  283. }
  284. void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
  285. {
  286. string inside_tags;
  287. if (Eat ('@')){
  288. tagtype = TagType.Directive;
  289. id = "";
  290. if (Eat (Token.DIRECTIVE))
  291. id = tokenizer.Value;
  292. attributes = GetAttributes ();
  293. if (!Eat ('%') || !Eat ('>'))
  294. OnError ("expecting '%>'");
  295. return;
  296. }
  297. if (Eat (Token.DOUBLEDASH)) {
  298. tokenizer.Verbatim = true;
  299. inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
  300. tokenizer.Verbatim = false;
  301. id = null;
  302. attributes = null;
  303. tagtype = TagType.ServerComment;
  304. return;
  305. }
  306. bool varname;
  307. bool databinding;
  308. varname = Eat ('=');
  309. databinding = !varname && Eat ('#');
  310. tokenizer.Verbatim = true;
  311. inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
  312. tokenizer.Verbatim = false;
  313. id = inside_tags;
  314. attributes = null;
  315. tagtype = (databinding ? TagType.DataBinding :
  316. (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
  317. }
  318. public event ParseErrorHandler Error;
  319. public event TagParsedHandler TagParsed;
  320. public event TextParsedHandler TextParsed;
  321. void OnError (string msg)
  322. {
  323. if (Error != null)
  324. Error (this, msg);
  325. }
  326. void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
  327. {
  328. if (TagParsed != null)
  329. TagParsed (this, tagtype, id, attributes);
  330. }
  331. void OnTextParsed (string text)
  332. {
  333. if (TextParsed != null)
  334. TextParsed (this, text);
  335. }
  336. }
  337. }