AspParser.cs 11 KB

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