-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
Parser.cpp
1228 lines (1014 loc) · 29.6 KB
/
Parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Adriano dos Santos Fernandes
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2008 Adriano dos Santos Fernandes <adrianosf@uol.com.br>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include "firebird.h"
#include <ctype.h>
#include "../dsql/Parser.h"
#include "../dsql/chars.h"
#include "../jrd/jrd.h"
#include "../jrd/DataTypeUtil.h"
#include "../common/keywords.h"
using namespace Firebird;
using namespace Jrd;
namespace
{
const int HASH_SIZE = 1021;
struct KeywordVersion
{
KeywordVersion(int aKeyword, MetaName* aStr, USHORT aVersion)
: keyword(aKeyword),
str(aStr),
version(aVersion)
{
}
int keyword;
MetaName* str;
USHORT version;
};
class KeywordsMap : public GenericMap<Pair<Left<MetaName, KeywordVersion> > >
{
public:
explicit KeywordsMap(MemoryPool& pool)
: GenericMap<Pair<Left<MetaName, KeywordVersion> > >(pool)
{
for (const TOK* token = keywordGetTokens(); token->tok_string; ++token)
{
MetaName* str = FB_NEW_POOL(pool) MetaName(token->tok_string);
put(*str, KeywordVersion(token->tok_ident, str, token->tok_version));
}
}
~KeywordsMap()
{
Accessor accessor(this);
for (bool found = accessor.getFirst(); found; found = accessor.getNext())
delete accessor.current()->second.str;
}
};
GlobalPtr<KeywordsMap> keywordsMap;
}
Parser::Parser(MemoryPool& pool, DsqlCompilerScratch* aScratch, USHORT aClientDialect,
USHORT aDbDialect, USHORT aParserVersion, const TEXT* string, size_t length,
SSHORT characterSet)
: PermanentStorage(pool),
scratch(aScratch),
client_dialect(aClientDialect),
db_dialect(aDbDialect),
parser_version(aParserVersion),
transformedString(pool),
strMarks(pool),
stmt_ambiguous(false)
{
yyps = 0;
yypath = 0;
yylvals = 0;
yylvp = 0;
yylve = 0;
yylvlim = 0;
yylpsns = 0;
yylpp = 0;
yylpe = 0;
yylplim = 0;
yylexp = 0;
yylexemes = 0;
lex.start = string;
lex.line_start = lex.last_token = lex.ptr = string;
lex.end = string + length;
lex.lines = 1;
lex.att_charset = characterSet;
lex.line_start_bk = lex.line_start;
lex.lines_bk = lex.lines;
lex.param_number = 1;
lex.prev_keyword = -1;
#ifdef DSQL_DEBUG
if (DSQL_debug & 32)
dsql_trace("Source DSQL string:\n%.*s", (int) length, string);
#endif
}
Parser::~Parser()
{
while (yyps)
{
yyparsestate* p = yyps;
yyps = p->save;
yyFreeState(p);
}
while (yypath)
{
yyparsestate* p = yypath;
yypath = p->save;
yyFreeState(p);
}
delete[] yylvals;
delete[] yylpsns;
delete[] yylexemes;
}
dsql_req* Parser::parse()
{
if (parseAux() != 0)
{
fb_assert(false);
return NULL;
}
transformString(lex.start, lex.end - lex.start, transformedString);
return DSQL_parse;
}
// Transform strings (or substrings) prefixed with introducer (_charset) to ASCII equivalent.
void Parser::transformString(const char* start, unsigned length, string& dest)
{
const static char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
const unsigned fromBegin = start - lex.start;
HalfStaticArray<char, 256> buffer;
const char* pos = start;
// We need only the "introduced" strings, in the bounds of "start" and "length" and in "pos"
// order. Let collect them.
SortedArray<StrMark> introducedMarks;
GenericMap<NonPooled<IntlString*, StrMark> >::ConstAccessor accessor(&strMarks);
for (bool found = accessor.getFirst(); found; found = accessor.getNext())
{
const StrMark& mark = accessor.current()->second;
if (mark.introduced && mark.pos >= fromBegin && mark.pos < fromBegin + length)
introducedMarks.add(mark);
}
for (FB_SIZE_T i = 0; i < introducedMarks.getCount(); ++i)
{
const StrMark& mark = introducedMarks[i];
const char* s = lex.start + mark.pos;
buffer.add(pos, s - pos);
if (!isspace(UCHAR(pos[s - pos - 1])))
buffer.add(' '); // fix _charset'' becoming invalid syntax _charsetX''
const FB_SIZE_T count = buffer.getCount();
const FB_SIZE_T newSize = count + 2 + mark.str->getString().length() * 2 + 1;
buffer.grow(newSize);
char* p = buffer.begin() + count;
*p++ = 'X';
*p++ = '\'';
const char* s2 = mark.str->getString().c_str();
for (const char* end = s2 + mark.str->getString().length(); s2 < end; ++s2)
{
*p++ = HEX_DIGITS[UCHAR(*s2) >> 4];
*p++ = HEX_DIGITS[UCHAR(*s2) & 0xF];
}
*p = '\'';
fb_assert(p < buffer.begin() + newSize);
pos = s + mark.length;
}
fb_assert(start + length - pos >= 0);
buffer.add(pos, start + length - pos);
dest.assign(buffer.begin(), MIN(string::max_length(), buffer.getCount()));
}
// Make a substring from the command text being parsed.
string Parser::makeParseStr(const Position& p1, const Position& p2)
{
const char* start = p1.firstPos;
const char* end = p2.lastPos;
string str;
transformString(start, end - start, str);
string ret;
if (DataTypeUtil::convertToUTF8(str, ret))
return ret;
return str;
}
// Make parameter node.
ParameterNode* Parser::make_parameter()
{
thread_db* tdbb = JRD_get_thread_data();
ParameterNode* node = FB_NEW_POOL(*tdbb->getDefaultPool()) ParameterNode(*tdbb->getDefaultPool());
node->dsqlParameterIndex = lex.param_number++;
return node;
}
// Set the position of a left-hand non-terminal based on its right-hand rules.
void Parser::yyReducePosn(YYPOSN& ret, YYPOSN* termPosns, YYSTYPE* /*termVals*/, int termNo,
int /*stkPos*/, int /*yychar*/, YYPOSN& /*yyposn*/, void*)
{
if (termNo == 0)
{
// Accessing termPosns[-1] seems to be the only way to get correct positions in this case.
ret.firstLine = ret.lastLine = termPosns[termNo - 1].lastLine;
ret.firstColumn = ret.lastColumn = termPosns[termNo - 1].lastColumn;
ret.firstPos = ret.lastPos = termPosns[termNo - 1].lastPos;
}
else
{
ret.firstLine = termPosns[0].firstLine;
ret.firstColumn = termPosns[0].firstColumn;
ret.firstPos = termPosns[0].firstPos;
ret.lastLine = termPosns[termNo - 1].lastLine;
ret.lastColumn = termPosns[termNo - 1].lastColumn;
ret.lastPos = termPosns[termNo - 1].lastPos;
}
/*** This allows us to see colored output representing the position reductions.
printf("%.*s", int(ret.firstPos - lex.start), lex.start);
printf("<<<<<");
printf("\033[1;31m%.*s\033[1;37m", int(ret.lastPos - ret.firstPos), ret.firstPos);
printf(">>>>>");
printf("%s\n", ret.lastPos);
***/
}
int Parser::yylex()
{
if (!yylexSkipSpaces())
return -1;
yyposn.firstLine = lex.lines;
yyposn.firstColumn = lex.ptr - lex.line_start;
yyposn.firstPos = lex.ptr - 1;
lex.prev_keyword = yylexAux();
const TEXT* ptr = lex.ptr;
const TEXT* last_token = lex.last_token;
const TEXT* line_start = lex.line_start;
const SLONG lines = lex.lines;
// Lets skip spaces before store lastLine/lastColumn. This is necessary to avoid yyReducePosn
// produce invalid line/column information - CORE-4381.
yylexSkipSpaces();
yyposn.lastLine = lex.lines;
yyposn.lastColumn = lex.ptr - lex.line_start;
lex.ptr = ptr;
lex.last_token = last_token;
lex.line_start = line_start;
lex.lines = lines;
// But the correct value for lastPos is the old (before the second yyLexSkipSpaces)
// value of lex.ptr.
yyposn.lastPos = ptr;
return lex.prev_keyword;
}
bool Parser::yylexSkipSpaces()
{
UCHAR tok_class;
SSHORT c;
// Find end of white space and skip comments
for (;;)
{
if (lex.ptr >= lex.end)
return false;
if (yylexSkipEol())
continue;
// Process comments
c = *lex.ptr++;
if (c == '-' && lex.ptr < lex.end && *lex.ptr == '-')
{
// single-line
lex.ptr++;
while (lex.ptr < lex.end)
{
if (yylexSkipEol())
break;
lex.ptr++;
}
if (lex.ptr >= lex.end)
return false;
continue;
}
else if (c == '/' && lex.ptr < lex.end && *lex.ptr == '*')
{
// multi-line
const TEXT& start_block = lex.ptr[-1];
lex.ptr++;
while (lex.ptr < lex.end)
{
if (yylexSkipEol())
continue;
if ((c = *lex.ptr++) == '*')
{
if (*lex.ptr == '/')
break;
}
}
if (lex.ptr >= lex.end)
{
// I need this to report the correct beginning of the block,
// since it's not a token really.
lex.last_token = &start_block;
yyerror("unterminated block comment");
return false;
}
lex.ptr++;
continue;
}
tok_class = classes(c);
if (!(tok_class & CHR_WHITE))
break;
}
return true;
}
bool Parser::yylexSkipEol()
{
bool eol = false;
const TEXT c = *lex.ptr;
if (c == '\r')
{
lex.ptr++;
if (lex.ptr < lex.end && *lex.ptr == '\n')
lex.ptr++;
eol = true;
}
else if (c == '\n')
{
lex.ptr++;
eol = true;
}
if (eol)
{
lex.lines++;
lex.line_start = lex.ptr; // + 1; // CVC: +1 left out.
}
return eol;
}
int Parser::yylexAux()
{
thread_db* tdbb = JRD_get_thread_data();
MemoryPool& pool = *tdbb->getDefaultPool();
SSHORT c = lex.ptr[-1];
UCHAR tok_class = classes(c);
char string[MAX_TOKEN_LEN];
// Depending on tok_class of token, parse token
lex.last_token = lex.ptr - 1;
if (tok_class & CHR_INTRODUCER)
{
// The Introducer (_) is skipped, all other idents are copied
// to become the name of the character set.
char* p = string;
for (; lex.ptr < lex.end && (classes(*lex.ptr) & CHR_IDENT); lex.ptr++)
{
if (lex.ptr >= lex.end)
return -1;
check_copy_incr(p, UPPER7(*lex.ptr), string);
}
check_bound(p, string);
if (p > string + MAX_SQL_IDENTIFIER_LEN)
yyabandon(-104, isc_dyn_name_longer);
*p = 0;
// make a string value to hold the name, the name is resolved in pass1_constant.
yylval.metaNamePtr = FB_NEW_POOL(pool) MetaName(pool, string, p - string);
return INTRODUCER;
}
// parse a quoted string, being sure to look for double quotes
if (tok_class & CHR_QUOTE)
{
StrMark mark;
mark.pos = lex.last_token - lex.start;
char* buffer = string;
SLONG buffer_len = sizeof(string);
const char* buffer_end = buffer + buffer_len - 1;
char* p;
for (p = buffer; ; ++p)
{
if (lex.ptr >= lex.end)
{
if (buffer != string)
gds__free (buffer);
yyerror("unterminated string");
return -1;
}
// Care about multi-line constants and identifiers
if (*lex.ptr == '\n')
{
lex.lines++;
lex.line_start = lex.ptr + 1;
}
// *lex.ptr is quote - if next != quote we're at the end
if ((*lex.ptr == c) && ((++lex.ptr == lex.end) || (*lex.ptr != c)))
break;
if (p > buffer_end)
{
char* const new_buffer = (char*) gds__alloc (2 * buffer_len);
// FREE: at outer block
if (!new_buffer) // NOMEM:
{
if (buffer != string)
gds__free (buffer);
return -1;
}
memcpy (new_buffer, buffer, buffer_len);
if (buffer != string)
gds__free (buffer);
buffer = new_buffer;
p = buffer + buffer_len;
buffer_len = 2 * buffer_len;
buffer_end = buffer + buffer_len - 1;
}
*p = *lex.ptr++;
}
if (p - buffer > MAX_STR_SIZE)
{
if (buffer != string)
gds__free (buffer);
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
Arg::Gds(isc_dsql_string_byte_length) <<
Arg::Num(p - buffer) <<
Arg::Num(MAX_STR_SIZE));
}
if (c == '"')
{
stmt_ambiguous = true;
// string delimited by double quotes could be
// either a string constant or a SQL delimited
// identifier, therefore marks the SQL statement as ambiguous
if (client_dialect == SQL_DIALECT_V6_TRANSITION)
{
if (buffer != string)
gds__free (buffer);
yyabandon (-104, isc_invalid_string_constant);
}
else if (client_dialect >= SQL_DIALECT_V6)
{
if (p - buffer >= MAX_TOKEN_LEN)
{
if (buffer != string)
gds__free (buffer);
yyabandon(-104, isc_token_too_long);
}
else if (p > &buffer[MAX_SQL_IDENTIFIER_LEN])
{
if (buffer != string)
gds__free (buffer);
yyabandon(-104, isc_dyn_name_longer);
}
else if (p - buffer == 0)
{
if (buffer != string)
gds__free (buffer);
yyabandon(-104, isc_dyn_zero_len_id);
}
Attachment* attachment = tdbb->getAttachment();
MetaName name(attachment->nameToMetaCharSet(tdbb, MetaName(buffer, p - buffer)));
yylval.metaNamePtr = FB_NEW_POOL(pool) MetaName(pool, name);
if (buffer != string)
gds__free (buffer);
return SYMBOL;
}
}
yylval.intlStringPtr = newIntlString(Firebird::string(buffer, p - buffer));
if (buffer != string)
gds__free (buffer);
mark.length = lex.ptr - lex.last_token;
mark.str = yylval.intlStringPtr;
strMarks.put(mark.str, mark);
return STRING;
}
/*
* Check for a numeric constant, which starts either with a digit or with
* a decimal point followed by a digit.
*
* This code recognizes the following token types:
*
* NUMBER: string of digits which fits into a 32-bit integer
*
* NUMBER64BIT: string of digits whose value might fit into an SINT64,
* depending on whether or not there is a preceding '-', which is to
* say that "9223372036854775808" is accepted here.
*
* SCALEDINT: string of digits and a single '.', where the digits
* represent a value which might fit into an SINT64, depending on
* whether or not there is a preceding '-'.
*
* FLOAT: string of digits with an optional '.', and followed by an "e"
* or "E" and an optionally-signed exponent.
*
* NOTE: we swallow leading or trailing blanks, but we do NOT accept
* embedded blanks:
*
* Another note: c is the first character which need to be considered,
* ptr points to the next character.
*/
fb_assert(lex.ptr <= lex.end);
// Hexadecimal string constant. This is treated the same as a
// string constant, but is defined as: X'bbbb'
//
// Where the X is a literal 'x' or 'X' character, followed
// by a set of nibble values in single quotes. The nibble
// can be 0-9, a-f, or A-F, and is converted from the hex.
// The number of nibbles should be even.
//
// The resulting value is stored in a string descriptor and
// returned to the parser as a string. This can be stored
// in a character or binary item.
if ((c == 'x' || c == 'X') && lex.ptr < lex.end && *lex.ptr == '\'')
{
bool hexerror = false;
// Remember where we start from, to rescan later.
// Also we'll need to know the length of the buffer.
const char* hexstring = ++lex.ptr;
int charlen = 0;
// Time to scan the string. Make sure the characters are legal,
// and find out how long the hex digit string is.
for (;;)
{
if (lex.ptr >= lex.end) // Unexpected EOS
{
hexerror = true;
break;
}
c = *lex.ptr;
if (c == '\'') // Trailing quote, done
{
++lex.ptr; // Skip the quote
break;
}
if (!(classes(c) & CHR_HEX)) // Illegal character
{
hexerror = true;
break;
}
++charlen; // Okay, just count 'em
++lex.ptr; // and advance...
}
hexerror = hexerror || (charlen & 1); // IS_ODD(charlen)
// If we made it this far with no error, then convert the string.
if (!hexerror)
{
// Figure out the length of the actual resulting hex string.
// Allocate a second temporary buffer for it.
Firebird::string temp;
// Re-scan over the hex string we got earlier, converting
// adjacent bytes into nibble values. Every other nibble,
// write the saved byte to the temp space. At the end of
// this, the temp.space area will contain the binary
// representation of the hex constant.
UCHAR byte = 0;
for (int i = 0; i < charlen; i++)
{
c = UPPER7(hexstring[i]);
// Now convert the character to a nibble
if (c >= 'A')
c = (c - 'A') + 10;
else
c = (c - '0');
if (i & 1) // nibble?
{
byte = (byte << 4) + (UCHAR) c;
temp.append(1, (char) byte);
}
else
byte = c;
}
if (temp.length() / 2 > MAX_STR_SIZE)
{
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
Arg::Gds(isc_dsql_string_byte_length) <<
Arg::Num(temp.length() / 2) <<
Arg::Num(MAX_STR_SIZE));
}
yylval.intlStringPtr = newIntlString(temp, "BINARY");
return STRING;
} // if (!hexerror)...
// If we got here, there was a parsing error. Set the
// position back to where it was before we messed with
// it. Then fall through to the next thing we might parse.
c = *lex.last_token;
lex.ptr = lex.last_token + 1;
}
if ((c == 'q' || c == 'Q') && lex.ptr + 3 < lex.end && *lex.ptr == '\'')
{
StrMark mark;
mark.pos = lex.last_token - lex.start;
char endChar = *++lex.ptr;
switch (endChar)
{
case '{':
endChar = '}';
break;
case '(':
endChar = ')';
break;
case '[':
endChar = ']';
break;
case '<':
endChar = '>';
break;
}
while (++lex.ptr + 1 < lex.end)
{
if (*lex.ptr == endChar && *++lex.ptr == '\'')
{
size_t len = lex.ptr - lex.last_token - 4;
if (len > MAX_STR_SIZE)
{
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
Arg::Gds(isc_dsql_string_byte_length) <<
Arg::Num(len) <<
Arg::Num(MAX_STR_SIZE));
}
yylval.intlStringPtr = newIntlString(Firebird::string(lex.last_token + 3, len));
++lex.ptr;
mark.length = lex.ptr - lex.last_token;
mark.str = yylval.intlStringPtr;
strMarks.put(mark.str, mark);
return STRING;
}
}
// If we got here, there was a parsing error. Set the
// position back to where it was before we messed with
// it. Then fall through to the next thing we might parse.
c = *lex.last_token;
lex.ptr = lex.last_token + 1;
}
// Hexadecimal numeric constants - 0xBBBBBB
//
// where the '0' and the 'X' (or 'x') are literal, followed
// by a set of nibbles, using 0-9, a-f, or A-F. Odd numbers
// of nibbles assume a leading '0'. The result is converted
// to an integer, and the result returned to the caller. The
// token is identified as a NUMBER if it's a 32-bit or less
// value, or a NUMBER64INT if it requires a 64-bit number.
if (c == '0' && lex.ptr + 1 < lex.end && (*lex.ptr == 'x' || *lex.ptr == 'X') &&
(classes(lex.ptr[1]) & CHR_HEX))
{
bool hexerror = false;
// Remember where we start from, to rescan later.
// Also we'll need to know the length of the buffer.
++lex.ptr; // Skip the 'X' and point to the first digit
const char* hexstring = lex.ptr;
int charlen = 0;
// Time to scan the string. Make sure the characters are legal,
// and find out how long the hex digit string is.
while (lex.ptr < lex.end)
{
c = *lex.ptr;
if (!(classes(c) & CHR_HEX)) // End of digit string
break;
++charlen; // Okay, just count 'em
++lex.ptr; // and advance...
if (charlen > 16) // Too many digits...
{
hexerror = true;
break;
}
}
// we have a valid hex token. Now give it back, either as
// an NUMBER or NUMBER64BIT.
if (!hexerror)
{
// if charlen > 8 (something like FFFF FFFF 0, w/o the spaces)
// then we have to return a NUMBER64BIT. We'll make a string
// node here, and let make.cpp worry about converting the
// string to a number and building the node later.
if (charlen > 8)
{
char cbuff[32];
cbuff[0] = 'X';
strncpy(&cbuff[1], hexstring, charlen);
cbuff[charlen + 1] = '\0';
char* p = &cbuff[1];
UCHAR byte = 0;
bool nibble = strlen(p) & 1;
yylval.scaledNumber.number = 0;
yylval.scaledNumber.scale = 0;
yylval.scaledNumber.hex = true;
while (*p)
{
if ((*p >= 'a') && (*p <= 'f'))
*p = UPPER(*p);
// Now convert the character to a nibble
SSHORT c;
if (*p >= 'A')
c = (*p - 'A') + 10;
else
c = (*p - '0');
if (nibble)
{
byte = (byte << 4) + (UCHAR) c;
nibble = false;
yylval.scaledNumber.number = (yylval.scaledNumber.number << 8) + byte;
}
else
{
byte = c;
nibble = true;
}
++p;
}
// The return value can be a negative number.
return NUMBER64BIT;
}
else
{
// we have an integer value. we'll return NUMBER.
// but we have to make a number value to be compatible
// with existing code.
// See if the string length is odd. If so,
// we'll assume a leading zero. Then figure out the length
// of the actual resulting hex string. Allocate a second
// temporary buffer for it.
bool nibble = (charlen & 1); // IS_ODD(temp.length)
// Re-scan over the hex string we got earlier, converting
// adjacent bytes into nibble values. Every other nibble,
// write the saved byte to the temp space. At the end of
// this, the temp.space area will contain the binary
// representation of the hex constant.
UCHAR byte = 0;
SINT64 value = 0;
for (int i = 0; i < charlen; i++)
{
c = UPPER(hexstring[i]);
// Now convert the character to a nibble
if (c >= 'A')
c = (c - 'A') + 10;
else
c = (c - '0');
if (nibble)
{
byte = (byte << 4) + (UCHAR) c;
nibble = false;
value = (value << 8) + byte;
}
else
{
byte = c;
nibble = true;
}
}
yylval.int32Val = (SLONG) value;
return NUMBER;
} // integer value
} // if (!hexerror)...
// If we got here, there was a parsing error. Set the
// position back to where it was before we messed with
// it. Then fall through to the next thing we might parse.
c = *lex.last_token;
lex.ptr = lex.last_token + 1;
} // headecimal numeric constants
if ((tok_class & CHR_DIGIT) ||
((c == '.') && (lex.ptr < lex.end) && (classes(*lex.ptr) & CHR_DIGIT)))
{
// The following variables are used to recognize kinds of numbers.
bool have_error = false; // syntax error or value too large
bool have_digit = false; // we've seen a digit
bool have_decimal = false; // we've seen a '.'
bool have_exp = false; // digit ... [eE]
bool have_exp_sign = false; // digit ... [eE] {+-]
bool have_exp_digit = false; // digit ... [eE] ... digit
FB_UINT64 number = 0;
FB_UINT64 limit_by_10 = MAX_SINT64 / 10;
SCHAR scale = 0;
for (--lex.ptr; lex.ptr < lex.end; lex.ptr++)
{
c = *lex.ptr;
if (have_exp_digit && (! (classes(c) & CHR_DIGIT)))
// First non-digit after exponent and digit terminates the token.
break;
if (have_exp_sign && (! (classes(c) & CHR_DIGIT)))
{
// only digits can be accepted after "1E-"
have_error = true;
break;
}
if (have_exp)
{
// We've seen e or E, but nothing beyond that.
if ( ('-' == c) || ('+' == c) )
have_exp_sign = true;
else if ( classes(c) & CHR_DIGIT )
// We have a digit: we haven't seen a sign yet, but it's too late now.
have_exp_digit = have_exp_sign = true;
else
{
// end of the token
have_error = true;
break;
}
}
else if ('.' == c)
{
if (!have_decimal)
have_decimal = true;
else
{
have_error = true;
break;
}
}
else if (classes(c) & CHR_DIGIT)
{
// Before computing the next value, make sure there will be no overflow.
have_digit = true;
if (number >= limit_by_10)
{
// possibility of an overflow
if ((number > limit_by_10) || (c > '8'))
{
have_error = true;
break;
}
}
number = number * 10 + (c - '0');
if (have_decimal)
--scale;
}
else if ( (('E' == c) || ('e' == c)) && have_digit )
have_exp = true;
else
// Unexpected character: this is the end of the number.
break;
}
// We're done scanning the characters: now return the right kind
// of number token, if any fits the bill.