~ubuntu-branches/ubuntu/feisty/monodevelop/feisty

« back to all changes in this revision

Viewing changes to Extras/AspNetEdit/AspNetEdit.Editor.Persistence/AspTokenizer.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-08-18 00:51:23 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060818005123-5iit07y0j7wjg55f
Tags: 0.11+svn20060818-0ubuntu1
* New SVN snapshot
  + Works with Gtk# 2.9.0
* debian/control:
  + Updated Build-Depends
* debian/patches/use_nunit2.2.dpatch,
  debian/patches/use_real_libs.dpatch:
  + Updated
* debian/patches/versioncontrol_buildfix.dpatch:
  + Fix build failure in the version control addin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// System.Web.Compilation.AspTokenizer
 
3
//
 
4
// Authors:
 
5
//      Gonzalo Paniagua Javier (gonzalo@ximian.com)
 
6
//
 
7
// (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
 
8
//
 
9
 
 
10
//
 
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:
 
18
// 
 
19
// The above copyright notice and this permission notice shall be
 
20
// included in all copies or substantial portions of the Software.
 
21
// 
 
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.
 
29
//
 
30
 
 
31
using System;
 
32
using System.Collections;
 
33
using System.IO;
 
34
using System.Text;
 
35
using System.Web;
 
36
 
 
37
namespace AspNetEdit.Editor.Persistence
 
