in graveyard/tools/org/apache/axis/tools/common/InputCppSourceCode.java [30:262]
public InputCppSourceCode(BufferedReader br, String name) throws Exception {
this.name = name;
String s = null;
StringBuffer buff = new StringBuffer();
for (int i = 1;; i++) {
try {
s = br.readLine();
} catch (Exception e) {
System.err.println(
"Ignoring exception thrown parsing file "
+ name
+ " line number "
+ i);
e.printStackTrace();
break;
}
if (s == null)
break;
buff.append(s + "\n");
}
String str = buff.toString();
// TODO: When checking for rest.startsWith("struct") should
// check the next letter after struct is not alphanumeric otherwise
// we'll get false matches on a function called structify() for
// instance. Also applies to enum, union, public, typedef, etc
String rest, text = "";
int scopedepth = 0;
String scope = "public";
String currentClass = null;
for (int idx = 0; idx < str.length(); /* No idx++ */
) {
rest = str.substring(idx);
if (Character.isWhitespace(rest.charAt(0))) {
int ridx = 0;
while (ridx < rest.length()
&& Character.isWhitespace(rest.charAt(ridx)))
ridx++;
text = rest.substring(0, ridx);
FilePart fp = new FilePart(text, FilePart.WHITESPACE);
parts.add(fp);
idx += ridx;
} else if (rest.startsWith("/*")) {
int ridx = rest.indexOf("*/"); // Don't use Utils here
text = str.substring(idx, idx + ridx + 2);
FilePart fp = new FilePart(text, FilePart.COMMENT);
parts.add(fp);
idx += text.length();
} else if (rest.startsWith("//")) {
text = str.substring(idx, idx + rest.indexOf("\n"));
FilePart fp = new FilePart(text, FilePart.COMMENT);
parts.add(fp);
idx += text.length();
} else if (rest.startsWith("#")) {
int ridx = rest.indexOf("\n");
char c = rest.charAt(ridx - 1);
while (-1 != ridx && '\\' == c) {
String rest2 = rest.substring(ridx + 1);
ridx += rest2.indexOf("\n") + 1;
c = rest.charAt(ridx - 1);
}
text = str.substring(idx, idx + ridx);
FilePart fp = new FilePart(text, FilePart.DIRECTIVE);
parts.add(fp);
idx += text.length();
} else if (rest.startsWith("}")) {
if (scopedepth <= 0)
Utils.rude(
"Braces do not match",
name,
lineNo(str, idx),
rest.substring(0, rest.indexOf("\n")));
else
scopedepth--;
// TODO: better checking that this brace really ends the class
if (0 == scopedepth)
currentClass = null;
scope = "public";
parts.add(new FilePart("}", FilePart.ENDSCOPE));
idx++;
} else if (rest.startsWith(";")) {
parts.add(new FilePart(";", FilePart.FIELD));
idx++;
} else if (
!Character.isLetter(rest.charAt(0))
&& '~' != rest.charAt(0)
&& '_' != rest.charAt(0)) {
Utils.rude(
"Lines must start with a letter ",
name,
lineNo(str, idx),
rest.substring(0, rest.indexOf("\n")));
} else if (MacroPart.isAMacro(rest)) {
MacroPart mp = MacroPart.create(rest);
parts.add(mp);
idx += mp.length();
} else if (beginsScope(rest)) {
scopedepth++;
text = rest.substring(0, Utils.indexOf(rest, "{") + 1);
FilePart fp = new FilePart(text, FilePart.BEGINSCOPE);
parts.add(fp);
idx += text.length();
if (Utils.startsWith(text, "class")) {
// TODO: cope with comments here
// TODO: split out classes into a ClassPart
StringTokenizer st =
new StringTokenizer(text, Utils.whitespace + ":");
st.nextToken(); // step over "class"
while (st.hasMoreTokens()) {
String word = st.nextToken();
if (Configuration.isAttribute(word))
continue;
currentClass = word;
break;
}
}
} else if (isEnumOrUnion(rest)) {
int ridx = Utils.findMatching(rest, '{', '}') + 1;
String rest2 = rest.substring(ridx);
ridx = idx + ridx + Utils.indexOf(rest2, ';') + 1;
text = str.substring(idx, ridx);
FilePart fp = new FilePart(text, FilePart.ENUM);
parts.add(fp);
idx += text.length();
} else if (
scopedepth > 0
&& (rest.startsWith("public")
|| rest.startsWith("protected")
|| rest.startsWith("private"))) {
int colon = rest.indexOf(":");
if (-1 == colon)
Utils.rude(
"No colon found after public or private ",
name,
lineNo(str, idx),
rest.substring(0, rest.indexOf("\n")));
scope = str.substring(idx, idx + colon);
text = str.substring(idx, idx + colon + 1);
FilePart fp = new FilePart(text, FilePart.CLASSATTRIBUTE);
parts.add(fp);
idx += text.length();
} else if (Utils.startsWith(rest, "typedef")) {
int semicolon = Utils.indexOf(rest, ';');
int brace = Utils.indexOf(rest, '{');
if (-1 == semicolon)
Utils.rude(
"No semicolon found after typedef",
name,
lineNo(str, idx),
rest.substring(0, rest.indexOf("\n")));
if (-1 == brace || semicolon < brace) {
// Simple typedef
text = str.substring(idx, idx + semicolon + 1);
} else {
// Typedef of a struct, etc
int endbrace = Utils.findMatching(rest, '{', '}');
String rest2 = rest.substring(endbrace);
semicolon = Utils.indexOf(rest2, ';');
text = str.substring(idx, idx + endbrace + semicolon + 1);
}
FilePart fp = new FilePart(text, FilePart.TYPEDEF);
parts.add(fp);
idx += text.length();
} else {
if (isMethod(rest)) {
int brace = Utils.indexOf(rest, '{');
Signature signature =
new Signature(str.substring(idx, idx + brace));
if (signature.failed())
Utils.rude(
"Signature parsing failed",
name,
lineNo(str, idx),
signature.getOriginal());
if (null != currentClass
&& null == signature.getClassName())
signature.setClassName(currentClass);
signature.setScope(scope);
String body = rest.substring(brace);
int endBrace = Utils.findMatching(body, '{', '}');
body = body.substring(0, endBrace + 1);
int endIdx =
idx + signature.originalLength() + body.length();
text = str.substring(idx, endIdx);
MethodPart mp =
new MethodPart(text, signature, body);
parts.add(mp);
idx += text.length();
} else if (isField(rest)) {
int semicolon = Utils.indexOf(rest, ';');
text = str.substring(idx, idx + semicolon + 1);
FilePart fp = new FilePart(text, FilePart.FIELD);
parts.add(fp);
idx += text.length();
} else if (isPrototype(rest)) {
int semicolon = Utils.indexOf(rest, ';');
text = str.substring(idx, idx + semicolon + 1);
PrototypePart pp = new PrototypePart(text, currentClass);
pp.setScope(scope);
parts.add(pp);
idx += text.length();
} else {
//TODO other file parts here - not sure if there are any others?
Utils.rude(
"Unrecognised file part",
name,
lineNo(str, idx),
rest.substring(0, rest.indexOf("\n")));
} // end if
} // end if
} // end for
}