AspParser.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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;
  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. bool wellFormedForServer = true;
  272. attributes = new TagAttributes ();
  273. while ((token = tokenizer.get_token ()) != Token.EOF){
  274. if (token == '<' && Eat ('%')) {
  275. tokenizer.Verbatim = true;
  276. attributes.Add ("", "<%" +
  277. GetVerbatim (tokenizer.get_token (), "%>") + "%>");
  278. tokenizer.Verbatim = false;
  279. tokenizer.InTag = true;
  280. continue;
  281. }
  282. if (token != Token.IDENTIFIER)
  283. break;
  284. id = tokenizer.Value;
  285. if (Eat ('=')){
  286. if (Eat (Token.ATTVALUE)){
  287. attributes.Add (id, tokenizer.Value);
  288. wellFormedForServer &= tokenizer.AlternatingQuotes;
  289. } else if (Eat ('<') && Eat ('%')) {
  290. tokenizer.Verbatim = true;
  291. attributes.Add (id, "<%" +
  292. GetVerbatim (tokenizer.get_token (), "%>") + "%>");
  293. tokenizer.Verbatim = false;
  294. tokenizer.InTag = true;
  295. } else {
  296. OnError ("expected ATTVALUE");
  297. return null;
  298. }
  299. } else {
  300. attributes.Add (id, null);
  301. }
  302. }
  303. tokenizer.put_back ();
  304. if (attributes.IsRunAtServer () && !wellFormedForServer) {
  305. OnError ("The server tag is not well formed.");
  306. return null;
  307. }
  308. return attributes;
  309. }
  310. string GetVerbatim (int token, string end)
  311. {
  312. StringBuilder vb_text = new StringBuilder ();
  313. int i = 0;
  314. if (tokenizer.Value.Length > 1){
  315. // May be we have a put_back token that is not a single character
  316. vb_text.Append (tokenizer.Value);
  317. token = tokenizer.get_token ();
  318. }
  319. end = end.ToLower (CultureInfo.InvariantCulture);
  320. while (token != Token.EOF){
  321. if (Char.ToLower ((char) token, CultureInfo.InvariantCulture) == end [i]){
  322. if (++i >= end.Length)
  323. break;
  324. token = tokenizer.get_token ();
  325. continue;
  326. } else if (i > 0) {
  327. for (int j = 0; j < i; j++)
  328. vb_text.Append (end [j]);
  329. i = 0;
  330. }
  331. vb_text.Append ((char) token);
  332. token = tokenizer.get_token ();
  333. }
  334. if (token == Token.EOF)
  335. OnError ("Expecting " + end + " and got EOF.");
  336. return RemoveComments (vb_text.ToString ());
  337. }
  338. string RemoveComments (string text)
  339. {
  340. int end;
  341. int start = text.IndexOf ("<%--");
  342. while (start != -1) {
  343. end = text.IndexOf ("--%>");
  344. if (end == -1 || end <= start + 1)
  345. break;
  346. text = text.Remove (start, end - start + 4);
  347. start = text.IndexOf ("<%--");
  348. }
  349. return text;
  350. }
  351. void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
  352. {
  353. string inside_tags;
  354. bool old = tokenizer.ExpectAttrValue;
  355. tokenizer.ExpectAttrValue = false;
  356. if (Eat ('@')){
  357. tokenizer.ExpectAttrValue = old;
  358. tagtype = TagType.Directive;
  359. id = "";
  360. if (Eat (Token.DIRECTIVE))
  361. id = tokenizer.Value;
  362. attributes = GetAttributes ();
  363. if (!Eat ('%') || !Eat ('>'))
  364. OnError ("expecting '%>'");
  365. return;
  366. }
  367. if (Eat (Token.DOUBLEDASH)) {
  368. tokenizer.ExpectAttrValue = old;
  369. tokenizer.Verbatim = true;
  370. inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
  371. tokenizer.Verbatim = false;
  372. id = null;
  373. attributes = null;
  374. tagtype = TagType.ServerComment;
  375. return;
  376. }
  377. tokenizer.ExpectAttrValue = old;
  378. bool varname;
  379. bool databinding;
  380. varname = Eat ('=');
  381. databinding = !varname && Eat ('#');
  382. tokenizer.Verbatim = true;
  383. inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
  384. tokenizer.Verbatim = false;
  385. id = inside_tags;
  386. attributes = null;
  387. tagtype = (databinding ? TagType.DataBinding :
  388. (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
  389. }
  390. public event ParseErrorHandler Error;
  391. public event TagParsedHandler TagParsed;
  392. public event TextParsedHandler TextParsed;
  393. void OnError (string msg)
  394. {
  395. if (Error != null)
  396. Error (this, msg);
  397. }
  398. void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
  399. {
  400. if (TagParsed != null)
  401. TagParsed (this, tagtype, id, attributes);
  402. }
  403. void OnTextParsed (string text)
  404. {
  405. if (TextParsed != null)
  406. TextParsed (this, text);
  407. }
  408. }
  409. }