Search in sources :

Example 1 with ParserException

use of com.mitchellbosecke.pebble.error.ParserException in project Orchid by JavaEden.

the class BaseTagParser method parseParams.

protected Map<String, Expression<?>> parseParams(String[] parameters, boolean enforceParamMap, Class<? extends OptionsHolder> paramsClass, TokenStream stream, Parser parser) throws ParserException {
    // Get list of available parameter names
    OptionsExtractor extractor = contextProvider.get().resolve(OptionsExtractor.class);
    List<String> remainingParameters = new ArrayList<>(extractor.getOptionNames(paramsClass));
    // parameter expressions will be added here
    Map<String, Expression<?>> paramExpressionMap = new HashMap<>();
    if (!isParameterEnd(stream)) {
        if (!EdenUtils.isEmpty(parameters) && !stream.current().test(Token.Type.NAME)) {
            // parse the parameters sequentially
            int i = 0;
            while (i < parameters.length && !isParameterEnd(stream) && !stream.current().test(Token.Type.NAME)) {
                Expression<?> parsedExpression = parser.getExpressionParser().parseExpression();
                paramExpressionMap.put(parameters[i], parsedExpression);
                i++;
            }
        }
        if (stream.current().test(Token.Type.NAME)) {
            // parse parameters as map of key=value pairs
            while (!isParameterEnd(stream)) {
                Token paramNameToken = stream.expect(Token.Type.NAME);
                final Optional<String> foundKey;
                if (enforceParamMap) {
                    foundKey = remainingParameters.stream().filter(key -> key.equalsIgnoreCase(paramNameToken.getValue())).findAny();
                } else {
                    foundKey = Optional.of(paramNameToken.getValue());
                }
                if (foundKey.isPresent()) {
                    String paramKey = foundKey.get();
                    if (enforceParamMap) {
                        remainingParameters.remove(paramKey);
                    }
                    stream.expect(Token.Type.PUNCTUATION, "=");
                    Expression<?> parsedExpression = parser.getExpressionParser().parseExpression();
                    paramExpressionMap.put(paramKey, parsedExpression);
                } else {
                    throw new ParserException(null, Clog.format("Could not parse parameter {}.", paramNameToken.getValue()), stream.current().getLineNumber(), "");
                }
            }
        }
    }
    return paramExpressionMap;
}
Also used : ParserException(com.mitchellbosecke.pebble.error.ParserException) RenderableNodeExpression(com.mitchellbosecke.pebble.node.expression.RenderableNodeExpression) Expression(com.mitchellbosecke.pebble.node.expression.Expression) FilterExpression(com.mitchellbosecke.pebble.node.expression.FilterExpression) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Token(com.mitchellbosecke.pebble.lexer.Token) OptionsExtractor(com.eden.orchid.api.options.OptionsExtractor)

Example 2 with ParserException

use of com.mitchellbosecke.pebble.error.ParserException 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

ParserException (com.mitchellbosecke.pebble.error.ParserException)2 Token (com.mitchellbosecke.pebble.lexer.Token)2 Expression (com.mitchellbosecke.pebble.node.expression.Expression)2 OptionsExtractor (com.eden.orchid.api.options.OptionsExtractor)1 TokenStream (com.mitchellbosecke.pebble.lexer.TokenStream)1 FilterExpression (com.mitchellbosecke.pebble.node.expression.FilterExpression)1 RenderableNodeExpression (com.mitchellbosecke.pebble.node.expression.RenderableNodeExpression)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1