copying the latest Sys.Web.Services from trunk.
[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                                                 if (attributes.IsRunAtServer ()) {
293                                                         OnError ("The server tag is not well formed.");
294                                                         break;
295                                                 }
296                                                 tokenizer.Verbatim = true;
297                                                 attributes.Add ("", GetVerbatim (tokenizer.get_token (), ">") + ">");
298                                                 tokenizer.Verbatim = false;
299                                         }
300                                 }
301
302                                 break;
303                         default:
304                                 tagtype = TagType.Text;
305                                 tokenizer.InTag = false;
306                                 id = "<" + tokenizer.Value;
307                                 break;
308                         }
309                 }
310
311                 TagAttributes GetAttributes ()
312                 {
313                         int token;
314                         TagAttributes attributes;
315                         string id;
316
317                         attributes = new TagAttributes ();
318                         while ((token = tokenizer.get_token ()) != Token.EOF){
319                                 if (token == '<' && Eat ('%')) {
320                                         tokenizer.Verbatim = true;
321                                         attributes.Add ("", "<%" + 
322                                                         GetVerbatim (tokenizer.get_token (), "%>") + "%>");
323                                         tokenizer.Verbatim = false;
324                                         tokenizer.InTag = true;
325                                         continue;
326                                 }
327                                         
328                                 if (token != Token.IDENTIFIER)
329                                         break;
330
331                                 id = tokenizer.Value;
332                                 if (Eat ('=')){
333                                         if (Eat (Token.ATTVALUE)){
334                                                 attributes.Add (id, tokenizer.Value);
335                                         } else if (Eat ('<') && Eat ('%')) {
336                                                 tokenizer.Verbatim = true;
337                                                 attributes.Add (id, "<%" + 
338                                                                 GetVerbatim (tokenizer.get_token (), "%>") + "%>");
339                                                 tokenizer.Verbatim = false;
340                                                 tokenizer.InTag = true;
341                                         } else {
342                                                 OnError ("expected ATTVALUE");
343                                                 return null;
344                                         }
345                                 } else {
346                                         attributes.Add (id, null);
347                                 }
348                         }
349
350                         tokenizer.put_back ();
351                         return attributes;
352                 }
353
354                 string GetVerbatim (int token, string end)
355                 {
356                         StringBuilder vb_text = new StringBuilder ();
357                         int i = 0;
358
359                         if (tokenizer.Value.Length > 1){
360                                 // May be we have a put_back token that is not a single character
361                                 vb_text.Append (tokenizer.Value);
362                                 token = tokenizer.get_token ();
363                         }
364
365                         while (token != Token.EOF){
366                                 if (Char.ToUpper ((char) token) == end [i]){
367                                         if (++i >= end.Length)
368                                                 break;
369                                         token = tokenizer.get_token ();
370                                         continue;
371                                 } else if (i > 0) {
372                                         for (int j = 0; j < i; j++)
373                                                 vb_text.Append (end [j]);
374                                         i = 0;
375                                 }
376
377                                 vb_text.Append ((char) token);
378                                 token = tokenizer.get_token ();
379                         } 
380
381                         return RemoveComments (vb_text.ToString ());
382                 }
383
384                 string RemoveComments (string text)
385                 {
386                         int end;
387                         int start = text.IndexOf ("<%--");
388
389                         while (start != -1) {
390                                 end = text.IndexOf ("--%>");
391                                 if (end == -1 || end <= start + 1)
392                                         break;
393
394                                 text = text.Remove (start, end - start + 4);
395                                 start = text.IndexOf ("<%--");
396                         }
397
398                         return text;
399                 }
400
401                 void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
402                 {
403                         string inside_tags;
404                         bool old = tokenizer.ExpectAttrValue;
405
406                         tokenizer.ExpectAttrValue = false;
407                         if (Eat ('@')){
408                                 tokenizer.ExpectAttrValue = old;
409                                 tagtype = TagType.Directive;
410                                 id = "";
411                                 if (Eat (Token.DIRECTIVE))
412                                         id = tokenizer.Value;
413
414                                 attributes = GetAttributes ();
415                                 if (!Eat ('%') || !Eat ('>'))
416                                         OnError ("expecting '%>'");
417
418                                 return;
419                         }
420                         
421                         if (Eat (Token.DOUBLEDASH)) {
422                                 tokenizer.ExpectAttrValue = old;
423                                 tokenizer.Verbatim = true;
424                                 inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
425                                 tokenizer.Verbatim = false;
426                                 id = null;
427                                 attributes = null;
428                                 tagtype = TagType.ServerComment;
429                                 return;
430                         }
431
432                         tokenizer.ExpectAttrValue = old;
433                         bool varname;
434                         bool databinding;
435                         varname = Eat ('=');
436                         databinding = !varname && Eat ('#');
437
438                         tokenizer.Verbatim = true;
439                         inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
440                         tokenizer.Verbatim = false;
441                         id = inside_tags;
442                         attributes = null;
443                         tagtype = (databinding ? TagType.DataBinding :
444                                   (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
445                 }
446
447                 public event ParseErrorHandler Error;
448                 public event TagParsedHandler TagParsed;
449                 public event TextParsedHandler TextParsed;
450
451                 void OnError (string msg)
452                 {
453                         if (Error != null)
454                                 Error (this, msg);
455                 }
456
457                 void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
458                 {
459                         if (TagParsed != null)
460                                 TagParsed (this, tagtype, id, attributes);
461                 }
462
463                 void OnTextParsed (string text)
464                 {
465                         if (TextParsed != null)
466                                 TextParsed (this, text);
467                 }
468         }
469
470 }
471