}
}, options, returnTokenList);
}
public Conditional<LexerResult> run(VALexer.LexerFactory lexerFactory, ILexerOptions options, boolean returnTokenList) throws LexerException, IOException {
VALexer pp = lexerFactory.create(options.getSmallFeatureModel());
for (Warning w : options.getWarnings())
pp.addWarning(w);
for (Feature f : options.getFeatures())
pp.addFeature(f);
PreprocessorListener listener = new PreprocessorListener(pp, options);
pp.setListener(listener);
pp.addMacro("__TYPECHEF__", FeatureExprLib.True());
PrintWriter output = null;
if (options.getLexOutputFile() != null && options.getLexOutputFile().length() > 0) {
output = new PrintWriter(new BufferedWriter(new FileWriter(options.getLexOutputFile())));
pp.openDebugFiles(options.getLexOutputFile());
} else if (options.isLexPrintToStdout())
output = new PrintWriter(new OutputStreamWriter(System.out));
if (options.getLexerPartialConfiguration() != null) {
for (String def : options.getLexerPartialConfiguration().getDefinedFeatures())
pp.addMacro(def, FeatureExprLib.True(), "1");
for (String undef : options.getLexerPartialConfiguration().getUndefinedFeatures())
pp.removeMacro(undef, FeatureExprLib.True());
}
for (Map.Entry<String, String> macro : options.getDefinedMacros().entrySet())
pp.addMacro(macro.getKey(), FeatureExprLib.True(), macro.getValue());
for (String undef : options.getUndefMacros())
pp.removeMacro(undef, FeatureExprLib.True());
for (String sysInclPath : options.getIncludePaths())
pp.addSystemIncludePath(sysInclPath);
for (String quoInclPath : options.getQuoteIncludePath())
pp.addQuoteIncludePath(quoInclPath);
for (String include : options.getIncludedHeaders())
pp.addInput(new VALexer.FileSource(new File(include)));
for (VALexer.LexerInput input : options.getInput())
pp.addInput(input);
if (options.getInput().isEmpty())
pp.addInput(new VALexer.StreamSource(System.in, "<console>"));
if (options.getFeatures().contains(Feature.DEBUG_INCLUDEPATH)) {
System.err.println("#" + "include \"...\" search starts here:");
for (String dir : pp.getQuoteIncludePath())
System.err.println(" " + dir);
System.err.println("#" + "include <...> search starts here:");
for (String dir : pp.getSystemIncludePath())
System.err.println(" " + dir);
System.err.println("End of search list.");
}
LexerError crash = null;
List<LexerToken> resultTokenList = new ArrayList<LexerToken>();
int outputLine = 1;
try {
// TokenFilter tokenFilter = new TokenFilter();
for (; ; ) {
LexerToken tok = pp.getNextToken();
if (tok == null)
break;
if (tok.isEOF())
break;
if (returnTokenList && (!options.isReturnLanguageTokensOnly() || tok.isLanguageToken())) {
resultTokenList.add(tok);
}
if (output != null) {
if (options.isAdjustLineNumbers()) {
//adjust line numbers to .pi file for debugging
String image = tok.getText();
while (image.indexOf('\n') >= 0) {
outputLine++;
image = image.substring(image.indexOf('\n') + 1);
}
tok.setLine(outputLine);
if (options.getLexOutputFile() != null)
tok.setSourceName(options.getLexOutputFile());
}
//write to .pi file
tok.lazyPrint(output);
}
}
} catch (Throwable e) {
Preprocessor.logger.severe(e.toString());
e.printStackTrace(System.err);
pp.printSourceStack(System.err);
crash = new LexerError(e.toString(), "", -1, -1);
} finally {
pp.debugPreprocessorDone();
if (output != null)
output.flush();
if (output != null && !options.isLexPrintToStdout())
output.close();
}