forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ncgen.l
811 lines (736 loc) · 22.8 KB
/
ncgen.l
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
%{
/*********************************************************************
* Copyright 1993, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Id: ncgen.l,v 1.24 2009/09/25 18:22:35 dmh Exp $
*********************************************************************/
/* Problems:
1. We assume the input is true utf8.
Unfortunately, we may actually get iso-latin-8859-1.
This means that there will be ambiguity about the characters
in the range 128-255 because they will look like n-byte unicode
when they are 1-byte 8859 characters. Because of our encoding,
8859 characters above 128 will be handles as n-byte utf8 and so
will probably not lex correctly.
Solution: assume utf8 and note in the documentation that
ISO8859 is specifically unsupported.
2. The netcdf function NC_check_name in string.c must be modified to
conform to the use of UTF8.
3. We actually have three tests for UTF8 of increasing correctness
(in the sense that the least correct will allow some sequences that
are technically illegal UTF8).
The tests are derived from the table at
http://www.w3.org/2005/03/23-lex-U
We include lexical definitions for all three, but use the second version.
4. Single character constants enclosed in '...' cannot be
utf-8, so we assume they are by default encoded using the 1-byte
subset of utf-8. It turns out that this subset is in fact
equivalent to US-Ascii (7-bit).
We could use ISO-8859-1, but that conflicts with UTF-8 above value 127.
*/
/* lex specification for tokens for ncgen */
/* Fill value used by ncdump from version 2.4 and later. Should match
definition of FILL_STRING in ../ncdump/vardata.h */
#define FILL_STRING "_"
#define XDR_INT32_MIN (-2147483647-1)
#define XDR_INT32_MAX 2147483647
#define XDR_INT64_MIN (-9223372036854775807LL-1)
#define XDR_INT64_MAX (9223372036854775807LL)
#undef DEBUG
#ifdef DEBUG
static int MIN_BYTE = NC_MIN_BYTE;
static int MIN_SHORT = NC_MIN_SHORT;
static int MIN_INT = NC_MIN_INT;
static int MAX_BYTE = NC_MAX_BYTE;
static int MAX_SHORT = NC_MAX_SHORT;
static int MAX_INT = NC_MAX_INT;
static int MAX_UBYTE = NC_MAX_UBYTE;
static int MAX_USHORT = NC_MAX_USHORT;
static unsigned int MAX_UINT = NC_MAX_UINT;
#undef NC_MIN_BYTE
#undef NC_MIN_SHORT
#undef NC_MIN_INT
#undef NC_MAX_BYTE
#undef NC_MAX_SHORT
#undef NC_MAX_INT
#undef NC_MAX_UBYTE
#undef NC_MAX_USHORT
#undef NC_MAX_UINT
#define NC_MIN_BYTE MIN_BYTE
#define NC_MIN_SHORT MIN_SHORT
#define NC_MIN_INT MIN_INT
#define NC_MAX_BYTE MAX_BYTE
#define NC_MAX_SHORT MAX_SHORT
#define NC_MAX_INT MAX_INT
#define NC_MAX_UBYTE MAX_UBYTE
#define NC_MAX_USHORT MAX_USHORT
#define NC_MAX_UINT MAX_UINT
#endif
#define TAG "BbSsLl"
#define tstdecimal(ch) ((ch) >= '0' && (ch) <= '9')
/*Mnemonics*/
#define ISIDENT 1
char errstr[100]; /* for short error messages */
int lineno; /* line number for error messages */
Bytebuffer* lextext; /* name or string with escapes removed */
#define YY_BREAK /* defining as nothing eliminates unreachable
statement warnings from flex output,
but make sure every action ends with
"return" or "break"! */
int specialconstants; /* 1 if nan, nanf, infinity, etc is used */
double double_val; /* last double value read */
float float_val; /* last float value read */
long long int64_val; /* last int64 value read */
int int32_val; /* last int32 value read */
short int16_val; /* last short value read */
unsigned long long uint64_val; /* last int64 value read */
unsigned int uint32_val; /* last int32 value read */
unsigned short uint16_val; /* last short value read */
char char_val; /* last char value read */
signed char byte_val; /* last byte value read */
unsigned char ubyte_val; /* last byte value read */
static Symbol* makepath(char* text);
static int lexdebug(int);
static unsigned long long parseULL(char* text, int*);
static nc_type downconvert(unsigned long long uint64, int, int, int);
static int tagmatch(nc_type nct, int tag, int hasU);
static int nct2lexeme(nc_type nct);
static struct Specialtoken {
char* name;
int token;
} specials[] = {
{"_FillValue",_FILLVALUE},
{"_Format",_FORMAT},
{"_Storage",_STORAGE},
{"_ChunkSizes",_CHUNKSIZES},
{"_Fletcher32",_FLETCHER32},
{"_DeflateLevel",_DEFLATELEVEL},
{"_Shuffle",_SHUFFLE},
{"_Endianness",_ENDIANNESS},
{"_NoFill",_NOFILL},
{NULL,0} /* null terminate */
};
%}
%x ST_C_COMMENT
%x TEXT
%p 6000
/* The most correct (validating) version of UTF8 character set
(Taken from: http://www.w3.org/2005/03/23-lex-U)
The lines of the expression cover the UTF8 characters as follows:
1. non-overlong 2-byte
2. excluding overlongs
3. straight 3-byte
4. excluding surrogates
5. straight 3-byte
6. planes 1-3
7. planes 4-15
8. plane 16
UTF8 ([\xC2-\xDF][\x80-\xBF]) \
| (\xE0[\xA0-\xBF][\x80-\xBF]) \
| ([\xE1-\xEC][\x80-\xBF][\x80-\xBF]) \
| (\xED[\x80-\x9F][\x80-\xBF]) \
| ([\xEE-\xEF][\x80-\xBF][\x80-\xBF]) \
| (\xF0[\x90-\xBF][\x80-\xBF][\x80-\xBF]) \
| ([\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]) \
| (\xF4[\x80-\x8F][\x80-\xBF][\x80-\xBF]) \
*/
/* Wish there was some way to ifdef lex files */
/*The most relaxed version of UTF8 (not used)
UTF8 ([\xC0-\xD6].)|([\xE0-\xEF]..)|([\xF0-\xF7]...)
*/
/*The partially relaxed version of UTF8, and the one used here */
UTF8 ([\xC0-\xD6][\x80-\xBF])|([\xE0-\xEF][\x80-\xBF][\x80-\xBF])|([\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF])
/* The old definition of ID
ID ([A-Za-z_]|{UTF8})([A-Z.@#\[\]a-z_0-9+-]|{UTF8})*
*/
/* Don't permit control characters or '/' in names, but other special
chars OK if escaped. Note that to preserve backwards
compatibility, none of the characters _.@+- should be escaped, as
they were previously permitted in names without escaping. */
idescaped \\[ !"#$%&'()*,:;<=>?\[\\\]^`{|}~]
numescaped \\[0-9]
/* New definition to conform to a subset of string.c */
ID ([a-zA-Z_]|{UTF8}|{numescaped})([a-zA-Z0-9_.@+-]|{UTF8}|{idescaped})*
escaped \\.
/* Capture a datasetidentifier */
/* DATASETID ([a-zA-Z0-9!#$%&*:;<=>?/^|~_.@+-]|{UTF8})* */
DATASETID [^{][^{]*
/* Note: this definition of string will work for utf8 as well,
although it is a very relaxed definition
*/
nonquotes ([^"\\]|{escaped})*
exp ([eE][+-]?[0-9]+)
OPAQUESTRING (0[xX][0-9A-Fa-f][0-9A-Fa-f]*)
PATH ([/]|([/]{ID})([/]{ID})*)
XUNUMBER {OPAQUESTRING}[SsLl]
NUMBER [+-]?[0-9][0-9]*[Uu]?[BbSsLl]?
DBLNUMBER [+-]?[0-9]*\.[0-9]*{exp}?[LlDd]?|[+-]?[0-9]*{exp}[LlDd]?
FLTNUMBER [+-]?[0-9]*\.[0-9]*{exp}?[Ff]|[+-]?[0-9]*{exp}[Ff]
SPECIAL "_FillValue"|"_Format"|"_Storage"|"_ChunkSizes"|"_Fletcher32"|"_DeflateLevel"|"_Shuffle"|"_Endianness"|"_NoFill"
USASCII [\x01-\x7F]
%%
[ \r\t\f]+ { /* whitespace */
break;
}
\/\/.* { /* comment */
break;
}
\"{nonquotes}\" {int len;
/* In netcdf4, this will be used in a variety
of places, so only remove escapes */
/*
if(yyleng > MAXTRST) {
yyerror("string too long, truncated\n");
yytext[MAXTRST-1] = '\0';
}
*/
/* FIX: Assumes unescape also does normalization */
bbSetalloc(lextext,yyleng+1); /*+1 for nul */
/* Adjust length */
bbSetlength(lextext,yyleng-2); /*-2 for quotes */
len = unescape(bbContents(lextext),
(char *)yytext+1,yyleng-2,!ISIDENT);
if(len < 0) {
sprintf(errstr,"Illegal character: %s",yytext);
yyerror(errstr);
}
bbSetlength(lextext,len);
bbNull(lextext);
return lexdebug(TERMSTRING);
}
{OPAQUESTRING} { /* drop leading 0x; pad to even number of chars */
char* p = yytext+2;
int len = yyleng - 2;
bbClear(lextext);
bbAppendn(lextext,p,len);
if((len % 2) == 1) bbAppend(lextext,'0');
bbNull(lextext);
/* convert all chars to lower case */
for(p=bbContents(lextext);*p;p++) *p = tolower(*p);
return lexdebug(OPAQUESTRING);
}
compound|struct|structure {return lexdebug(COMPOUND);}
enum {return lexdebug(ENUM);}
opaque {return lexdebug(OPAQUE);}
float|real {return lexdebug(FLOAT_K);}
char {return lexdebug(CHAR_K);}
byte {return lexdebug(BYTE_K);}
ubyte {return lexdebug(UBYTE_K);}
short {return lexdebug(SHORT_K);}
ushort {return lexdebug(USHORT_K);}
long|int|integer {return lexdebug(INT_K);}
ulong|uint|uinteger {return lexdebug(UINT_K);}
int64 {return lexdebug(INT64_K);}
uint64 {return lexdebug(UINT64_K);}
double {return lexdebug(DOUBLE_K);}
unlimited|UNLIMITED {int32_val = -1;
return lexdebug(NC_UNLIMITED_K);}
types: {return lexdebug(TYPES);}
dimensions: {return lexdebug(DIMENSIONS);}
variables: {return lexdebug(VARIABLES);}
data: {return lexdebug(DATA);}
group: {return lexdebug(GROUP);}
(netcdf|NETCDF|netCDF) {BEGIN(TEXT);return lexdebug(NETCDF);}
DoubleInf|-?Infinity { /* missing value (pre-2.4 backward compatibility) */
if (yytext[0] == '-') {
double_val = NEGNC_INFINITE;
} else {
double_val = NC_INFINITE;
}
specialconstants = 1;
return lexdebug(DOUBLE_CONST);
}
NaN|nan { /* missing value (pre-2.4 backward compatibility) */
double_val = NAN;
specialconstants = 1;
return lexdebug(DOUBLE_CONST);
}
FloatInf|-?Infinityf|-?Inff {/* missing value (pre-2.4 backward compatibility)*/
if (yytext[0] == '-') {
float_val = NEGNC_INFINITEF;
} else {
float_val = NC_INFINITEF;
}
specialconstants = 1;
return lexdebug(FLOAT_CONST);
}
NaNf|nanf { /* missing value (pre-2.4 backward compatibility) */
float_val = NANF;
specialconstants = 1;
return lexdebug(FLOAT_CONST);
}
NIL|nil|Nil {
#ifdef USE_NETCDF4
if(l_flag == L_C || l_flag == L_BINARY)
return lexdebug(NIL);
yyerror("NIL only allowed for netcdf-4 and for -lc or -lb");
#else
yyerror("NIL only allowed for netcdf-4 and for -lc or -lb");
#endif
}
{PATH} {
bbClear(lextext);
bbAppendn(lextext,(char*)yytext,yyleng+1); /* include null */
bbNull(lextext);
yylval.sym = makepath(bbContents(lextext));
return lexdebug(PATH);
}
{SPECIAL} {struct Specialtoken* st;
bbClear(lextext);
bbAppendn(lextext,(char*)yytext,yyleng+1); /* include null */
bbNull(lextext);
for(st=specials;st->name;st++) {
if(strcmp(bbContents(lextext),st->name)==0) {return lexdebug(st->token);}
}
return 0;
}
<TEXT>{DATASETID} {
int c;
char* p; char* q;
/* copy the trimmed name */
bbClear(lextext);
bbAppendn(lextext,(char*)yytext,yyleng+1); /* include null */
bbNull(lextext);
p = bbContents(lextext);
q = p;
while((c=*p++)) {if(c > ' ') *q++ = c;}
*q = '\0';
datasetname = bbDup(lextext);
BEGIN(INITIAL);
return lexdebug(DATASETID);
}
{ID} { char* id; int len;
bbClear(lextext);
bbAppendn(lextext,(char*)yytext,yyleng+1); /* include null */
bbNull(lextext);
id = bbContents(lextext);
len = unescape(id,id,bbLength(lextext),ISIDENT);
bbSetlength(lextext,len);
if (STREQ(id, FILL_STRING)) return lexdebug(FILLMARKER);
yylval.sym = install(id);
return lexdebug(IDENT);
}
{NUMBER} {
/*
We need to try to see what size of integer ((u)int).
Technically, the user should specify, but...
If out of any integer range, then complain
*/
int slen = strlen(ncgtext);
int tag = ncgtext[slen-1];
int hasU = 0;
int signchar = 0;
int isneg = 0;
int c = ncgtext[0];
int fail = 0;
nc_type nct = 0;
char* pos = NULL;
if(!tstdecimal(c)) {
pos = ncgtext+1;
isneg = (c == '-');
} else
pos = ncgtext;
if(tag != '\0' && strchr(TAG,tag) != NULL) {
if(slen > 2) {
c = ncgtext[slen-2];
hasU = (c == 'U' || c == 'u') ? 1 : 0;
}
} else
tag = 0;
if(isneg && hasU) {
sprintf(errstr,"Unsigned integer cannot be signed: %s",ncgtext);
yyerror(errstr);
goto done;
}
uint64_val = parseULL(pos,&fail);
if(fail) {
sprintf(errstr,"integer constant out of range: %s",ncgtext);
yyerror(errstr);
goto done;
}
/* Down convert to smallest possible range */
nct = downconvert(uint64_val,isneg,tag,hasU);
switch (k_flag) {
case NC_FORMAT_64BIT_DATA:
case NC_FORMAT_NETCDF4:
return lexdebug(nct2lexeme(nct));
case NC_FORMAT_CLASSIC:
case NC_FORMAT_64BIT_OFFSET:
case NC_FORMAT_NETCDF4_CLASSIC:
if(nct > NC_INT) {
sprintf(errstr,"Illegal integer constant for classic format: %s",ncgtext);
yyerror(errstr);
goto done;
}
}
if(!tagmatch(nct,tag,hasU)) {
sprintf(errstr,"Integer out of range for tag: %s",ncgtext);
yyerror(errstr);
goto done;
}
return lexdebug(nct2lexeme(nct));
done: return 0;
}
{XUNUMBER} {
int c;
int token = 0;
int slen = strlen(yytext);
int tag = yytext[slen-1];
char* hex = yytext+2; /* point to first true hex digit */
int xlen = (slen - 3); /* true hex length */
yytext[slen-1] = '\0';
if(xlen > 16) { /* truncate hi order digits */
hex += (xlen - 16);
}
/* convert to an unsigned long long */
uint64_val = 0;
while((c=*hex++)) {
unsigned int hexdigit = (c <= '9'?(c-'0'):(c-'a')+0xa);
uint64_val = ((uint64_val << 4) | hexdigit);
}
switch (tag) {
case 'S': case 's':
uint16_val = (unsigned short)uint64_val;
token = USHORT_CONST;
break;
case 'L': case 'l':
token = UINT64_CONST;
break;
default: /* should never happen */
if (sscanf((char*)yytext, "%i", &uint32_val) != 1) {
sprintf(errstr,"bad unsigned int constant: %s",(char*)yytext);
yyerror(errstr);
}
token = UINT_CONST;
}
return lexdebug(token);
}
{DBLNUMBER} {
if (sscanf((char*)yytext, "%le", &double_val) != 1) {
sprintf(errstr,"bad long or double constant: %s",(char*)yytext);
yyerror(errstr);
}
return lexdebug(DOUBLE_CONST);
}
{FLTNUMBER} {
if (sscanf((char*)yytext, "%e", &float_val) != 1) {
sprintf(errstr,"bad float constant: %s",(char*)yytext);
yyerror(errstr);
}
return lexdebug(FLOAT_CONST);
}
\'[^\\]\' {
(void) sscanf((char*)&yytext[1],"%c",&byte_val);
return lexdebug(BYTE_CONST);
}
\'\\[0-7][0-7][0-7]\' {
int oct = unescapeoct(&yytext[2]);
if(oct < 0) {
sprintf(errstr,"bad octal character constant: %s",(char*)yytext);
yyerror(errstr);
}
byte_val = (unsigned int)oct;
return lexdebug(BYTE_CONST);
}
\'\\[xX][0-9a-fA-F][0-9a-fA-F]\' {
int hex = unescapehex(&yytext[3]);
if(byte_val < 0) {
sprintf(errstr,"bad hex character constant: %s",(char*)yytext);
yyerror(errstr);
}
byte_val = (unsigned int)hex;
return lexdebug(BYTE_CONST);
}
\'\\.\' {
switch ((char)yytext[2]) {
case 'a': byte_val = '\007'; break; /* not everyone under-
* stands '\a' yet */
case 'b': byte_val = '\b'; break;
case 'f': byte_val = '\f'; break;
case 'n': byte_val = '\n'; break;
case 'r': byte_val = '\r'; break;
case 't': byte_val = '\t'; break;
case 'v': byte_val = '\v'; break;
case '\\': byte_val = '\\'; break;
case '?': byte_val = '\177'; break;
case '\'': byte_val = '\''; break;
default: byte_val = (char)yytext[2];
}
return lexdebug(BYTE_CONST);
}
\n {
lineno++ ;
break;
}
"/""*" {/*initial*/
BEGIN(ST_C_COMMENT);
break;
}
<ST_C_COMMENT>([^*]|"*"[^/])* {/* continuation */
break;
}
<ST_C_COMMENT>"*/" {/* final */
BEGIN(INITIAL);
break;
}
<ST_C_COMMENT><<EOF>> {/* final, error */
fprintf(stderr,"unterminated /**/ comment");
BEGIN(INITIAL);
break;
}
. {/* Note: this next rule will not work for UTF8 characters */
return lexdebug(yytext[0]) ;
}
%%
static int
lexdebug(int token)
{
if(debug >= 2)
{
char* text = yytext;
text[yyleng] = 0;
fprintf(stderr,"Token=%d |%s| line=%d\n",token,text,lineno);
}
return token;
}
int
lex_init(void)
{
lineno = 1;
lextext = bbNew();
if(0) unput(0); /* keep -Wall quiet */
return 0;
}
static Symbol*
makepath(char* text0)
{
/* Create a reference symbol.
Convert path to a sequence of symbols.
Use last name as symbol name (with root group reference ('/') as exception).
*/
Symbol* refsym = NULL;
/* walk the path converting to a sequence of symbols */
if(strcmp(text0,"/")==0) {
/* special case of root reference */
refsym = rootgroup;
} else {
List* prefix = listnew();
/* split the text into IDENT chunks, convert to symbols */
Symbol* container = rootgroup;
char *ident, *p;
char* text = strdup(text0);
int c,lastident;
ident=text+1; p=ident; /* skip leading '/' */
do {
lastident = 0;
switch ((c=*p)) {
default: p++; break;
case '\\': p++; if(*p == '/') p++; break;
case '\0': /* treat null terminator like trailing '/' (mostly) */
lastident=1; /* this is the last ident in the path */
/*fall thru */
case '/':
*p='\0';
if(!lastident) {
unescape(ident,ident,strlen(ident),ISIDENT);
refsym = lookupingroup(NC_GRP,ident,container);
if(refsym == NULL) {
sprintf(errstr,"Undefined or forward referenced group: %s",ident);
yyerror(errstr);
refsym = rootgroup;
} else {
listpush(prefix,(void*)refsym);
}
} else { /* lastident is true */
unescape(ident,ident,strlen(ident),ISIDENT);
refsym = install(ident);
refsym->objectclass = NC_GRP;/* tentative */
refsym->ref.is_ref = 1;
refsym->container = container;
refsym->subnodes = listnew();
}
container = refsym;
ident=p+1; p=ident;
break;
}
} while(c != '\0');
refsym->prefix = prefix;
free(text);
}
return refsym;
}
/*
Parse a simple string of digitis into an unsigned long long
Return the value.
*/
static unsigned long long
parseULL(char* text, int* failp)
{
int result = 0;
extern int errno;
char* endptr;
unsigned long long uint64 = 0;
errno = 0; endptr = NULL;
assert(tstdecimal(text[0]));
#ifdef HAVE_STRTOULL
uint64 = strtoull(text,&endptr,10);
if(errno == ERANGE) {
if(failp) *failp = ERANGE;
return 0;
}
#else /*!(defined HAVE_STRTOLL && defined HAVE_STRTOULL)*/
sscanf((char*)text, "%llu", &uint64);
/* Have no useful way to detect out of range */
#endif /*!(defined HAVE_STRTOLL && defined HAVE_STRTOULL)*/
return uint64;
}
/**
Given the raw bits, the sign char, the tag, and hasU
fill in the appropriate *_val field
and return the type
*/
static nc_type
downconvert(unsigned long long uint64, int isneg, int tag, int hasU)
{
nc_type nct = NC_NAT;
int bit64 = (uint64 >> 63);
int allones = (uint64 == 0xffffffffffffffffUL);
long long int64 = *((long long*)&uint64);
if(isneg && hasU)
return NC_NAT;
/* Special cases: all (u)int64 values */
if(allones && (hasU || !isneg)) /* bare max uint64 value */
return NC_UINT64;
if((int64 < NC_MIN_INT) || (int64 > NC_MAX_INT)) {
if(isneg && !hasU) {
int64_val = - int64;
return NC_INT64;
}
if(!isneg && hasU) {
uint64_val = uint64;
return NC_UINT64;
}
/* Bare big integer, assume int64 unless sign bit set */
if(!isneg && !hasU && bit64) {
uint64_val = uint64;
return NC_UINT64;
} else if(!bit64) {
int64_val = int64;
return NC_INT64;
}
/*assert(!isneg)*/
uint64_val = uint64;
return NC_UINT64;
}
if(isneg) int64 = -int64;
/* special case:
If there is no tag and the size is ok,
then always return NC_INT or NC_UINT.
*/
if(!tag) {
if(!hasU) {
if((int64 >= NC_MIN_INT) && (int64 <= NC_MAX_INT)) {
int32_val = (int)int64;
return NC_INT;
}
}
if(uint64 <= NC_MAX_UINT) {
uint32_val = (unsigned int)uint64;
return NC_INT;
}
}
/* assert (tag != 0) */
/* Pick smallest enclosing type;
for historical reasons (ncgen3), technically out of range
values are allowed and conversion is attempted;
e.g. 65535s; is legal and is return as an unsigned short.
*/
if(hasU) {
switch (tag) {
case 'B': case 'b':
if((int64 >= 0) && (int64 <= NC_MAX_UBYTE)) {
nct = NC_UBYTE;
ubyte_val = (unsigned char)int64;
}; break;
case 'S': case 's':
if((int64 >= 0) && (int64 <= NC_MAX_USHORT)) {
nct = NC_USHORT;
uint16_val = (unsigned short)int64;
} break;
case 'L': case 'l':
if((int64 >= 0) && (int64 <= NC_MAX_UINT64)) {
nct = NC_UINT64;
uint64_val = uint64;
} break;
default:
return NC_NAT;
}
} else { /* !hasU */
switch (tag) {
case 'B': case 'b':
if((int64 >= NC_MIN_BYTE) && (int64 <= NC_MAX_BYTE)) {
nct = NC_BYTE;
byte_val = (signed char)int64;
} else {/* force to unsigned value */
uint64_val = uint64 & 0xff;
nct = NC_UBYTE;
}
break;
case 'S': case 's':
if((int64 >= NC_MIN_SHORT) && (int64 <= NC_MAX_SHORT)) {
nct = NC_SHORT;
int16_val = (signed short)int64;
} else {/* force to unsigned value */
uint64_val = uint64 & 0xffff;
nct = NC_USHORT;
}
break;
case 'L': case 'l':
if((uint64 <= NC_MAX_INT64)) {
nct = NC_INT64;
int64_val = int64;
} else {/* force to unsigned value */
uint64_val = uint64;
nct = NC_UINT64;
}
break;
default:
return NC_NAT;
}
}
return nct;
}
static int
nct2lexeme(nc_type nct)
{
switch(nct) {
case NC_BYTE: return BYTE_CONST;
case NC_CHAR: return CHAR_CONST;
case NC_SHORT: return SHORT_CONST;
case NC_INT: return INT_CONST;
case NC_UBYTE: return UBYTE_CONST;
case NC_USHORT: return USHORT_CONST;
case NC_UINT: return UINT_CONST;
case NC_INT64: return INT64_CONST;
case NC_UINT64: return UINT64_CONST;
}
return 0;
}
static int
tagmatch(nc_type nct, int tag, int hasU)
{
if(hasU) switch(nct) {
case NC_UBYTE: return (tag == 0 || tag == 'B' || tag == 'b');
case NC_USHORT: return (tag == 0 || tag == 'S' || tag == 's');
case NC_UINT: return (tag == 0);
case NC_UINT64: return (tag == 0 || tag == 'L' || tag == 'l');
default: return 0;
} else switch(nct) {
case NC_BYTE: return (tag == 0 || tag == 'B' || tag == 'b');
case NC_SHORT: return (tag == 0 || tag == 'S' || tag == 's');
case NC_INT: return (tag == 0);
case NC_INT64: return (tag == 0 || tag == 'L' || tag == 'l');
default: return 0;
}
return 0;
}