aboutsummaryrefslogtreecommitdiff
path: root/Source/CParse/cscanner.c
blob: 1d185f5977ede8f5ef56f13795dbc38ccd1f00b0 (plain)
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
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
/* -----------------------------------------------------------------------------
 * scanner.cxx
 *
 *     SWIG1.1 tokenizer.
 *
 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
 *
 * Copyright (C) 1998-2000.  The University of Chicago
 * Copyright (C) 1995-1998.  The University of Utah and The Regents of the
 *                           University of California.
 *
 * See the file LICENSE for information on usage and redistribution.
 * ----------------------------------------------------------------------------- */

char cvsroot_cscanner_c[] = "$Header$";

#include "cparse.h"
#include "parser.h"
#include <string.h>
#include <ctype.h>

#define  YYBSIZE  8192

typedef struct InFile {
  DOHFile *f;
  int     line_number;
  char   *in_file;
  struct InFile *prev;
} InFile;

InFile  *in_head;

DOHFile *LEX_in = 0;
static DOHString     *header = 0;
static DOHString     *comment = 0;
DOHString            *scanner_ccode = 0;            /* String containing C code */
static char          *yybuffer = 0;

static char    yytext[YYBSIZE];
static int     yylen = 0;
int            cparse_line = 1;
char          *cparse_file;
int            cparse_start_line = 0;
static  int    comment_start;
static  int    scan_init  = 0;
static  int    num_brace = 0;
static  int    last_brace = 0;
static  int    last_id = 0;
static  int    rename_active = 0;
        int    cparse_cplusplus;

/* -----------------------------------------------------------------------------
 * Swig_cparse_cplusplus()
 * ----------------------------------------------------------------------------- */

void
Swig_cparse_cplusplus(int v) {
  cparse_cplusplus = v;
}

/* ----------------------------------------------------------------------
 * locator()
 *
 * Support for locator strings.   These are strings of the form
 * @filename,line,id@ emitted by the SWIG preprocessor.  They
 * are primarily used for macro line number reporting 
 * ---------------------------------------------------------------------- */

typedef struct Locator {
  char           *filename;
  int             line_number;
  struct Locator *next;
} Locator;

static Locator *locs = 0;
  
static void
scanner_locator(String *loc) {
  int c;
  Locator *l;
  Seek(loc,1,SEEK_SET);
  c = Getc(loc);
  if (c == '@') {
    /* Empty locator.  We pop the last location off */
    if (locs) {
      cparse_file = locs->filename;
      cparse_line = locs->line_number;
      l = locs->next;
      free(locs);
      locs = l;
    }
    /*    Printf(stderr,"location: %s:%d\n",cparse_file,cparse_line);*/
    return;
  }

  /* We're going to push a new location */
  l = (Locator *) malloc(sizeof(Locator));
  l->filename = cparse_file;
  l->line_number = cparse_line;
  l->next = locs;
  locs = l;
  
  /* Now, parse the new location out of the locator string */
  {
    String *fn = NewString("");
    Putc(c,fn);
    
    while ((c = Getc(loc)) != EOF) {
      if ((c == '@') || (c == ',')) break;
      Putc(c,fn);
    }

    cparse_file = Swig_copy_string(Char(fn));
    Clear(fn);
  
    cparse_line = 1;
    /* Get the line number */
    while ((c = Getc(loc)) != EOF) {
      if ((c == '@') || (c == ',')) break;
      Putc(c,fn);
    }

    cparse_line = atoi(Char(fn));
    Clear(fn);

    /* Get the rest of it */
    while (( c= Getc(loc)) != EOF) {
      if (c == '@') break;
      Putc(c,fn);
    }
    /*  Printf(stderr,"location: %s:%d\n",cparse_file,cparse_line); */
    Delete(fn);
  }
}

/**************************************************************
 * scanner_init()
 *
 * Initialize buffers
 **************************************************************/

void scanner_init() {
  yybuffer = (char *) malloc(YYBSIZE);
  scan_init = 1;
  header = NewString("");
  comment = NewString("");
  scanner_ccode = NewString("");
}

/**************************************************************
 * scanner_file(FILE *f)
 *
 * Start reading from new file
 **************************************************************/
void scanner_file(DOHFile *f) {
  InFile *in;

  in = (InFile *) malloc(sizeof(InFile));
  in->f = f;
  in->in_file = cparse_file;
  in->line_number = 1;
  if (!in_head) in->prev = 0;
  else in->prev = in_head;
  in_head = in;
  LEX_in = f;
  cparse_line = 1;
}

/**************************************************************
 * scanner_close()
 *
 * Close current input file and go to next
 **************************************************************/

