git @ Cat's Eye Technologies OpenZz / master src / zlex.c
master

Tree @master (Download .tar.gz)

zlex.c @masterraw · history · blame

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
/* 
    Zz Dynamic Parser Library
    Copyright (C) 1989 - I.N.F.N - S.Cabasino, P.S.Paolucci, G.M.Todesco

    The Zz Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    The Zz Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*==============================================================================

  ZLEX.C

  Lexical analyzer
  (by Simone Cabasino)


==============================================================================*/
#define ZLEX

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <stdarg.h>
#include "avl.h"
#include "zlex.h"
#include "mem.h"
#include "err.h"
//#define DEBUG
#include "trace.h"

/*----------------------------------------------------------------------------*/

#define cntlen(CNT)	\
	(*(((struct s_content *)(CNT))->tag->len)) (CNT) 
#define cntcpy(T,S)	\
	(*(((struct s_content*)(S))->tag->copy)) ((T),(S)) 
#define cntsprint(B,CNT) \
	(*(((struct s_content*)(CNT))->tag->sprint)) ((B),(CNT)) 


#define LEFT_ASSOCIATIVE 1
#define RIGHT_ASSOCIATIVE 0

#define ZLEX_PRECEDENCE(T)	\
	(((T)->tag==tag_ident || (T)->tag==tag_char) ? \
		   *(((unsigned char*) ((T)->value))-2)\
		:  100)


#define ZLEX_IS_LEFT_ASSOCIATIVE(T)	\
	(((T)->tag==tag_ident || (T)->tag==tag_char) ? \
		   (*(((char*) ((T)->value))-3) == LEFT_ASSOCIATIVE)\
		:  1)

/*----------------------------------------------------------------------------*/

#define lexical_error(FMT, ARGS...) zz_error(LEXICAL_ERROR, FMT , ## ARGS)

/*----------------------------------------------------------------------------*/

static int zlex_case_insensitive = 1;
static int zlex_realconst_double = 0; /* real const default type */
static int zlex_intconst_int64   = 0;
static int zlex_parse_eol        = 1;

static int zlex_strsaved_mem=0;
static int zlex_qstring_mem=0;
static int zlex_tag_mem=0;
static int ignore_block_flag=0;
static int braket_level=0;

#define MAX_TOKEN_LENGTH	255

/*----------------------------------------------------------------------------*/

extern int fprint_list(),sprint_list(),fprint_list_image(),sprint_list_image();

static int tags_initialized=0;

static int cur_prec = 100;
static int cur_assoc = LEFT_ASSOCIATIVE;

int str_term_n=0;

struct s_term {
	char *name;
	};

TREE *str_term_table=0;


/*----------------------------------------------------------------------------*/

#define isident(a) (isalnum(a) || (a)== '$' || (a) == '_')

#define TERM_MAGIC 123

/*----------------------------------------------------------------------------*/

int sprint_string(buffer,cnt)
char *buffer;
struct s_content *cnt;
{
  //zz_assert(cnt && s_content_tag(*cnt)==tag_qstring);
  strcpy(buffer, s_content_svalue(*cnt));
}

int fprint_string(chan,cnt)
FILE *chan;
struct s_content *cnt;
{
  //zz_assert(cnt && s_content_tag(*cnt)==tag_qstring);
  fprintf(chan,"%s", s_content_svalue(*cnt));
}

/*----------------------------------------------------------------------------*/

int sprint_int(buffer,cnt)
char *buffer;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_int);
  sprintf(buffer,"%d",s_content_ivalue(*cnt));
}

int fprint_int(chan,cnt)
FILE *chan;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_int);
  fprintf(chan,"%d",s_content_ivalue(*cnt));
}

/*----------------------------------------------------------------------------*/

int sprint_int64(buffer,cnt)
char *buffer;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_int64);
  sprintf(buffer,"%Ld",s_content_llvalue(*cnt));
}

int fprint_int64(chan,cnt)
FILE *chan;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_int64);
  fprintf(chan,"%Ld",s_content_llvalue(*cnt));
}

/*----------------------------------------------------------------------------*/

int sprint_hex(buffer,cnt)
char *buffer;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_hex);
  sprintf(buffer,"0x%X",s_content_value(*cnt));
}

int fprint_hex(chan,cnt)
FILE *chan;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_hex);
  fprintf(chan,"0x%X",s_content_value(*cnt));
}

