1 {*********************************************************}
3 { Zeos Database Objects }
4 { String tokenizing classes for Sybase }
6 { Originally written by Janos Fegyverneki }
8 {*********************************************************}
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
13 { License Agreement: }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
46 { http://www.sourceforge.net/projects/zeoslib. }
49 { Zeos Development Group. }
50 {********************************************************@}
59 Classes, {$IFDEF MSEgui}mclasses,{$ENDIF} SysUtils,
60 ZTokenizer, ZCompatibility, ZGenericSqlToken;
64 {** Implements a Sybase-specific number state object. }
65 TZSybaseNumberState = class (TZNumberState)
67 function NextToken(Stream: TStream; FirstChar: Char;
68 Tokenizer: TZTokenizer): TZToken; override;
71 {** Implements a Sybase-specific quote string state object. }
72 TZSybaseQuoteState = class (TZQuoteState)
74 function NextToken(Stream: TStream; FirstChar: Char;
75 Tokenizer: TZTokenizer): TZToken; override;
77 function EncodeString(const Value: string; QuoteChar: Char): string; override;
78 function DecodeString(const Value: string; QuoteChar: Char): string; override;
82 This state will either delegate to a comment-handling
83 state, or return a token with just a slash in it.
85 TZSybaseCommentState = class (TZCppCommentState)
87 function NextToken(Stream: TStream; FirstChar: Char;
88 Tokenizer: TZTokenizer): TZToken; override;
91 {** Implements a symbol state object. }
92 TZSybaseSymbolState = class (TZSymbolState)
97 {** Implements a word state object. }
98 TZSybaseWordState = class (TZGenericSQLWordState)
103 {** Implements a default tokenizer object. }
104 TZSybaseTokenizer = class (TZTokenizer)
111 { TZSybaseNumberState }
114 Return a number token from a reader.
115 @return a number token from a reader
117 function TZSybaseNumberState.NextToken(Stream: TStream; FirstChar: Char;
118 Tokenizer: TZTokenizer): TZToken;
124 function ReadHexDigits: string;
128 while Stream.Read(LastChar, SizeOf(Char)) > 0 do
130 if CharInSet(LastChar, ['0'..'9','a'..'f','A'..'F']) then
132 Result := Result + LastChar;
133 HexDecimal := HexDecimal or CharInSet(LastChar, ['a'..'f','A'..'F']);
138 Stream.Seek(-SizeOf(Char), soFromCurrent);
144 function ReadDecDigits: string;
148 while Stream.Read(LastChar, SizeOf(Char)) > 0 do
150 if CharInSet(LastChar, ['0'..'9']) then
152 Result := Result + LastChar;
157 Stream.Seek(-SizeOf(Char), soFromCurrent);
165 FloatPoint := FirstChar = '.';
168 Result.Value := FirstChar;
169 Result.TokenType := ttUnknown;
171 { Reads the first part of the number before decimal point }
172 if not FloatPoint then
174 Result.Value := Result.Value + ReadDecDigits;
175 FloatPoint := (LastChar = '.') and not HexDecimal;
178 Stream.Read(LastChar, SizeOf(Char));
179 Result.Value := Result.Value + LastChar;
183 { Reads the second part of the number after decimal point }
185 Result.Value := Result.Value + ReadDecDigits;
187 { Reads a power part of the number }
188 if not HexDecimal and CharInSet(LastChar, ['e','E']) then
190 Stream.Read(LastChar, SizeOf(Char));
191 Result.Value := Result.Value + LastChar;
194 Stream.Read(LastChar, SizeOf(Char));
195 if CharInSet(LastChar, ['0'..'9','-','+']) then
196 Result.Value := Result.Value + LastChar + ReadDecDigits
199 Result.Value := Copy(Result.Value, 1, Length(Result.Value) - 1);
200 Stream.Seek(-2*SizeOf(Char), soFromCurrent);
204 { Reads the nexdecimal number }
205 if (Result.Value = '0') and CharInSet(LastChar, ['x','X']) then
207 Stream.Read(LastChar, SizeOf(Char));
208 Result.Value := Result.Value + LastChar + ReadHexDigits;
212 { Prepare the result }
213 if Result.Value = '.' then
215 if Tokenizer.SymbolState <> nil then
216 Result := Tokenizer.SymbolState.NextToken(Stream, FirstChar, Tokenizer);
221 Result.TokenType := ttHexDecimal
222 else if FloatPoint then
223 Result.TokenType := ttFloat
224 else Result.TokenType := ttInteger;
228 { TZSybaseQuoteState }
231 Return a quoted string token from a reader. This method
232 will collect characters until it sees a match to the
233 character that the tokenizer used to switch to this state.
235 @return a quoted string token from a reader
237 function TZSybaseQuoteState.NextToken(Stream: TStream; FirstChar: Char;
238 Tokenizer: TZTokenizer): TZToken;
243 Result.Value := FirstChar;
245 while Stream.Read(ReadChar, SizeOf(Char)) > 0 do
247 if ((LastChar = FirstChar) and (ReadChar <> FirstChar)
248 and (FirstChar <> '[')) or ((FirstChar = '[') and (LastChar = ']')) then
250 Stream.Seek(-SizeOf(Char), soFromCurrent);
253 Result.Value := Result.Value + ReadChar;
254 if (LastChar = FirstChar) and (ReadChar = FirstChar) then
256 else LastChar := ReadChar;
259 if CharInSet(FirstChar, ['"', '[']) then
260 Result.TokenType := ttWord
261 else Result.TokenType := ttQuoted;
265 Encodes a string value.
266 @param Value a string value to be encoded.
267 @param QuoteChar a string quote character.
268 @returns an encoded string.
270 function TZSybaseQuoteState.EncodeString(const Value: string; QuoteChar: Char): string;
272 if QuoteChar = '[' then
273 Result := '[' + Value + ']'
274 else if CharInSet(QuoteChar, [#39, '"']) then
275 Result := QuoteChar + Value + QuoteChar
276 else Result := Value;
280 Decodes a string value.
281 @param Value a string value to be decoded.
282 @param QuoteChar a string quote character.
283 @returns an decoded string.
285 function TZSybaseQuoteState.DecodeString(const Value: string; QuoteChar: Char): string;
288 if Length(Value) >= 2 then
290 if CharInSet(QuoteChar, [#39, '"']) and (Value[1] = QuoteChar)
291 and (Value[Length(Value)] = QuoteChar) then
293 if Length(Value) > 2 then
294 Result := AnsiDequotedStr(Value, QuoteChar)
297 else if (QuoteChar = '[') and (Value[1] = QuoteChar)
298 and (Value[Length(Value)] = ']') then
299 Result := Copy(Value, 2, Length(Value) - 2)
303 { TZSybaseCommentState }
306 Gets a Sybase specific comments like # or /* */.
307 @return either just a slash token, or the results of
308 delegating to a comment-handling state
310 function TZSybaseCommentState.NextToken(Stream: TStream; FirstChar: Char;
311 Tokenizer: TZTokenizer): TZToken;
316 Result.Value := FirstChar;
317 Result.TokenType := ttUnknown;
319 if FirstChar = '-' then
321 ReadNum := Stream.Read(ReadChar, SizeOf(Char));
322 if (ReadNum > 0) and (ReadChar = '-') then
324 Result.TokenType := ttComment;
325 Result.Value := '--' + GetSingleLineComment(Stream);
330 Stream.Seek(-SizeOf(Char), soFromCurrent);
333 else if FirstChar = '/' then
335 ReadNum := Stream.Read(ReadChar, SizeOf(Char));
336 if (ReadNum > 0) and (ReadChar = '*') then
338 Result.TokenType := ttComment;
339 Result.Value := '/*' + GetMultiLineComment(Stream);
344 Stream.Seek(-SizeOf(Char), soFromCurrent);
348 if (Result.TokenType = ttUnknown) and (Tokenizer.SymbolState <> nil) then
349 Result := Tokenizer.SymbolState.NextToken(Stream, FirstChar, Tokenizer);
352 { TZSybaseSymbolState }
355 Creates this Sybase-specific symbol state object.
357 constructor TZSybaseSymbolState.Create;
368 { TZSybaseWordState }
371 Constructs this Sybase-specific word state object.
373 constructor TZSybaseWordState.Create;
375 SetWordChars(#0, #191, False);
376 SetWordChars(#192, high(char), True);
377 SetWordChars('a', 'z', True);
378 SetWordChars('A', 'Z', True);
379 SetWordChars('0', '9', True);
380 SetWordChars('$', '$', True);
381 SetWordChars('_', '_', True);
382 SetWordChars('@', '@', True);
383 SetWordChars('#', '#', True);
386 { TZSybaseTokenizer }
389 Constructs a tokenizer with a default state table (as
390 described in the class comment).
392 constructor TZSybaseTokenizer.Create;
394 EscapeState := TZEscapeState.Create;
395 WhitespaceState := TZWhitespaceState.Create;
397 SymbolState := TZSybaseSymbolState.Create;
398 NumberState := TZSybaseNumberState.Create;
399 QuoteState := TZSybaseQuoteState.Create;
400 WordState := TZSybaseWordState.Create;
401 CommentState := TZSybaseCommentState.Create;
403 SetCharacterState(#0, #32, WhitespaceState);
404 SetCharacterState(#33, #191, SymbolState);
405 SetCharacterState(#192, High(Char), WordState);
407 SetCharacterState('a', 'z', WordState);
408 SetCharacterState('A', 'Z', WordState);
409 SetCharacterState('_', '_', WordState);
410 SetCharacterState('$', '$', WordState);
411 SetCharacterState('@', '@', WordState);
412 SetCharacterState('#', '#', WordState);
414 SetCharacterState('0', '9', NumberState);
415 SetCharacterState('.', '.', NumberState);
417 SetCharacterState('"', '"', QuoteState);
418 SetCharacterState('''', '''', QuoteState);
419 SetCharacterState('[', '[', QuoteState);
420 SetCharacterState(']', ']', QuoteState);
422 SetCharacterState('/', '/', CommentState);
423 SetCharacterState('-', '-', CommentState);