void scanner_close() {
  InFile *p;
  if (!in_head) return;
  Delete(LEX_in);
  p = in_head->prev;
  if (p != 0) {
    LEX_in = p->f;
    cparse_line = p->line_number;
    cparse_file = p->in_file;
  } else {
    LEX_in = 0;
  }
  free(in_head);
  in_head = p;
}

/**************************************************************
 * char nextchar()
 *
 * gets next character from input.
 * If we're in inlining mode, we actually retrieve a character
 * from inline_yybuffer instead.
 **************************************************************/

char nextchar() {
    int c = 0;

    while (LEX_in) {
      c = Getc(LEX_in);
      if (c == EOF) {
	scanner_close();
      } else {
	break;
      }
    }
    if (!LEX_in) return 0;
    if (yylen >= YYBSIZE) {
      Printf(stderr,"** FATAL ERROR.  Buffer overflow in scanner.cxx.\nReport this to swig-dev@cs.uchicago.edu.\n");
      exit (EXIT_FAILURE);
    }
    yytext[yylen] = c;
    yylen++;
    if (c == '\n') {
      cparse_line++;
    }
    return(c);
}

void retract(int n) {
  int i;
  for (i = 0; i < n; i++) {
    yylen--;
    if (yylen >= 0) {
      Ungetc(yytext[yylen],LEX_in);
      if (yytext[yylen] == '\n') {
	cparse_line--;
      }
    }
  }
  if (yylen < 0) yylen = 0;
}

/**************************************************************
 * start_inline(char *text, int line)
 *
 * This grabs a chunk of text and tries to inline it into
 * the current file.  (This is kind of wild, but cool when
 * it works).
 *
 * If we're already in inlining mode, we will save the code
 * as a new fragment.
 **************************************************************/

void start_inline(char *text, int line) {
  InFile *in;

  /* Save current state */
  in_head->line_number = cparse_line;
  in_head->in_file = cparse_file;

  in = (InFile *) malloc(sizeof(InFile));
  in->f = NewString(text);
  Seek(in->f,0,SEEK_SET);
  in->in_file = Swig_copy_string(cparse_file);
  in->line_number = line;
  in->prev = in_head;
  in_head = in;
  LEX_in = in->f;
  cparse_line = line;
}

/**************************************************************
 * yycomment(char *, int line)
 *
 * Inserts a comment into a documentation entry.
 **************************************************************/

void yycomment(char *a, int b, int c) {
}


/* -----------------------------------------------------------------------------
 * skip_balanced()
 *
 * Skips a piece of code enclosed in begin/end symbols such as '{...}' or
 * (...).  Ignores symbols inside comments or strings.
 * ----------------------------------------------------------------------------- */

void
skip_balanced(int startchar, int endchar) {
    char c;
    int  num_levels = 1;
    int  state = 0;
    char temp[2] = {0,0};
    int  start_line = cparse_line;

    Clear(scanner_ccode);
    Putc(startchar,scanner_ccode);
    temp[0] = (char) startchar;
    while (num_levels > 0) {
      c = nextchar();
      if (c == 0) {
	  Swig_error(cparse_file, start_line, "Missing '%c'. Reached end of input.\n", endchar);
	  return;
      }
      Putc(c,scanner_ccode);
      switch(state) {
      case 0:
	if (c == startchar) num_levels++;
	else if (c == endchar) num_levels--;
	    else if (c == '/') state = 10;
	else if (c == '\"') state = 20;
	else if (c == '\'') state = 30;
	break;
      case 10:
	if (c == '/') state = 11;
	else if (c == '*') state = 12;
	else state = 0;
	break;
      case 11:
	if (c == '\n') state = 0;
	else state = 11;
	break;
      case 12:
	if (c == '*') state = 13;
	break;
      case 13:
	if (c == '*') state = 13;
	else if (c == '/') state = 0;
	else state = 12;
	break;
      case 20:
	if (c == '\"') state = 0;
	else if (c == '\\') state = 21;
	break;
      case 21:
	state = 20;
	break;
      case 30:
	if (c == '\'') state = 0;
	else if (c == '\\') state = 31;
	break;
      case 31:
	state = 30;
	break;
      default:
	break;
      }
      yylen = 0;
    }
    if (endchar == '}') num_brace--;
    return;
}

/**************************************************************
 * void skip_decl(void)
 *
 * This tries to skip over an entire declaration.   For example
 *
 *  friend ostream& operator<<(ostream&, const char *s);
 *
 * or
 *  friend ostream& operator<<(ostream&, const char *s) { };
 *
 **************************************************************/

void skip_decl(void) {
  char c;
  int  done = 0;
  while (!done) {
    if ((c = nextchar()) == 0) {
      Swig_error(cparse_file,cparse_line,"Missing semicolon. Reached end of input.\n");
      return;
    }
    if (c == '{') {
      last_brace = num_brace;
      num_brace++;
      break;
    }
    yylen = 0;
    if (c == ';') done = 1;
  }
  if (!done) {
    while (num_brace > last_brace) {
      if ((c = nextchar()) == 0) {
	Swig_error(cparse_file,cparse_line,"Missing '}'. Reached end of input.\n");
	return;
      }
      if (c == '{') num_brace++;
      if (c == '}') num_brace--;
      yylen = 0;
    }
  }
}