/*----------------------------------------------------------------------------*/

int sprint_float(buffer,cnt)
char *buffer;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_float);
  sprintf(buffer,"%g",s_content_fvalue(*cnt));
}

int fprint_float(chan,cnt)
FILE *chan;
struct s_content *cnt;
{ 
 zz_assert(cnt && s_content_tag(*cnt)==tag_float);
 fprintf(chan,"%g",s_content_fvalue(*cnt));
}

/*----------------------------------------------------------------------------*/

int sprint_double(buffer,cnt)
char *buffer;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_double);
  sprintf(buffer,"%g",s_content_dvalue(*cnt));
}

int fprint_double(chan,cnt)
FILE *chan;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_double);
  fprintf(chan,"%g",s_content_dvalue(*cnt));
}

/*----------------------------------------------------------------------------*/

int sprint_eol(buffer,cnt)
char *buffer;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_eol);
  strcpy(buffer,"EOL");
}

int fprint_eol(chan,cnt)
FILE *chan;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_eol);
  fprintf(chan,"EOL");
}

/*----------------------------------------------------------------------------*/

int sprint_eof(buffer,cnt)
char *buffer;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_eof);
  strcpy(buffer,"EOF");
}

int fprint_eof(chan,cnt)
FILE *chan;
struct s_content *cnt;
{
  zz_assert(cnt && s_content_tag(*cnt)==tag_eof);
  fprintf(chan,"EOF");
}


/*----------------------------------------------------------------------------*/

int sprint_none(buffer,cnt)
char *buffer;
struct s_content *cnt;
{strcpy(buffer,"NONE");}

int fprint_none(chan,cnt)
FILE *chan;
struct s_content *cnt;
{fprintf(chan,"NONE");}


/*------------------------------------------------------------------------*/
/* 
   ???_cast() casting operations for numerical types.
   Returned structure is the 'tgt' param with results. 
*/
static 
struct s_content *int_cast(src, tgt_tag, tgt)
     struct s_content *src, *tgt;
     struct s_tag *tgt_tag;
{
  if (src->tag != tag_int) {
    printz("Error: int_cast(), source tag type not int: %s\n", src->tag->name);
    return 0;
  }

  if (tgt_tag == tag_int)
    s_content_ivalue(*tgt) = (int32_t)s_content_ivalue(*src);
  else if (tgt_tag == tag_int64)
    s_content_llvalue(*tgt) = (int64_t)s_content_ivalue(*src);
  else if (tgt_tag == tag_float)
    s_content_fvalue(*tgt) = (float)s_content_ivalue(*src);
  else if (tgt_tag == tag_double)
    s_content_dvalue(*tgt) = (double)s_content_ivalue(*src);
  else {
    printz("Error: int_cast(), bad type for arithmetic target: %s\n", tgt_tag->name);
    return 0;
  }

  tgt->tag == tgt_tag;
  return tgt;
}

static 
struct s_content *int64_cast(src, tgt_tag, tgt)
     struct s_content *src, *tgt;
     struct s_tag *tgt_tag;
{
  if (src->tag != tag_int64) {
    printz("Error: int64_cast(), source tag type not int64: %s\n", src->tag->name);
    return 0;
  }

  if (tgt_tag == tag_int)
    s_content_ivalue(*tgt) = (int32_t)s_content_llvalue(*src);
  else if (tgt_tag == tag_int64)
    s_content_llvalue(*tgt) = (int64_t)s_content_llvalue(*src);
  else if (tgt_tag == tag_float)
    s_content_fvalue(*tgt) = (float)s_content_llvalue(*src);
  else if (tgt_tag == tag_double)
    s_content_dvalue(*tgt) = (double)s_content_llvalue(*src);
  else {
    printz("Error: int64_cast(), bad type for arithmetic target: %s\n", tgt_tag->name);
    return 0;
  }

  tgt->tag == tgt_tag;
  return tgt;
}

static 
struct s_content *float_cast(src, tgt_tag, tgt)
     struct s_content *src, *tgt;
     struct s_tag *tgt_tag;
{
  if (src->tag != tag_float) {
    printz("Error: float_cast(), source tag type not float: %s\n", src->tag->name);
    return 0;
  }

