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 id = tokenizer.Value;
248 attributes = GetAttributes ();
249 } catch (Exception e) {
254 tagtype = TagType.Tag;
255 if (Eat ('/') && Eat ('>'))
256 tagtype = TagType.SelfClosing;
258 OnError ("expecting '>'. Got '" + tokenizer.Value + "'");
262 tagtype = TagType.Text;
263 tokenizer.InTag = false;
264 id = "<" + tokenizer.Value;
269 TagAttributes GetAttributes ()
272 TagAttributes attributes;
275 attributes = new TagAttributes ();
276 while ((token = tokenizer.get_token ()) != Token.EOF){
277 if (token != Token.IDENTIFIER)
279 id = tokenizer.Value;
281 if (Eat (Token.ATTVALUE)){
282 attributes.Add (id, tokenizer.Value);
284 //TODO: support data binding syntax without quotes
285 OnError ("expected ATTVALUE");
290 attributes.Add (id, null);
294 tokenizer.put_back ();
298 string GetVerbatim (int token, string end)
300 StringBuilder vb_text = new StringBuilder ();
303 if (tokenizer.Value.Length > 1){
304 // May be we have a put_back token that is not a single character
305 vb_text.Append (tokenizer.Value);
306 token = tokenizer.get_token ();
309 while (token != Token.EOF){
310 if (Char.ToUpper ((char) token) == end [i]){
311 if (++i >= end.Length)
313 token = tokenizer.get_token ();
316 for (int j = 0; j < i; j++)
317 vb_text.Append (end [j]);
321 vb_text.Append ((char) token);
322 token = tokenizer.get_token ();
325 return RemoveComments (vb_text.ToString ());
328 string RemoveComments (string text)
331 int start = text.IndexOf ("<%--");
333 while (start != -1) {
334 end = text.IndexOf ("--%>");
335 if (end == -1 || end <= start + 1)
338 text = text.Remove (start, end - start + 4);
339 start = text.IndexOf ("<%--");
345 void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
350 tagtype = TagType.Directive;
352 if (Eat (Token.DIRECTIVE))
353 id = tokenizer.Value;
355 attributes = GetAttributes ();
356 if (!Eat ('%') || !Eat ('>'))
357 OnError ("expecting '%>'");
362 if (Eat (Token.DOUBLEDASH)) {
363 tokenizer.Verbatim = true;
364 inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
365 tokenizer.Verbatim = false;
368 tagtype = TagType.ServerComment;
375 databinding = !varname && Eat ('#');
377 tokenizer.Verbatim = true;
378 inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
379 tokenizer.Verbatim = false;
382 tagtype = (databinding ? TagType.DataBinding :
383 (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
386 public event ParseErrorHandler Error;
387 public event TagParsedHandler TagParsed;
388 public event TextParsedHandler TextParsed;
390 void OnError (string msg)
396 void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
398 if (TagParsed != null)
399 TagParsed (this, tagtype, id, attributes);
402 void OnTextParsed (string text)
404 if (TextParsed != null)
405 TextParsed (this, text);