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.
37 using System.Collections;
39 namespace Mono.Data.SqlExpressions {
40 internal class Tokenizer : yyParser.yyInput {
41 private static readonly IDictionary tokenMap = new Hashtable ();
42 private static readonly Object [] tokens = {
51 Token.PARENT, "parent",
67 Token.SUBSTRING, "substring",
68 Token.ISNULL, "isnull",
71 Token.CONVERT, "convert"
81 for (int i = 0; i < tokens.Length; i += 2)
82 tokenMap.Add (tokens [i + 1], tokens [i]);
85 public Tokenizer (string strInput)
87 input = strInput.ToCharArray ();
91 private char Current() {
96 if (pos + 1 >= input.Length)
98 return input [pos + 1];
101 private bool MoveNext() {
103 if (pos >= input.Length)
109 private bool SkipWhiteSpace ()
111 if (pos >= input.Length)
114 while (Char.IsWhiteSpace (Current ())) {
122 private object ReadNumber ()
124 StringBuilder sb = new StringBuilder ();
125 sb.Append (Current ());
128 while (Char.IsDigit (next = Next ()) || next == '.') {
134 string str = sb.ToString ();
136 if (str.IndexOf(".") == -1)
137 return Int64.Parse (str);
139 return double.Parse (str);
142 private char ProcessEscapes(char c)
166 throw new SyntaxErrorException (String.Format ("Invalid escape sequence: '\\{0}'.", c));
172 private string ReadString (char terminator)
174 return ReadString (terminator, false /* canEscape */);
177 private string ReadString (char terminator,
178 bool canEscape // twice the terminator is not a terminator
181 bool terminated = false;
182 StringBuilder sb = new StringBuilder ();
183 while (MoveNext ()) {
184 if (Current () == terminator) {
185 if (Next () == terminator) {
186 sb.Append (ProcessEscapes (Current ()));
193 sb.Append (ProcessEscapes (Current ()));
197 throw new SyntaxErrorException (String.Format ("invalid string at {0}{1}<--",
201 return sb.ToString ();
204 private string ReadIdentifier ()
206 StringBuilder sb = new StringBuilder ();
207 sb.Append (Current ());
210 while ((next = Next ()) == '_' || Char.IsLetterOrDigit (next) || next == '\\') {
211 sb.Append (ProcessEscapes (next));
216 return sb.ToString ();
219 private int ParseIdentifier ()
221 string strToken = ReadIdentifier ();
222 object tokenObj = tokenMap[strToken.ToLower()];
225 return (int)tokenObj;
228 return Token.Identifier;
231 private int ParseToken ()
234 switch (cur = Current ()) {
236 return Token.PAROPEN;
239 return Token.PARCLOSE;
272 val = ReadString (']');
273 return Token.Identifier;
276 string date = ReadString ('#');
277 val = DateTime.Parse (date);
278 return Token.DateLiteral;
282 val = ReadString (cur, true);
283 return Token.StringLiteral;
286 if (Char.IsDigit (cur)) {
288 return Token.NumberLiteral;
289 } else if (Char.IsLetter (cur) || cur == '_')
290 return ParseIdentifier ();
293 throw new SyntaxErrorException ("invalid token: '" + cur + "'");
296 ///////////////////////////
297 // yyParser.yyInput methods
298 ///////////////////////////
300 /** move on to next token.
301 @return false if positioned beyond tokens.
302 @throws IOException on input error.
304 public bool advance ()
306 if (!SkipWhiteSpace())
313 /** classifies current token.
314 Should not be called if advance() returned false.
315 @return current %token or single character.
322 /** associated with current token.
323 Should not be called if advance() returned false.
324 @return value for token().
326 public Object value ()