String lastLine = null;
while (true) {
try {
line = lineNumberReader.readLine();
} catch (IOException e) {
throw new PreProcessorException("Error while reading next line"
+ getLineNoString(lineNumberReader), e);
}
if (line == null) {
break;
} else if (line.startsWith("#")) {
line = line.trim();
}
if (lastLine != null) {
line = lastLine + " " + line;
lastLine = null;
}
if (!ifdefValue.empty()
&& !((Boolean) ifdefValue.peek()).booleanValue()
&& !line.startsWith("#")) {
continue;
} else if (line.startsWith("#") && line.endsWith("\\")) {
lastLine = line.substring(0, line.lastIndexOf("\\"));
continue;
} else if (line.startsWith("#include") && validLine) {
if (depth < MAX_DEPTH) {
String inc = line.replaceAll("#include", "").trim();
addComment(buffer, line);
InputStream incis = resolveInclude(inc,
getLineNoString(lineNumberReader));
buffer.append(readIdl(incis, depth + 1));
addComment(buffer, "end of " + line);
} else {
throw new PreProcessorException("More than " + MAX_DEPTH
+ " nested #includes are not allowed"
+ getLineNoString(lineNumberReader));
}
} else if (line.startsWith("#define")) {
String def = line.replaceAll("#define", "").trim();
def = def.replaceAll("\"", "");
StringTokenizer tok = new StringTokenizer(def, " ");
if (tok.countTokens() == 1) {
def = tok.nextToken();
if (defs.containsKey(def))
throw new PreProcessorException("Variable " + def
+ " is already defined"
+ getLineNoString(lineNumberReader));
defs.put(def, null);
} else if (tok.countTokens() == 2) {
defs.put(tok.nextToken(), tok.nextToken());
}
addComment(buffer, line);
} else if (line.startsWith("#undef")) {
String def = line.replaceAll("#undef", "").trim();
def = def.replaceAll("\"", "");
if (defs.containsKey(def)) {
defs.remove(def);
} else {
throw new PreProcessorException("Undifined variable " + def
+ getLineNoString(lineNumberReader));
}
addComment(buffer, line);
} else if (line.startsWith("#ifdef")) {
String def = line.replaceAll("#ifdef", "").trim();
def = def.replaceAll("\"", "");
if (defs.containsKey(def)) {
ifdefValue.push(Boolean.TRUE);
} else {
validLine = false;
ifdefValue.push(Boolean.FALSE);
}
elseNotValid = false;
addComment(buffer, line);
} else if (line.startsWith("#ifndef")) {
String def = line.replaceAll("#ifndef", "").trim();
def = def.replaceAll("\"", "");
if (defs.containsKey(def)) {
validLine = false;
ifdefValue.push(Boolean.FALSE);
} else {
ifdefValue.push(Boolean.TRUE);
}
elseNotValid = false;
addComment(buffer, line);
} else if (line.startsWith("#else")) {
if (elseNotValid)
throw new PreProcessorException("Invalid #else preprocessor directive" + getLineNoString(lineNumberReader));
// invert last element
boolean lastval = ((Boolean) ifdefValue.peek()).booleanValue();
ifdefValue.setElementAt(new Boolean(!lastval), ifdefValue.size() - 1);
validLine = isAllTrue(ifdefValue);
elseNotValid = true;
addComment(buffer, line);
} else if (line.startsWith("#endif")) {
if (ifdefValue.empty())
throw new PreProcessorException("Invalid #endif preprocessor directive" + getLineNoString(lineNumberReader));
ifdefValue.pop();
validLine = isAllTrue(ifdefValue);
elseNotValid = false;
addComment(buffer, line);
} else if (line.startsWith("#")) {
// TODO: log
System.out
.println("Ignoring unsupported preprocessor directive "
+ line + getLineNoString(lineNumberReader));
} else if (validLine) {
buffer.append(line);
buffer.append('\n');
// System.out.println(line);
}
}
if (!ifdefValue.empty()) {
throw new PreProcessorException(
"One or more #ifdef/#ifndef preprocessor directives are not properly closed" + getLineNoString(lineNumberReader));
}
try {
lineNumberReader.close();