  if (tgt_tag == tag_int)
    s_content_ivalue(*tgt) = (int32_t)s_content_fvalue(*src);
  else if (tgt_tag == tag_int64)
    s_content_llvalue(*tgt) = (int64_t)s_content_fvalue(*src);
  else if (tgt_tag == tag_float)
    s_content_fvalue(*tgt) = s_content_fvalue(*src);
  else if (tgt_tag == tag_double)
    s_content_dvalue(*tgt) = (double)s_content_fvalue(*src);
  else {
    printz("Error: float_cast(), bad type for arithmetic target: %s\n", tgt_tag->name);
    return 0;
  }

  tgt->tag == tgt_tag;
  return tgt;
}


static 
struct s_content *double_cast(src, tgt_tag, tgt)
     struct s_content *src, *tgt;
     struct s_tag *tgt_tag;
{
  if (src->tag != tag_double) {
    printz("Error: double_cast(), source tag type not double: %s\n", src->tag->name);
    return 0;
  }

  if (tgt_tag == tag_int)
    s_content_ivalue(*tgt) = (int32_t)s_content_dvalue(*src);
  else if (tgt_tag == tag_int64)
    s_content_llvalue(*tgt) = (int64_t)s_content_dvalue(*src);
  else if (tgt_tag == tag_float)
    s_content_fvalue(*tgt) = (float)s_content_dvalue(*src);
  else if (tgt_tag == tag_double)
    s_content_dvalue(*tgt) = s_content_dvalue(*src);
  else {
    printz("Error: double_cast(), bad type for arithmetic target: %s\n", tgt_tag->name);
    return 0;
  }

  tgt->tag == tgt_tag;
  return tgt;
}


static 
struct s_content *invalid_cast(src, tgt_tag, tgt)
     struct s_content *src;
     struct s_tag *tgt_tag;
     struct s_content *tgt;
{
  printf("Error - non numerical type - casting not allowed");
  return 0;
}

/*----------------------------------------------------------------------------*/

static int fprint_zlex(FILE *chan, void *cnt);
static int sprint_zlex(char *stringout, void *cnt);
static int fprint_zlex_image(FILE *chan, void *cnt);
static int sprint_zlex_image(char *stringout, void *cnt);

#if 0

/*
  there is a bug in GNU LibC 2.1.3 (RH6.2) and %z is not overloadable
 */

static int 
print_scontent (FILE *stream,
		const struct printf_info *info,
		const void *const *args)
{
  struct s_content *w;
  char buffer[4096];
  int len;
  w = *((struct s_content **) (args[0]));
  if(info->spec=='z')
    sprint_zlex(buffer, w);
  else if(info->spec=='w')
    sprint_zlex_image(buffer, w);
  else
    assert(!"should never happen");
  len = strlen(buffer);
  assert(len < sizeof(buffer));
  len = fprintf (stream, "%s",buffer);
  return len;
}
     
     
static int 
print_scontent_arginfo (const struct printf_info *info, 
			size_t n,
			int *argtypes)
{
  /* We always take exactly one argument and this is a pointer to the
     structure.. */
  assert(0);
  if (n > 0)
    argtypes[0] = PA_POINTER;
  return 1;
}
     

static void init_print_zlex()
{
  register_printf_function ('z', print_scontent, print_scontent_arginfo);
  register_printf_function ('w', print_scontent, print_scontent_arginfo);
}
#endif

