2 // System.Web.Compilation.AspParser
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
10 using System.Collections;
14 namespace System.Web.Compilation
16 delegate void ParseErrorHandler (ILocation location, string message);
17 delegate void TextParsedHandler (ILocation location, string text);
18 delegate void TagParsedHandler (ILocation location, TagType tagtype, string id, TagAttributes attributes);
20 class AspParser : ILocation
22 AspTokenizer tokenizer;
23 int beginLine, endLine;
24 int beginColumn, endColumn;
25 int beginPosition, endPosition;
30 public AspParser (string filename, TextReader input)
32 this.filename = filename;
33 fileText = input.ReadToEnd ();
34 StringReader reader = new StringReader (fileText);
35 tokenizer = new AspTokenizer (reader);
38 public int BeginLine {
39 get { return beginLine; }
42 public int BeginColumn {
43 get { return beginColumn; }
47 get { return endLine; }
50 public int EndColumn {
51 get { return endColumn; }
54 public string PlainText {
56 if (beginPosition >= endPosition)
59 return fileText.Substring (beginPosition, endPosition - beginPosition);
63 public string Filename {
64 get { return filename; }
67 public string VerbatimID {
69 tokenizer.Verbatim = true;
70 verbatimID = value.ToUpper ();
74 bool Eat (int expected_token)
76 if (tokenizer.get_token () != expected_token) {
77 tokenizer.put_back ();
81 endLine = tokenizer.EndLine;
82 endColumn = tokenizer.EndColumn;
88 beginLine = tokenizer.BeginLine;
89 beginColumn = tokenizer.BeginColumn;
90 beginPosition = tokenizer.Position - 1;
95 endLine = tokenizer.EndLine;
96 endColumn = tokenizer.EndColumn;
97 endPosition = tokenizer.Position;
104 TagAttributes attributes;
106 StringBuilder text = new StringBuilder ();
108 while ((token = tokenizer.get_token ()) != Token.EOF) {
111 if (tokenizer.Verbatim){
112 string end_verbatim = "</" + verbatimID + ">";
113 string verbatim_text = GetVerbatim (token, end_verbatim);
115 if (verbatim_text == null)
116 OnError ("Unexpected EOF processing " + verbatimID);
118 tokenizer.Verbatim = false;
121 endPosition -= end_verbatim.Length;
122 OnTextParsed (verbatim_text);
123 beginPosition = endPosition;
124 endPosition += end_verbatim.Length;
125 OnTagParsed (TagType.Close, verbatimID, null);
130 GetTag (out tagtype, out id, out attributes);
132 if (tagtype == TagType.ServerComment)
135 if (tagtype == TagType.Text)
138 OnTagParsed (tagtype, id, attributes);
145 text.Append (tokenizer.Value);
146 token = tokenizer.get_token ();
147 } while (token != '<' && token != Token.EOF);
149 tokenizer.put_back ();
151 OnTextParsed (text.ToString ());
155 bool GetInclude (string str, out string pathType, out string filename)
159 str = str.Substring (2).Trim ();
160 int len = str.Length;
161 int lastQuote = str.LastIndexOf ('"');
162 if (len < 10 || lastQuote != len - 1 || !str.StartsWith ("#include "))
165 str = str.Substring (9).Trim ();
166 bool isfile = (str.StartsWith ("file"));
167 if (!isfile && !str.StartsWith ("virtual"))
170 pathType = (isfile) ? "file" : "virtual";
171 if (str.Length < pathType.Length + 3)
174 str = str.Substring (pathType.Length).Trim ();
175 if (str.Length < 3 || str [0] != '=')
179 for (; index < str.Length; index++) {
180 if (Char.IsWhiteSpace (str [index]))
182 else if (str [index] == '"')
186 if (index == str.Length || index == lastQuote)
189 str = str.Substring (index);
190 if (str.Length == 2) { // only quotes
191 OnError ("Empty file name.");
195 filename = str.Trim ().Substring (index, str.Length - 2);
196 if (filename.LastIndexOf ('"') != -1)
197 return false; // file=""" -> no error
202 void GetTag (out TagType tagtype, out string id, out TagAttributes attributes)
204 int token = tokenizer.get_token ();
206 tagtype = TagType.ServerComment;
211 GetServerTag (out tagtype, out id, out attributes);
214 if (!Eat (Token.IDENTIFIER))
215 OnError ("expecting TAGNAME");
217 id = tokenizer.Value;
219 OnError ("expecting '>'. Got '" + id + "'");
221 tagtype = TagType.Close;
224 bool double_dash = Eat (Token.DOUBLEDASH);
226 tokenizer.put_back ();
228 tokenizer.Verbatim = true;
229 string end = double_dash ? "-->" : ">";
230 string comment = GetVerbatim (tokenizer.get_token (), end);
231 tokenizer.Verbatim = false;
233 OnError ("Unfinished HTML comment/DTD");
235 string pathType, filename;
236 if (double_dash && GetInclude (comment, out pathType, out filename)) {
237 tagtype = TagType.Include;
238 attributes = new TagAttributes ();
239 attributes.Add (pathType, filename);
241 tagtype = TagType.Text;
242 id = "<!" + comment + end;
245 case Token.IDENTIFIER:
246 if (this.filename == "@@inner_string@@") {
247 // Actually not tag but "xxx < yyy" stuff in inner_string!
248 tagtype = TagType.Text;
249 tokenizer.InTag = false;
250 id = "<" + tokenizer.Odds + tokenizer.Value;
252 id = tokenizer.Value;
254 attributes = GetAttributes ();
255 } catch (Exception e) {
260 tagtype = TagType.Tag;
261 if (Eat ('/') && Eat ('>'))
262 tagtype = TagType.SelfClosing;
264 OnError ("expecting '>'. Got '" + tokenizer.Value + "'");
269 tagtype = TagType.Text;
270 tokenizer.InTag = false;
271 id = "<" + tokenizer.Value;
276 TagAttributes GetAttributes ()
279 TagAttributes attributes;
282 attributes = new TagAttributes ();
283 while ((token = tokenizer.get_token ()) != Token.EOF){
284 if (token != Token.IDENTIFIER)
286 id = tokenizer.Value;
288 if (Eat (Token.ATTVALUE)){
289 attributes.Add (id, tokenizer.Value);
290 } else if (Eat ('<') && Eat ('%')) {
291 attributes.Add (id, "<%" +
292 GetVerbatim (tokenizer.get_token (), "%>"));
294 OnError ("expected ATTVALUE");
299 attributes.Add (id, null);
303 tokenizer.put_back ();
307 string GetVerbatim (int token, string end)
309 StringBuilder vb_text = new StringBuilder ();
312 if (tokenizer.Value.Length > 1){
313 // May be we have a put_back token that is not a single character
314 vb_text.Append (tokenizer.Value);
315 token = tokenizer.get_token ();
318 while (token != Token.EOF){
319 if (Char.ToUpper ((char) token) == end [i]){
320 if (++i >= end.Length)
322 token = tokenizer.get_token ();
325 for (int j = 0; j < i; j++)
326 vb_text.Append (end [j]);
330 vb_text.Append ((char) token);
331 token = tokenizer.get_token ();
334 return RemoveComments (vb_text.ToString ());
337 string RemoveComments (string text)
340 int start = text.IndexOf ("<%--");
342 while (start != -1) {
343 end = text.IndexOf ("--%>");
344 if (end == -1 || end <= start + 1)
347 text = text.Remove (start, end - start + 4);
348 start = text.IndexOf ("<%--");
354 void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
359 tagtype = TagType.Directive;
361 if (Eat (Token.DIRECTIVE))
362 id = tokenizer.Value;
364 attributes = GetAttributes ();
365 if (!Eat ('%') || !Eat ('>'))
366 OnError ("expecting '%>'");
371 if (Eat (Token.DOUBLEDASH)) {
372 tokenizer.Verbatim = true;
373 inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
374 tokenizer.Verbatim = false;
377 tagtype = TagType.ServerComment;
384 databinding = !varname && Eat ('#');
386 tokenizer.Verbatim = true;
387 inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
388 tokenizer.Verbatim = false;
391 tagtype = (databinding ? TagType.DataBinding :
392 (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
395 public event ParseErrorHandler Error;
396 public event TagParsedHandler TagParsed;
397 public event TextParsedHandler TextParsed;
399 void OnError (string msg)
405 void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
407 if (TagParsed != null)
408 TagParsed (this, tagtype, id, attributes);
411 void OnTextParsed (string text)
413 if (TextParsed != null)
414 TextParsed (this, text);