summaryrefslogtreecommitdiff
path: root/web/noweb/contrib/conrado/d2tex
blob: 5b807b7ce0a9d5e193566505ded5b30bd8f17e62 (plain)
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
#! /bin/sh
KEYGEN=$(mktemp)
trap "rm -f $KEYGEN; exit 1" 1 2 3 15
cat > $KEYGEN <<END_OF_FILE
COMMENT#1
ASSERT#1
PROGRAM
ENDPROGRAM
USES
ENDUSES
MODULE
ENDMODULE
SPECIFICATION
ENDSPECIFICATION
IMPLEMENTATION
ENDIMPLEMENTATION
IMPORT
ENDIMPORT
TYPE
ENDTYPE
VAR
ENDVAR
CONST
ENDCONST
ARRAY
RECORD
ENDRECORD
BEGIN
IF
ELSE
ENDIF
THEN
SKIP
WHILE
ENDWHILE
DO
DDO
ENDDO
FORALL
FOR
ENDFOR
ENDFORALL
PARALLEL
REPEAT
UNTIL
AND
OR
NOT
CAT
OF
IN
DIV
MOD
PROCEDURE
ENDPROCEDURE
FUNCTION
ENDFUNCTION
RETURNS
INP
OUTP
INOUTP
PRIVATE
@@@
END_OF_FILE

nawk '
BEGIN           { code=0 ; quoting=0 ; inside_comm=0 
                  while ((getline kw < "'"$KEYGEN"'") >0)
                    keywords[kw]++
                }
/^@begin code/  { 
   printf "@literal \\begin{code}{%s}\\let\\maybehbox=\\hbox\n", substr($0, 13)
   code=1; next
}
/^@end code/    { code=0 ; printf "@literal \\end{code}\n"; next}
/^@quote$/      { quoting = 1 }
/^@endquote$/   { quoting = 0 }
/^@text /       { if (code) { print_code(substr($0, 7)) }
                  else      { print }
                  next
                }
{print}
function print_code(line) {
        temp = line; 
        comm = "";
        if (temp ~ /\/\*/) 
	   { r = index(temp, "/*"); 
             inside_comm = 1; comm = "\\COMMENT{" substr(temp, r+3);
             if (comm ~ /\*\//) { inside_comm = 0; comm = comm "}"; temp = ""; }
           }
        if (temp ~ /\*\//)
           { comm = temp "}"; temp = ""; inside_comm = 0; }

        sub(/\*\//,"",comm);
        sub(/\/\*.*/,"",temp);

        if (temp != "" && inside_comm == 0)
	{
         temp = operators(temp);
         temp = substitute_keywords(temp);
         temp = mark_vars_types(temp);
        }
        line = temp comm;
        print "@literal " line;
}

function substitute_keywords(s, kw) {
  for (kw in keywords)
    if (s ~ kw)
     {
	gsub("(^|[ \\t]+)" kw "($|[ \\t]+)", " \\" kw " ", s);   
    	gsub("(^|[ \\t]+)" kw ";", " \\" kw ";", s);   
    	gsub("(^|[ \\t]+)" kw "\\.", " \\" kw ".", s);   
    	gsub("[(]" kw, "(\\" kw, s);
	gsub("," kw, ",\\" kw, s);
     }
  return s
}

function mark_vars_types(s  ) {
     gsub(/\\OF[ \t\n]*[a-zA-Z_]+/,"|&|", s);
     gsub(/\\RETURN[ \t\n]*[a-zA-Z_]+/,"|&|", s);
     gsub(/[a-zA-Z_]+[ \t\n]*=[ \t\n](\\RECORD|\\ARRAY)/,"|&|", s);
     gsub(/[a-zA-Z_]+[ \t\n]*=[ \t\n]\\{/,"|&|",s);
     gsub(/:[\t\n ]*[a-zA-Z_]+/, "|&|" ,s);
     gsub(/[a-zA-Z_]+[ \\t\\n]*\(/,"|&|",s);
     gsub(/\(\|/, "|(", s);
     gsub(/\|:[ \t\n]*/, ":|", s);
     gsub(/\|\\de[ \t\n]*/, "\\de |", s);
     gsub(/\|\\RETURN[ \t\n]*/, "\\RETURN |", s);
     gsub(/\=[\t\n ]*\\ARRAY\|/, "| = \\ARRAY", s);
     gsub(/\=[\t\n ]*\\RECORD\|/, "| = \\RECORD", s);
     gsub(/\=[\t\n ]*\\{\|/, "| = \\{", s);
     return s
}

function operators(s ) {
     gsub(/ >= /," \\ge ", s);
     gsub(/ <= /, " \\le ", s);
     gsub(/ != /, " \\not= ", s);
     return s;
}
' "$@"
rm -f $KEYGEN