void init_zlex(void)
{
if(init_zlex_done) return;
init_zlex_done=1;

printz_code('z',fprint_zlex,sprint_zlex);
printz_code('w',fprint_zlex_image,sprint_zlex_image);
/* init_print_zlex(); */

tag_int = find_tag("int");
tag_int->sprint=sprint_int;
tag_int->fprint=fprint_int;
tag_int->cast=int_cast;

tag_int64 = find_tag("int64");
tag_int64->sprint=sprint_int64;
tag_int64->fprint=fprint_int64;
tag_int64->cast=int64_cast;

tag_hex = find_tag("hex");
tag_hex->sprint=sprint_hex;
tag_hex->fprint=fprint_hex;
tag_hex->cast=invalid_cast;

tag_float = find_tag("float");
tag_float ->sprint=sprint_float ;
tag_float ->fprint=fprint_float ;
tag_float->cast=float_cast;

tag_double = find_tag("double");
tag_double ->sprint=sprint_double ;
tag_double ->fprint=fprint_double ;
tag_double->cast=double_cast;

tag_qstring = find_tag("qstring");
tag_qstring ->sprint=sprint_string ;
tag_qstring ->fprint=fprint_string ;
tag_qstring->cast=invalid_cast;

tag_ident = find_tag("ident");
tag_ident ->sprint=sprint_string ;
tag_ident ->fprint=fprint_string ;
tag_ident->cast=invalid_cast;

tag_eol = find_tag("eol");
tag_eol ->sprint=sprint_eol ;
tag_eol ->fprint=fprint_eol ;
tag_eol->cast=invalid_cast;

tag_eof = find_tag("eof");
tag_eof ->sprint=sprint_eof ;
tag_eof ->fprint=fprint_eof ;
tag_eof->cast=invalid_cast;

tag_char = find_tag("char");
tag_char ->sprint=sprint_string ;
tag_char ->fprint=fprint_string ;
tag_char->cast=invalid_cast;

tag_none = find_tag("none");
tag_none ->sprint=sprint_none ;
tag_none ->fprint=fprint_none ;
tag_none->cast=invalid_cast;

tag_address = find_tag("address");
tag_address->cast=invalid_cast;

tag_procedure = find_tag("procedure");
tag_procedure->cast=invalid_cast;

tag_qprocedure = find_tag("qprocedure");
tag_qprocedure->cast=invalid_cast;

tag_list = find_tag("list");
tag_list->sprint=sprint_list;
tag_list->fprint=fprint_list;
tag_list->cast=invalid_cast;

tag_cont = find_tag("cont");
tag_cont->cast=invalid_cast;

tag_special = find_tag("special");
tag_special->cast=invalid_cast;

tag_sint = find_tag("sint");
tag_sint->cast=invalid_cast;

tag_param = find_tag("param");
tag_param->cast=invalid_cast;

tag_bead =find_tag("bead");
tag_bead->cast=invalid_cast;

tags_initialized=1;
}

/*----------------------------------------------------------------------------*/

static int sprint_zlex(char *stringout, void *_cnt)
{
  struct s_content *cnt= (struct s_content *)_cnt;
  if(cnt->tag)
    (*cnt->tag->sprint)(stringout,cnt);
  else
    strcpy(stringout,"NULL");
  return 1;
}

/*----------------------------------------------------------------------------*/

static int fprint_zlex(FILE *chan, void *_cnt)
{
  struct s_content *cnt= (struct s_content *)_cnt;
  if(cnt->tag)
    (*cnt->tag->fprint)(chan,cnt);
  else
    fprintf(chan,"NULL");
  return 1;
}

/*----------------------------------------------------------------------------*/

static int sprint_zlex_image(char *stringout, void *_cnt)
{
  struct s_content *cnt= (struct s_content *)_cnt;
  if(cnt->tag)
    {
      if(cnt->tag==tag_eol)
	sprintf(stringout,"\n");
      else if(cnt->tag==tag_list)
	{
	  sprint_list_image(stringout,cnt);
	}
      else
	(*cnt->tag->sprint)(stringout,cnt);
    }
  return 1;
}

/*----------------------------------------------------------------------------*/

static int fprint_zlex_image(FILE *chan, void *_cnt)
{
  struct s_content *cnt= (struct s_content *)_cnt;
  if(cnt->tag)
    {
      if(cnt->tag==tag_eol)
	fprintf(chan,"\n");
      else if(cnt->tag==tag_list)
	{
	  fprint_list_image(chan,cnt);
	}
      else
	(*cnt->tag->fprint)(chan,cnt);
    }
  return 1;
}
/*----------------------------------------------------------------------------*/


void zlex_set_precedence(cnt,prec,left_assoc)
struct s_content *cnt;
int prec,left_assoc;
{
char *s;
if(cnt->tag!=tag_ident && cnt->tag!=tag_char)
  {
   printz("zlex_set_precedence: unable to change prec of token /%z/\n",cnt);
   return;
  }
if(prec<0 || prec>255 || (left_assoc!=1 && left_assoc!=0))
  {
   printz("zlex_set_precedence: bad prec/assoc values (%d %d) for token /%z/\n",
	prec,left_assoc,cnt);
   return;
  }
s = (char*)s_content_value(*cnt);
s--;
if(*s!=TERM_MAGIC)
  {
   lexical_error("zlex_set_precedence: bad magic");
   return;
  }
*--s = prec;
*--s = left_assoc? LEFT_ASSOCIATIVE:RIGHT_ASSOCIATIVE;
}


