X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=base%2Ftcmdifgen;h=514dc019a113f50bc27db466e4b76bcc2b51830f;hb=9b7d11070d3e9dc1eb61cbccd5155f47a27047c3;hp=3a317549d62e7c003caa17ee5afa225847b0a01f;hpb=310a482133795d0bee9621003c0a0b3813a47037;p=chiark-tcl.git diff --git a/base/tcmdifgen b/base/tcmdifgen index 3a31754..514dc01 100755 --- a/base/tcmdifgen +++ b/base/tcmdifgen @@ -36,7 +36,7 @@ sub parse ($$) { 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*//; $this_indent= length $&; @@ -47,29 +47,25 @@ sub parse ($$) { unshift @i, $this_indent; } - if (@i==0 && m/^Table\s+(\w+)$/) { + if (@i==0 && m/^Table\s+(\w+)\s+(\w+)$/) { zilch(); $c_table= $1; - $table_x{$c_table}{C}= 'ParserCommandTableEntry'; - $entrytype_x{ParserCommandTableEntry}= ''; + $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==1 && m/^Of\s+(\w+)$/ && defined $c_table) { - die unless length $c_table; - $table_x{$c_table}{C}= $1; - $entrytype_x{$1}= ''; } 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/^\w+$/) { + if (m/^[-_0-9A-Za-z]+$/) { $c_entry= $_; - } elsif (m/^(\w+)\s+(\S.*)$/) { + } elsif (m/^([-_0-9A-Za-z]+)\s+(\S.*)$/) { $c_entry= $1; $tables{$c_table}{$c_entry}{I} .= ", $2"; } else { @@ -105,6 +101,8 @@ sub parse ($$) { $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); @@ -121,13 +119,13 @@ foreach $t (sort keys %types) { $type= $types{$t}; $c= $type->{C}; $xta= $type->{X}; - $decl= "int pat_$t(Tcl_Interp*, Tcl_Obj*, "; - $decl .= subst_in_decl('*', $c, "type $t"); + $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*, ".subst_in_decl('',$c).");\n"; + $decl= "Tcl_Obj *ret_$t(Tcl_Interp *ip, ".subst_in_decl('val',$c).");\n"; o('h',170, $decl); } @@ -146,11 +144,12 @@ foreach $c_table (sort keys %tables) { $x_table= $table_x{$c_table}; $op_tab= ''; - foreach $c_entry (keys %$r_table) { + 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}(ClientData cd,". + $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}("; + $do_decl= "int do_${c_table}_${c_entry_c}("; @do_al= ('ClientData cd', 'Tcl_Interp *ip'); @do_aa= qw(cd ip); $pa_init= ''; @@ -180,6 +179,7 @@ foreach $c_table (sort keys %tables) { $pa_body .= " if (!objc--) goto end_optional;\n"; $any_optl= 1; } else { + die if $any_optl; $any_mand++; } $paarg= "&a_$n"; @@ -188,8 +188,13 @@ foreach $c_table (sort keys %tables) { $pa_vars .= " const void *v_$n= 0;\n"; $paarg= "&v_$n"; $pafin= "\n a_$n= v_$n; "; - $a =~ m/\,/ or die; $a = "$', sizeof($`)"; - o('h', 210, "extern const $` $'".'[]'.";\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; @@ -197,13 +202,19 @@ foreach $c_table (sort keys %tables) { push @do_aa, "a_$n"; } if (exists $r_entry->{V}) { + if ($any_mand) { + $pa_body .= " objc -= $any_mand;\n"; + } $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"; + push @do_aa, "objc+1", "objv-1"; } else { - $pa_body .= " if (--objc) { e=\"too many args\"; goto e_err; }\n"; - $any_eerr= 1; + if (!$any_optl) { + $pa_argc .= " if (objc != $any_mand) {". + " e=\"wrong number of args\"; goto e_err; }\n"; + $any_eerr= 1; + } } if ($any_optl) { $pa_body .= "end_optional:\n"; @@ -216,7 +227,7 @@ foreach $c_table (sort keys %tables) { $pa_rslt .= " Tcl_SetObjResult(ip, ret_$t(ip, result));\n"; } $pa_body .= "\n"; - $pa_body .= " rc= do_${c_table}_${c_entry}("; + $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"; @@ -256,13 +267,13 @@ foreach $c_table (sort keys %tables) { o('h',100, $do_decl.";\n") or die $!; - $op_tab .= sprintf(" { %-20s, %-50s%s },\n", - "\"$c_entry\"", - "pa_${c_table}_${c_entry}", + $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} pcmdtab_$c_table".'[]'; + $decl= "const $x_table->{C} ".lc($x_table->{C}).'s[]'; o('h', 500, "extern $decl;\n"); o('c', 100, "$decl = {\n". @@ -281,7 +292,8 @@ o(h, 0, o(h, 400, "void setstringresult(Tcl_Interp*, const char*);\n". - "int pat_enum(Tcl_Interp*, Tcl_Obj*, const void**, const void*, size_t);\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"); @@ -328,7 +340,7 @@ sub make_decl ($$$) { my ($n, $t, $ta) = @_; my ($type); if ($t eq 'enum') { - $ta =~ m/\,/ or die "enum with bad args \`$ta'\n"; + $ta =~ m/\,/ or die "invalid enum type \`$t'\n"; $c= "const $` *@"; } else { defined $types{$t} or die "unknown type $t\n"; @@ -356,6 +368,7 @@ sub badsyntax ($$$) { } __DATA__ -Type int: int -Type obj: Tcl_Obj *@ -Type charfrom(const char*): int +Type int: int +Type obj: Tcl_Obj *@ + +Type charfrom(const char *opts, const char *what): int