2 // System.Web.Compilation.AspParser
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.ComponentModel;
32 using System.Collections;
33 using System.Globalization;
36 using System.Web.Util;
37 using System.Security.Cryptography;
39 namespace System.Web.Compilation
41 delegate void ParseErrorHandler (ILocation location, string message);
42 delegate void TextParsedHandler (ILocation location, string text);
43 delegate void TagParsedHandler (ILocation location, TagType tagtype, string id, TagAttributes attributes);
45 delegate void ParsingCompleteHandler ();
48 class AspParser : ILocation
50 static readonly object errorEvent = new object ();
51 static readonly object tagParsedEvent = new object ();
52 static readonly object textParsedEvent = new object ();
54 static readonly object parsingCompleteEvent = new object();
58 AspTokenizer tokenizer;
59 int beginLine, endLine;
60 int beginColumn, endColumn;
61 int beginPosition, endPosition;
65 StringReader fileReader;
67 EventHandlerList events = new EventHandlerList ();
69 public event ParseErrorHandler Error {
70 add { events.AddHandler (errorEvent, value); }
71 remove { events.RemoveHandler (errorEvent, value); }
74 public event TagParsedHandler TagParsed {
75 add { events.AddHandler (tagParsedEvent, value); }
76 remove { events.RemoveHandler (tagParsedEvent, value); }
79 public event TextParsedHandler TextParsed {
80 add { events.AddHandler (textParsedEvent, value); }
81 remove { events.RemoveHandler (textParsedEvent, value); }
85 public event ParsingCompleteHandler ParsingComplete {
86 add { events.AddHandler (parsingCompleteEvent, value); }
87 remove { events.RemoveHandler (parsingCompleteEvent, value); }
91 public AspParser (string filename, TextReader input)
93 this.filename = filename;
94 this.fileText = input.ReadToEnd ();
95 this.fileReader = new StringReader (this.fileText);
96 tokenizer = new AspTokenizer (this.fileReader);
100 public byte[] MD5Checksum {
102 if (checksum == null)
105 return checksum.Hash;
110 public int BeginLine {
111 get { return beginLine; }
114 public int BeginColumn {
115 get { return beginColumn; }
119 get { return endLine; }
122 public int EndColumn {
123 get { return endColumn; }
126 public string FileText {
128 if (fileText != null)
135 public string PlainText {
137 if (beginPosition >= endPosition || fileText == null)
140 return fileText.Substring (beginPosition, endPosition - beginPosition);
144 public string Filename {
145 get { return filename; }
148 public string VerbatimID {
150 tokenizer.Verbatim = true;
155 bool Eat (int expected_token)
157 if (tokenizer.get_token () != expected_token) {
158 tokenizer.put_back ();
162 endLine = tokenizer.EndLine;
163 endColumn = tokenizer.EndColumn;
169 beginLine = tokenizer.BeginLine;
170 beginColumn = tokenizer.BeginColumn;
171 beginPosition = tokenizer.Position - 1;
176 endLine = tokenizer.EndLine;
177 endColumn = tokenizer.EndColumn;
178 endPosition = tokenizer.Position;
183 if (tokenizer == null) {
184 OnError ("AspParser not initialized properly.");
190 TagAttributes attributes;
191 TagType tagtype = TagType.Text;
192 StringBuilder text = new StringBuilder ();
195 while ((token = tokenizer.get_token ()) != Token.EOF) {
198 if (tokenizer.Verbatim){
199 string end_verbatim = "</" + verbatimID + ">";
200 string verbatim_text = GetVerbatim (token, end_verbatim);
202 if (verbatim_text == null)
203 OnError ("Unexpected EOF processing " + verbatimID);
205 tokenizer.Verbatim = false;
208 endPosition -= end_verbatim.Length;
209 OnTextParsed (verbatim_text);
210 beginPosition = endPosition;
211 endPosition += end_verbatim.Length;
212 OnTagParsed (TagType.Close, verbatimID, null);
217 GetTag (out tagtype, out id, out attributes);
219 if (tagtype == TagType.ServerComment)
222 if (tagtype == TagType.Text)
225 OnTagParsed (tagtype, id, attributes);
230 if (tokenizer.Value.Trim () == "" && tagtype == TagType.Directive) {
236 text.Append (tokenizer.Value);
237 token = tokenizer.get_token ();
238 } while (token != '<' && token != Token.EOF);
240 tokenizer.put_back ();
242 OnTextParsed (text.ToString ());
245 if (fileReader != null) {
250 checksum = tokenizer.Checksum;
256 OnParsingComplete ();
260 bool GetInclude (string str, out string pathType, out string filename)
264 str = str.Substring (2).Trim ();
265 int len = str.Length;
266 int lastQuote = str.LastIndexOf ('"');
267 if (len < 10 || lastQuote != len - 1)
270 if (!StrUtils.StartsWith (str, "#include ", true))
273 str = str.Substring (9).Trim ();
274 bool isfile = (StrUtils.StartsWith (str ,"file", true));
275 if (!isfile && !StrUtils.StartsWith (str, "virtual", true))
278 pathType = (isfile) ? "file" : "virtual";
279 if (str.Length < pathType.Length + 3)
282 str = str.Substring (pathType.Length).Trim ();
283 if (str.Length < 3 || str [0] != '=')
287 for (; index < str.Length; index++) {
288 if (Char.IsWhiteSpace (str [index]))
290 else if (str [index] == '"')
294 if (index == str.Length || index == lastQuote)
297 str = str.Substring (index);
298 if (str.Length == 2) { // only quotes
299 OnError ("Empty file name.");
303 filename = str.Trim ().Substring (index, str.Length - 2);
304 if (filename.LastIndexOf ('"') != -1)
305 return false; // file=""" -> no error
310 void GetTag (out TagType tagtype, out string id, out TagAttributes attributes)
312 int token = tokenizer.get_token ();
314 tagtype = TagType.ServerComment;
319 GetServerTag (out tagtype, out id, out attributes);
322 if (!Eat (Token.IDENTIFIER))
323 OnError ("expecting TAGNAME");
325 id = tokenizer.Value;
327 OnError ("expecting '>'. Got '" + id + "'");
329 tagtype = TagType.Close;
332 bool double_dash = Eat (Token.DOUBLEDASH);
334 tokenizer.put_back ();
336 tokenizer.Verbatim = true;
337 string end = double_dash ? "-->" : ">";
338 string comment = GetVerbatim (tokenizer.get_token (), end);
339 tokenizer.Verbatim = false;
341 OnError ("Unfinished HTML comment/DTD");
343 string pathType, filename;
344 if (double_dash && GetInclude (comment, out pathType, out filename)) {
345 tagtype = TagType.Include;
346 attributes = new TagAttributes ();
347 attributes.Add (pathType, filename);
349 tagtype = TagType.Text;
350 id = "<!" + comment + end;
353 case Token.IDENTIFIER:
354 if (this.filename == "@@inner_string@@") {
355 // Actually not tag but "xxx < yyy" stuff in inner_string!
356 tagtype = TagType.Text;
357 tokenizer.InTag = false;
358 id = "<" + tokenizer.Odds + tokenizer.Value;
360 id = tokenizer.Value;
362 attributes = GetAttributes ();
363 } catch (Exception e) {
368 tagtype = TagType.Tag;
369 if (Eat ('/') && Eat ('>')) {
370 tagtype = TagType.SelfClosing;
371 } else if (!Eat ('>')) {
372 if (attributes.IsRunAtServer ()) {
373 OnError ("The server tag is not well formed.");
376 tokenizer.Verbatim = true;
377 attributes.Add ("", GetVerbatim (tokenizer.get_token (), ">") + ">");
378 tokenizer.Verbatim = false;
384 tagtype = TagType.Text;
385 tokenizer.InTag = false;
386 id = "<" + tokenizer.Value;
391 TagAttributes GetAttributes ()
394 TagAttributes attributes;
396 bool wellFormedForServer = true;
398 attributes = new TagAttributes ();
399 while ((token = tokenizer.get_token ()) != Token.EOF){
400 if (token == '<' && Eat ('%')) {
401 tokenizer.Verbatim = true;
402 attributes.Add ("", "<%" +
403 GetVerbatim (tokenizer.get_token (), "%>") + "%>");
404 tokenizer.Verbatim = false;
405 tokenizer.InTag = true;
409 if (token != Token.IDENTIFIER)
412 id = tokenizer.Value;
414 if (Eat (Token.ATTVALUE)){
415 attributes.Add (id, tokenizer.Value);
416 wellFormedForServer &= tokenizer.AlternatingQuotes;
417 } else if (Eat ('<') && Eat ('%')) {
418 tokenizer.Verbatim = true;
419 attributes.Add (id, "<%" +
420 GetVerbatim (tokenizer.get_token (), "%>") + "%>");
421 tokenizer.Verbatim = false;
422 tokenizer.InTag = true;
424 OnError ("expected ATTVALUE");
428 attributes.Add (id, null);
432 tokenizer.put_back ();
434 if (attributes.IsRunAtServer () && !wellFormedForServer) {
435 OnError ("The server tag is not well formed.");
442 string GetVerbatim (int token, string end)
444 StringBuilder vb_text = new StringBuilder ();
445 StringBuilder tmp = new StringBuilder ();
448 if (tokenizer.Value.Length > 1){
449 // May be we have a put_back token that is not a single character
450 vb_text.Append (tokenizer.Value);
451 token = tokenizer.get_token ();
454 end = end.ToLower (CultureInfo.InvariantCulture);
456 for (int k = 0; k < end.Length; k++)
457 if (end [0] == end [k])
460 while (token != Token.EOF){
461 if (Char.ToLower ((char) token, CultureInfo.InvariantCulture) == end [i]){
462 if (++i >= end.Length)
464 tmp.Append ((char) token);
465 token = tokenizer.get_token ();
468 if (repeated > 1 && i == repeated && (char) token == end [0]) {
469 vb_text.Append ((char) token);
470 token = tokenizer.get_token ();
473 vb_text.Append (tmp.ToString ());
474 tmp.Remove (0, tmp.Length);
478 vb_text.Append ((char) token);
479 token = tokenizer.get_token ();
482 if (token == Token.EOF)
483 OnError ("Expecting " + end + " and got EOF.");
485 return RemoveComments (vb_text.ToString ());
488 string RemoveComments (string text)
491 int start = text.IndexOf ("<%--");
493 while (start != -1) {
494 end = text.IndexOf ("--%>");
495 if (end == -1 || end <= start + 1)
498 text = text.Remove (start, end - start + 4);
499 start = text.IndexOf ("<%--");
505 void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
508 bool old = tokenizer.ExpectAttrValue;
510 tokenizer.ExpectAttrValue = false;
512 tokenizer.ExpectAttrValue = old;
513 tagtype = TagType.Directive;
515 if (Eat (Token.DIRECTIVE))
516 id = tokenizer.Value;
518 attributes = GetAttributes ();
519 if (!Eat ('%') || !Eat ('>'))
520 OnError ("expecting '%>'");
525 if (Eat (Token.DOUBLEDASH)) {
526 tokenizer.ExpectAttrValue = old;
527 tokenizer.Verbatim = true;
528 inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
529 tokenizer.Verbatim = false;
532 tagtype = TagType.ServerComment;
536 tokenizer.ExpectAttrValue = old;
540 databinding = !varname && Eat ('#');
542 tokenizer.Verbatim = true;
543 inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
544 tokenizer.Verbatim = false;
547 tagtype = (databinding ? TagType.DataBinding :
548 (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
551 public override string ToString ()
553 StringBuilder sb = new StringBuilder ("AspParser {");
554 if (filename != null && filename.Length > 0)
555 sb.AppendFormat ("{0}:{1}.{2}", filename, beginLine, beginColumn);
558 return sb.ToString ();
561 void OnError (string msg)
563 ParseErrorHandler eh = events [errorEvent] as ParseErrorHandler;
568 void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
570 TagParsedHandler eh = events [tagParsedEvent] as TagParsedHandler;
572 eh (this, tagtype, id, attributes);
575 void OnTextParsed (string text)
577 TextParsedHandler eh = events [textParsedEvent] as TextParsedHandler;
583 void OnParsingComplete ()
585 ParsingCompleteHandler eh = events [parsingCompleteEvent] as ParsingCompleteHandler;