/*----------------------------------------------------------------------------*/

char *zlex_strsave(name)
const char *name;
{
char *tmp;
struct s_term *term;
const char *cs;
char *s;
char *t;
struct symbol **p;
struct s_string_hd *hd;
int dsc,len;
tmp=0;
if(zlex_case_insensitive)
  {
   tmp = (char*)malloc(strlen(name)+1);
   cs = name;
   t = tmp;
   while(*cs)
     {
      if(*cs>='A' && *cs<='Z') *t= *cs+'a'-'A';
      else *t= *cs;
      cs++;
      t++;
     }
   *t='\0';
   name = tmp;
  }
/* one shot init */
if(!str_term_table)
  str_term_table = avl_tree_nodup_str(struct s_term,name);

term = avl_locate(str_term_table,name);
if(!term)
  {
   term = (struct s_term *)calloc(1,sizeof(struct s_term));
   len = strlen(name);
   s = (char*) malloc(len+1+3);
   *s++ = cur_assoc;
   *s++ = cur_prec;
   *s++ = TERM_MAGIC; 
   term->name = s;
   strcpy(term->name,name);
   zlex_strsaved_mem += sizeof(struct s_term) + len+1+3;
   str_term_n++;
   avl_insert(str_term_table,term);
  }
if(tmp) free(tmp);
return term->name;
}



/*----------------------------------------------------------------------------*/

int std_len(struct s_content *cnt)
{
  //return strlen(cnt->tag->name)+9;
  zz_assert(!"any use????");
}

/*----------------------------------------------------------------------------*/

int std_sprint(buffer,cnt)
char *buffer;
struct s_content *cnt;
{
sprintf(buffer,"%s:%08X",cnt->tag->name,s_content_value(*cnt));
}

/*----------------------------------------------------------------------------*/

int std_fprint(chan,cnt)
FILE *chan;
struct s_content *cnt;
{
fprintf(chan,"%s:%08X",cnt->tag->name,s_content_value(*cnt));
}

/*----------------------------------------------------------------------------*/

int std_copy(tar,src)
struct s_content *tar,*src;
{
*tar = *src;
}

/*----------------------------------------------------------------------------*/

static TREE *tag_tree=0;

/*----------------------------------------------------------------------------*/

static struct s_tag* create_tag(const char* name)
{
  struct s_tag *tag;
  tag = (struct s_tag*)calloc(1,sizeof(struct s_tag));
  zz_assert(tag);
  tag->name      = zlex_strsave(name);
  tag->sprint    = std_sprint;
  tag->fprint    = std_fprint;
  tag->copy      = std_copy;
  tag->len       = std_len;
  tag->delete    = 0;
  tag->cast      = invalid_cast;
  tag->param_on  = 0;
  tag->param_off = 0;
  avl_insert(tag_tree,tag);
  zlex_tag_mem+=sizeof(struct s_tag);
  return tag;
}

/*----------------------------------------------------------------------------*/

static struct s_tag* 
search_tag(const char* name)
{
  struct s_tag *tag;

  zz_assert(name);

  if(!tag_tree)
    tag_tree=avl_tree_nodup_str(struct s_tag,name);

  tag = avl_locate(tag_tree,name);
  return tag;
}

/*----------------------------------------------------------------------------*/

struct s_tag* 
find_tag(const char* name)
{
  struct s_tag *tag;

  tag = search_tag(name);
  if(!tag) 
    tag = create_tag(name);
  return tag;
}

/*----------------------------------------------------------------------------*/

