2 // System.Web.Compilation.AspTokenizer
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.
32 using System.Collections;
36 namespace System.Web.Compilation
40 public const int EOF = 0x0200000;
41 public const int IDENTIFIER = 0x0200001;
42 public const int DIRECTIVE = 0x0200002;
43 public const int ATTVALUE = 0x0200003;
44 public const int TEXT = 0x0200004;
45 public const int DOUBLEDASH = 0x0200005;
46 public const int CLOSING = 0x0200006;
51 static char [] lfcr = new char [] { '\n', '\r' };
54 StringBuilder sb, odds;
60 bool alternatingQuotes;
68 public AspTokenizer (TextReader reader)
71 sb = new StringBuilder ();
72 odds= new StringBuilder();
74 hasPutBack = inTag = false;
79 get { return verbatim; }
80 set { verbatim = value; }
83 public void put_back ()
86 throw new HttpException ("put_back called twice!");
89 position -= Value.Length;
92 public int get_token ()
96 position += Value.Length;
103 current_token = NextToken ();
104 return current_token;
107 bool is_identifier_start_character (char c)
109 return (Char.IsLetter (c) || c == '_' );
112 bool is_identifier_part_character (char c)
114 return (Char.IsLetterOrDigit (c) || c == '_' || c == '-');
117 void ungetc (int value)
122 // Only '/' passes through here now.
123 // If we ever let \n here, update 'line'
138 if (c == '\r' && sr.Peek () == '\n') {
156 int ReadAttValue (int start)
161 if (start == '"' || start == '\'') {
165 sb.Append ((char) start);
170 bool inServerTag = false;
171 alternatingQuotes = true;
173 while ((c = sr.Peek ()) != -1) {
174 if (c == '%' && last == '<') {
176 } else if (inServerTag && c == '>' && last == '%') {
178 } else if (!inServerTag) {
179 if (!quoted && c == '/') {
184 } else if (c == '>') {
188 } else if (!quoted && (c == '>' || Char.IsWhiteSpace ((char) c))) {
190 } else if (quoted && c == quoteChar && last != '\\') {
194 } else if (quoted && c == quoteChar) {
195 alternatingQuotes = false;
198 sb.Append ((char) c);
203 return Token.ATTVALUE;
212 while ((c = read_char ()) != -1){
215 sb.Append ((char) c);
219 if (inTag && expectAttrValue && (c == '"' || c == '\''))
220 return ReadAttValue (c);
224 sb.Append ((char) c);
230 sb.Append ((char) c);
234 if (current_token == '<' && "%/!".IndexOf ((char) c) != -1){
235 sb.Append ((char) c);
239 if (inTag && current_token == '%' && "@#=".IndexOf ((char) c) != -1){
240 if (odds.Length == 0 || odds.ToString ().IndexOfAny (lfcr) < 0) {
241 sb.Append ((char) c);
244 sb.Append ((char) c);
248 if (inTag && c == '-' && sr.Peek () == '-'){
251 return Token.DOUBLEDASH;
255 sb.Append ((char) c);
256 while ((c = sr.Peek ()) != -1 && c != '<')
257 sb.Append ((char) read_char ());
259 return (c != -1 || sb.Length > 0) ? Token.TEXT : Token.EOF;
262 if (inTag && current_token == '=' && !Char.IsWhiteSpace ((char) c))
263 return ReadAttValue (c);
265 if (inTag && is_identifier_start_character ((char) c)){
266 sb.Append ((char) c);
267 while ((c = sr.Peek ()) != -1) {
268 if (!is_identifier_part_character ((char) c) && c != ':')
270 sb.Append ((char) read_char ());
273 if (current_token == '@' && Directive.IsDirective (sb.ToString ()))
274 return Token.DIRECTIVE;
276 return Token.IDENTIFIER;
279 if (!Char.IsWhiteSpace ((char) c)) {
280 sb.Append ((char) c);
283 // keep otherwise discarded characters in case we need.
284 odds.Append((char) c);
290 public string Value {
296 val = sb.ToString ();
303 return odds.ToString();
308 get { return inTag; }
309 set { inTag = value; }
312 // Hack for preventing confusion with VB comments (see bug #63451)
313 public bool ExpectAttrValue {
314 get { return expectAttrValue; }
315 set { expectAttrValue = value; }
318 public bool AlternatingQuotes {
319 get { return alternatingQuotes; }
322 public int BeginLine {
323 get { return begline; }
326 public int BeginColumn {
327 get { return begcol; }
334 public int EndColumn {
338 public int Position {
339 get { return position; }