Search in sources :

Example 1 with SoyFileSupplier

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;
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSupplier(com.google.template.soy.base.internal.SoyFileSupplier)

Example 2 with SoyFileSupplier

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);
}
Also used : BufferedReader(java.io.BufferedReader) SoyFileSupplier(com.google.template.soy.base.internal.SoyFileSupplier) IOException(java.io.IOException)

Example 3 with SoyFileSupplier

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);
    }
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) IdGenerator(com.google.template.soy.base.internal.IdGenerator) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) SoyFileSupplier(com.google.template.soy.base.internal.SoyFileSupplier) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) VersionedFile(com.google.template.soy.shared.SoyAstCache.VersionedFile)

Aggregations

SoyFileSupplier (com.google.template.soy.base.internal.SoyFileSupplier)3 IdGenerator (com.google.template.soy.base.internal.IdGenerator)1 IncrementingIdGenerator (com.google.template.soy.base.internal.IncrementingIdGenerator)1 ErrorReporter (com.google.template.soy.error.ErrorReporter)1 VersionedFile (com.google.template.soy.shared.SoyAstCache.VersionedFile)1 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)1 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)1 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1