/* This function is called when a backslash is found in a string */
static void get_escape() {
  int result = 0;
  int state = 0;
  char  c;

  while(1) {
    c = nextchar();
    if (c == 0) break;
    switch(state) {
    case 0:
      if (c == 'n') {
	yytext[yylen-1] = '\n';
	return;
      }
      if (c == 'r') {
	yytext[yylen-1] = '\r';
	return;
      }
      if (c == 't') {
	yytext[yylen-1] = '\t';
	return;
      }
      if (c == 'a') {
	yytext[yylen-1] = '\a';
	return;
      }
      if (c == 'b') {
	yytext[yylen-1] = '\b';
	return;
      }
      if (c == 'f') {
	yytext[yylen-1] = '\f';
	return;
      }
      if (c == '\\') {
	yytext[yylen-1] = '\\';
	return;
      }
      if (c == 'v') {
	yytext[yylen-1] = '\v';
	return;
      }
      if (c == 'e') {
	yytext[yylen-1] = '\033';
	return;
      }
      if (c == '\'') {
	yytext[yylen-1] = '\'';
	return;
      }
      if (c == '\"') {
	yytext[yylen-1] = '\"';
	return;
      }
      if (c == '\n') {
	yylen--;
	return;
      }
      if (c == '0') {
	state = 10;
      }
      else if (c == 'x') {
	state = 20;
      } else {
	yytext[yylen-1] = '\\';
	yytext[yylen] = c;
	yylen++;
	return;
      }
      break;
    case 10:
      if (!isdigit(c)) {
	retract(1);
	yytext[yylen-1] = (char) result;
	return;
      }
      result = (result << 3) + (c - '0');
      yylen--;
      break;
    case 20:
      if (!isxdigit(c)) {
	retract(1);
	yytext[yylen-1] = (char) result;
	return;
      }
      if (isdigit(c))
	result = (result << 4) + (c - '0');
      else
	result = (result << 4) + (10 + tolower(c) - 'a');
      yylen--;
      break;
    }
  }
  return;
}

/**************************************************************
 * int yylook()
 *
 * Lexical scanner.
 * See Aho,Sethi, and Ullman,  pg. 106
 **************************************************************/

