(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap.Utilclass / SchemaTokenCreator.cs
blob8ba3cd59285233d127a6b9504dcbf2b7d44ca7f8
1 /******************************************************************************
2 * The MIT License
3 * Copyright (c) 2003 Novell Inc. www.novell.com
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the Software), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *******************************************************************************/
24 // Novell.Directory.Ldap.Utilclass.SchemaTokenCreator.cs
26 // Author:
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
32 using System;
34 namespace Novell.Directory.Ldap.Utilclass
37 public class SchemaTokenCreator
39 private string basestring;
40 private bool cppcomments=false; // C++ style comments enabled
41 private bool ccomments=false; // C style comments enabled
42 private bool iseolsig=false;
43 private bool cidtolower;
44 private bool pushedback;
45 private int peekchar;
46 private sbyte[] ctype;
47 private int linenumber=1;
48 private int ichar=1;
49 private char[] buf;
51 private System.IO.StreamReader reader = null;
52 private System.IO.StringReader sreader = null;
53 private System.IO.Stream input = null;
55 public System.String StringValue;
56 public double NumberValue;
57 public int lastttype;
59 private void Initialise()
61 ctype = new sbyte[256];
62 buf = new char[20];
63 peekchar=System.Int32.MaxValue;
64 WordCharacters('a', 'z');
65 WordCharacters('A', 'Z');
66 WordCharacters(128 + 32, 255);
67 WhitespaceCharacters(0, ' ');
68 CommentCharacter('/');
69 QuoteCharacter('"');
70 QuoteCharacter('\'');
71 parseNumbers();
74 public SchemaTokenCreator(System.IO.Stream instream)
76 Initialise();
77 if (instream == null)
79 throw new System.NullReferenceException();
81 input = instream;
84 public SchemaTokenCreator(System.IO.StreamReader r)
86 Initialise();
87 if (r == null)
89 throw new System.NullReferenceException();
91 reader = r;
94 public SchemaTokenCreator(System.IO.StringReader r)
96 Initialise();
97 if (r == null)
99 throw new System.NullReferenceException();
101 sreader = r;
104 public void pushBack()
106 pushedback = true;
109 public int CurrentLine
113 return linenumber;
117 public System.String ToStringValue()
119 System.String strval;
120 switch (lastttype)
123 case (int)TokenTypes.EOF:
124 strval = "EOF";
125 break;
127 case (int) TokenTypes.EOL:
128 strval = "EOL";
129 break;
131 case (int) TokenTypes.WORD:
132 strval = StringValue;
133 break;
135 case (int) TokenTypes.STRING:
136 strval = StringValue;
137 break;
139 case (int) TokenTypes.NUMBER:
140 case (int) TokenTypes.REAL:
141 strval = "n=" + NumberValue;
142 break;
144 default:
146 if (lastttype < 256 && ((ctype[lastttype] & (sbyte)CharacterTypes.STRINGQUOTE) != 0))
148 strval = StringValue;
149 break;
152 char[] s = new char[3];
153 s[0] = s[2] = '\'';
154 s[1] = (char) lastttype;
155 strval = new System.String(s);
156 break;
160 return strval;
163 public void WordCharacters(int min, int max)
165 if (min < 0)
166 min = 0;
167 if (max >= ctype.Length)
168 max = ctype.Length - 1;
169 while (min <= max)
170 ctype[min++] |= (sbyte)CharacterTypes.ALPHABETIC;
173 public void WhitespaceCharacters(int min, int max)
175 if (min < 0)
176 min = 0;
177 if (max >= ctype.Length)
178 max = ctype.Length - 1;
179 while (min <= max)
180 ctype[min++] = (sbyte)CharacterTypes.WHITESPACE;
183 public void OrdinaryCharacters(int min, int max)
185 if (min < 0)
186 min = 0;
187 if (max >= ctype.Length)
188 max = ctype.Length - 1;
189 while (min <= max)
190 ctype[min++] = 0;
193 public void OrdinaryCharacter(int ch)
195 if (ch >= 0 && ch < ctype.Length)
196 ctype[ch] = 0;
199 public void CommentCharacter(int ch)
201 if (ch >= 0 && ch < ctype.Length)
202 ctype[ch] = (sbyte)CharacterTypes.COMMENTCHAR;
205 public void InitTable()
207 for (int i = ctype.Length; --i >= 0; )
208 ctype[i] = 0;
211 public void QuoteCharacter(int ch)
213 if (ch >= 0 && ch < ctype.Length)
214 ctype[ch] = (sbyte)CharacterTypes.STRINGQUOTE;
217 public void parseNumbers()
219 for (int i = '0'; i <= '9'; i++)
220 ctype[i] |= (sbyte)CharacterTypes.NUMERIC;
221 ctype['.'] |= (sbyte)CharacterTypes.NUMERIC;
222 ctype['-'] |= (sbyte)CharacterTypes.NUMERIC;
225 private int read()
227 if (sreader !=null )
229 return sreader.Read();
231 else if (reader != null)
233 return reader.Read();
235 else if (input != null)
236 return input.ReadByte();
237 else
238 throw new System.SystemException();
241 public int nextToken()
243 if (pushedback)
245 pushedback = false;
246 return lastttype;
249 StringValue = null;
251 int curc = peekchar;
252 if (curc < 0)
253 curc = System.Int32.MaxValue;
254 if (curc == (System.Int32.MaxValue - 1))
256 curc = read();
257 if (curc < 0)
258 return lastttype = (int) TokenTypes.EOF;
259 if (curc == '\n')
260 curc = System.Int32.MaxValue;
262 if (curc == System.Int32.MaxValue)
264 curc = read();
265 if (curc < 0)
266 return lastttype = (int) TokenTypes.EOF;
268 lastttype = curc;
269 peekchar = System.Int32.MaxValue;
271 int ctype = curc < 256?this.ctype[curc]:(sbyte)CharacterTypes.ALPHABETIC;
272 while ((ctype & (sbyte)CharacterTypes.WHITESPACE) != 0)
274 if (curc == '\r')
276 linenumber++;
277 if (iseolsig)
279 peekchar = (System.Int32.MaxValue - 1);
280 return lastttype = (int) TokenTypes.EOL;
282 curc = read();
283 if (curc == '\n')
284 curc = read();
286 else
288 if (curc == '\n')
290 linenumber++;
291 if (iseolsig)
293 return lastttype = (int) TokenTypes.EOL;
296 curc = read();
298 if (curc < 0)
299 return lastttype = (int) TokenTypes.EOF;
300 ctype = curc < 256?this.ctype[curc]:(sbyte)CharacterTypes.ALPHABETIC;
303 if ((ctype & (sbyte)CharacterTypes.NUMERIC) != 0)
305 bool checkb = false;
306 if (curc == '-')
308 curc = read();
309 if (curc != '.' && (curc < '0' || curc > '9'))
311 peekchar = curc;
312 return lastttype = '-';
314 checkb = true;
316 double dvar = 0;
317 int tempvar = 0;
318 int checkdec = 0;
319 while (true)
321 if (curc == '.' && checkdec == 0)
322 checkdec = 1;
323 else if ('0' <= curc && curc <= '9')
325 dvar = dvar * 10 + (curc - '0');
326 tempvar += checkdec;
328 else
329 break;
330 curc = read();
332 peekchar = curc;
333 if (tempvar != 0)
335 double divby = 10;
336 tempvar--;
337 while (tempvar > 0)
339 divby *= 10;
340 tempvar--;
342 dvar = dvar / divby;
344 NumberValue = checkb?- dvar:dvar;
345 return lastttype = (int) TokenTypes.NUMBER;
348 if ((ctype & (sbyte)CharacterTypes.ALPHABETIC) != 0)
350 int i = 0;
353 if (i >= buf.Length)
355 char[] nb = new char[buf.Length * 2];
356 Array.Copy((System.Array) buf, 0, (System.Array) nb, 0, buf.Length);
357 buf = nb;
359 buf[i++] = (char) curc;
360 curc = read();
361 ctype = curc < 0?(sbyte)CharacterTypes.WHITESPACE:curc < 256?this.ctype[curc]:(sbyte)CharacterTypes.ALPHABETIC;
363 while ((ctype & ((sbyte)CharacterTypes.ALPHABETIC | (sbyte)CharacterTypes.NUMERIC)) != 0);
364 peekchar = curc;
365 StringValue = new String(buf, 0, i);
366 if (cidtolower)
367 StringValue = StringValue.ToLower();
368 return lastttype = (int) TokenTypes.WORD;
371 if ((ctype & (sbyte)CharacterTypes.STRINGQUOTE) != 0)
373 lastttype = curc;
374 int i = 0;
375 int rc = read();
376 while (rc >= 0 && rc != lastttype && rc != '\n' && rc != '\r')
378 if (rc == '\\')
380 curc = read();
381 int first = curc;
382 if (curc >= '0' && curc <= '7')
384 curc = curc - '0';
385 int loopchar = read();
386 if ('0' <= loopchar && loopchar <= '7')
388 curc = (curc << 3) + (loopchar - '0');
389 loopchar = read();
390 if ('0' <= loopchar && loopchar <= '7' && first <= '3')
392 curc = (curc << 3) + (loopchar - '0');
393 rc = read();
395 else
396 rc = loopchar;
398 else
399 rc = loopchar;
401 else
403 switch (curc)
406 case 'f':
407 curc = 0xC;
408 break;
410 case 'a':
411 curc = 0x7;
412 break;
414 case 'b':
415 curc = '\b';
416 break;
418 case 'v':
419 curc = 0xB;
420 break;
422 case 'n':
423 curc = '\n';
424 break;
426 case 'r':
427 curc = '\r';
428 break;
430 case 't':
431 curc = '\t';
432 break;
434 default:
435 break;
438 rc = read();
441 else
443 curc = rc;
444 rc = read();
446 if (i >= buf.Length)
448 char[] nb = new char[buf.Length * 2];
449 Array.Copy((System.Array) buf, 0, (System.Array) nb, 0, buf.Length);
450 buf = nb;
452 buf[i++] = (char) curc;
455 peekchar = (rc == lastttype)?System.Int32.MaxValue:rc;
457 StringValue = new String(buf, 0, i);
458 return lastttype;
461 if (curc == '/' && (cppcomments || ccomments))
463 curc = read();
464 if (curc == '*' && ccomments)
466 int prevc = 0;
467 while ((curc = read()) != '/' || prevc != '*')
469 if (curc == '\r')
471 linenumber++;
472 curc = read();
473 if (curc == '\n')
475 curc = read();
478 else
480 if (curc == '\n')
482 linenumber++;
483 curc = read();
486 if (curc < 0)
487 return lastttype = (int) TokenTypes.EOF;
488 prevc = curc;
490 return nextToken();
492 else if (curc == '/' && cppcomments)
494 while ((curc = read()) != '\n' && curc != '\r' && curc >= 0)
496 peekchar = curc;
497 return nextToken();
499 else
501 if ((this.ctype['/'] & (sbyte)CharacterTypes.COMMENTCHAR) != 0)
503 while ((curc = read()) != '\n' && curc != '\r' && curc >= 0)
505 peekchar = curc;
506 return nextToken();
508 else
510 peekchar = curc;
511 return lastttype = '/';
516 if ((ctype & (sbyte)CharacterTypes.COMMENTCHAR) != 0)
518 while ((curc = read()) != '\n' && curc != '\r' && curc >= 0)
520 peekchar = curc;
521 return nextToken();
524 return lastttype = curc;