AspParser.cs 9.5 KB

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