2
0

AspParser.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Globalization;
  32. using System.IO;
  33. using System.Text;
  34. namespace System.Web.Compilation
  35. {
  36. delegate void ParseErrorHandler (ILocation location, string message);
  37. delegate void TextParsedHandler (ILocation location, string text);
  38. delegate void TagParsedHandler (ILocation location, TagType tagtype, string id, TagAttributes attributes);
  39. class AspParser : ILocation
  40. {
  41. AspTokenizer tokenizer;
  42. int beginLine, endLine;
  43. int beginColumn, endColumn;
  44. int beginPosition, endPosition;
  45. string filename;
  46. string fileText;
  47. string verbatimID;
  48. public AspParser (string filename, TextReader input)
  49. {
  50. this.filename = filename;
  51. fileText = input.ReadToEnd ();
  52. StringReader reader = new StringReader (fileText);
  53. tokenizer = new AspTokenizer (reader);
  54. }
  55. public int BeginLine {
  56. get { return beginLine; }
  57. }
  58. public int BeginColumn {
  59. get { return beginColumn; }
  60. }
  61. public int EndLine {
  62. get { return endLine; }
  63. }
  64. public int EndColumn {
  65. get { return endColumn; }
  66. }
  67. public string PlainText {
  68. get {
  69. if (beginPosition >= endPosition)
  70. return null;
  71. return fileText.Substring (beginPosition, endPosition - beginPosition);
  72. }
  73. }
  74. public string Filename {
  75. get { return filename; }
  76. }
  77. public string VerbatimID {
  78. set {
  79. tokenizer.Verbatim = true;
  80. verbatimID = value.ToUpper (CultureInfo.InvariantCulture);
  81. }
  82. }
  83. bool Eat (int expected_token)
  84. {
  85. if (tokenizer.get_token () != expected_token) {
  86. tokenizer.put_back ();
  87. return false;
  88. }
  89. endLine = tokenizer.EndLine;
  90. endColumn = tokenizer.EndColumn;
  91. return true;
  92. }
  93. void BeginElement ()
  94. {
  95. beginLine = tokenizer.BeginLine;
  96. beginColumn = tokenizer.BeginColumn;
  97. beginPosition = tokenizer.Position - 1;
  98. }
  99. void EndElement ()
  100. {
  101. endLine = tokenizer.EndLine;
  102. endColumn = tokenizer.EndColumn;
  103. endPosition = tokenizer.Position;
  104. }
  105. public void Parse ()
  106. {
  107. int token;
  108. string id;
  109. TagAttributes attributes;
  110. TagType tagtype = TagType.Text;
  111. StringBuilder text = new StringBuilder ();
  112. while ((token = tokenizer.get_token ()) != Token.EOF) {
  113. BeginElement ();
  114. if (tokenizer.Verbatim){
  115. string end_verbatim = "</" + verbatimID + ">";
  116. string verbatim_text = GetVerbatim (token, end_verbatim);
  117. if (verbatim_text == null)
  118. OnError ("Unexpected EOF processing " + verbatimID);
  119. tokenizer.Verbatim = false;
  120. EndElement ();
  121. endPosition -= end_verbatim.Length;
  122. OnTextParsed (verbatim_text);
  123. beginPosition = endPosition;
  124. endPosition += end_verbatim.Length;
  125. OnTagParsed (TagType.Close, verbatimID, null);
  126. continue;
  127. }
  128. if (token == '<') {
  129. GetTag (out tagtype, out id, out attributes);
  130. EndElement ();
  131. if (tagtype == TagType.ServerComment)
  132. continue;
  133. if (tagtype == TagType.Text)
  134. OnTextParsed (id);
  135. else
  136. OnTagParsed (tagtype, id, attributes);
  137. continue;
  138. }
  139. if (tokenizer.Value.Trim () == "" && tagtype == TagType.Directive) {
  140. continue;
  141. }
  142. text.Length = 0;
  143. do {
  144. text.Append (tokenizer.Value);
  145. token = tokenizer.get_token ();
  146. } while (token != '<' && token != Token.EOF);
  147. tokenizer.put_back ();
  148. EndElement ();
  149. OnTextParsed (text.ToString ());
  150. }
  151. }
  152. bool GetInclude (string str, out string pathType, out string filename)
  153. {
  154. pathType = null;
  155. filename = null;
  156. str = str.Substring (2).Trim ();
  157. int len = str.Length;
  158. int lastQuote = str.LastIndexOf ('"');
  159. if (len < 10 || lastQuote != len - 1)
  160. return false;
  161. if (!str.ToLower (CultureInfo.InvariantCulture).StartsWith ("#include "))
  162. return false;
  163. str = str.Substring (9).Trim ();
  164. bool isfile = (str.ToLower (CultureInfo.InvariantCulture).StartsWith ("file"));
  165. if (!isfile && !str.ToLower (CultureInfo.InvariantCulture).StartsWith ("virtual"))
  166. return false;
  167. pathType = (isfile) ? "file" : "virtual";
  168. if (str.Length < pathType.Length + 3)
  169. return false;
  170. str = str.Substring (pathType.Length).Trim ();
  171. if (str.Length < 3 || str [0] != '=')
  172. return false;
  173. int index = 1;
  174. for (; index < str.Length; index++) {
  175. if (Char.IsWhiteSpace (str [index]))
  176. index++;
  177. else if (str [index] == '"')
  178. break;
  179. }
  180. if (index == str.Length || index == lastQuote)
  181. return false;
  182. str = str.Substring (index);
  183. if (str.Length == 2) { // only quotes
  184. OnError ("Empty file name.");
  185. return false;
  186. }
  187. filename = str.Trim ().Substring (index, str.Length - 2);
  188. if (filename.LastIndexOf ('"') != -1)
  189. return false; // file=""" -> no error
  190. return true;
  191. }
  192. void GetTag (out TagType tagtype, out string id, out TagAttributes attributes)
  193. {
  194. int token = tokenizer.get_token ();
  195. tagtype = TagType.ServerComment;
  196. id = null;
  197. attributes = null;
  198. switch (token){
  199. case '%':
  200. GetServerTag (out tagtype, out id, out attributes);
  201. break;
  202. case '/':
  203. if (!Eat (Token.IDENTIFIER))
  204. OnError ("expecting TAGNAME");
  205. id = tokenizer.Value;
  206. if (!Eat ('>'))
  207. OnError ("expecting '>'. Got '" + id + "'");
  208. tagtype = TagType.Close;
  209. break;
  210. case '!':
  211. bool double_dash = Eat (Token.DOUBLEDASH);
  212. if (double_dash)
  213. tokenizer.put_back ();
  214. tokenizer.Verbatim = true;
  215. string end = double_dash ? "-->" : ">";
  216. string comment = GetVerbatim (tokenizer.get_token (), end);
  217. tokenizer.Verbatim = false;
  218. if (comment == null)
  219. OnError ("Unfinished HTML comment/DTD");
  220. string pathType, filename;
  221. if (double_dash && GetInclude (comment, out pathType, out filename)) {
  222. tagtype = TagType.Include;
  223. attributes = new TagAttributes ();
  224. attributes.Add (pathType, filename);
  225. } else {
  226. tagtype = TagType.Text;
  227. id = "<!" + comment + end;
  228. }
  229. break;
  230. case Token.IDENTIFIER:
  231. if (this.filename == "@@inner_string@@") {
  232. // Actually not tag but "xxx < yyy" stuff in inner_string!
  233. tagtype = TagType.Text;
  234. tokenizer.InTag = false;
  235. id = "<" + tokenizer.Odds + tokenizer.Value;
  236. } else {
  237. id = tokenizer.Value;
  238. try {
  239. attributes = GetAttributes ();
  240. } catch (Exception e) {
  241. OnError (e.Message);
  242. break;
  243. }
  244. tagtype = TagType.Tag;
  245. if (Eat ('/') && Eat ('>')) {
  246. tagtype = TagType.SelfClosing;
  247. } else if (!Eat ('>')) {
  248. if (attributes.IsRunAtServer ()) {
  249. OnError ("The server tag is not well formed.");
  250. break;
  251. }
  252. tokenizer.Verbatim = true;
  253. attributes.Add ("", GetVerbatim (tokenizer.get_token (), ">") + ">");
  254. tokenizer.Verbatim = false;
  255. }
  256. }
  257. break;
  258. default:
  259. tagtype = TagType.Text;
  260. tokenizer.InTag = false;
  261. id = "<" + tokenizer.Value;
  262. break;
  263. }
  264. }
  265. TagAttributes GetAttributes ()
  266. {
  267. int token;
  268. TagAttributes attributes;
  269. string id;
  270. attributes = new TagAttributes ();
  271. while ((token = tokenizer.get_token ()) != Token.EOF){
  272. if (token == '<' && Eat ('%')) {
  273. tokenizer.Verbatim = true;
  274. attributes.Add ("", "<%" +
  275. GetVerbatim (tokenizer.get_token (), "%>") + "%>");
  276. tokenizer.Verbatim = false;
  277. tokenizer.InTag = true;
  278. continue;
  279. }
  280. if (token != Token.IDENTIFIER)
  281. break;
  282. id = tokenizer.Value;
  283. if (Eat ('=')){
  284. if (Eat (Token.ATTVALUE)){
  285. attributes.Add (id, tokenizer.Value);
  286. } else if (Eat ('<') && Eat ('%')) {
  287. tokenizer.Verbatim = true;
  288. attributes.Add (id, "<%" +
  289. GetVerbatim (tokenizer.get_token (), "%>") + "%>");
  290. tokenizer.Verbatim = false;
  291. tokenizer.InTag = true;
  292. } else {
  293. OnError ("expected ATTVALUE");
  294. return null;
  295. }
  296. } else {
  297. attributes.Add (id, null);
  298. }
  299. }
  300. tokenizer.put_back ();
  301. return attributes;
  302. }
  303. string GetVerbatim (int token, string end)
  304. {
  305. StringBuilder vb_text = new StringBuilder ();
  306. int i = 0;
  307. if (tokenizer.Value.Length > 1){
  308. // May be we have a put_back token that is not a single character
  309. vb_text.Append (tokenizer.Value);
  310. token = tokenizer.get_token ();
  311. }
  312. while (token != Token.EOF){
  313. if (Char.ToUpper ((char) token, CultureInfo.InvariantCulture) == end [i]){
  314. if (++i >= end.Length)
  315. break;
  316. token = tokenizer.get_token ();
  317. continue;
  318. } else if (i > 0) {
  319. for (int j = 0; j < i; j++)
  320. vb_text.Append (end [j]);
  321. i = 0;
  322. }
  323. vb_text.Append ((char) token);
  324. token = tokenizer.get_token ();
  325. }
  326. return RemoveComments (vb_text.ToString ());
  327. }
  328. string RemoveComments (string text)
  329. {
  330. int end;
  331. int start = text.IndexOf ("<%--");
  332. while (start != -1) {
  333. end = text.IndexOf ("--%>");
  334. if (end == -1 || end <= start + 1)
  335. break;
  336. text = text.Remove (start, end - start + 4);
  337. start = text.IndexOf ("<%--");
  338. }
  339. return text;
  340. }
  341. void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
  342. {
  343. string inside_tags;
  344. bool old = tokenizer.ExpectAttrValue;
  345. tokenizer.ExpectAttrValue = false;
  346. if (Eat ('@')){
  347. tokenizer.ExpectAttrValue = old;
  348. tagtype = TagType.Directive;
  349. id = "";
  350. if (Eat (Token.DIRECTIVE))
  351. id = tokenizer.Value;
  352. attributes = GetAttributes ();
  353. if (!Eat ('%') || !Eat ('>'))
  354. OnError ("expecting '%>'");
  355. return;
  356. }
  357. if (Eat (Token.DOUBLEDASH)) {
  358. tokenizer.ExpectAttrValue = old;
  359. tokenizer.Verbatim = true;
  360. inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
  361. tokenizer.Verbatim = false;
  362. id = null;
  363. attributes = null;
  364. tagtype = TagType.ServerComment;
  365. return;
  366. }
  367. tokenizer.ExpectAttrValue = old;
  368. bool varname;
  369. bool databinding;
  370. varname = Eat ('=');
  371. databinding = !varname && Eat ('#');
  372. tokenizer.Verbatim = true;
  373. inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
  374. tokenizer.Verbatim = false;
  375. id = inside_tags;
  376. attributes = null;
  377. tagtype = (databinding ? TagType.DataBinding :
  378. (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
  379. }
  380. public event ParseErrorHandler Error;
  381. public event TagParsedHandler TagParsed;
  382. public event TextParsedHandler TextParsed;
  383. void OnError (string msg)
  384. {
  385. if (Error != null)
  386. Error (this, msg);
  387. }
  388. void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
  389. {
  390. if (TagParsed != null)
  391. TagParsed (this, tagtype, id, attributes);
  392. }
  393. void OnTextParsed (string text)
  394. {
  395. if (TextParsed != null)
  396. TextParsed (this, text);
  397. }
  398. }
  399. }