int
zz_lex_add_new_tag(const char*        this_tag_name, 
		   zz_tag_sprint_cb   this_tag_sprint, 
		   zz_tag_fprint_cb   this_tag_fprint,
		   zz_tag_cdtor       this_tag_ctor,
		   zz_tag_cdtor       this_tag_dtor,
		   zz_tag_cast        this_tag_cast)
{
  struct s_tag *newtag, *oldtag;

  if(!this_tag_name) {
    zz_error(FATAL_ERROR, "%s: can't add new tag with null name\n", __FUNCTION__);
    return 0;    
  }
  oldtag = search_tag(this_tag_name);
  if(oldtag) {
    zz_error(FATAL_ERROR, "%s: can't add new tag '%s' (already exists)\n", __FUNCTION__, this_tag_name);
    return 0;
  }

  newtag = create_tag(this_tag_name);
  if(this_tag_sprint)
    newtag->sprint=this_tag_sprint;
  if(this_tag_fprint)
    newtag->fprint=this_tag_fprint;
  if(this_tag_cast)
    newtag->cast=this_tag_cast;
  if(this_tag_ctor)
    newtag->param_on=this_tag_ctor;
  if(this_tag_dtor)
    newtag->param_off=this_tag_dtor;
  return 1;
}

/*----------------------------------------------------------------------------*/

int
zz_lex_remove_tag(const char* tag_name)
{
  struct s_tag *tag;

  if(!tag_name) {
    zz_error(FATAL_ERROR, "%s: null tag\n", __FUNCTION__);
    return 0;    
  }
  tag = search_tag(tag_name);
  if(!tag) {
    zz_error(FATAL_ERROR, "%s: can't find tag '%s'\n", __FUNCTION__, tag_name);
    return 0;
  }  
  if(!avl_remove(tag_tree, tag_name)) {
    zz_error(FATAL_ERROR, "%s: can't remove tag '%s'\n", __FUNCTION__, tag_name);
    return 0;
  }
  return 1;
}

/*----------------------------------------------------------------------------*/

static void for_all_str_term(void (*action)())
{
if(str_term_table)
  avl_scan(str_term_table,action);
}

/*----------------------------------------------------------------------------*/

static int esc_tran(c)
char c;
{
switch(c)
  {
   case 'r':  return '\r';
   case 'n':  return '\n';
   case '7':  return '\7';
   case '?':  return '\033';
   case 't':  return '\t';
   case '\"': return '\"';
   case '\'': return '\'';
   case '\\': return '\\';
   default:   return c;
  }
}



/*---------------------------------------------------------------------------*/


