AspParser.cs 11 KB

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