AspParser.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 '>'. Got '" + id + "'");
  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. try {
  207. attributes = GetAttributes ();
  208. } catch (Exception e) {
  209. OnError (e.Message);
  210. break;
  211. }
  212. tagtype = TagType.Tag;
  213. if (Eat ('/') && Eat ('>'))
  214. tagtype = TagType.SelfClosing;
  215. else if (!Eat ('>'))
  216. OnError ("expecting '>'. Got '" + tokenizer.Value + "'");
  217. break;
  218. default:
  219. tagtype = TagType.Text;
  220. tokenizer.InTag = false;
  221. id = "<" + tokenizer.Value;
  222. break;
  223. }
  224. }
  225. TagAttributes GetAttributes ()
  226. {
  227. int token;
  228. TagAttributes attributes;
  229. string id;
  230. attributes = new TagAttributes ();
  231. while ((token = tokenizer.get_token ()) != Token.EOF){
  232. if (token != Token.IDENTIFIER)
  233. break;
  234. id = tokenizer.Value;
  235. if (Eat ('=')){
  236. if (Eat (Token.ATTVALUE)){
  237. attributes.Add (id, tokenizer.Value);
  238. } else if (Eat ('<') && Eat ('%')) {
  239. attributes.Add (id, "<%" +
  240. GetVerbatim (tokenizer.get_token (), "%>"));
  241. } else {
  242. OnError ("expected ATTVALUE");
  243. return null;
  244. }
  245. } else {
  246. attributes.Add (id, null);
  247. }
  248. }
  249. tokenizer.put_back ();
  250. return attributes;
  251. }
  252. string GetVerbatim (int token, string end)
  253. {
  254. StringBuilder vb_text = new StringBuilder ();
  255. int i = 0;
  256. if (tokenizer.Value.Length > 1){
  257. // May be we have a put_back token that is not a single character
  258. vb_text.Append (tokenizer.Value);
  259. token = tokenizer.get_token ();
  260. }
  261. while (token != Token.EOF){
  262. if (Char.ToUpper ((char) token) == end [i]){
  263. if (++i >= end.Length)
  264. break;
  265. token = tokenizer.get_token ();
  266. continue;
  267. } else if (i > 0) {
  268. for (int j = 0; j < i; j++)
  269. vb_text.Append (end [j]);
  270. i = 0;
  271. }
  272. vb_text.Append ((char) token);
  273. token = tokenizer.get_token ();
  274. }
  275. return RemoveComments (vb_text.ToString ());
  276. }
  277. string RemoveComments (string text)
  278. {
  279. int end;
  280. int start = text.IndexOf ("<%--");
  281. while (start != -1) {
  282. end = text.IndexOf ("--%>");
  283. if (end == -1 || end <= start + 1)
  284. break;
  285. text = text.Remove (start, end - start + 4);
  286. start = text.IndexOf ("<%--");
  287. }
  288. return text;
  289. }
  290. void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
  291. {
  292. string inside_tags;
  293. if (Eat ('@')){
  294. tagtype = TagType.Directive;
  295. id = "";
  296. if (Eat (Token.DIRECTIVE))
  297. id = tokenizer.Value;
  298. attributes = GetAttributes ();
  299. if (!Eat ('%') || !Eat ('>'))
  300. OnError ("expecting '%>'");
  301. return;
  302. }
  303. if (Eat (Token.DOUBLEDASH)) {
  304. tokenizer.Verbatim = true;
  305. inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
  306. tokenizer.Verbatim = false;
  307. id = null;
  308. attributes = null;
  309. tagtype = TagType.ServerComment;
  310. return;
  311. }
  312. bool varname;
  313. bool databinding;
  314. varname = Eat ('=');
  315. databinding = !varname && Eat ('#');
  316. tokenizer.Verbatim = true;
  317. inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
  318. tokenizer.Verbatim = false;
  319. id = inside_tags;
  320. attributes = null;
  321. tagtype = (databinding ? TagType.DataBinding :
  322. (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
  323. }
  324. public event ParseErrorHandler Error;
  325. public event TagParsedHandler TagParsed;
  326. public event TextParsedHandler TextParsed;
  327. void OnError (string msg)
  328. {
  329. if (Error != null)
  330. Error (this, msg);
  331. }
  332. void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
  333. {
  334. if (TagParsed != null)
  335. TagParsed (this, tagtype, id, attributes);
  336. }
  337. void OnTextParsed (string text)
  338. {
  339. if (TextParsed != null)
  340. TextParsed (this, text);
  341. }
  342. }
  343. }