File: commonDoc.awk

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (198 lines) | stat: -rwxr-xr-x 5,771 bytes parent folder | download | duplicates (7)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/awk -f
#
# This awk script contains common functions that may be used by other scripts.
# Contains functions that help parsing storing and printing API doc comments,
# think of Doxygen or JavaDoc.
# the callback in: rts/ExternalAI/Interface/SSkirmishAICallback.h
# use like this:
# 	awk -f yourScript.awk -f common.awk [additional-params]
# this should work with all flavours of AWK (eg. gawk, mawk, nawk, ...)
#

# NOTE: This functions HAS TO be implemented by the script using this as sort of
#       an include!
# This function has to return true (1) if a doc comment (eg: /** foo bar */)
# can be deleted.
# If there is no special condition you want to apply,
# it should always return true (1),
# cause there are additional mechanism to prevent accidential deleting.
#function canDeleteDocumentation() {
#	return 1;
#}


BEGIN {
	# initialize things
}

# Returns true (1) if the current line is part of a documentation comment
function isInsideDoc() {
	return isInsideDocComment__doc;
}
# Returns true (1) if stored docummentation is available
function hasStoredDoc() {
	return docComLines_num__doc > 0;
}
# Returns an array with doc lines, beginning at index 0
# and ending at index getStoredDocLines()-1
#function getStoredDoc() {
#	return docComLines__doc;
#}
# Returns the number of stored doc lines
#function getStoredDocLines() {
#	return docComLines_num__doc;
#}
function refineTextOfDocLine(rawText__doc) {

	refinedText__doc = rawText__doc;

	# filter out the @brief tag, as it is not supported by JavaDoc
	if (JAVA_MODE) {
		sub(/@brief[ \t]*/, "", refinedText__doc);
	}

	return refinedText__doc;
}

function printStoredDoc(indent__doc) {

	# print the documentation comment only if it is not empty
	if (hasStoredDoc()) {
		print(indent__doc "/**");
		for (l__doc=0; l__doc < docComLines_num__doc; l__doc++) {
			docLine__doc = docComLines__doc[l__doc];
			print(indent__doc " * " docLine__doc);
		}
		print(indent__doc " */");
	}
}

# Stores the current doc lines in a container at a specified index
# This container can then be passed to printFunctionComment_Common() eg.
function storeDocLines(container__doc, index__doc) {

	container__doc[index__doc, "*"] = docComLines_num__doc;
	for (l__doc=0; l__doc < docComLines_num__doc; l__doc++) {
		container__doc[index__doc, l__doc] = refineTextOfDocLine(docComLines__doc[l__doc]);
	}
	docComLines_num__doc = 0;
}

# Prints a saved doc comment to a file with the specified indent
# expects the first doc-line at docsMap__doc[docIndex__doc, 0]
# expects the number of doc-lines in docsMap__doc[docIndex__doc, "*"]
function printFunctionComment_Common(outFile__doc, container__doc, index__doc, indent__doc) {

	numLines__doc = container__doc[index__doc, "*"];
	# print the documentation comment only if it is not empty
	if (numLines__doc > 0) {
		print(indent__doc "/**") >> outFile__doc;
		for (l__doc=0; l__doc < numLines__doc; l__doc++) {
			docLine__doc = container__doc[index__doc, l__doc];
			print(indent__doc " * " docLine__doc) >> outFile__doc;
		}
		print(indent__doc " */") >> outFile__doc;
	}
}

# Returns a string containing the requested, saved doc comment
# expects the first doc-line at docsMap__doc[docIndex__doc, 0]
# expects the number of doc-lines in docsMap__doc[docIndex__doc, "*"]
function getFunctionComment_Common(container__doc, index__doc) {

	funcComment__doc = "";

	numLines__doc = container__doc[index__doc, "*"];
	if (numLines__doc > 0) {
		for (l__doc=0; l__doc < numLines__doc; l__doc++) {
			docLine__doc = container__doc[index__doc, l__doc];
			funcComment__doc = funcComment__doc "\n" docLine__doc;
		}
	}

	return funcComment__doc;
}

# end of doc comment: */
/\*\// {

	if (isInsideDocComment__doc == 1) {
		usefullLinePart__doc = $0;
		sub(/\*\/.*/, "", usefullLinePart__doc);
		sub(/^[ \t]*(\*)?/, "", usefullLinePart__doc);
		sub(/^[ \t]/, "", usefullLinePart__doc);
		usefullLinePart__doc = rtrim(usefullLinePart__doc);
		if (usefullLinePart__doc != "") {
			docComLines__doc[docComLines_num__doc++] = usefullLinePart__doc;
		}
	}
	isInsideDocComment__doc = 0;
}


# inside of doc comment
{
	if (singleLineDocComment__doc == 1) {
		singleLineDocComment__doc = 0;
		isInsideDocComment__doc = 0;
	}

	if (isInsideDocComment__doc == 1) {
		usefullLinePart__doc = $0;
		sub(/^[ \t]*(\*)?/, "", usefullLinePart__doc);
		sub(/^[ \t]/, "", usefullLinePart__doc);
		usefullLinePart__doc = rtrim(usefullLinePart__doc);
		docComLines__doc[docComLines_num__doc++] = usefullLinePart__doc;
	} else {
		if (trim($0) != "") {
			linesWithNoDocComment__doc++;
		}
		# delete the last stored doc comment if it is not applicable to anything
		if (linesWithNoDocComment__doc > 2 && canDeleteDocumentation()) {
			docComLines_num__doc = 0;
		}
	}
}

# beginn of doc comment: /**
/^[ \t]*\/\*\*/ {

	isInsideDocComment__doc = 1;
	docComLines_num__doc = 0;
	linesWithNoDocComment__doc = 0;
	singleLineDocComment__doc = 0;

	usefullLinePart__doc = $0;
	sub(/^[ \t]*\/\*\*/, "", usefullLinePart__doc);
	sub(/^[ \t]/, "", usefullLinePart__doc);
	if (sub(/\*\/.*/, "", usefullLinePart__doc)) {
		isInsideDocComment__doc = 0;
	}
	usefullLinePart__doc = rtrim(usefullLinePart__doc);
	if (usefullLinePart__doc != "") {
		docComLines__doc[docComLines_num__doc++] = usefullLinePart__doc;
	}
}

# beginn of single line doc comment: ///
/^[ \t]*\/\/\// {

	isInsideDocComment__doc = 1;
	docComLines_num__doc = 0;
	linesWithNoDocComment__doc = 0;
	singleLineDocComment__doc = 1;

	usefullLinePart__doc = $0;
	sub(/^[ \t]*\/\/\//, "", usefullLinePart__doc);
	sub(/^[ \t]/, "", usefullLinePart__doc);
	usefullLinePart__doc = rtrim(usefullLinePart__doc);
	if (usefullLinePart__doc != "") {
		docComLines__doc[docComLines_num__doc++] = usefullLinePart__doc;
	}
}


END {
	# finalize things
}