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