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.Collections;
35 namespace System.Web.Compilation
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);
41 class AspParser : ILocation
43 AspTokenizer tokenizer;
44 int beginLine, endLine;
45 int beginColumn, endColumn;
46 int beginPosition, endPosition;
51 public AspParser (string filename, TextReader input)
53 this.filename = filename;
54 fileText = input.ReadToEnd ();
55 StringReader reader = new StringReader (fileText);
56 tokenizer = new AspTokenizer (reader);
59 public int BeginLine {
60 get { return beginLine; }
63 public int BeginColumn {
64 get { return beginColumn; }
68 get { return endLine; }
71 public int EndColumn {
72 get { return endColumn; }
75 public string PlainText {
77 if (beginPosition >= endPosition)
80 return fileText.Substring (beginPosition, endPosition - beginPosition);
84 public string Filename {
85 get { return filename; }
88 public string VerbatimID {
90 tokenizer.Verbatim = true;
91 verbatimID = value.ToUpper ();
95 bool Eat (int expected_token)
97 if (tokenizer.get_token () != expected_token) {
98 tokenizer.put_back ();
102 endLine = tokenizer.EndLine;
103 endColumn = tokenizer.EndColumn;
109 beginLine = tokenizer.BeginLine;
110 beginColumn = tokenizer.BeginColumn;
111 beginPosition = tokenizer.Position - 1;
116 endLine = tokenizer.EndLine;
117 endColumn = tokenizer.EndColumn;
118 endPosition = tokenizer.Position;
125 TagAttributes attributes;
126 TagType tagtype = TagType.Text;
127 StringBuilder text = new StringBuilder ();
129 while ((token = tokenizer.get_token ()) != Token.EOF) {
132 if (tokenizer.Verbatim){
133 string end_verbatim = "</" + verbatimID + ">";
134 string verbatim_text = GetVerbatim (token, end_verbatim);
136 if (verbatim_text == null)
137 OnError ("Unexpected EOF processing " + verbatimID);
139 tokenizer.Verbatim = false;
142 endPosition -= end_verbatim.Length;
143 OnTextParsed (verbatim_text);
144 beginPosition = endPosition;
145 endPosition += end_verbatim.Length;
146 OnTagParsed (TagType.Close, verbatimID, null);
151 GetTag (out tagtype, out id, out attributes);
153 if (tagtype == TagType.ServerComment)
156 if (tagtype == TagType.Text)
159 OnTagParsed (tagtype, id, attributes);
164 if (tokenizer.Value.Trim () == "" && tagtype == TagType.Directive) {
170 text.Append (tokenizer.Value);
171 token = tokenizer.get_token ();
172 } while (token != '<' && token != Token.EOF);
174 tokenizer.put_back ();
176 OnTextParsed (text.ToString ());
180 bool GetInclude (string str, out string pathType, out string filename)
184 str = str.Substring (2).Trim ();
185 int len = str.Length;
186 int lastQuote = str.LastIndexOf ('"');
187 if (len < 10 || lastQuote != len - 1)
190 if (!str.ToLower ().StartsWith ("#include "))
193 str = str.Substring (9).Trim ();
194 bool isfile = (str.ToLower ().StartsWith ("file"));
195 if (!isfile && !str.ToLower ().StartsWith ("virtual"))
198 pathType = (isfile) ? "file" : "virtual";
199 if (str.Length < pathType.Length + 3)
202 str = str.Substring (pathType.Length).Trim ();
203 if (str.Length < 3 || str [0] != '=')
207 for (; index < str.Length; index++) {
208 if (Char.IsWhiteSpace (str [index]))
210 else if (str [index] == '"')
214 if (index == str.Length || index == lastQuote)
217 str = str.Substring (index);
218 if (str.Length == 2) { // only quotes
219 OnError ("Empty file name.");
223 filename = str.Trim ().Substring (index, str.Length - 2);
224 if (filename.LastIndexOf ('"') != -1)
225 return false; // file=""" -> no error
230 void GetTag (out TagType tagtype, out string id, out TagAttributes attributes)
232 int token = tokenizer.get_token ();
234 tagtype = TagType.ServerComment;
239 GetServerTag (out tagtype, out id, out attributes);
242 if (!Eat (Token.IDENTIFIER))
243 OnError ("expecting TAGNAME");
245 id = tokenizer.Value;
247 OnError ("expecting '>'. Got '" + id + "'");
249 tagtype = TagType.Close;
252 bool double_dash = Eat (Token.DOUBLEDASH);
254 tokenizer.put_back ();
256 tokenizer.Verbatim = true;
257 string end = double_dash ? "-->" : ">";
258 string comment = GetVerbatim (tokenizer.get_token (), end);
259 tokenizer.Verbatim = false;
261 OnError ("Unfinished HTML comment/DTD");
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);
269 tagtype = TagType.Text;
270 id = "<!" + comment + end;
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;
280 id = tokenizer.Value;
282 attributes = GetAttributes ();
283 } catch (Exception e) {
288 tagtype = TagType.Tag;
289 if (Eat ('/') && Eat ('>'))
290 tagtype = TagType.SelfClosing;
292 OnError ("expecting '>'. Got '" + tokenizer.Value + "'");
297 tagtype = TagType.Text;
298 tokenizer.InTag = false;
299 id = "<" + tokenizer.Value;
304 TagAttributes GetAttributes ()
307 TagAttributes attributes;
310 attributes = new TagAttributes ();
311 while ((token = tokenizer.get_token ()) != Token.EOF){
312 if (token != Token.IDENTIFIER)
314 id = tokenizer.Value;
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 (), "%>"));
322 OnError ("expected ATTVALUE");
327 attributes.Add (id, null);
331 tokenizer.put_back ();
335 string GetVerbatim (int token, string end)
337 StringBuilder vb_text = new StringBuilder ();
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 ();
346 while (token != Token.EOF){
347 if (Char.ToUpper ((char) token) == end [i]){
348 if (++i >= end.Length)
350 token = tokenizer.get_token ();
353 for (int j = 0; j < i; j++)
354 vb_text.Append (end [j]);
358 vb_text.Append ((char) token);
359 token = tokenizer.get_token ();
362 return RemoveComments (vb_text.ToString ());
365 string RemoveComments (string text)
368 int start = text.IndexOf ("<%--");
370 while (start != -1) {
371 end = text.IndexOf ("--%>");
372 if (end == -1 || end <= start + 1)
375 text = text.Remove (start, end - start + 4);
376 start = text.IndexOf ("<%--");
382 void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
387 tagtype = TagType.Directive;
389 if (Eat (Token.DIRECTIVE))
390 id = tokenizer.Value;
392 attributes = GetAttributes ();
393 if (!Eat ('%') || !Eat ('>'))
394 OnError ("expecting '%>'");
399 if (Eat (Token.DOUBLEDASH)) {
400 tokenizer.Verbatim = true;
401 inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
402 tokenizer.Verbatim = false;
405 tagtype = TagType.ServerComment;
412 databinding = !varname && Eat ('#');
414 tokenizer.Verbatim = true;
415 inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
416 tokenizer.Verbatim = false;
419 tagtype = (databinding ? TagType.DataBinding :
420 (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
423 public event ParseErrorHandler Error;
424 public event TagParsedHandler TagParsed;
425 public event TextParsedHandler TextParsed;
427 void OnError (string msg)
433 void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
435 if (TagParsed != null)
436 TagParsed (this, tagtype, id, attributes);
439 void OnTextParsed (string text)
441 if (TextParsed != null)
442 TextParsed (this, text);