Search in sources :

Example 1 with TokenStream

use of com.mitchellbosecke.pebble.lexer.TokenStream in project Orchid by JavaEden.

the class PebbleCompiler method compile.

@Override
public void compile(OutputStream os, @Nullable OrchidResource resource, String extension, String input, Map<String, Object> data) {
    try {
        PebbleTemplateLoader loader = new PebbleTemplateLoader(contextProvider.get(), contextProvider.get().getTheme());
        PebbleEngine engine = new PebbleEngine.Builder().loader(loader).executorService(executor).extension(extensionArray).methodAccessValidator(new NoOpMethodAccessValidator()).newLineTrimming(false).tagCache(tagCache).templateCache(templateCache).build();
        engine.getExtensionRegistry().getAttributeResolver().add(new GetMethodAttributeResolver());
        LexerImpl lexer = new LexerImpl(engine.getSyntax(), engine.getExtensionRegistry().getUnaryOperators().values(), engine.getExtensionRegistry().getBinaryOperators().values());
        TokenStream tokenStream = lexer.tokenize(new StringReader(input), "");
        Parser parser = new ParserImpl(engine.getExtensionRegistry().getUnaryOperators(), engine.getExtensionRegistry().getBinaryOperators(), engine.getExtensionRegistry().getTokenParsers(), new ParserOptions());
        RootNode root = parser.parse(tokenStream);
        PebbleTemplateImpl compiledTemplate = new PebbleTemplateImpl(engine, root, "");
        for (NodeVisitorFactory visitorFactory : engine.getExtensionRegistry().getNodeVisitors()) {
            visitorFactory.createVisitor(compiledTemplate).visit(root);
        }
        ByteArrayOutputStream os1 = new ByteArrayOutputStream();
        Writer writer = new OutputStreamWriter(os1, StandardCharsets.UTF_8);
        compiledTemplate.evaluate(writer, data);
        writer.close();
        os.write(os1.toByteArray());
    } catch (PebbleException e) {
        OrchidExtensionsKt.logSyntaxError(input, extension, e.getLineNumber(), 0, e.getMessage(), e.getCause());
    } catch (Exception e) {
        Clog.e("Error rendering Pebble template (see template source below)", e);
        Clog.e(input);
        contextProvider.get().diagnosisMessage(() -> ExceptionUtils.getStackTrace(e));
    }
}
Also used : RootNode(com.mitchellbosecke.pebble.node.RootNode) TokenStream(com.mitchellbosecke.pebble.lexer.TokenStream) NodeVisitorFactory(com.mitchellbosecke.pebble.extension.NodeVisitorFactory) ParserOptions(com.mitchellbosecke.pebble.parser.ParserOptions) PebbleTemplateImpl(com.mitchellbosecke.pebble.template.PebbleTemplateImpl) NoOpMethodAccessValidator(com.mitchellbosecke.pebble.attributes.methodaccess.NoOpMethodAccessValidator) ParserImpl(com.mitchellbosecke.pebble.parser.ParserImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PebbleException(com.mitchellbosecke.pebble.error.PebbleException) PebbleEngine(com.mitchellbosecke.pebble.PebbleEngine) Parser(com.mitchellbosecke.pebble.parser.Parser) PebbleException(com.mitchellbosecke.pebble.error.PebbleException) StringReader(java.io.StringReader) LexerImpl(com.mitchellbosecke.pebble.lexer.LexerImpl) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 2 with TokenStream

use of com.mitchellbosecke.pebble.lexer.TokenStream in project Orchid by JavaEden.

the class ContentTagParser method parse.

public RenderableNode parse(Token token, Parser parser) throws ParserException {
    TokenStream stream = parser.getStream();
    int lineNumber = token.getLineNumber();
    // skip over the tag name token
    stream.next();
    // parameter expressions will be added here
    paramExpressionMap = parseParams(parameters, true, tagClass, stream, parser);
    List<Expression<?>> bodyFilters = parseBodyFilters(stream, parser);
    stream.expect(Token.Type.EXECUTE_END);
    tagBodyExpression = parseBody(bodyFilters, name, stream, parser);
    return new PebbleWrapperTemplateTag.TemplateTagNode(lineNumber, this);
}
Also used : TokenStream(com.mitchellbosecke.pebble.lexer.TokenStream) Expression(com.mitchellbosecke.pebble.node.expression.Expression)

Example 3 with TokenStream

use of com.mitchellbosecke.pebble.lexer.TokenStream in project Orchid by JavaEden.

the class DynamicTabbedTagParser method parse.

public RenderableNode parse(Token token, Parser parser) throws ParserException {
    TokenStream stream = parser.getStream();
    int lineNumber = token.getLineNumber();
    paramExpressionMap = parseParams(tagParameters, true, tagClass, stream, parser);
    stream.expect(Token.Type.EXECUTE_END);
    tagBodyExpression = parseBody(new ArrayList<>(), name, stream, parser);
    return new PebbleWrapperTemplateTag.TemplateTagNode(lineNumber, this);
}
Also used : TokenStream(com.mitchellbosecke.pebble.lexer.TokenStream) ArrayList(java.util.ArrayList)

Example 4 with TokenStream

use of com.mitchellbosecke.pebble.lexer.TokenStream in project Orchid by JavaEden.

the class TabbedTagParser method parse.

public RenderableNode parse(Token token, Parser parser) throws ParserException {
    TokenStream stream = parser.getStream();
    // skip over the tag name token
    stream.next();
    if (stream.current().test(Token.Type.NAME, "dynamic")) {
        stream.next();
        childParser = new DynamicTabbedTagParser(contextProvider, name, tagParameters, tagClass);
        return childParser.parse(token, parser);
    } else {
        childParser = new NonDynamicTabbedTagParser(contextProvider, name, tagParameters, tagClass, tabParameters, tabClass);
        return childParser.parse(token, parser);
    }
}
Also used : TokenStream(com.mitchellbosecke.pebble.lexer.TokenStream)

Example 5 with TokenStream

use of com.mitchellbosecke.pebble.lexer.TokenStream in project Orchid by JavaEden.

the class NonDynamicTabbedTagParser method parse.

public RenderableNode parse(Token token, Parser parser) throws ParserException {
    TokenStream stream = parser.getStream();
    int lineNumber = token.getLineNumber();
    tagBodyParams = new LinkedHashMap<>();
    tagBodyExpressions = new LinkedHashMap<>();
    // get Tag params
    paramExpressionMap = parseParams(tagParameters, true, tagClass, stream, parser);
    // get Tag filters, which are applied to all tabs
    bodyFilters = parseBodyFilters(stream, parser);
    stream.expect(Token.Type.EXECUTE_END);
    // parse tabs
    while (true) {
        // optionally allow line-breaks between tabs
        if (stream.current().test(Token.Type.TEXT)) {
            // do not allow text to be outside of a tab
            Token textToken = stream.expect(Token.Type.TEXT);
            if (!EdenUtils.isEmpty(textToken.getValue())) {
                throw new ParserException(null, "Error parsing " + name + " tabbed tag: Text must be included within a tab.", lineNumber, "");
            }
        }
        // start parsing tab
        stream.expect(Token.Type.EXECUTE_START);
        // this is actually the final Tag's closing tag, so exit the loop
        if (stream.current().test(Token.Type.NAME, "end" + name))
            break;
        // get tab name
        Token tabNameToken = stream.expect(Token.Type.NAME);
        String tabName = tabNameToken.getValue();
        // make sure we only have one of each tab key
        if (tagBodyExpressions.containsKey(tabName)) {
            throw new ParserException(null, "Error parsing " + name + " tabbed tag: Tab " + tabName + " already used.", lineNumber, "");
        }
        // get tab params
        Map<String, Expression<?>> tabParams = parseParams(tabParameters, true, tabClass, stream, parser);
        tagBodyParams.put(tabName, tabParams);
        // get tab filters
        List<Expression<?>> tabBodyFilters = parseBodyFilters(stream, parser);
        List<Expression<?>> fullTabFilters = Stream.concat(tabBodyFilters.stream(), bodyFilters.stream()).collect(Collectors.toList());
        // end opening tab
        stream.expect(Token.Type.EXECUTE_END);
        // subcompile until the end of the tab, which closes this tab
        Expression<?> tabBody = parseBody(fullTabFilters, tabName, stream, parser);
        tagBodyExpressions.put(tabName, tabBody);
    }
    stream.expect(Token.Type.NAME, "end" + name);
    stream.expect(Token.Type.EXECUTE_END);
    return new PebbleWrapperTemplateTag.TemplateTagNode(lineNumber, this);
}
Also used : ParserException(com.mitchellbosecke.pebble.error.ParserException) TokenStream(com.mitchellbosecke.pebble.lexer.TokenStream) Expression(com.mitchellbosecke.pebble.node.expression.Expression) Token(com.mitchellbosecke.pebble.lexer.Token)

Aggregations

TokenStream (com.mitchellbosecke.pebble.lexer.TokenStream)6 Expression (com.mitchellbosecke.pebble.node.expression.Expression)2 PebbleEngine (com.mitchellbosecke.pebble.PebbleEngine)1 NoOpMethodAccessValidator (com.mitchellbosecke.pebble.attributes.methodaccess.NoOpMethodAccessValidator)1 ParserException (com.mitchellbosecke.pebble.error.ParserException)1 PebbleException (com.mitchellbosecke.pebble.error.PebbleException)1 NodeVisitorFactory (com.mitchellbosecke.pebble.extension.NodeVisitorFactory)1 LexerImpl (com.mitchellbosecke.pebble.lexer.LexerImpl)1 Token (com.mitchellbosecke.pebble.lexer.Token)1 RootNode (com.mitchellbosecke.pebble.node.RootNode)1 Parser (com.mitchellbosecke.pebble.parser.Parser)1 ParserImpl (com.mitchellbosecke.pebble.parser.ParserImpl)1 ParserOptions (com.mitchellbosecke.pebble.parser.ParserOptions)1 PebbleTemplateImpl (com.mitchellbosecke.pebble.template.PebbleTemplateImpl)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1