use of com.mitchellbosecke.pebble.lexer.Token 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;
}
use of com.mitchellbosecke.pebble.lexer.Token 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);
}
Aggregations