void zlex(ptr, cnt)
char **ptr;
struct s_content *cnt;
{
double df;
char buffer[MAX_TOKEN_LENGTH+1];
int i;
if(!init_zlex_done) init_zlex();
while (**ptr==' ' || **ptr=='\t') (*ptr)++;

switch(**ptr)
  {
   /* IDENTIFICATORE */
    //   CASE_IDENT_START:
  case 'a' ... 'z': 
  case 'A' ... 'Z': 
  case '$': 
  case '_':
     i=0;
     buffer[i++] = *((*ptr) ++);
     while( i<MAX_TOKEN_LENGTH && isident(**ptr))
       buffer[i++] = *((*ptr) ++);
     if (i >= MAX_TOKEN_LENGTH)
       {
        lexical_error("Identifier too long. truncated.");
        i=MAX_TOKEN_LENGTH;
       }
     buffer[i]=0;
// Sap:     cnt->value = zlex_strsave(buffer);
     s_content_value(*cnt) = (int) zlex_strsave(buffer);
     cnt->tag   = tag_ident;
     break;

   /* COSTANTE NUMERICA HEX/INT/FLOAT/DOUBLE */
   case '0' ... '9':
     if(**ptr=='0' 
         && (*(*ptr+1)=='x' || *(*ptr+1)=='X') 
         && isxdigit(*(*ptr+2)) )
       {
        /* HEX */
        (*ptr)+=2;
        i=0;
        buffer[i++] = *((*ptr) ++);
        while( i<=16 && isxdigit(**ptr))
          buffer[i++] = *((*ptr) ++);
	buffer[i]='\0';
	if(**ptr=='l' || zlex_intconst_int64) {
	  if (i > 16) {
	    lexical_error("too long hex constant truncated /%s/",buffer);
	    buffer[16]='\0';
	  }
	  if(**ptr=='l') (*ptr) ++;
	  sscanf(buffer,"%Lx",&(s_content_llvalue(*cnt)));
	  cnt->tag   = tag_int64;
	} else if (**ptr=='i' || !zlex_intconst_int64) {
	  if (i > 8) {
	    lexical_error("too long hex constant truncated /%s/",buffer);
	    buffer[8]='\0';
	  }
	  if(**ptr=='i') (*ptr) ++;
	  sscanf(buffer,"%x",&(s_content_value(*cnt)));
	  cnt->tag   = tag_int;
	}
       }
     else
       {
        /* INT/FLOAT/DOUBLE */
	 i=0;
	 while( i<255 && isdigit(**ptr))
	   buffer[i++] = *((*ptr) ++);

	 if(**ptr=='.')
	   {
	     /* FLOAT||DOUBLE */
	     buffer[i++] = *((*ptr) ++);
	     while( i<39 && isdigit(**ptr))
	       buffer[i++] = *((*ptr) ++);
	     
	     if ((**ptr=='e' || **ptr=='E') &&
		 ( (*(*ptr+1)=='-' && isdigit(*(*ptr+2)) ) ||
		   (*(*ptr+1)=='+' && isdigit(*(*ptr+2)) ) ||
		   isdigit(*(*ptr+1))
		   )
		 )
	       {
		 buffer[i++] = *((*ptr) ++);
		 
		 if(**ptr=='-' || **ptr=='+')
		   buffer[i++] = *((*ptr) ++);
		 
		 while(isdigit(**ptr))
		   buffer[i++] = *((*ptr) ++);
	       }
	     
	   zz_assert(i<MAX_TOKEN_LENGTH);
           buffer[i]='\0';
           sscanf(buffer,"%lf",&df);
	   if(**ptr=='d' || (zlex_realconst_double && (**ptr!='f'))) {
	     if(**ptr=='d') (*ptr) ++;
	     s_content_dvalue(*cnt) = df;
	     cnt->tag   = tag_double;
	   } else if(**ptr=='f' || !zlex_realconst_double) {
	     if(**ptr=='f') (*ptr) ++;
	     s_content_fvalue(*cnt) = (float)df;
	     cnt->tag   = tag_float;
	   } else {
	     zz_assert(!"should never happen");
	   }
          }
        else
          {
           /* INT */
           buffer[i]='\0';
	   if(**ptr=='l' || (zlex_intconst_int64 && **ptr!='i')) {
	     if (i > 30) {
	       lexical_error("too long integer constant /%s/ truncated",buffer);
	       buffer[30]='\0';
	     }
	     if(**ptr=='l') (*ptr) ++;
	     sscanf(buffer,"%Li",&(s_content_llvalue(*cnt)));
	     zz_trace("got int64 = (%s=%Li)\n", buffer, s_content_llvalue(*cnt));
	     cnt->tag   = tag_int64;
	   } else if (**ptr=='i' || !zlex_intconst_int64){
	     int scret=0;
	     if (i > (10+(((**ptr=='-') || (**ptr=='+'))?1:0))) {
	       lexical_error("too long integer constant /%s/ truncated",buffer);
	       buffer[10]='\0';
	     }
	     if(**ptr=='i') (*ptr) ++;
	     scret=sscanf(buffer,"%d",&(s_content_value(*cnt)));
	     cnt->tag   = tag_int;
	     if(scret!=1) {
	       lexical_error("can't parse integer constant /%s/", buffer);
	     }
	   } else {
	     zz_assert(!"should never happen");
	   }
          }
       }
     break;

  /* END OF FILE */
  case '\255':
    zz_trace("got EOF\n");
    cnt->tag = tag_eof;
    s_content_value(*cnt)=0;
    break;

  /* END OF LINE */
  case '\0':
    zz_trace("got EOL\n");
#if 1
    if(zlex_parse_eol) {
      cnt->tag = tag_eol;
    }else{
      cnt->tag = tag_cont;
      *ptr = 0;
      //(*ptr)++;
    }
#else
    cnt->tag = tag_eol;
#endif
    s_content_value(*cnt)=0;
    break;

  case '\n':
    zz_trace("got linefeed\n");
    (*ptr)++;
#if 1
    if(zlex_parse_eol) {
      cnt->tag = tag_eol;
    }else{
      cnt->tag = tag_cont;
    }
#else
    cnt->tag = tag_eol;
#endif
    s_content_value(*cnt)=0;
    break;

  /* ESCAPED SEQUENCE */
/*
  case '\\':
    (*ptr)++;
    buffer[0] = esc_tran(*(*ptr)++);
    buffer[1] = '\0';
    cnt->value= (long)zlex_strsave(buffer);
    cnt->tag=tag_char;
    break;
*/
     
  /* QSTRING */
  case '\"':
    i=0;
    (*ptr) ++; /* schippo il primo " */
    while(i<MAX_TOKEN_LENGTH && **ptr && **ptr != '\"')
      {
       if (**ptr == '\\')
         {
          (*ptr)++;
          buffer[i++] = esc_tran(*(*ptr)++);
	 }
       else
         buffer[i++] = *(*ptr)++;
      }
    buffer[i]='\0';
    if(!**ptr || i>=MAX_TOKEN_LENGTH)
       lexical_error("qstring not terminated or too long /%s/",buffer);
    if(**ptr=='\"') (*ptr) ++;
// Sap:    cnt->value = (char*)malloc(strlen(buffer)+1);

    //s_content_value(*cnt) = (int)malloc(strlen(buffer)+1);
    //strcpy((char*)s_content_value(*cnt),buffer);
    s_content_svalue(*cnt) = (char*)strdup(buffer);
    zz_assert(s_content_svalue(*cnt));

    cnt->tag=tag_qstring;
    zlex_qstring_mem+=strlen(buffer)+1;
    break;

  /* COMMENTI */
  case '!':
    zz_trace("got !\n");
    if (*((*ptr)+1)=='!')
      {
       while(*(*ptr)++);
       s_content_value(*cnt)=0;
       cnt->tag=tag_eol;
      }
    else
      {
       (*ptr)++;
// Sap:       cnt->value= zlex_strsave("!");
       s_content_value(*cnt)= (int)zlex_strsave("!");
       cnt->tag=tag_char;
      }
    break;

  /* COMMENTI C++ STYLE*/
  case '/':
    if (*((*ptr)+1)=='/')
      {
       while(*(*ptr)++);
       s_content_value(*cnt)=0;
       cnt->tag=tag_eol;
      }
    else
      {
       (*ptr)++;
// Sap:       cnt->value= zlex_strsave("/");
       s_content_value(*cnt)= (int)zlex_strsave("/");
       cnt->tag=tag_char;
      }
    break;

  /* CONT. MARK */
  case '.':
    if (zlex_parse_eol && (*((*ptr)+1)=='.'&& *((*ptr)+2)=='.'))
      {
       (*ptr)+=3;
       while(**ptr) (*ptr)++;
       s_content_value(*cnt)=0;
       cnt->tag=tag_cont;
      }
    else
      {
       (*ptr)++;
// Sap:       cnt->value = zlex_strsave(".");
       s_content_svalue(*cnt) =  zlex_strsave(".");
       cnt->tag=tag_char;
      }
    break;
  default:
    buffer[0]= *(*ptr)++;
    buffer[1]= '\0';
// Sap:    cnt->value = zlex_strsave(buffer);
    s_content_svalue(*cnt) = zlex_strsave(buffer);
    cnt->tag=tag_char;
  }
 zz_trace("token is '%z'\n", cnt); 
} /* end zlex() */


