package de.javagl.code.translation;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.parser.DefaultLogService;
import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IncludeFileContentProvider;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.internal.core.parser.scanner.InternalFileContent;
import org.eclipse.cdt.internal.core.parser.scanner.InternalFileContentProvider;
import org.eclipse.core.runtime.CoreException;
class TranslationUnitCreator
{
// TODO: Included files have to be "inlined" into the source
// code, because otherwise the comment indexing produces
// great mess. The combined source code has to be written
// to a temp file, whose path is given instead of this:
private static final String COMBINED_FILE_PATH = "";
private static class LocalIncludeFileContentProvider extends InternalFileContentProvider
{
@Override
public InternalFileContent getContentForInclusion(String path)
{
if (!getInclusionExists(path))
{
return null;
}
String code = readFileAsString(path);
FileContent fileContent = FileContent.create(path, code.toCharArray());
return (InternalFileContent)fileContent;
}
@Override
public InternalFileContent getContentForInclusion(IIndexFileLocation indexedFileLocation, String astPath)
{
// Seems to be not used anyhow...
return (InternalFileContent)FileContent.create(indexedFileLocation);
}
}
public static IASTTranslationUnit createTranslationUnit(String code, String... includePaths) throws CoreException
{
Map<String, String> definedSymbols = new HashMap<String, String>();
IScannerInfo scannerInfo = new ScannerInfo(definedSymbols, includePaths);
IncludeFileContentProvider includeFileContentProvider =
new LocalIncludeFileContentProvider();
FileContent fileContent = FileContent.create(COMBINED_FILE_PATH, code.toCharArray());
IParserLogService parserLogService = new DefaultLogService();
IASTTranslationUnit translationUnit =
GPPLanguage.getDefault().getASTTranslationUnit(
fileContent, scannerInfo, includeFileContentProvider,
null, 0, parserLogService);
return translationUnit;
}
/**
* Read the contents of the file with the given name, and return it as a
* String
*
* @param filename The name of the file
* @return The contents of the file
* @throws RuntimeException If the file could not be read
*/
public static String readFileAsString(String filename)
{
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
StringBuilder sb = new StringBuilder();
String line = null;
while (true)
{
line = br.readLine();
if (line == null)
{
break;
}
sb.append(line + "\n");
}
return sb.toString();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
throw new RuntimeException("File not found " + filename, e);
}
catch (IOException e)
{
e.printStackTrace();
throw new RuntimeException("Error while reading file " + filename, e);
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException ex)
{}
}
}
}
}