AspParser.cs 9.0 KB

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