6 parse('builtins','DATA');
12 } elsif (m/^\-w(c|h)$/) {
14 } elsif (m/^\-o(.+)$/) {
17 die "unknown option $_\n";
19 if (!defined $prefix) { $prefix= $_; $prefix =~ s/\.[^.]+$//; }
20 $x= new IO::File $_,'r' or die "$_: $!\n";
25 die "must say -w<something>\n" if !defined $write;
35 while (defined($_= $f->getline)) {
39 s/\t/ ' 'x(8-(length $`) % 8) /eg;
42 $this_indent= length $&;
43 while (@i && $this_indent < $i[0]) {
46 if ($this_indent && (!@i || $this_indent > $i[0])) {
47 unshift @i, $this_indent;
50 if (@i==0 && m/^Table\s+(\w+)$/) {
53 $table_x{$c_table}{C}= 'ParserCommandTableEntry';
54 $entrytype_x{ParserCommandTableEntry}= '';
55 } elsif (@i==0 && m/^Untabled$/) {
58 } elsif (@i==0 && m/^(C|H)\-Include\s+(\S.*)$/) {
59 o(lc $1, 30, "#include $2\n");
60 } elsif (@i==1 && m/^Of\s+(\w+)$/ && defined $c_table) {
61 die unless length $c_table;
62 $table_x{$c_table}{C}= $1;
64 } elsif (@i==0 && m/^EntryExtra\s+(\w+)$/) {
67 } elsif (@i>=1 && defined $c_entryextra) {
68 $entrytype_x{$c_entryextra} .= " $_\n";
69 } elsif (@i==1 && m/^[a-z].*$/ && defined $c_table) {
72 } elsif (m/^(\w+)\s+(\S.*)$/) {
74 $tables{$c_table}{$c_entry}{I} .= ", $2";
76 badsyntax($wh,$.,"bad entry");
78 $tables{$c_table}{$c_entry}{A} = [ ];
79 } elsif (@i==2 && m/^(\w+)\s+\.\.\.$/ && defined $c_entry) {
80 $tables{$c_table}{$c_entry}{V}= $1;
81 } elsif (@i==2 && m/^(\??)([a-z]\w*)\s*(\S.*)/
82 && defined $c_entry) {
83 ($opt, $var, $type) = ($1,$2,$3);
84 if ($type =~ m/^\w+$/) {
86 } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
90 push @{ $tables{$c_table}{$c_entry}{A} },
91 { N => $var, T => $type, A => $xtypeargs, O => ($opt eq '?') };
92 } elsif (@i==2 && m/^\=\>\s*(\S.*)$/ && defined $c_entry) {
93 $tables{$c_table}{$c_entry}{R}= $1;
94 } elsif (@i==0 && m/^Type\s+([^\:]+)\:\s+(\S.*)$/) {
95 ($typename,$ctype)= ($1,$2);
96 $ctype .= ' @' unless $ctype =~ m/\@/;
97 if ($typename =~ m/^\w+$/) {
99 } elsif ($typename =~ m/^(\w+)\((.+)\)$/) {
103 badsyntax($wh,$.,"bad type name/args");
105 $types{$typename}= { C => $ctype, X => $xtypeargs };
106 } elsif (@i==0 && s/^Init\s+(\w+)\s+(\S.*)//) {
109 badsyntax($wh,$., sprintf
110 "bad directive (indent level %d)", scalar @i);
113 $f->error and die $!;
117 #print Dumper(\%tables),"\n";
118 #print Dumper(\%types),"\n";
120 foreach $t (sort keys %types) {
124 $decl= "int pat_$t(Tcl_Interp*, Tcl_Obj*, ";
125 $decl .= subst_in_decl('*', $c, "type $t");
126 $decl .= ", $xta", if length $xta;
130 $decl= "Tcl_Obj *ret_$t(Tcl_Interp*, ".subst_in_decl('',$c).");\n";
134 foreach $c_entrytype (sort keys %entrytype_x) {
135 o('h', 20, "typedef struct $c_entrytype $c_entrytype;\n");
137 "struct $c_entrytype {\n".
138 " const char *name;\n".
139 " Tcl_ObjCmdProc *func;\n".
140 $entrytype_x{$c_entrytype}.
144 foreach $c_table (sort keys %tables) {
145 $r_table= $tables{$c_table};
146 $x_table= $table_x{$c_table};
149 foreach $c_entry (keys %$r_table) {
150 $r_entry= $r_table->{$c_entry};
151 $pa_decl= "int pa_${c_table}_${c_entry}(ClientData cd,".
152 " Tcl_Interp *ip, int objc, Tcl_Obj *const *objv)";
153 $do_decl= "int do_${c_table}_${c_entry}(";
154 @do_al= ('ClientData cd', 'Tcl_Interp *ip');
157 $pa_argc= " objc--; objv++;\n";
158 $pa_vars= " int rc;\n";
166 foreach $arg (@{ $r_entry->{A} }) {
170 push @do_al, make_decl($n, $t, $arg->{A});
171 $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init);
174 $pa_argc .= " if (objc < $any_mand) {".
175 " e=\"too few args\"; goto e_err; }\n";
176 $pa_body .= " objc -= $any_mand;\n";
180 $pa_body .= " if (!objc--) goto end_optional;\n";
188 $pa_vars .= " const void *v_$n= 0;\n";
190 $pafin= "\n a_$n= v_$n; ";
191 $a =~ m/\,/ or die; $a = "$', sizeof($`)";
192 o('h', 210, "extern const $` $'".'[]'.";\n");
194 $pa_body .= " rc= pat_$t(ip, *objv++, $paarg";
195 $pa_body .= ", ".$a if length $a;
196 $pa_body .= ");$pafin if (rc) goto rc_err;\n";
199 if (exists $r_entry->{V}) {
201 push @do_al, subst_in_decl("${va}c", 'int @');
202 push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
203 push @do_aa, "objc-1", "objv+1";
205 $pa_body .= " if (--objc) { e=\"too many args\"; goto e_err; }\n";
209 $pa_body .= "end_optional:\n";
211 if (exists $r_entry->{R}) {
213 push @do_al, make_decl("*result", $t);
214 $pa_vars .= make_decl_init("result", $t, '', \$pa_init);
215 push @do_aa, "&result";
216 $pa_rslt .= " Tcl_SetObjResult(ip, ret_$t(ip, result));\n";
219 $pa_body .= " rc= do_${c_table}_${c_entry}(";
220 $pa_body .= join ', ', @do_aa;
222 $pa_body .= " if (rc) goto rc_err;\n";
224 $pa_rslt .= " rc= TCL_OK;\n\n";
225 $pa_rslt .= "rc_err:\n";
227 $pa_fini .= " return rc;\n";
229 $pa_vars .= " const char *e;\n";
231 $pa_fini .= "e_err:\n";
232 $pa_fini .= " setstringresult(ip,e);";
233 $pa_fini .= " rc= TCL_ERROR; goto rc_err;\n";
236 $pa_init .= "\n" if length $pa_init;
239 if (length $c_table) {
243 o('h',90, "$pa_decl;\n");
246 $static.$pa_decl." {\n".
254 $do_decl .= join ', ', @do_al;
256 o('h',100, $do_decl.";\n") or die $!;
259 $op_tab .= sprintf(" { %-20s, %-50s%s },\n",
261 "pa_${c_table}_${c_entry}",
264 if (length $c_table) {
265 $decl= "const $x_table->{C} pcmdtab_$c_table".'[]';
266 o('h', 500, "extern $decl;\n");
275 o(c, 0, "#include \"$prefix.h\"\n");
278 "#ifndef INCLUDED_\U${prefix}_H\n".
279 "#define INCLUDED_\U${prefix}_H\n\n".
280 "#include <tcl.h>\n");
283 "void setstringresult(Tcl_Interp*, const char*);\n".
284 "int pat_enum(Tcl_Interp*, Tcl_Obj*, const void**, const void*, size_t);\n");
287 "#endif /*INCLUDED_\U${prefix}_H*/\n");
289 if (defined $output) {
290 $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
295 print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
296 foreach $pr (sort keys %{ $o{$write} }) {
297 print $oh "\n" or die $!;
298 print $oh $o{$write}{$pr} or die $!;
302 die $! unless $oh->close;
304 if (defined $output) {
305 rename "$output.tmp", $output or die $!;
309 my ($wh,$pr,$s) = @_;
310 $o{$wh}{sprintf "%010d", $pr} .= $s;
313 sub make_decl_init ($$$$) {
314 my ($n, $t, $a, $initcode) = @_;
316 $o= make_decl($n,$t,$a);
317 if (exists $type_init{$t}) {
318 $init= $type_init{$t};
319 $$initcode .= " ".subst_in("$n", $init)."\n"
327 sub make_decl ($$$) {
328 my ($n, $t, $ta) = @_;
331 $ta =~ m/\,/ or die "enum with bad args \`$ta'\n";
334 defined $types{$t} or die "unknown type $t\n";
337 return subst_in_decl($n,$c);
340 sub subst_in_decl ($$$) {
341 my ($val, $pat, $why) = @_;
342 local ($_) = subst_in($val, $pat, $why);
348 my ($val, $pat, $why) = @_;
349 $pat =~ m/\@/ or die "$pat for $val in $why ?";
354 sub badsyntax ($$$) {
355 die "$_[0]:$_[1]: $_[2]\n";
361 Type charfrom(const char*): int