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;
53 StringBuilder sb, odds;
59 bool alternatingQuotes;
67 public AspTokenizer (TextReader reader)
70 sb = new StringBuilder ();
71 odds= new StringBuilder();
73 hasPutBack = inTag = false;
78 get { return verbatim; }
79 set { verbatim = value; }
82 public void put_back ()
85 throw new HttpException ("put_back called twice!");
88 position -= Value.Length;
91 public int get_token ()
95 position += Value.Length;
102 current_token = NextToken ();
103 return current_token;
106 bool is_identifier_start_character (char c)
108 return (Char.IsLetter (c) || c == '_' );
111 bool is_identifier_part_character (char c)
113 return (Char.IsLetterOrDigit (c) || c == '_' || c == '-');
116 void ungetc (int value)
121 // Only '/' passes through here now.
122 // If we ever let \n here, update 'line'
137 if (c == '\r' && sr.Peek () == '\n') {
155 int ReadAttValue (int start)
160 if (start == '"' || start == '\'') {
164 sb.Append ((char) start);
169 bool inServerTag = false;
170 alternatingQuotes = true;
172 while ((c = sr.Peek ()) != -1) {
173 if (c == '%' && last == '<') {
175 } else if (inServerTag && c == '>' && last == '%') {
177 } else if (!inServerTag) {
178 if (!quoted && c == '/') {
183 } else if (c == '>') {
187 } else if (!quoted && (c == '>' || Char.IsWhiteSpace ((char) c))) {
189 } else if (quoted && c == quoteChar && last != '\\') {
193 } else if (quoted && c == quoteChar) {
194 alternatingQuotes = false;
197 sb.Append ((char) c);
202 return Token.ATTVALUE;
211 while ((c = read_char ()) != -1){
214 sb.Append ((char) c);
218 if (inTag && expectAttrValue && (c == '"' || c == '\''))
219 return ReadAttValue (c);
223 sb.Append ((char) c);
229 sb.Append ((char) c);
233 if (current_token == '<' && "%/!".IndexOf ((char) c) != -1){
234 sb.Append ((char) c);
238 if (inTag && current_token == '%' && "@#=".IndexOf ((char) c) != -1){
239 sb.Append ((char) c);
243 if (inTag && c == '-' && sr.Peek () == '-'){
246 return Token.DOUBLEDASH;
250 sb.Append ((char) c);
251 while ((c = sr.Peek ()) != -1 && c != '<')
252 sb.Append ((char) read_char ());
254 return (c != -1 || sb.Length > 0) ? Token.TEXT : Token.EOF;
257 if (inTag && current_token == '=' && !Char.IsWhiteSpace ((char) c))
258 return ReadAttValue (c);
260 if (inTag && is_identifier_start_character ((char) c)){
261 sb.Append ((char) c);
262 while ((c = sr.Peek ()) != -1) {
263 if (!is_identifier_part_character ((char) c) && c != ':')
265 sb.Append ((char) read_char ());
268 if (current_token == '@' && Directive.IsDirective (sb.ToString ()))
269 return Token.DIRECTIVE;
271 return Token.IDENTIFIER;
274 if (!Char.IsWhiteSpace ((char) c)) {
275 sb.Append ((char) c);
278 // keep otherwise discarded characters in case we need.
279 odds.Append((char) c);
285 public string Value {
291 val = sb.ToString ();
298 return odds.ToString();
303 get { return inTag; }
304 set { inTag = value; }
307 // Hack for preventing confusion with VB comments (see bug #63451)
308 public bool ExpectAttrValue {
309 get { return expectAttrValue; }
310 set { expectAttrValue = value; }
313 public bool AlternatingQuotes {
314 get { return alternatingQuotes; }
317 public int BeginLine {
318 get { return begline; }
321 public int BeginColumn {
322 get { return begcol; }
329 public int EndColumn {
333 public int Position {
334 get { return position; }