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
;
66 public AspTokenizer (TextReader reader
)
69 sb
= new StringBuilder ();
70 odds
= new StringBuilder();
72 hasPutBack
= inTag
= false;
77 get { return verbatim; }
78 set { verbatim = value; }
81 public void put_back ()
84 throw new HttpException ("put_back called twice!");
87 position
-= Value
.Length
;
90 public int get_token ()
94 position
+= Value
.Length
;
101 current_token
= NextToken ();
102 return current_token
;
105 bool is_identifier_start_character (char c
)
107 return (Char
.IsLetter (c
) || c
== '_' );
110 bool is_identifier_part_character (char c
)
112 return (Char
.IsLetterOrDigit (c
) || c
== '_' || c
== '-');
115 void ungetc (int value)
120 // Only '/' passes through here now.
121 // If we ever let \n here, update 'line'
136 if (c
== '\r' && sr
.Peek () == '\n') {
154 int ReadAttValue (int start
)
159 if (start
== '"' || start
== '\'') {
163 sb
.Append ((char) start
);
168 bool inServerTag
= false;
170 while ((c
= sr
.Peek ()) != -1) {
171 if (c
== '%' && last
== '<') {
173 } else if (inServerTag
&& c
== '>' && last
== '%') {
175 } else if (!inServerTag
) {
176 if (!quoted
&& c
== '/') {
181 } else if (c
== '>') {
185 } else if (!quoted
&& (c
== '>' || Char
.IsWhiteSpace ((char) c
))) {
187 } else if (quoted
&& c
== quoteChar
&& last
!= '\\') {
193 sb
.Append ((char) c
);
198 return Token
.ATTVALUE
;
207 while ((c
= read_char ()) != -1){
210 sb
.Append ((char) c
);
214 if (inTag
&& expectAttrValue
&& (c
== '"' || c
== '\''))
215 return ReadAttValue (c
);
219 sb
.Append ((char) c
);
225 sb
.Append ((char) c
);
229 if (current_token
== '<' && "%/!".IndexOf ((char) c
) != -1){
230 sb
.Append ((char) c
);
234 if (inTag
&& current_token
== '%' && "@#=".IndexOf ((char) c
) != -1){
235 sb
.Append ((char) c
);
239 if (inTag
&& c
== '-' && sr
.Peek () == '-'){
242 return Token
.DOUBLEDASH
;
246 sb
.Append ((char) c
);
247 while ((c
= sr
.Peek ()) != -1 && c
!= '<')
248 sb
.Append ((char) read_char ());
250 return (c
!= -1 || sb
.Length
> 0) ? Token
.TEXT
: Token
.EOF
;
253 if (inTag
&& current_token
== '=' && !Char
.IsWhiteSpace ((char) c
))
254 return ReadAttValue (c
);
256 if (inTag
&& is_identifier_start_character ((char) c
)){
257 sb
.Append ((char) c
);
258 while ((c
= sr
.Peek ()) != -1) {
259 if (!is_identifier_part_character ((char) c
) && c
!= ':')
261 sb
.Append ((char) read_char ());
264 if (current_token
== '@' && Directive
.IsDirective (sb
.ToString ()))
265 return Token
.DIRECTIVE
;
267 return Token
.IDENTIFIER
;
270 if (!Char
.IsWhiteSpace ((char) c
)) {
271 sb
.Append ((char) c
);
274 // keep otherwise discarded characters in case we need.
275 odds
.Append((char) c
);
281 public string Value
{
287 val
= sb
.ToString ();
294 return odds
.ToString();
299 get { return inTag; }
300 set { inTag = value; }
303 // Hack for preventing confusion with VB comments (see bug #63451)
304 public bool ExpectAttrValue
{
305 get { return expectAttrValue; }
306 set { expectAttrValue = value; }
309 public int BeginLine
{
310 get { return begline; }
313 public int BeginColumn
{
314 get { return begcol; }
321 public int EndColumn
{
325 public int Position
{
326 get { return position; }