use of com.google.template.soy.base.internal.SoyFileSupplier in project closure-templates by google.
the class TemplateSubject method doParse.
private ErrorReporter doParse() {
SoyFileSupplier sourceFile = SoyFileSupplier.Factory.create("{namespace test}\n" + "{template .foo}\n" + actual() + "\n" + "{/template}", SoyFileKind.SRC, "example.soy");
ErrorReporter errorReporter = ErrorReporter.create(ImmutableMap.of(sourceFile.getFilePath(), sourceFile));
SoyFileSetNode fileSet = SoyFileSetParserBuilder.forSuppliers(sourceFile).errorReporter(errorReporter).parse().fileSet();
fileNode = fileSet.numChildren() == 1 ? fileSet.getChild(0) : null;
return errorReporter;
}
use of com.google.template.soy.base.internal.SoyFileSupplier in project closure-templates by google.
the class ErrorReporterImpl method getSourceLine.
/**
* Returns a snippet of source code surrounding the given {@link SourceLocation}, or {@link
* Optional#absent()} if source code is unavailable. (This happens, for example, when anyone uses
* {@link SourceLocation#UNKNOWN}, which is why no one should use it.)
*/
Optional<String> getSourceLine(SourceLocation sourceLocation) {
// Try to find a snippet of source code associated with the exception and print it.
SoyFileSupplier supplier = filePathsToSuppliers.get(sourceLocation.getFilePath());
if (supplier == null) {
return Optional.absent();
}
String result;
try (BufferedReader reader = new BufferedReader(supplier.open())) {
// Line numbers are 1-indexed
for (int linenum = 1; linenum < sourceLocation.getBeginLine(); ++linenum) {
// Skip preceding lines
reader.readLine();
}
// returns null on EOF
result = reader.readLine();
} catch (IOException ioe) {
return Optional.absent();
}
return Optional.fromNullable(result);
}
use of com.google.template.soy.base.internal.SoyFileSupplier in project closure-templates by google.
the class SoyFileSetParser method parseWithVersions.
/**
* Parses a set of Soy files, returning a structure containing the parse tree and template
* registry.
*/
private ParseResult parseWithVersions() throws IOException {
IdGenerator nodeIdGen = (cache() != null) ? cache().getNodeIdGenerator() : new IncrementingIdGenerator();
SoyFileSetNode soyTree = new SoyFileSetNode(nodeIdGen.genId(), nodeIdGen);
boolean filesWereSkipped = false;
// generator but fail to lock on it. Eliminate the id system to avoid this whole issue.
synchronized (nodeIdGen) {
// Avoid using the same ID generator in multiple threads.
for (SoyFileSupplier fileSupplier : soyFileSuppliers().values()) {
SoyFileSupplier.Version version = fileSupplier.getVersion();
VersionedFile cachedFile = cache() != null ? cache().get(fileSupplier.getFilePath(), version) : null;
SoyFileNode node;
if (cachedFile == null) {
node = parseSoyFileHelper(fileSupplier, nodeIdGen, typeRegistry());
// a malformed parse tree.
if (node == null) {
filesWereSkipped = true;
continue;
}
// Run passes that are considered part of initial parsing.
passManager().runSingleFilePasses(node, nodeIdGen);
// Run passes that check the tree.
if (cache() != null) {
cache().put(fileSupplier.getFilePath(), VersionedFile.of(node, version));
}
} else {
node = cachedFile.file();
}
soyTree.addChild(node);
}
TemplateRegistry registry;
// Run passes that check the tree iff we successfully parsed every file.
if (!filesWereSkipped) {
registry = passManager().runWholeFilesetPasses(soyTree);
} else {
registry = new TemplateRegistry(soyTree, errorReporter());
}
return ParseResult.create(soyTree, registry);
}
}
Aggregations