int yylook(void) {

    int      state;
    char     c = 0;

    state = 0;
    yylen = 0;
    while(1) {

/*	printf("State = %d\n", state);   */
	switch(state) {

	case 0 :
	  if((c = nextchar()) == 0) return (0);

	  /* Process delimeters */

	  if (c == '\n') {
	    state = 0;
	    yylen = 0;
	    last_id = 0;
	  } else if (isspace(c) || (c=='\\')) {
	    state = 0;
	    yylen = 0;
	    last_id = 0;
	  }

	  else if ((isalpha(c)) || (c == '_')) state = 7;
	  else if (c == '$') state = 75;

	  /* Look for single character symbols */

	  else if (c == '(') return (LPAREN);
	  else if (c == ')') return (RPAREN);
	  else if (c == ';') return (SEMI);
	  else if (c == ',') return (COMMA);
	  else if (c == '*') return (STAR);
	  else if (c == '}') {
	    num_brace--;
	    if (num_brace < 0) {
	      Swig_error(cparse_file, cparse_line, "Syntax error. Extraneous '}'\n");
	      state = 0;
	      num_brace = 0;
	    } else {
	      return (RBRACE);
	    }
	  }
	  else if (c == '{') {
	    last_brace = num_brace;
	    num_brace++;
	    return (LBRACE);
	  }
	  else if (c == '=') return (EQUAL);
	  else if (c == '+') return (PLUS);
          else if (c == '-') return (MINUS);
	  else if (c == '&') {
	    state = 300;
	  } 
	  else if (c == '|') {
	    state = 301;
	  }
	  else if (c == '^') return (XOR);
          else if (c == '<') state = 60;
	  else if (c == '>') state = 61;
	  else if (c == '~') {
	    return (NOT);
	  }
          else if (c == '!') return (LNOT);
	  else if (c == '\\') {
	    state = 99;
	  }
  	  else if (c == '[') return (LBRACKET);
	  else if (c == ']') return (RBRACKET);

	  /* Look for multi-character sequences */

	  else if (c == '/') state = 1;    /* Comment (maybe) */
	  else if (c == '\"') state = 2;   /* Possibly a string */
	  else if (c == '#') state = 3;    /* CPP */
	  else if (c == '%') state = 4;    /* Directive */
	  else if (c == '@') state = 4;    /* Objective C keyword */
	  else if (c == ':') state = 5;    /* maybe double colon */
	  else if (c == '0') state = 83;   /* An octal or hex value */
	  else if (c == '\'') state = 9;   /* A character constant */
	  else if (c == '.') state = 100;  /* Maybe a number, maybe just a period */
	  else if (c == '`') {
	    state = 200; /* Back-tick type */
	    yylen = 0;
	  }
	  else if (isdigit(c)) state = 8;  /* A numerical value */

	  else state = 99;
	  break;
	case 1:  /*  Comment block */
	  if ((c = nextchar()) == 0) return(0);
	  if (c == '/') {
	    comment_start = cparse_line;
	    Clear(comment);
	    state = 10;        /* C++ style comment */
	  } else if (c == '*') {
	    comment_start = cparse_line;
	    Clear(comment);
	    state = 12;   /* C style comment */
	  } else {
	    retract(1);
	    return(SLASH);
	  }
	  break;
	case 300: /* & or && */
	  if ((c = nextchar()) == 0) return(AND);
	  if (c == '&') return(LAND);
	  else {
	    retract(1);
	    return(AND);
	  }

	case 301: /* | or || */
	  if ((c = nextchar()) == 0) return(OR);
	  if (c == '|') return(LOR);
	  else {
	    retract(1);
	    return(OR);
	  }
	case 10:  /* C++ style comment */
	  if ((c = nextchar()) == 0) {
	    Swig_error(cparse_file,-1, "Unterminated comment detected.\n");
	    return 0;
	  }
	  if (c == '\n') {
	    Putc(c,comment);
	    /* Add the comment to documentation */
	    /*	    yycomment(Char(comment),comment_start, column_start);*/
	    yylen = 0;
	    state = 0;
	  } else {
	    state = 10;
	    Putc(c,comment);
	    yylen = 0;
	  }
	  break;

	case 12: /* C style comment block */
	  if ((c = nextchar()) == 0) {
	    Swig_error(cparse_file,-1,"Unterminated comment detected.\n");
	    return 0;
	  }
	  if (c == '*') {
	    state = 13;
	  } else {
	    Putc(c,comment);
	    yylen = 0;
	    state = 12;
	  }
	  break;
	case 13: /* Still in C style comment */
	  if ((c = nextchar()) == 0) {
	    Swig_error(cparse_file,-1,"Unterminated comment detected.\n");
	    return 0;
	  }
	  if (c == '*') {
	    Putc(c,comment);
	    state = 13;
	  } else if (c == '/') {

	    /* Look for locator markers */
	    {
	      char *loc = Char(comment);
	      if (Len(comment)) {
		if ((*loc == '@') && (*(loc+Len(comment)-1) == '@')) {
		  /* Locator */
		  scanner_locator(comment);
		}
	      }
	    }
	    /*	    yycomment(Char(comment),comment_start,column_start); */
	    yylen = 0;
	    state = 0;
	  } else {
	    Putc('*',comment);
	    Putc(c,comment);
	    yylen = 0;
	    state = 12;
	  }
	  break;

	case 2: /* Processing a string */
	  if ((c = nextchar()) == 0) {
	    Swig_error(cparse_file,-1, "Unterminated string detected.\n");
	    return 0;
	  }
	  if (c == '\"') {
	    yytext[yylen-1] = 0;
	    yylval.id = Swig_copy_string(yytext+1);
	    return(STRING);
	  } else if (c == '\\') {
	    yylen--;
	    get_escape();
	    break;
	  } else state = 2;
	  break;

	case 3: /* a CPP directive */
	  if (( c= nextchar()) == 0) return 0;
	  if (c == '\n') {
	    retract(1);
	    yytext[yylen] = 0;
	    yylval.id = yytext;
	    return(POUND);
	  }
	  break;

	case 4: /* A wrapper generator directive (maybe) */
	  if (( c= nextchar()) == 0) return 0;
	  if (c == '{') {
	    state = 40;   /* Include block */
	    Clear(header);
	    cparse_start_line = cparse_line;
	  } else if ((isalpha(c)) || (c == '_')) state = 7;
	  else if (c == '}') {
	    Swig_error(cparse_file,cparse_line, "Misplaced %%}.\n");
	    return 0;
	  } else {
	    retract(1);
	    return(MODULO);
	  }
	  break;

	case 40: /* Process an include block */
	  if ((c = nextchar()) == 0) {
	    Swig_error(cparse_file,-1, "Unterminated include block detected.\n");
	    return 0;
	  }
	  yylen = 0;
	  if (c == '%') state = 41;
	  else {
	    Putc(c,header);
	    yylen = 0;
	    state = 40;
	  }
	  break;
	case 41: /* Still processing include block */
	  if ((c = nextchar()) == 0) {
	    Swig_error(cparse_file,-1, "Unterminated include block detected.\n");
	    return 0;
	  }
	  if (c == '}') {
	    yylval.str = NewString(header);
	    return(HBLOCK);
	  } else {
	    Putc('%',header);
	    Putc(c,header);
	    yylen = 0;
	    state = 40;
	  }
	  break;

	case 5: /* Maybe a double colon */

	  if (( c= nextchar()) == 0) return 0;
	  if ( c == ':') {
	    state = 51;
	  } else {
	    retract(1);
	    return COLON;
	  }
	  break;
	case 51: /* Maybe a ::*, ::~, or :: */
	  if (( c = nextchar()) == 0) return 0;
	  if (c == '*') {
	    return DSTAR;
	  } if (c == '~') {
	    return DCNOT;
	  } else {
	    retract(1);
	    if (!last_id) {
	      retract(2);
	      return NONID;
	    } else {
	      return DCOLON;
	    }
	  }

	case 60: /* shift operators */
	  if ((c = nextchar()) == 0) return (0);
	  if (c == '<') return LSHIFT;
	  else {
	    retract(1);
	    return LESSTHAN;
	  }
	  break;
	case 61:
	  if ((c = nextchar()) == 0) return (0);
	  if (c == '>') return RSHIFT;
	  else {
	    retract(1);
            return GREATERTHAN;
	  }
	  break;
	case 7: /* Identifier */
	  if ((c = nextchar()) == 0) return(0);
	  if (isalnum(c) || (c == '_') || (c == '.') || (c == '$')) {
	    state = 7;
	  } else {
	    retract(1);
	    return(ID);
	  }
	  break;
	case 75: /* Special identifier $*/
	  if ((c = nextchar()) == 0) return(0);
	  if (isalnum(c) || (c == '_') || (c == '*') || (c == '&')) {
	    state = 7;
	  } else {
	    retract(1);
	    return(ID);
	  }
	  break;

	case 8: /* A numerical digit */
	  if ((c = nextchar()) == 0) return(0);
	  if (c == '.') {state = 81;}
	  else if ((c == 'e') || (c == 'E')) {state = 86;}
	  else if ((c == 'f') || (c == 'F')) {
	     return(NUM_FLOAT);
	  }
	  else if (isdigit(c)) { state = 8;}
	  else if ((c == 'l') || (c == 'L')) {
	    state = 87;
	  } else if ((c == 'u') || (c == 'U')) {
	    state = 88;
	  } else {
	      retract(1);
	      return(NUM_INT);
	    }
	  break;
	case 81: /* A floating pointer number of some sort */
	  if ((c = nextchar()) == 0) return(0);
	  if (isdigit(c)) state = 81;
	  else if ((c == 'e') || (c == 'E')) state = 82;
          else if ((c == 'f') || (c == 'F') || (c == 'l') || (c == 'L')) {
	    return(NUM_FLOAT);
	  } else {
	    retract(1);
	    return(NUM_FLOAT);
	  }
	  break;
	case 82:
	  if ((c = nextchar()) == 0) return(0);
	  if ((isdigit(c)) || (c == '-') || (c == '+')) state = 86;
	  else {
	    retract(2);
	    yytext[yylen-1] = 0;
	    return(NUM_INT);
	  }
	  break;
	case 83:
	  /* Might be a hexidecimal or octal number */
	  if ((c = nextchar()) == 0) return(0);
	  if (isdigit(c)) state = 84;
	  else if ((c == 'x') || (c == 'X')) state = 85;
	  else if (c == '.') state = 81;
	  else if ((c == 'l') || (c == 'L')) {
	    state = 87;
	  } else if ((c == 'u') || (c == 'U')) {
	    state = 88;
	  } else {
	    retract(1);
	    return(NUM_INT);
	  }
	  break;
	case 84:
	  /* This is an octal number */
	  if ((c = nextchar()) == 0) return (0);
	  if (isdigit(c)) state = 84;
	  else if ((c == 'l') || (c == 'L')) {
	    state = 87;
	  } else if ((c == 'u') || (c == 'U')) {
	    state = 88;
	  } else {
	    retract(1);
	    return(NUM_INT);
	  }
	  break;
	case 85:
	  /* This is an hex number */
	  if ((c = nextchar()) == 0) return (0);
	  if ((isdigit(c)) || (c=='a') || (c=='b') || (c=='c') ||
	      (c=='d') || (c=='e') || (c=='f') || (c=='A') ||
	      (c=='B') || (c=='C') || (c=='D') || (c=='E') ||
	      (c=='F'))
	    state = 85;
	  else if ((c == 'l') || (c == 'L')) {
	    state = 87;
	  } else if ((c == 'u') || (c == 'U')) {
	    state = 88;
	  } else {
	    retract(1);
	    return(NUM_INT);
	  }
	  break;

	case 86:
	  /* Rest of floating point number */

	  if ((c = nextchar()) == 0) return (0);
	  if (isdigit(c)) state = 86;
          else if ((c == 'f') || (c == 'F') || (c == 'l') || (c == 'L')) {
	    return(NUM_FLOAT);
	  } else {
	    retract(1);
	    return(NUM_FLOAT);
	  }
	  /* Parse a character constant. ie. 'a' */
	  break;

	case 87 :
	  /* A long integer of some sort */
	  if ((c = nextchar()) == 0) return (NUM_LONG);
	  if ((c == 'u') || (c == 'U')) {
	    return(NUM_ULONG);
	  } else if ((c == 'l') || (c == 'L')) {
	    state = 870;
	  } else {
	    retract(1);
	    return(NUM_LONG);
	  }
	  break;

	case 870:
	  if ((c = nextchar()) == 0) return (NUM_LONGLONG);
	  if ((c == 'u') || (c == 'U')) {
	    return (NUM_ULONGLONG);
	  } else {
	    retract(1);
	    return(NUM_LONGLONG);
	  }

	case 88:
	  /* An unsigned integer of some sort */
	  if ((c = nextchar()) == 0) return (NUM_UNSIGNED);
	  if ((c == 'l') || (c == 'L')) {
	    state = 880;
	  } else {
	    retract(1);
	    return(NUM_UNSIGNED);
	  }
	  break;

	case 880:
	  if ((c = nextchar()) == 0) return (NUM_ULONG);
	  if ((c == 'l') || (c == 'L')) return (NUM_ULONGLONG);
	  else {
	    retract(1);
	    return(NUM_ULONG);
	  }
	  
	case 9:
	  if ((c = nextchar()) == 0) return (0);
	  if (c == '\\') {
	    yylen--;
	    get_escape();
	  } else if (c == '\'') {
	    yytext[yylen-1] = 0;
	    yylval.str = NewString(yytext+1);
	    if (yylen == 2) {
	      Swig_error(cparse_file, cparse_line, "Empty character constant\n");
	    }
	    return(CHARCONST);
	  }
	  break;

	case 100:
	  if ((c = nextchar()) == 0) return (0);
	  if (isdigit(c)) state = 81;
	  else {
	    retract(1);
	    return(PERIOD);
	  }
	  break;
	case 200:
	  if ((c = nextchar()) == 0) return (0);
	  if (c == '`') {
	    yytext[yylen-1] = 0;
	    yylval.type = NewString(yytext);
	    return(TYPE_RAW);
	  }
	  break;

	default:
	  Swig_error(cparse_file, cparse_line, "Illegal character '%c'=%d.\n",c,c);
	  state = 0;
	  return(ILLEGAL);
	}
    }
}