38
{
 
39
        class Token
 
40
        {
 
41
                public const int EOF            = 0x0200000;
 
42
                public const int IDENTIFIER     = 0x0200001;
 
43
                public const int DIRECTIVE      = 0x0200002;
 
44
                public const int ATTVALUE       = 0x0200003;
 
45
                public const int TEXT           = 0x0200004;
 
46
                public const int DOUBLEDASH     = 0x0200005;
 
47
                public const int CLOSING        = 0x0200006;
 
48
        }
 
49
 
 
50
        class AspTokenizer
 
51
        {
 
52
                TextReader sr;
 
53
                int current_token;
 
54
                StringBuilder sb, odds;
 
55
                int col, line;
 
56
                int begcol, begline;
 
57
                int position;
 
58
                bool inTag;
 
59
                bool expectAttrValue;
 
60
                bool hasPutBack;
 
61
                bool verbatim;
 
62
                bool have_value;
 
63
                bool have_unget;
 
64
                int unget_value;
 
65
                string val;
 
66
                
 
67
                public AspTokenizer (TextReader reader)
 
68
                {
 
69
                        this.sr = reader;
 
70
                        sb = new StringBuilder ();
 
71
                        odds= new StringBuilder();
 
72
                        col = line = 1;
 
73
                        hasPutBack = inTag = false;
 
74
                }
 
75
 
 
76
                public bool Verbatim
 
77
                {
 
78
                        get { return verbatim; }
 
79
                        set { verbatim = value; }
 
80
                }
 
81
 
 
82
                public void put_back ()
 
83
                {
 
84
                        if (hasPutBack)
 
85
                                throw new HttpException ("put_back called twice!");
 
86
                        
 
87
                        hasPutBack = true;
 
88
                        position -= Value.Length;
 
89
                }
 
90
                
 
91
                public int get_token ()
 
92
                {
 
93
                        if (hasPutBack){
 
94
                                hasPutBack = false;
 
95
                                position += Value.Length;
 
96
                                return current_token;
 
97
                        }
 
98
 
 
99
                        begline = line;
 
100
                        begcol = col;
 
101
                        have_value = false;
 
102
                        current_token = NextToken ();
 
103
                        return current_token;
 
104
                }
 
105
 
 
106
                bool is_identifier_start_character (char c)
 
107
                {
 
108
                        return (Char.IsLetter (c) || c == '_' );
 
109
                }
 
110
 
 
111
                bool is_identifier_part_character (char c)
 
112
                {
 
113
                        return (Char.IsLetterOrDigit (c) || c == '_' || c == '-');
 
114
                }
 
115
 
 
116
                void ungetc (int value)
 
117
                {
 
118
                        have_unget = true;
 
119
                        unget_value = value;
 
120
 
 
121
                        // Only '/' passes through here now.
 
122
                        // If we ever let \n here, update 'line'
 
123
                        position--;
 
124
                        col--;
 
125
                }
 
126
                
 
127
                int read_char ()
 
128
                {
 
129
                        int c;
 
130
                        if (have_unget) {
 
131
                                c = unget_value;
 
132
                                have_unget = false;
 
133
                        } else {
 
134
                                c = sr.Read ();
 
135
                        }
 
136
 
 
137
                        if (c == '\r' && sr.Peek () == '\n') {
 
138
                                c = sr.Read ();
 
139
                                position++;
 
140
                        }
 
141
 
 
142
                        if (c == '\n'){
 
143
                                col = -1;
 
144
                                line++;
 
145
                        }
 
146
 
 
147
                        if (c != -1) {
 
148
                                col++;
 
149
                                position++;
 
150
                        }
 
151
 
 
152
                        return c;
 
153
                }
 
154
 
 
155
                int ReadAttValue (int start)
 
156
                {
 
157
                        int quoteChar = 0;
 
158
                        bool quoted = false;
 
159
 
 
160
                        if (start == '"' || start == '\'') {
 
161
                                quoteChar = start;
 
162
                                quoted = true;
 
163
                        } else {
 
164
                                sb.Append ((char) start);
 
165
                        }
 
166
 
 
167
                        int c;
 
168
                        int last = 0;
 
169
                        bool inServerTag = false;
 
170
                        
 
171
                        while ((c = sr.Peek ()) != -1) {
 
172
                                if (c == '%' && last == '<') {
 
173
                                        inServerTag = true;
 
174
                                } else if (inServerTag && c == '>' && last == '%') {
 
175
                                        inServerTag = false;
 
176
                                } else if (!inServerTag) {
 
177
                                        if (!quoted && c == '/') {
 
178
                                                read_char ();
 
179
                                                c = sr.Peek ();
 
180
                                                if (c == -1) {
 
181
                                                        c = '/';
 
182
                                                } else if (c == '>') {
 
183
                                                        ungetc ('/');
 
184
                                                        break;
 
185
                                                }
 
186
                                        } else if (!quoted && (c == '>' || Char.IsWhiteSpace ((char) c))) {
 
187
                                                break;
 
188
                                        } else if (quoted && c == quoteChar && last != '\\') {
 
189
                                                read_char ();
 
190
                                                break;
 
191
                                        }
 
192
                                }
 
193
 
 
194
                                sb.Append ((char) c);
 
195
                                read_char ();
 
196
                                last = c;
 
197
                        }
 
198
 
 
199
                        return Token.ATTVALUE;
 
200
                }
 
201
 
 
202
                int NextToken ()
 
203
                {
 
204
                        int c;
 
205
                        
 
206
                        sb.Length = 0;
 
207
                        odds.Length=0;
 
208
                        while ((c = read_char ()) != -1){
 
209
                                if (verbatim){
 
210
                                        inTag = false;
 
211
                                        sb.Append  ((char) c);
 
212
                                        return c;
 
213
                                }
 
214
 
 
215
                                if (inTag && expectAttrValue && (c == '"' || c == '\''))
 
216
                                        return ReadAttValue (c);
 
217
                                
 
218
                                if (c == '<'){
 
219
                                        inTag = true;
 
220
                                        sb.Append ((char) c);
 
221
                                        return c;
 
222
                                }
 
223
 
 
224
                                if (c == '>'){
 
225
                                        inTag = false;
 
226
                                        sb.Append ((char) c);
 
227
                                        return c;
 
228
                                }
 
229
 
 
230
                                if (current_token == '<' && "%/!".IndexOf ((char) c) != -1){
 
231
                                        sb.Append ((char) c);
 
232
                                        return c;
 
233
                                }
 
234
 
 
235
                                if (inTag && current_token == '%' && "@#=".IndexOf ((char) c) != -1){
 
236
                                        sb.Append ((char) c);
 
237
                                        return c;
 
238
                                }
 
239
 
 
240
                                if (inTag && c == '-' && sr.Peek () == '-'){
 
241
                                        sb.Append ("--");
 
242
                                        read_char ();
 
243
                                        return Token.DOUBLEDASH;
 
244
                                }
 
245
 
 
246
                                if (!inTag){
 
247
                                        sb.Append ((char) c);
 
248
                                        while ((c = sr.Peek ()) != -1 && c != '<')
 
249
                                                sb.Append ((char) read_char ());
 
250
 
 
251
                                        return (c != -1 || sb.Length > 0) ? Token.TEXT : Token.EOF;
 
252
                                }
 
253
 
 
254
                                if (inTag && current_token == '=' && !Char.IsWhiteSpace ((char) c))
 
255
                                        return ReadAttValue (c);
 
256
 
 
257
                                if (inTag && is_identifier_start_character ((char) c)){
 
258
                                        sb.Append ((char) c);
 
259
                                        while ((c = sr.Peek ()) != -1) {
 
260
                                                if (!is_identifier_part_character ((char) c) && c != ':')
 
261
                                                        break;
 
262
                                                sb.Append ((char) read_char ());
 
263
                                        }
 
264
 
 
265
                                        if (current_token == '@' && Directive.IsDirective (sb.ToString ()))
 
266
                                                return Token.DIRECTIVE;
 
267
                                        
 
268
                                        return Token.IDENTIFIER;
 
269
                                }
 
270
 
 
271
                                if (!Char.IsWhiteSpace ((char) c)) {
 
272
                                        sb.Append  ((char) c);
 
273
                                        return c;
 
274
                                }
 
275
                                // keep otherwise discarded characters in case we need.
 
276
                                odds.Append((char) c);
 
277
                        }
 
278
 
 
279
                        return Token.EOF;
 
280
                }
 
281
 
 
282
                public string Value {
 
283
                        get {
 
284
                                if (have_value)
 
285
                                        return val;
 
286
 
 
287
                                have_value = true;
 
288
                                val = sb.ToString ();
 
289
                                return val;
 
290
                        }
 
291
                }
 
292
 
 
293
                public string Odds {
 
294
                        get {
 
295
                                return odds.ToString();
 
296
                        }
 
297
                }
 
298
 
 
299
                public bool InTag {
 
300
                        get { return inTag; }
 
301
                        set { inTag = value; }
 
302
                }
 
303
 
 
304
                // Hack for preventing confusion with VB comments (see bug #63451)
 
305
                public bool ExpectAttrValue {
 
306
                        get { return expectAttrValue; }
 
307
                        set { expectAttrValue = value; }
 
308
                }
 
309
                
 
310
                public int BeginLine {
 
311
                        get { return begline; }
 
312
                }
 
313
 
 
314
                public int BeginColumn {
 
315
                        get { return begcol; }
 
316
                }
 
317
 
 
318
                public int EndLine {
 
319
                        get { return line; }
 
320
                }
 
321
 
 
322
                public int EndColumn {
 
323
                        get { return col; }
 
324
                }
 
325
 
 
326
                public int Position {
 
327
                        get { return position; }
 
328
                }
 
329
        }
 
330
}
 
331