File: optlib2c

package info (click to toggle)
universal-ctags 0%2Bgit20181215-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,444 kB
  • sloc: ansic: 84,242; vhdl: 5,924; sh: 5,830; perl: 1,743; cpp: 1,599; cs: 1,193; python: 812; sql: 572; f90: 534; php: 479; yacc: 459; fortran: 341; makefile: 325; asm: 311; objc: 284; ruby: 261; xml: 245; java: 157; tcl: 133; cobol: 122; lisp: 113; erlang: 61; ada: 55; ml: 49; awk: 43
file content (870 lines) | stat: -rwxr-xr-x 21,464 bytes parent folder | download
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
#!/usr/bin/env perl
#
# optlib2c - a tool translating ctags option file to C
#
# Copyright (C) 2016 Masatake YAMATO
# Copyright (C) 2016 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use warnings;
#use Data::Dumper;
#print Dumper(X);

sub show_help {
    print<<EOF;
Usage:
	$0 --help
	$0 FILE.ctags > FILE.c
EOF
}


########################################################################
#
# PARSE
#
########################################################################
my $options =
    [
     [ qr/^--options=(.*)/, sub {
	   parse_optlib ($1, $_[0]);
       } ],
     #
     # TODO: We should not assumed the order of flags.
     #
     [ qr/^--langdef=([^\{]+)(((\{base=([^\{]+)\})(\{(dedicated|shared|bidirectional)\})?)?)(\{_autoFQTag\})?/, sub {
	   die "LANG is already defined as $_[0]->{'langdef'}: $1"
	       if (defined $_[0]->{'langdef'});
	   $_[0]->{'langdef'} = $1;
	   $_[0]->{'base'} = $5 if defined $5;
	   $_[0]->{'direction'} = $7 if defined $7;
	   $_[0]->{'autoFQTag'} = (defined $8)? 1: 0;
       } ],
     [ qr/^--kinddef-(.*)=([^,]),([^,]+),([^\{]+)(\{_refonly\})?/, sub {
	 die "Don't use --kinddef-<LANG>=+ option before defining the language"
	     if (! defined $_[0]->{'langdef'});
	 die "Adding a kind is allowed only to the language specified with --langdef: $1"
	   unless ($_[0]->{'langdef'} eq $1);
	 my $refonly = 0;
	 $refonly = 1 if defined $5;
	 push @{$_[0]->{'kinddefs'}}, { letter => $2, name => $3, desc => $4,
					refonly => $refonly, roles => [] };
       } ],
     [ qr/^--_extradef-(.*)=([^,]+),(.+)/, sub {
	 die "Don't use --_extradef-<LANG>=+ option before defining the language"
	     if (! defined $_[0]->{'langdef'});
	 die "Adding an extra is allowed only to the language specified with --langdef: $1"
	     unless ($_[0]->{'langdef'} eq $1);
	 push @{$_[0]->{'extradefs'}}, { name => $2, desc => $3 };
       } ],
     [ qr/^--_fielddef-(.*)=([^,]+),(.+)/, sub {
	 die "Don't use --_fielddef-<LANG>=+ option before defining the language"
	     if (! defined $_[0]->{'langdef'});
	 die "Adding a field is allowed only to the language specified with --langdef: $1"
	     unless ($_[0]->{'langdef'} eq $1);
	 push @{$_[0]->{'fielddefs'}}, { name => $2, desc => $3 };
       } ],
     [ qr/^--_roledef-(.*)=([a-zA-Z])\.([a-zA-Z][a-zA-Z0-9]*),(.+)/, sub {
	   die "Don't use --_roledef-<LANG>=+ option before defining the language"
	     if (! defined $_[0]->{'langdef'});
	   die "Adding a field is allowed only to the language specified with --langdef: $1"
	     unless ($_[0]->{'langdef'} eq $1);

	   my $kind_found = 0;
	   for (@{$_[0]->{'kinddefs'}}) {
	       if ($_->{'letter'} eq $2) {
		   my $role = { name => $3, desc => $4, owner => $_ };
		   push @{$_->{'roles'}}, $role;
		   $kind_found = 1;
		   last;
	       }
	   }
	   die "no such kind, \"$2\" where role \"$3\" is attached to" if (! $kind_found);
       } ],
     [ qr/^--languages=-(.*)/, sub {
	   die "Don't use --languages=- option before defining the language"
	       if (! defined $_[0]->{'langdef'});
	   die "Only language specified with --langdef can be disabled: $1"
	       unless ($_[0]->{'langdef'} eq $1);
	   $_[0]->{'disabled'} = 1
       } ],
     [ qr/^--language=(.*)/, sub {
	   die "--languages can be used only for disabling a language defined with --langdef: $1";
       } ],
     [ qr/^--map-([^=]*)=\+(.*)/, sub {
	   die "Don't use --map-<LANG>=+ option before defining the language"
	       if (! defined $_[0]->{'langdef'});
	   die "Adding a map is allowed only to the language specified with --langdef: $1"
	       unless ($_[0]->{'langdef'} eq $1);
	   my $spec = $2;
	   if ($spec =~ /\((.*)\)/) {
	       push @{$_[0]->{'patterns'}}, $1;
	   } elsif ($spec =~ /\.(.*)/) {
	       push @{$_[0]->{'extensions'}}, $1;
	   } else {
	       die "Unexpected notation is used in the argument for --map-$1= option";
	   }
       } ],
     [ qr/^--map-([^=]*)=[^+].*/, sub {
	   die "map manipulation other than the appending(--map-<LANG>=+...) is not supported";
       } ],
     [ qr /^--alias-([^=]*)=\+(.*)/, sub {
	   die "Don't use --alias-<LANG>=+ option before defining the language"
	       if (! defined $_[0]->{'langdef'});
	   die "Adding an alias is allowed only to the language specified with --langdef: $1"
	       unless ($_[0]->{'langdef'} eq $1);
	   push @{$_[0]->{'aliases'}}, $2;
       } ],
     [ qr/^--alias-([^=]*)=[^+].*/, sub {
	   die "alias manipulation other than the appending(--alias-<LANG>=+...) is not supported";
       } ],
     [ qr/^--regex-([^=]*)=(.*)/, sub {
	   die "Don't use --regex-<LANG>= option before defining the language"
	       if (! defined $_[0]->{'langdef'});
	   die "Adding a regex is allowed only to the language specified with --langdef: $1"
	       unless ($_[0]->{'langdef'} eq $1);
	   parse_regex ($2, $_[0], 0);
       } ],
     [ qr/^--mline-regex-([^=]*)=(.*)/, sub {
	   die "Don't use --mline-regex-<LANG>= option before defining the language"
	       if (! defined $_[0]->{'langdef'});
	   die "Adding a multiline regex is allowed only to the language specified with --langdef: $1"
	       unless ($_[0]->{'langdef'} eq $1);
	   parse_regex ($2, $_[0], 1);
       } ],

     [ qr/^--kinds-([^=]*)=-(.*)/, sub {
	   die "Don't use --kinds-<LANG>= option before defining the language"
	       if (! defined $_[0]->{'langdef'});
	   parse_kinds ($2, $_[0]);
       } ],
     [ qr/^--kinds-([^=]*)=(.*)/, sub {
	   die "--kinds-<LANG>= can be used only for disabling a kind: $1";
       } ],
     [ qr/^--extras-([^=]*)=([-+])\{(.+)\}$/, sub {
	   die "Don't use --extras-<LANG>= option before defining the language"
	       if (! defined $_[0]->{'langdef'});
	   die "Enabling/disabling an extra is allowed only to the language specified with --langdef: $1"
	       unless ($_[0]->{'langdef'} eq $1);
	   die "Specifing multiple extras in one --extras-... is not handled: {$3}"
	       if ( index ($3, '{') != -1 );
	   parse_extras ($2, $3, $_[0]);
       } ],
     [ qr/^--extras-([^=]*)=\{/, sub {
	 die "--extras-<LANG>= can be used only for enabling or disabling an extra: $1";
       } ],
     [ qr/^--extras-([^=]*)=(.)\{/, sub {
	 die "Unknown flag($2) is passed to --extras-<LANG>= option";
       } ],
     [ qr/^--fields-([^=]*)=([-+])\{(.+)\}$/, sub {
	   die "Don't use --fields-<LANG>= option before defining the language"
	       if (! defined $_[0]->{'langdef'});
	   die "Enabling/disabling a field is allowed only to the language specified with --langdef: $1"
	       unless ($_[0]->{'langdef'} eq $1);
	   die "Specifing multiple fields in one --fields-... is not handled: {$3}"
	       if ( index ($3, '{') != -1 );
	   parse_fields ($2, $3, $_[0]);
       } ],
     [ qr/^--fields-([^=]*)=\{/, sub {
	 die "--fields-<LANG>= can be used only for enabling or disabling a field: $1";
       } ],
     [ qr/^--fields-([^=]*)=(.)\{/, sub {
	 die "Unknown flag($2) is passed to --fields-<LANG>= option";
       } ],
     [ qr/^--langmap=.*/, sub {
	 die "Use --map-<LANG> option instead of --langmap";
       } ],
     [ qr/^--_tabledef-([^=]*)=(.*)/, sub {
	 die "Don't use --_tabledef-<LANG>= option before defining the language"
	   if (! defined $_[0]->{'langdef'});
	 die "Adding a table is allowed only to the language specified with --langdef: $1"
	   unless ($_[0]->{'langdef'} eq $1);
	 $_[0]->{'tabledefs'}->{$2} = [];
	 push @{$_[0]->{'tablenames'}}, "$2";
       } ],
     [ qr/^--_mtable-regex-([^=]*)=([^\/]+)(\/.*)/, sub {
	 die "Don't use --_mtable-regex-<LANG>= option before defining the language"
	   if (! defined $_[0]->{'langdef'});
	 die "Adding a multitable regex is allowed only to the language specified with --langdef: $1"
	   unless ($_[0]->{'langdef'} eq $1);
	 parse_regex ($3, $_[0], 1, $2);
       } ],
     [ qr/^--_mtable-extend-([^=]*)=(.*)\+(.*)/, sub {
	 die "Don't use --_mline-extend-<LANG>= option before defining the language"
	   if (! defined $_[0]->{'langdef'});
	 die "Extending a multitable regex is allowed only to the language specified with --langdef: $1"
	   unless ($_[0]->{'langdef'} eq $1);
	 extend_table($_[0], $2, $3);
       } ],
     [ qr/^-.*/, sub {
	 die "Unhandled option: \"$&\"";
       } ],
     [ qr/.*/, sub {
	 die "Unhandled argument: \"$&\"";
       } ],
    ];

sub parse_line {
    my ($line, $opts) = @_;
    my $r = 0;

    for (@{$options}) {
	my ($pat, $action) = @{$_};
	if ($line =~ $pat) {
	    $action -> ($opts);
	    $r = 1;
	    last;
	}
    }
    $r;
}

sub gather_chars {
    my $input = shift;
    my $output = "";

    my $escape = 0;
    my $c;

    # See scanSeparators() of lregex.c.
    while (defined ($c = shift @{$input})) {
	if ($escape) {
	    if ($c eq '/') {
		$output = $output . $c;
	    } elsif ($c eq 't') {
		$output = $output . '\\' . 't';
	    } elsif ($c eq 'n') {
		$output = $output . '\\' . 'n';
	    } elsif ($c eq '\\') {
		$output = $output . '\\\\' . '\\\\';
	    } else {
		$output = $output . '\\\\' . $c;
	    }
	    $escape = 0;
	} elsif ($c eq '"') {
	    $output = $output . '\\' . $c;
	} elsif ($c eq '\\') {
	    $escape = 1;
	} elsif ($c eq '/') {
	    last;
	} else {
	    $output = $output . $c;
	}
    }

    return ($output, (defined $c)? $c: "");
}

sub parse_regex {
    my ($regspec, $opts, $mline, $table) = @_;

    my @chars = split //, $regspec;

    # TODO:
    #	ctags allows using a char other than '/'
    #
    if (! ($chars[0] eq '/')) {
	if (!$mline) {
	    die "--regex-<LANG>= option is not started from /: $regspec";
	} else {
	    die "--mline-regex-<LANG>= option is not started from /: $regspec";
	}
    }

    shift @chars;
    my $last_char;

    my $regex;
    ($regex, $last_char) = gather_chars (\@chars);
    if (! ($last_char eq '/')) {
	if (!$mline) {
	    die "The pattern in --regex-<LANG>= option is not ended with /: $regspec";
	} else {
	    die "The pattern in --mline-regex-<LANG>= option is not ended with /: $regspec";
	}
    }

    my $name;
    ($name, $last_char) = gather_chars (\@chars);
    if (! ($last_char eq '/')) {
	die "Wrong kind/flags syntax: $regspec";
    }

    my $tmp;
    ($tmp, $last_char) = gather_chars (\@chars);
    my $kind = "";
    my $flags;

    if ( (! @chars) && (! ($last_char eq '/'))) {
	$flags = $tmp;

    } else {
	$kind = $tmp;
    }

    if ($last_char eq '/') {
	($flags, $last_char) = gather_chars (\@chars);
    }

    if (defined $table) {

      if (! (substr ($regex, 0, 1) eq '^')) {
	$regex = '^' . $regex;
      }
      push @{$opts->{'tabledefs'}->{"$table"}}, { 'regex' => $regex,
						  'name'  => $name,
						  'kind'  => $kind,
						  'flags' => $flags,
						  'mline' => $mline,
						};
    } else {
      push @{$opts->{'regexs'}}, { 'regex' => $regex,
				   'name'  => $name,
				   'kind'  => $kind,
				   'flags' => $flags,
				   'mline' => $mline,
				 };
    }

    if ($flags =~ '{scope=') {
	$opts->{'useCork'} = 1;
    }
}

sub extend_table {
  my ($opts, $dist, $src) = @_;

  for (@{$opts->{'tabledefs'}->{$src}}) {
    push @{$opts->{'tabledefs'}->{$dist}}, $_;
  }
}

sub parse_kinds {
    my ($kinds, $opts) = @_;
    for (split //, $kinds) {
	push @{$opts->{'disabledKinds'}}, $_;
    }
}

sub parse_optlib {
    my ($optlib, $opts) = @_;

    open(my $optlibfh, "<", $optlib)
	or die "cannot open the optlib file: \"$optlib\"";

    while (<$optlibfh>) {
	chomp;
	if ( /^#.*/ ) {
	    next;
	} else {
	    s/^\s*//;
	    next if ( /^\s*$/ );
	    if (! parse_line ($_, $opts)) {
		return undef;
	    }
	}
    }
    return $opts;
}

sub parse_extras {
    my ($flag, $extra, $opts) = @_;

    $flag = ( $flag eq '+' )?1: 0;

    # TODO: Hash table should be used for manage 'extradefs'.
    for (@{$opts->{'extradefs'}}) {
	if ($_->{'name'} eq $extra)
	{
	    $_->{'enabled'} = $flag;
	}
    }
}

sub parse_fields {
    my ($flag, $field, $opts) = @_;

    $flag = ( $flag eq '+' )?1: 0;

    # TODO: Hash table should be used for manage 'fielddefs'.
    for (@{$opts->{'fielddefs'}}) {
	if ($_->{'name'} eq $field)
	{
	    $_->{'enabled'} = $flag;
	}
    }
}

sub emit_header {
    my ($optlib, $opts) = @_;

    print <<EOF;
/*
 * Generated by $0 from ${optlib}, Don't edit this manually.
 */
#include "general.h"
#include "parse.h"
#include "routines.h"
#include "field.h"
#include "xtag.h"
EOF

    if (defined $opts->{'base'}) {
    print <<EOF;
#include "subparser.h"
EOF
    }
    print <<EOF;


EOF
}


########################################################################
#
# EMIT
#
########################################################################

sub emit_initializer {
    my $opts = shift;
    my $may_unused = (@{$opts->{'disabledKinds'}}) ? "": " CTAGS_ATTR_UNUSED";

    print <<EOF;
static void initialize$opts->{'Clangdef'}Parser (const langType language$may_unused)
{
EOF
    for (@{$opts->{'disabledKinds'}}) {
	print <<EOF;
	{
		kindDefinition *kdef = getLanguageKindForLetter (language, '$_');
		enableKind (kdef, false);
	}
EOF
      }

    if ($opts->{'tablenames'}) {
      print "\n";

      for (@{$opts->{'tablenames'}}) {
	print <<EOF;
	addLanguageRegexTable (language, "$_");
EOF
      }

      print "\n";

      for (@{$opts->{'tablenames'}}) {
	my $table = $_;
	for (@{$opts->{'tabledefs'}->{"$table"}}) {
	  print <<EOF;
	addLanguageTagMultiTableRegex (language, "$table",
	                               "$_->{'regex'}",
	                               "$_->{'name'}", "$_->{'kind'}", "$_->{'flags'}", NULL);
EOF
	}
      }
    }
    print <<EOF;
}

EOF
    0;
}

sub emit_list {
    my ($opts, $key) = @_;

    print <<EOF;
	static const char *const $key [] = {
EOF
    for (@{$opts->{$key}}) {
	print <<EOF;
		\"$_\",
EOF
    }

    print <<EOF;
		NULL
	};

EOF
}

sub emit_aliases {
    emit_list $_[0], "aliases";
}

sub emit_extensions {
    emit_list $_[0], "extensions";
}

sub emit_patterns {
    emit_list $_[0], "patterns";
}

sub emit_roledefs {
    my $opts = shift;


    for (@{$opts->{'kinddefs'}}) {
	next unless @{$_->{'roles'}};
	my $Kind = capitalize($_->{'name'});
	print <<EOF;
	static roleDefinition $opts->{'Clangdef'}${Kind}RoleTable [] = {
EOF
	for (@{$_->{'roles'}}) {
	    print <<EOF;
		{ true, "$_->{'name'}", "$_->{'desc'}" },
EOF
	}

	print <<EOF;
	};
EOF
    }
    print <<EOF;
EOF
}

sub emit_kinddefs {
    my $opts = shift;

    return if (! @{$opts->{'kinddefs'}});

    print <<EOF;
	static kindDefinition $opts->{'Clangdef'}KindTable [] = {
EOF
    for (@{$opts->{'kinddefs'}}) {
      print <<EOF;
		{
EOF
      print <<EOF;
		  true, \'$_->{'letter'}\', "$_->{'name'}", "$_->{'desc'}",
EOF
      if ($_->{'refonly'}) {
	  print <<EOF;
		  .referenceOnly = true,
EOF
      }
      my $Kind = capitalize($_->{'name'});
      if (@{$_->{'roles'}}) {
	  print <<EOF;
		  ATTACH_ROLES($opts->{'Clangdef'}${Kind}RoleTable),
EOF
      }
      print <<EOF;
		},
EOF
    }
    print <<EOF;
	};
EOF
}

sub emit_regexs {
    my $opts = shift;

    return if (! @{$opts->{'regexs'}});

    print <<EOF;
	static tagRegexTable $opts->{'Clangdef'}TagRegexTable [] = {
EOF
    for (@{$opts->{'regexs'}}) {
	my $flags = $_-> {'flags'}? '"' . $_-> {'flags'} . '"': "NULL";
	my $mline = $_-> {'mline'}? "true": "false";
	print <<EOF;
		{"$_->{'regex'}", "$_->{'name'}",
		"$_->{'kind'}", $flags, NULL, $mline},
EOF
    }
    print <<EOF;
	};

EOF
}

sub emit_dependencies {
    my $opts = shift;

    return if (! defined $opts->{'base'});

    my $direction = "SUBPARSER_BASE_RUNS_SUB";

    if (defined $opts->{'direction'})
    {
	if ($opts->{'direction'} eq 'shared')
	{
	    $direction = "SUBPARSER_BASE_RUNS_SUB";
	} elsif ($opts->{'direction'} eq 'dedicated')
	{
	    $direction = "SUBPARSER_SUB_RUNS_BASE";
	} elsif ($opts->{'direction'} eq 'bidirectional')
	{
	    $direction = "SUBPARSER_BI_DIRECTION";
	}
    }

    print <<EOF;
	static subparser $opts->{'Clangdef'}Subparser = {
		.direction = $direction,
	};
	static parserDependency $opts->{'Clangdef'}Dependencies [] = {
		[0] = { DEPTYPE_SUBPARSER, "$opts->{'base'}", &$opts->{'Clangdef'}Subparser },
	};
EOF

}

sub emit_xtags {
    my $opts = shift;

    return if (! @{$opts->{'extradefs'}});

    print <<EOF;
	static xtagDefinition $opts->{'Clangdef'}XtagTable [] = {
EOF
    for (@{$opts->{'extradefs'}}) {
      my $enabled = $_->{"enabled"}? "true": "false";
      print <<EOF;
		{
		  .enabled     = $enabled,
		  .name        = "$_->{'name'}",
		  .description = "$_->{'desc'}",
		},
EOF
    }
    print <<EOF;
	};
EOF
}

sub emit_fields {
    my $opts = shift;

    return if (! @{$opts->{'fielddefs'}});

    print <<EOF;
	static fieldDefinition $opts->{'Clangdef'}FieldTable [] = {
EOF
    for (@{$opts->{'fielddefs'}}) {
      my $enabled = $_->{"enabled"}? "true": "false";
      print <<EOF;
		{
		  .enabled     = $enabled,
		  .name        = "$_->{'name'}",
		  .description = "$_->{'desc'}",
		},
EOF
    }
    print <<EOF;
	};
EOF
}

sub emit_fields_initialization {
    my $opts = shift;
    my $enabled = $opts->{"disabled"} ? "false": "true";
    my $method;

    $method  = "METHOD_NOT_CRAFTED";
    if (@{$opts->{"regexs"}} || $opts->{'tablenames'}) {
	$method = "${method}|METHOD_REGEX";
    }

    print <<EOF;
	def->enabled       = ${enabled};
	def->extensions    = extensions;
	def->patterns      = patterns;
	def->aliases       = aliases;
	def->method        = ${method};
EOF

    if ($opts->{'useCork'}) {
	print <<EOF;
	def->useCork       = 1;
EOF
    }
    if (@{$opts->{'kinddefs'}}) {
	print <<EOF;
	def->kindTable = $opts->{'Clangdef'}KindTable;
	def->kindCount = ARRAY_SIZE($opts->{'Clangdef'}KindTable);
EOF
    }

    if (@{$opts->{'fielddefs'}}) {
	print <<EOF;
	def->fieldTable = $opts->{'Clangdef'}FieldTable;
	def->fieldCount = ARRAY_SIZE($opts->{'Clangdef'}FieldTable);
EOF
    }

    if (@{$opts->{'extradefs'}}) {
	print <<EOF;
	def->xtagTable = $opts->{'Clangdef'}XtagTable;
	def->xtagCount = ARRAY_SIZE($opts->{'Clangdef'}XtagTable);
EOF
    }

    if (@{$opts->{'regexs'}}) {
	print <<EOF;
	def->tagRegexTable = $opts->{'Clangdef'}TagRegexTable;
	def->tagRegexCount = ARRAY_SIZE($opts->{'Clangdef'}TagRegexTable);
EOF
    }

    if (defined $opts->{'base'}) {
	print <<EOF;
	def->dependencies = $opts->{'Clangdef'}Dependencies;
	def->dependencyCount = ARRAY_SIZE($opts->{'Clangdef'}Dependencies);
EOF
    }

    if ($opts->{'autoFQTag'}) {
	print <<EOF;
	def->requestAutomaticFQTag = true;
EOF
    }
    print <<EOF;
	def->initialize    = initialize$opts->{'Clangdef'}Parser;

EOF
}

sub emit {
    my ($optlib, $opts) = @_;

    emit_header ($optlib, $opts);
    emit_initializer $opts;

    print <<EOF;
extern parserDefinition* $opts->{'Clangdef'}Parser (void)
{
EOF

    emit_extensions      $opts;
    emit_aliases         $opts;
    emit_patterns        $opts;
    emit_roledefs        $opts;
    emit_kinddefs        $opts;
    emit_fields          $opts;
    emit_xtags           $opts;
    emit_regexs          $opts;
    emit_dependencies    $opts;

    print "\n";

    print <<EOF;
	parserDefinition* const def = parserNew ("$opts->{'langdef'}");

EOF

    emit_fields_initialization $opts;

    print <<EOF;
	return def;
}
EOF
}

########################################################################
#
# REARRANGE
#
########################################################################

sub capitalize {
    my ($str) = $_[0];
    my $c = substr ($str, 0, 1);

    $c =~ tr/a-z/A-Z/;

    return $c . substr($str, 1);
}

sub rearrange {
    my ($opts) = @_;
    my $langdef = $opts -> {'langdef'};
    $opts -> {'Clangdef'} = capitalize ($langdef);
}


########################################################################
#
# MAIN
#
########################################################################

sub main {
    my $optlib;
    my %opts = (langdef  => undef,
		Clangdef => undef,
		disabled => 0,
		patterns => [],
		extensions => [],
		aliases => [],
		disabledKinds => [],
		regexs => [# { regex => "", name => "", kind => "", flags => "", mline => 1|0 },
			  ],
		kinddefs => [# { letter => '', name => "", desc => "" },
			    ],
		extradefs => [ # { name => "", desc => "", enabled => 1|0 },
			     ],
		fielddefs => [ # { name => "", desc => "", enabled => 1|0 },
			      ],
		base => undef,
		tableNames => [ # ""
			       ],
		tabledefs => { # "" => [{ regex => "", name => "", kind => "", flags => "" }...],
			     },
		useCork   => 0,
	       );

    for (@_) {
	if ( ($_ eq '-h') || ($_ eq '--help') ) {
	    show_help;
	    exit 0;
	} elsif ( /^-.*/ ) {
	    die "unrecongnized option: $_";
	} else {
	    if ($optlib) {
		die "Too man input files: @_";
	    }
	    $optlib=$_;
	}
    }

    if (! $optlib) {
	die "No optlib file name is given";
    }

    if (! parse_optlib ($optlib, \%opts)) {
	exit 1;
    }

    rearrange (\%opts);

    if (! emit ($optlib, \%opts) ) {
	exit 1;
    }
    0;
}

main @ARGV;

# optlib2c ends here.