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;
291 } else if (!Eat ('>')) {
292 if (attributes.IsRunAtServer ()) {
293 OnError ("The server tag is not well formed.");
296 tokenizer.Verbatim = true;
297 attributes.Add ("", GetVerbatim (tokenizer.get_token (), ">") + ">");
298 tokenizer.Verbatim = false;
304 tagtype = TagType.Text;
305 tokenizer.InTag = false;
306 id = "<" + tokenizer.Value;
311 TagAttributes GetAttributes ()
314 TagAttributes attributes;
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;
328 if (token != Token.IDENTIFIER)
331 id = tokenizer.Value;
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;
342 OnError ("expected ATTVALUE");
346 attributes.Add (id, null);
350 tokenizer.put_back ();
354 string GetVerbatim (int token, string end)
356 StringBuilder vb_text = new StringBuilder ();
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 ();
365 while (token != Token.EOF){
366 if (Char.ToUpper ((char) token) == end [i]){
367 if (++i >= end.Length)
369 token = tokenizer.get_token ();
372 for (int j = 0; j < i; j++)
373 vb_text.Append (end [j]);
377 vb_text.Append ((char) token);
378 token = tokenizer.get_token ();
381 return RemoveComments (vb_text.ToString ());
384 string RemoveComments (string text)
387 int start = text.IndexOf ("<%--");
389 while (start != -1) {
390 end = text.IndexOf ("--%>");
391 if (end == -1 || end <= start + 1)
394 text = text.Remove (start, end - start + 4);
395 start = text.IndexOf ("<%--");
401 void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
404 bool old = tokenizer.ExpectAttrValue;
406 tokenizer.ExpectAttrValue = false;
408 tokenizer.ExpectAttrValue = old;
409 tagtype = TagType.Directive;
411 if (Eat (Token.DIRECTIVE))
412 id = tokenizer.Value;
414 attributes = GetAttributes ();
415 if (!Eat ('%') || !Eat ('>'))
416 OnError ("expecting '%>'");
421 if (Eat (Token.DOUBLEDASH)) {
422 tokenizer.ExpectAttrValue = old;
423 tokenizer.Verbatim = true;
424 inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
425 tokenizer.Verbatim = false;
428 tagtype = TagType.ServerComment;
432 tokenizer.ExpectAttrValue = old;
436 databinding = !varname && Eat ('#');
438 tokenizer.Verbatim = true;
439 inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
440 tokenizer.Verbatim = false;
443 tagtype = (databinding ? TagType.DataBinding :
444 (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
447 public event ParseErrorHandler Error;
448 public event TagParsedHandler TagParsed;
449 public event TextParsedHandler TextParsed;
451 void OnError (string msg)
457 void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
459 if (TagParsed != null)
460 TagParsed (this, tagtype, id, attributes);
463 void OnTextParsed (string text)
465 if (TextParsed != null)
466 TextParsed (this, text);