dotter

graphiz helpers
git clone git://deadbeef.fr/dotter.git
Log | Files | Refs | README | LICENSE

uml (3210B)


      1 #!/usr/bin/awk -f
      2 
      3 function print_ft_name() {
      4 	printf( "\"" );
      5 	for( i = 2; i < NF; ++i )
      6 	{
      7 		printf( "%s ", $i );
      8 	}
      9 	printf( "%s\"", $NF );
     10 }
     11 
     12 function end_class() {
     13 	if( class != "" )
     14 	{
     15 		printf( "}\"\n]\n\n" );
     16 	}
     17 	class = "";
     18 	inside = "";
     19 }
     20 
     21 function fun( overload ) {
     22 	if( inside != "fun" )
     23 	{
     24 		printf( "|" );
     25 		inside = "fun";
     26 	}
     27 	printf( "%s %s(", scope, $3 )
     28 	for( i = 4; i <= NF; ++i )
     29 	{
     30 		gsub( ":", " ", $i );
     31 		printf( "%s%s", i == 4 ? "" : ", ", $i );
     32 	}
     33 	printf( "): %s %s\l", $2, overload );
     34 }
     35 
     36 function relation( from, to, tail, head, label, multiplicity ) {
     37 	printf( "%s -> %s [ dir = \"both\", arrowtail = \"%s\", arrowhead = \"%s\"", from, to, tail, head );
     38 	if( label != "" )
     39 	{
     40 		printf( ", label = \"%s\"", label );
     41 	}
     42 	if( multiplicity != "" )
     43 	{
     44 		if( multiplicity ~ /[0-9]*:[0-9n]*/ )
     45 		{
     46 			split( multiplicity, labels, ":" );
     47 			printf( ", taillabel = \"%s\", headlabel = \"%s\"", labels[1], labels[2] );
     48 		}
     49 		else
     50 		{
     51 			printf( ", taillabel = \"%s\", headlabel = \"%s\"", multiplicity, multiplicity );
     52 		}
     53 	}
     54 }
     55 
     56 function close_relation() {
     57 	printf( " ];\n" );
     58 }
     59 
     60 function depends( type ) {
     61 	end_class();
     62 	relation( $2, $3, "none", "vee", type );
     63 	printf( " style = \"dotted\"" );
     64 	close_relation();
     65 }
     66 
     67 BEGIN {
     68 	printf( "digraph %s\n{\n", FILENAME );
     69 	class = "";
     70 	scope = "";
     71 	inside = "";
     72 }
     73 
     74 $0 == "private"   { scope = "[-]"; }
     75 $0 == "protected" { scope = "[#]"; }
     76 $0 == "public"    { scope = "[+]"; }
     77 $0 == "package"   { scope = "[~]"; }
     78 
     79 $1 == "splines" {
     80 	end_class();
     81 	printf( "splines = %s;\n", $2 );
     82 }
     83 
     84 $1 == "package_font" {
     85 	end_class();
     86 	printf( "fontname = " );
     87 	print_ft_name();
     88 	printf( ";\n" );
     89 }
     90 
     91 $1 == "class_font" {
     92 	end_class();
     93 	printf( "node [fontname = " );
     94 	print_ft_name();
     95 	printf( "];\n" );
     96 }
     97 
     98 $1 == "link_font" {
     99 	end_class();
    100 	printf( "edge [fontname = " );
    101 	print_ft_name();
    102 	printf( "];\n" );
    103 }
    104 
    105 $1 == "class" {
    106 	end_class();
    107 	class = $2;
    108 	printf( "%s\n[\n\tshape=\"record\";\n\tlabel=\"{%s", class, class );
    109 }
    110 
    111 $1 == "enum" || $1 == "bitfield" {
    112 	end_class();
    113 	class = $2;
    114 	printf( "%s\n[\n\tshape=\"record\";\n\tlabel=\"{«%s» %s", class, $1, class );
    115 }
    116 
    117 $1 == "fun" {
    118 	fun( "" );
    119 }
    120 
    121 $1 == "virtual" {
    122 	fun( "[[VIRT]]" );
    123 }
    124 
    125 $1 == "abstract" {
    126 	fun( "[[ABST]]" );
    127 }
    128 
    129 $1 == "var" {
    130 	if( inside != "var" )
    131 	{
    132 		printf( "|" );
    133 		inside = "var";
    134 	}
    135 	printf( "%s %s : %s", scope, $3, $2 );
    136 	if( NF == 4 )
    137 	{
    138 		printf( " = %s", $4 );
    139 	}
    140 	printf( "\l" );
    141 }
    142 
    143 $1 == "val" {
    144 	if( inside != "val" )
    145 	{
    146 		printf( "|" );
    147 		inside = "val";
    148 	}
    149 	printf( "%s %s", scope, $2 );
    150 	printf( "\l" );
    151 }
    152 
    153 $1 == "inherit" {
    154 	end_class();
    155 	relation( $2, $3, "none", "empty" );
    156 	close_relation();
    157 }
    158 
    159 $1 == "associate" {
    160 	end_class();
    161 	relation( $2, $3, "none", "vee", $4, $5 );
    162 	close_relation();
    163 }
    164 
    165 $1 == "aggreg" {
    166 	end_class();
    167 	relation( $2, $3, "odiamond", "vee", $4, $5 );
    168 	close_relation();
    169 }
    170 
    171 $1 == "compose" {
    172 	end_class();
    173 	relation( $2, $3, "diamond", "vee", $4, $5 );
    174 	close_relation();
    175 }
    176 
    177 $1 == "depends" {
    178 	depends( "" );
    179 }
    180 
    181 $1 == "friend" {
    182 	depends( "friend" );
    183 }
    184 
    185 $1 == "package" {
    186 	end_class();
    187 	printf( "subgraph cluster%s { label = \"%s\" ", $2, $2 );
    188 }
    189 
    190 $1 == "endpackage" {
    191 	end_class();
    192 	printf( "}" );
    193 }
    194 
    195 END {
    196 	end_class();
    197 	printf( "}\n" );
    198 }