/*----------------------------------------------------------------------------*/

void show_zlex_memory(void)
{
PRINTMEM("zlex.qstring.mex",zlex_qstring_mem)
PRINTMEM("zlex.strsaved.mem",zlex_strsaved_mem)
PRINTMEM("zlex.tag.mem",zlex_tag_mem)
}

/*----------------------------------------------------------------------------*/

int zlex_set_case_sensitive(cs)
     int cs;
{
  zlex_case_insensitive=!cs;
  return 1;
}

int zlex_set_parse_eol(peol)
     int peol;
{
  zz_trace("zlex_set_parse_eol(%d)\n", peol);
  zlex_parse_eol=peol;
  return 1;
}

int zlex_set_default_real_as_double(peol)
     int peol;
{
  zz_trace("zlex_set_default_real_as_double(%d)\n", peol);
  zlex_realconst_double=peol;
  return 1;
}

int zlex_set_default_integer_as_int64(peol)
     int peol;
{
  zz_trace("zlex_set_default_real_as_double(%d)\n", peol);
  zlex_intconst_int64=peol;
  return 1;
}

/*------------------------------------------------------------------------*/


void ignore_block(void)
{
ignore_block_flag = 1;
braket_level = 0;
}


/*---------------------------------------------------------------------------*/

static char rcsid[] = "$Id: zlex.c,v 1.34 2002/06/03 11:06:13 kibun Exp $ ";