AspParser.cs 12 KB

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