2 // SqlWhereClauseTokenizer.cs
5 // Juraj Skripsky (juraj@hotfeet.ch)
7 // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Globalization;
38 using System.Collections;
40 namespace Mono.Data.SqlExpressions {
41 internal class Tokenizer : yyParser.yyInput {
42 private static readonly IDictionary tokenMap = new Hashtable ();
43 private static readonly Object [] tokens = {
52 Token.PARENT, "parent",
57 Token.NOT_IN, "not in",
59 Token.NOT_LIKE, "not like",
70 Token.SUBSTRING, "substring",
71 Token.ISNULL, "isnull",
74 Token.CONVERT, "convert"
84 for (int i = 0; i < tokens.Length; i += 2)
85 tokenMap.Add (tokens [i + 1], tokens [i]);
88 public Tokenizer (string strInput)
90 input = strInput.ToCharArray ();
94 private char Current() {
99 if (pos + 1 >= input.Length)
101 return input [pos + 1];
104 private bool MoveNext() {
106 if (pos >= input.Length)
112 private bool SkipWhiteSpace ()
114 if (pos >= input.Length)
117 while (Char.IsWhiteSpace (Current ())) {
125 private object ReadNumber ()
127 StringBuilder sb = new StringBuilder ();
128 sb.Append (Current ());
131 while (Char.IsDigit (next = Next ()) || next == '.') {
137 string str = sb.ToString ();
139 if (str.IndexOf ('.') < 0)
140 return Int64.Parse (str, CultureInfo.InvariantCulture);
142 return double.Parse (str, CultureInfo.InvariantCulture);
145 private char ProcessEscapes(char c)
169 throw new SyntaxErrorException (String.Format ("Invalid escape sequence: '\\{0}'.", c));
175 private string ReadString (char terminator)
177 return ReadString (terminator, false /* canEscape */);
180 private string ReadString (char terminator,
181 bool canEscape // twice the terminator is not a terminator
184 bool terminated = false;
185 StringBuilder sb = new StringBuilder ();
186 while (MoveNext ()) {
187 if (Current () == terminator) {
188 if (Next () == terminator) {
189 sb.Append (ProcessEscapes (Current ()));
196 sb.Append (ProcessEscapes (Current ()));
200 throw new SyntaxErrorException (String.Format ("invalid string at {0}{1}<--",
204 return sb.ToString ();
207 private string ReadIdentifier ()
209 StringBuilder sb = new StringBuilder ();
210 sb.Append (Current ());
214 while ((next = Next ()) == '_' || Char.IsLetterOrDigit (next) || next == '\\') {
215 sb.Append (ProcessEscapes (next));
219 ret = sb.ToString ();
220 if (String.Compare (ret,
223 StringComparison.OrdinalIgnoreCase
225 true, CultureInfo.InvariantCulture
229 while (Char.IsWhiteSpace ((next = Next ()))) {
238 switch (Current ()) {
254 int tlen = target.Length;
256 while (tlen-- > 0 && Char.IsLetter ((next = Next ()))) {
257 if (target [idx++] != Char.ToLowerInvariant (next)) {
266 ret = sb.ToString ();
272 private int ParseIdentifier ()
274 string strToken = ReadIdentifier ();
275 object tokenObj = tokenMap[strToken.ToLower()];
278 return (int)tokenObj;
281 return Token.Identifier;
284 private int ParseToken ()
287 switch (cur = Current ()) {
289 return Token.PAROPEN;
292 return Token.PARCLOSE;
325 val = ReadString (']');
326 return Token.Identifier;
329 string date = ReadString ('#');
330 val = DateTime.Parse (date, CultureInfo.InvariantCulture);
331 return Token.DateLiteral;
335 val = ReadString (cur, true);
336 return Token.StringLiteral;
339 if (Char.IsDigit (cur)) {
341 return Token.NumberLiteral;
342 } else if (Char.IsLetter (cur) || cur == '_')
343 return ParseIdentifier ();
346 throw new SyntaxErrorException ("invalid token: '" + cur + "'");
349 ///////////////////////////
350 // yyParser.yyInput methods
351 ///////////////////////////
353 /** move on to next token.
354 @return false if positioned beyond tokens.
355 @throws IOException on input error.
357 public bool advance ()
359 if (!SkipWhiteSpace())
366 /** classifies current token.
367 Should not be called if advance() returned false.
368 @return current %token or single character.
375 /** associated with current token.
376 Should not be called if advance() returned false.
377 @return value for token().
379 public Object value ()