while (@ARGV) {
$_= shift @ARGV;
- die if m/^\-/;
- $x= new IO::File $_,'r';
- parse($_,$x);
+ if (m/^\-p(\w+)/) {
+ $prefix= $1;
+ } elsif (m/^\-w(c|h)$/) {
+ $write= $1;
+ } elsif (m/^\-o(.+)$/) {
+ $output= $1;
+ } elsif (m/^\-/) {
+ die "unknown option $_\n";
+ } else {
+ if (!defined $prefix) { $prefix= $_; $prefix =~ s/\.[^.]+$//; }
+ $x= new IO::File $_,'r' or die "$_: $!\n";
+ parse($_,$x);
+ }
+}
+
+die "must say -w<something>\n" if !defined $write;
+
+sub zilch () {
+ undef $c_table;
+ undef $c_entryextra;
+ undef $c_of;
}
sub parse ($$) {
my ($wh,$f) = @_;
while (defined($_= $f->getline)) {
- chomp; s/^\s+//; s/\s+$//;
- next if m/^\#/;
+ chomp; s/\s+$//;
+ next if m/^\s*\#/;
next if !m/\S/;
- s/\t/ ' 'x(8-(length $`) % 8) /eg;
+ while (s/\t/ ' 'x(8 - (length $`) % 8) /e) { }
- s/^\s+//;
+ s/^\s*//;
$this_indent= length $&;
- while (@i && $this_indent < $i[0]) { shift @i; }
+ while (@i && $this_indent < $i[0]) {
+ shift @i;
+ }
if ($this_indent && (!@i || $this_indent > $i[0])) {
unshift @i, $this_indent;
}
- if (@i==0 && m/^Table\s+(\w+)$/) {
+ if (@i==0 && m/^Table\s+(\w+)\s+(\w+)$/) {
+ zilch();
$c_table= $1;
- undef $c_entry;
- } elsif (@i==1 && m/^([a-z]\w*)$/ && defined $c_table) {
- $c_entry= $1;
+ $table_x{$c_table}{C}= $2;
+ $entrytype_x{$2}= '';
+ } elsif (@i==0 && m/^Untabled$/) {
+ zilch();
+ $c_table= '';
+ } elsif (@i==0 && m/^(C|H)\-Include\s+(\S.*)$/) {
+ o(lc $1, 30, "#include $2\n");
+ } elsif (@i==0 && m/^EntryExtra\s+(\w+)$/) {
+ zilch();
+ $c_entryextra= $1;
+ } elsif (@i>=1 && defined $c_entryextra) {
+ $entrytype_x{$c_entryextra} .= " $_\n";
+ } elsif (@i==1 && m/^[a-z].*$/ && defined $c_table) {
+ if (m/^[-_0-9A-Za-z]+$/) {
+ $c_entry= $_;
+ } elsif (m/^([-_0-9A-Za-z]+)\s+(\S.*)$/) {
+ $c_entry= $1;
+ $tables{$c_table}{$c_entry}{I} .= ", $2";
+ } else {
+ badsyntax($wh,$.,"bad entry");
+ }
$tables{$c_table}{$c_entry}{A} = [ ];
- } elsif (@i==2 && m/^(?:\?)?([a-z]\w*)\s.*(\S.*)/
+ } elsif (@i==2 && m/^(\w+)\s+\.\.\.$/ && defined $c_entry) {
+ $tables{$c_table}{$c_entry}{V}= $1;
+ } elsif (@i==2 && m/^(\??)([a-z]\w*)\s*(\S.*)/
&& defined $c_entry) {
- push @{ $tables{$c_table}{$c_entry}{A} }, [ $2, $3, $1 eq '?' ];
- } elsif (@i==2 && m/^\=\>\s.*(\S.*)/ && defined $c_entry) {
+ ($opt, $var, $type) = ($1,$2,$3);
+ if ($type =~ m/^\w+$/) {
+ $xtypeargs='';
+ } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
+ $type= $1;
+ $xtypeargs= $2;
+ }
+ push @{ $tables{$c_table}{$c_entry}{A} },
+ { N => $var, T => $type, A => $xtypeargs, O => ($opt eq '?') };
+ } elsif (@i==2 && m/^\=\>\s*(\S.*)$/ && defined $c_entry) {
$tables{$c_table}{$c_entry}{R}= $1;
- } elsif (@i==2 && m/^\.\.\.$/ && defined $c_entry) {
- $tables{$c_table}{$c_entry}{V}= 1;
- } elsif (@i==0 && s/^Type\s+$//) {
-
- ($typename,$_)= ($1,$2);
- $_ .= '@' unless m/\@/;
- $types{$typename}= $_;
+ } elsif (@i==0 && m/^Type\s+([^\:]+)\:\s+(\S.*)$/) {
+ ($typename,$ctype)= ($1,$2);
+ $ctype .= ' @' unless $ctype =~ m/\@/;
+ if ($typename =~ m/^\w+$/) {
+ $xtypeargs='';
+ } elsif ($typename =~ m/^(\w+)\((.+)\)$/) {
+ $typename=$1;
+ $xtypeargs=$2;
+ } else {
+ badsyntax($wh,$.,"bad type name/args");
+ }
+ $types{$typename}= { C => $ctype, X => $xtypeargs };
+ } elsif (@i==0 && s/^Init\s+(\w+)\s+(\S.*)//) {
+ $type_init{$1}= $2;
+ } elsif (@i==0 && s/^Fini\s+(\w+)\s+(\S.*)//) {
+ $type_fini{$1}= $2;
} else {
badsyntax($wh,$., sprintf
"bad directive (indent level %d)", scalar @i);
$f->close;
}
-print Dumper(\%tables),"\n";
-print Dumper(\%types),"\n";
+#print Dumper(\%tables),"\n";
+#print Dumper(\%types),"\n";
+
+foreach $t (sort keys %types) {
+ $type= $types{$t};
+ $c= $type->{C};
+ $xta= $type->{X};
+ $decl= "int pat_$t(Tcl_Interp *ip, Tcl_Obj *obj, ";
+ $decl .= subst_in_decl('*val', $c, "type $t");
+ $decl .= ", $xta", if length $xta;
+ $decl .= ");\n";
+ o('h',160, $decl);
+
+ $decl= "Tcl_Obj *ret_$t(Tcl_Interp *ip, ".subst_in_decl('val',$c).");\n";
+ o('h',170, $decl);
+}
+
+foreach $c_entrytype (sort keys %entrytype_x) {
+ o('h', 20, "typedef struct $c_entrytype $c_entrytype;\n");
+ o('h', 100,
+ "struct $c_entrytype {\n".
+ " const char *name;\n".
+ " Tcl_ObjCmdProc *func;\n".
+ $entrytype_x{$c_entrytype}.
+ "};\n\n");
+}
+
+foreach $c_table (sort keys %tables) {
+ $r_table= $tables{$c_table};
+ $x_table= $table_x{$c_table};
+ $op_tab= '';
+
+ foreach $c_entry (sort keys %$r_table) {
+ $c_entry_c= $c_entry; $c_entry_c =~ y/-/_/;
+ $r_entry= $r_table->{$c_entry};
+ $pa_decl= "int pa_${c_table}_${c_entry_c}(ClientData cd,".
+ " Tcl_Interp *ip, int objc, Tcl_Obj *const *objv)";
+ $do_decl= "int do_${c_table}_${c_entry_c}(";
+ @do_al= ('ClientData cd', 'Tcl_Interp *ip');
+ @do_aa= qw(cd ip);
+ $pa_init= '';
+ $pa_argc= " objc--; objv++;\n";
+ $pa_vars= " int rc;\n";
+ $pa_body= '';
+ $pa_rslt= '';
+ $pa_free= '';
+ $pa_fini= '';
+ $any_mand= 0;
+ $any_optl= 0;
+ $any_eerr= 0;
+ $any_eargc= 0;
+ $pa_hint= '';
+ $pa_hint .= "$c_table " if length $c_table;
+ $pa_hint.= $c_entry;
+ foreach $arg (@{ $r_entry->{A} }) {
+ $n= $arg->{N};
+ $t= $arg->{T};
+ $a= $arg->{A};
+ push @do_al, make_decl($n, $t, $arg->{A});
+ $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init);
+ if ($arg->{O}) {
+ $pa_hint .= " ?$n?";
+ if ($any_mand) {
+ $any_mand= 0;
+ $any_eerr= 1;
+ }
+ $pa_body .= " if (!objc--) goto end_optional;\n";
+ $any_optl= 1;
+ } else {
+ $pa_hint .= " $n";
+ $pa_body .= " if (!objc--) goto wrong_count_args;\n";
+ $any_mand++;
+ $any_eargc= 1;
+ die if $any_optl;
+ }
+ $paarg= "&a_$n";
+ $pafin= '';
+ if ($t eq 'enum') {
+ $pa_vars .= " const void *v_$n= 0;\n";
+ $paarg= "&v_$n";
+ $pafin= "\n a_$n= v_$n; ";
+ $a =~ m/\,/ or die "invalid enum type \`$a'\n";
+ $a_tab = lc($`).'s';
+ $a = "$a_tab, sizeof($`), $'";
+ o('h', 210, "extern const $` $a_tab".'[]'.";\n");
+ }
+ if (exists $type_fini{$t}) {
+ $pa_fini .= ' '.subst_in("a_$n", $type_fini{$t})."\n";
+ }
+ $pa_body .= " rc= pat_$t(ip, *objv++, $paarg";
+ $pa_body .= ", ".$a if length $a;
+ $pa_body .= ");$pafin if (rc) goto rc_err;\n";
+ push @do_aa, "a_$n";
+ }
+ if (exists $r_entry->{V}) {
+ $pa_hint .= " ...";
+ $va= $r_entry->{V};
+ push @do_al, subst_in_decl("${va}c", 'int @');
+ push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
+ push @do_aa, "objc+1", "objv-1";
+ } else {
+ if (!$any_optl) {
+ $pa_body .= " if (objc) goto wrong_count_args;\n";
+ $any_eargc= 1;
+ }
+ }
+ if ($any_optl) {
+ $pa_body .= "end_optional:\n";
+ }
+ if (exists $r_entry->{R}) {
+ $t= $r_entry->{R};
+ push @do_al, make_decl("*result", $t);
+ $pa_vars .= make_decl_init("result", $t, '', \$pa_init);
+ push @do_aa, "&result";
+ $pa_rslt .= " Tcl_SetObjResult(ip, ret_$t(ip, result));\n";
+ }
+ $pa_body .= "\n";
+ $pa_body .= " rc= do_${c_table}_${c_entry_c}(";
+ $pa_body .= join ', ', @do_aa;
+ $pa_body .= ");\n";
+ $pa_body .= " if (rc) goto rc_err;\n";
+
+ $pa_rslt .= " rc= TCL_OK;\n\n";
+ $pa_rslt .= "rc_err:\n";
+
+ $pa_fini .= " return rc;\n";
+ if ($any_eargc) {
+ $pa_fini .= "\nwrong_count_args:\n";
+ $pa_fini .= " e=\"wrong # args: should be \\\"$pa_hint\\\"\";\n";
+ $pa_fini .= " goto e_err;";
+ $any_eerr= 1;
+ }
+ if ($any_eerr) {
+ $pa_vars .= " const char *e;\n";
+ $pa_fini .= "\n";
+ $pa_fini .= "e_err:\n";
+ $pa_fini .= " setstringresult(ip,e);";
+ $pa_fini .= " rc= TCL_ERROR; goto rc_err;\n";
+ }
+ $pa_vars .= "\n";
+ $pa_init .= "\n" if length $pa_init;
+ $pa_fini .= "}\n\n";
+
+ if (length $c_table) {
+ $static= 'static ';
+ } else {
+ $static= '';
+ o('h',90, "$pa_decl;\n");
+ }
+ o('c',100,
+ $static.$pa_decl." {\n".
+ $pa_vars.
+ $pa_init.
+ $pa_argc.
+ $pa_body.
+ $pa_rslt.
+ $pa_free.
+ $pa_fini);
+ $do_decl .= join ', ', @do_al;
+ $do_decl .= ")";
+ o('h',100, $do_decl.";\n") or die $!;
+
+
+ $op_tab .= sprintf(" { %-20s %-40s%s },\n",
+ "\"$c_entry\",",
+ "pa_${c_table}_${c_entry_c}",
+ $r_entry->{I});
+ }
+ if (length $c_table) {
+ $decl= "const $x_table->{C} ".lc($x_table->{C}).'s[]';
+ o('h', 500, "extern $decl;\n");
+ o('c', 100,
+ "$decl = {\n".
+ $op_tab.
+ " { 0 }\n".
+ "};\n\n");
+ }
+}
+
+o(c, 0, "#include \"$prefix.h\"\n");
+
+o(h, 0,
+ "#ifndef INCLUDED_\U${prefix}_H\n".
+ "#define INCLUDED_\U${prefix}_H\n\n".
+ "#include <tcl.h>\n");
+
+o(h, 400,
+ "void setstringresult(Tcl_Interp*, const char*);\n".
+ "int pat_enum(Tcl_Interp*, Tcl_Obj*, const void**,".
+ " const void*, size_t, const char *what);\n");
+
+o(h, 999,
+ "#endif /*INCLUDED_\U${prefix}_H*/\n");
+
+if (defined $output) {
+ $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
+} else {
+ $oh= 'STDOUT';
+}
+
+print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
+foreach $pr (sort keys %{ $o{$write} }) {
+ print $oh "\n" or die $!;
+ print $oh $o{$write}{$pr} or die $!;
+}
+
+die if $oh->error;
+die $! unless $oh->close;
+
+if (defined $output) {
+ rename "$output.tmp", $output or die $!;
+}
+
+sub o ($$) {
+ my ($wh,$pr,$s) = @_;
+ $o{$wh}{sprintf "%010d", $pr} .= $s;
+}
+
+sub make_decl_init ($$$$) {
+ my ($n, $t, $a, $initcode) = @_;
+ my ($o,$init);
+ $o= make_decl($n,$t,$a);
+ if (exists $type_init{$t}) {
+ $init= $type_init{$t};
+ $$initcode .= " ".subst_in("$n", $init)."\n"
+ if length $init;
+ } else {
+ $o .= ' =0';
+ }
+ return " ".$o.";\n";
+}
+
+sub make_decl ($$$) {
+ my ($n, $t, $ta) = @_;
+ my ($type);
+ if ($t eq 'enum') {
+ $ta =~ m/\,/ or die "invalid enum type \`$t'\n";
+ $c= "const $` *@";
+ } else {
+ defined $types{$t} or die "unknown type $t\n";
+ $c= $types{$t}{C};
+ }
+ return subst_in_decl($n,$c);
+}
+
+sub subst_in_decl ($$$) {
+ my ($val, $pat, $why) = @_;
+ local ($_) = subst_in($val, $pat, $why);
+ s/ *(\**) *$/$1/;
+ return $_;
+}
+
+sub subst_in ($$$) {
+ my ($val, $pat, $why) = @_;
+ $pat =~ m/\@/ or die "$pat for $val in $why ?";
+ $pat =~ s/\@/$val/g;
+ return $pat;
+}
sub badsyntax ($$$) {
die "$_[0]:$_[1]: $_[2]\n";
}
__DATA__
-Type int
- int
-Type obj
- Tcl_Obj *
-Type charfrom
- (const char*)
+Type int: int
+Type obj: Tcl_Obj *@
+
+Type charfrom(const char *opts, const char *what): int