static int check_typedef = 0;

void scanner_check_typedef() {
  check_typedef = 1;
}

void scanner_ignore_typedef() {
  check_typedef = 0;
}

void scanner_last_id(int x) {
  last_id = x;
}

void scanner_clear_rename() {
  rename_active = 0;
}

static int next_token = 0;
void scanner_next_token(int tok) {
  next_token = tok;
}

/**************************************************************
 * int yylex()
 *
 * Gets the lexene and returns tokens.
 *************************************************************/

int yylex(void) {

    int   l;

    if (!scan_init) {
      scanner_init();
    }

    if (next_token) {
      l = next_token;
      next_token = 0;
      return l;
    }
    l = yylook();


    if (l == NONID) {
      last_id = 1;
    } else {
      last_id = 0;
    }

    /* We got some sort of non-white space object.  We set the start_line
       variable unless it has already been set */

    if (!cparse_start_line) {
      cparse_start_line = cparse_line;
    }

    /* Copy the lexene */

    yytext[yylen] = 0;
    switch(l) {

    case NUM_INT:
    case NUM_FLOAT:
    case NUM_ULONG:
    case NUM_LONG:
    case NUM_UNSIGNED:
    case NUM_LONGLONG:
    case NUM_ULONGLONG:
      if (l == NUM_INT) yylval.dtype.type = T_INT;
      if (l == NUM_FLOAT) yylval.dtype.type = T_DOUBLE;
      if (l == NUM_ULONG) yylval.dtype.type = T_ULONG;
      if (l == NUM_LONG) yylval.dtype.type = T_LONG;
      if (l == NUM_UNSIGNED) yylval.dtype.type = T_UINT;
      if (l == NUM_LONGLONG) yylval.dtype.type = T_LONGLONG;
      if (l == NUM_ULONGLONG) yylval.dtype.type = T_ULONGLONG;
      yylval.dtype.val = NewString(yytext);
      yylval.dtype.bitfield = 0;
      yylval.dtype.throws = 0;
      return(l);
      break;
      
    case ID:

	if (yytext[0] != '%') {
	  /* Look for keywords now */
	  
	  if (strcmp(yytext,"int") == 0) {
	    yylval.type = NewSwigType(T_INT);
	    return(TYPE_INT);
	  }
	  if (strcmp(yytext,"double") == 0) {
	    yylval.type = NewSwigType(T_DOUBLE);
	    return(TYPE_DOUBLE);
	  }
	  if (strcmp(yytext,"void") == 0) {
	    yylval.type = NewSwigType(T_VOID);
	    return(TYPE_VOID);
	  }
	  if (strcmp(yytext,"char") == 0) {
	    yylval.type = NewSwigType(T_CHAR);
	    return(TYPE_CHAR);
	  }
	  if (strcmp(yytext,"short") == 0) {
	    yylval.type = NewSwigType(T_SHORT);
	    return(TYPE_SHORT);
	  }
	  if (strcmp(yytext,"long") == 0) {
	    yylval.type = NewSwigType(T_LONG);
	    return(TYPE_LONG);
	  }
	  if (strcmp(yytext,"float") == 0) {
	    yylval.type = NewSwigType(T_FLOAT);
	    return(TYPE_FLOAT);
	  }
	  if (strcmp(yytext,"signed") == 0) {
	    yylval.type = NewSwigType(T_INT);
	    return(TYPE_SIGNED);
	  }
	  if (strcmp(yytext,"unsigned") == 0) {
	    yylval.type = NewSwigType(T_UINT);
	    return(TYPE_UNSIGNED);
	  }
	  if (strcmp(yytext,"bool") == 0) {
	    yylval.type = NewSwigType(T_BOOL);
	    return(TYPE_BOOL);
	  }
	  /* C++ keywords */
	  
	  if (cparse_cplusplus) {
	    if (strcmp(yytext,"class") == 0) return(CLASS);
	    if (strcmp(yytext,"private") == 0) return(PRIVATE);
	    if (strcmp(yytext,"public") == 0) return(PUBLIC);
	    if (strcmp(yytext,"protected") == 0) return(PROTECTED);
	    if (strcmp(yytext,"friend") == 0) return(FRIEND);
	    if (strcmp(yytext,"virtual") == 0) return(VIRTUAL);
	    if (strcmp(yytext,"operator") == 0) {
	      String *s = NewString("operator");
	      int c;
	      int state = 0;
	      int sticky = 0;
	      int isconversion = 0;
	      int count = 0;
	      while ((c = nextchar())) {
		if (((c == '(') || (c == ';')) && state) {
		  retract(1);
		  break;
		}
		count++;
		if (!isspace(c)) {
		  if ((!state) && (isalpha(c))) isconversion = 1;
		  if (!state && !sticky) Putc(' ',s);
		  Putc(c,s);
		  sticky = 0;
		  state = 1;
		} else {
		  if (!sticky) Putc(' ',s);
		  sticky = 1;
		}
	      }
	      Chop(s);
	      yylval.str = s;
	      while(Replaceall(s,"[ ", "["));
	      if (isconversion) {
		String *ns = Swig_symbol_string_qualify(s,0);
		yylval.str = ns;
	      }
	      if (isconversion && !rename_active) {
		char *t = Char(s) + 9;
		if (!((strcmp(t,"new") == 0) || (strcmp(t,"delete") == 0) 
		      || (strcmp(t,"new[]") == 0) || (strcmp(t,"delete[]") == 0))) {
		  /*		  retract(strlen(t));*/
		  retract(count);
		  return COPERATOR;
		}
	      }
	      return(OPERATOR);
	    }
	    if (strcmp(yytext,"throw") == 0) return(THROW);
	    if (strcmp(yytext,"try") == 0) return (yylex());
	    if (strcmp(yytext,"catch") == 0) return (CATCH);
	    if (strcmp(yytext,"inline") == 0) return(yylex());
	    if (strcmp(yytext,"mutable") == 0) return(yylex());
	    if (strcmp(yytext,"explicit") == 0) return(yylex());
	    if (strcmp(yytext,"export") == 0) return(yylex());
	    if (strcmp(yytext,"typename") == 0) return (TYPENAME);
	    if (strcmp(yytext,"template") == 0) {
	      yylval.ivalue = cparse_line;
	      return(TEMPLATE);
	    }
	    if (strcmp(yytext,"delete") == 0) {
	      return(DELETE);
	    }
	    if (strcmp(yytext,"using") == 0) {
	      return(USING);
	    }
	    if (strcmp(yytext,"namespace") == 0) {
	      return(NAMESPACE);
	    }
	  } else {
	    if (strcmp(yytext,"class") == 0) {
	      Swig_warning(WARN_PARSE_CLASS_KEYWORD,cparse_file,cparse_line, "class keyword used, but not in C++ mode.\n");
	    }
	  }
	  
	  /* Objective-C keywords */
#ifdef OBJECTIVEC
	  if ((ObjC) && (yytext[0] == '@')) {
	    if (strcmp(yytext,"@interface") == 0) return (OC_INTERFACE);
	    if (strcmp(yytext,"@end") == 0) return (OC_END);
	    if (strcmp(yytext,"@public") == 0) return (OC_PUBLIC);
	    if (strcmp(yytext,"@private") == 0) return (OC_PRIVATE);
	    if (strcmp(yytext,"@protected") == 0) return (OC_PROTECTED);
	    if (strcmp(yytext,"@class") == 0) return(OC_CLASS);
	    if (strcmp(yytext,"@implementation") == 0) return(OC_IMPLEMENT);
	    if (strcmp(yytext,"@protocol") == 0) return(OC_PROTOCOL);
	  }
#endif
	  
	  /* Misc keywords */
	  
	  if (strcmp(yytext,"extern") == 0) return(EXTERN);
	  if (strcmp(yytext,"const") == 0) return(CONST);
	  if (strcmp(yytext,"static") == 0) return(STATIC);
	  if (strcmp(yytext,"struct") == 0) return(STRUCT);
	  if (strcmp(yytext,"union") == 0) return(UNION);
	  if (strcmp(yytext,"enum") == 0) return(ENUM);
	  if (strcmp(yytext,"sizeof") == 0) return(SIZEOF);
	  
	  if (strcmp(yytext,"typedef") == 0) {
	    yylval.ivalue = 0;
	    return(TYPEDEF);
	  }
	  
	  /* Ignored keywords */
	  
	  if (strcmp(yytext,"volatile") == 0) return(VOLATILE);
	  
	  /* SWIG directives */
	} else {
	  if (strcmp(yytext,"%module") == 0) return(MODULE);
	  if (strcmp(yytext,"%insert") == 0) return(INSERT);
	  if (strcmp(yytext,"%name") == 0) return(NAME);
	  if (strcmp(yytext,"%rename") == 0) {
	    rename_active = 1;
	    return(RENAME);
	  }
	  if (strcmp(yytext,"%namewarn") == 0) {
	    rename_active = 1;
	    return (NAMEWARN);
	  }
	  if (strcmp(yytext,"%includefile") == 0) return(INCLUDE);
	  if (strcmp(yytext,"%val") == 0) {
	    Swig_warning(WARN_DEPRECATED_VAL, cparse_file, cparse_line, "%%val directive deprecated (ignored).\n");
	    return (yylex());
	  }
	  if (strcmp(yytext,"%out") == 0) {
	    Swig_warning(WARN_DEPRECATED_OUT, cparse_file, cparse_line, "%%out directive deprecated (ignored).\n");
	    return(yylex());
	  }
	  if (strcmp(yytext,"%constant") == 0) return(CONSTANT);
	  if (strcmp(yytext,"%typedef") == 0) {
	    yylval.ivalue = 1;
	    return(TYPEDEF);
	  }
	  if (strcmp(yytext,"%native") == 0) return(NATIVE);
	  if (strcmp(yytext,"%pragma") == 0) return(PRAGMA);
	  if (strcmp(yytext,"%extend") == 0) return(EXTEND);
	  if (strcmp(yytext,"%fragment") == 0) return(FRAGMENT);
	  if (strcmp(yytext,"%inline") == 0) return(INLINE);
	  if (strcmp(yytext,"%typemap") == 0) return(TYPEMAP);
	  if (strcmp(yytext,"%feature") == 0) return(FEATURE);
	  if (strcmp(yytext,"%except") == 0) return(EXCEPT);
	  if (strcmp(yytext,"%importfile") == 0) return(IMPORT);
	  if (strcmp(yytext,"%echo") == 0) return(ECHO);
	  if (strcmp(yytext,"%apply") == 0) return(APPLY);
	  if (strcmp(yytext,"%clear") == 0) return(CLEAR);
	  if (strcmp(yytext,"%types") == 0) return(TYPES);
	  if (strcmp(yytext,"%parms") == 0) return(PARMS);
	  if (strcmp(yytext,"%varargs") == 0) return(VARARGS);
	  if (strcmp(yytext,"%template") == 0) return (SWIGTEMPLATE);
	  if (strcmp(yytext,"%warn") == 0) return(WARN);
	}
	/* Have an unknown identifier, as a last step, we'll do a typedef lookup on it. */

        /* Need to fix this */
	if (check_typedef) {
	  if (SwigType_istypedef(yytext)) {
	    yylval.type = NewString(yytext);
	    return(TYPE_TYPEDEF);
	  }
	}
	yylval.id = Swig_copy_string(yytext);
	last_id = 1;
	return(ID);
    case POUND:
      return yylex();
    default:
      return(l);
    }
}