Skip to content

Commit

Permalink
Qute: add parser hook to set up parser before it parses
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Mar 10, 2020
1 parent 7f470ab commit 7a67cb3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ public final class EngineBuilder {
private final List<TemplateLocator> locators;
private final List<ResultMapper> resultMappers;
private Function<String, SectionHelperFactory<?>> sectionHelperFunc;
private final List<ParserHook> parserHooks;

EngineBuilder() {
this.sectionHelperFactories = new HashMap<>();
this.valueResolvers = new ArrayList<>();
this.namespaceResolvers = new ArrayList<>();
this.locators = new ArrayList<>();
this.resultMappers = new ArrayList<>();
this.parserHooks = new ArrayList<>();
}

public EngineBuilder addSectionHelper(SectionHelperFactory<?> factory) {
Expand Down Expand Up @@ -115,6 +117,11 @@ public EngineBuilder addLocator(TemplateLocator locator) {
return this;
}

public EngineBuilder addParserHook(ParserHook parserHook) {
this.parserHooks.add(parserHook);
return this;
}

/**
*
* @param resultMapper
Expand All @@ -132,7 +139,7 @@ public EngineBuilder computeSectionHelper(Function<String, SectionHelperFactory<

public Engine build() {
return new EngineImpl(sectionHelperFactories, valueResolvers, namespaceResolvers, locators, resultMappers,
sectionHelperFunc);
sectionHelperFunc, parserHooks);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ class EngineImpl implements Engine {
private final List<ResultMapper> resultMappers;
private final PublisherFactory publisherFactory;
private final AtomicLong idGenerator = new AtomicLong(0);
private final List<ParserHook> parserHooks;

EngineImpl(Map<String, SectionHelperFactory<?>> sectionHelperFactories, List<ValueResolver> valueResolvers,
List<NamespaceResolver> namespaceResolvers, List<TemplateLocator> locators,
List<ResultMapper> resultMappers, Function<String, SectionHelperFactory<?>> sectionHelperFunc) {
List<ResultMapper> resultMappers, Function<String, SectionHelperFactory<?>> sectionHelperFunc,
List<ParserHook> parserHooks) {
this.sectionHelperFactories = new HashMap<>(sectionHelperFactories);
this.valueResolvers = sort(valueResolvers);
this.namespaceResolvers = ImmutableList.copyOf(namespaceResolvers);
Expand All @@ -64,12 +66,21 @@ class EngineImpl implements Engine {
}
this.resultMappers = sort(resultMappers);
this.sectionHelperFunc = sectionHelperFunc;
this.parserHooks = parserHooks;
}

@Override
public Template parse(String content, Variant variant) {
String generatedId = generateId();
return new Parser(this).parse(new StringReader(content), Optional.ofNullable(variant), generatedId, generatedId);
return newParser(null).parse(new StringReader(content), Optional.ofNullable(variant), generatedId, generatedId);
}

private Parser newParser(String id) {
Parser parser = new Parser(this);
for (ParserHook parserHook : parserHooks) {
parserHook.beforeParsing(parser, id);
}
return parser;
}

@Override
Expand Down Expand Up @@ -132,7 +143,7 @@ private Template load(String id) {
Optional<TemplateLocation> location = locator.locate(id);
if (location.isPresent()) {
try (Reader r = location.get().read()) {
return new Parser(this).parse(ensureBufferedReader(r), location.get().getVariant(), id, generateId());
return newParser(id).parse(ensureBufferedReader(r), location.get().getVariant(), id, generateId());
} catch (IOException e) {
LOGGER.warn("Unable to close the reader for " + id, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Simple non-reusable parser.
*/
class Parser implements Function<String, Expression> {
class Parser implements Function<String, Expression>, ParserHelper {

private static final Logger LOGGER = Logger.getLogger(Parser.class);
private static final String ROOT_HELPER_NAME = "$root";
Expand Down Expand Up @@ -715,4 +715,10 @@ public String toString() {

}

@Override
public void addParameter(String name, String type) {
// {@org.acme.Foo foo}
Scope currentScope = scopeStack.peek();
currentScope.put(name, Expressions.TYPE_INFO_SEPARATOR + type + Expressions.TYPE_INFO_SEPARATOR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.quarkus.qute;

public interface ParserHelper {
public void addParameter(String name, String type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.qute;

public interface ParserHook {

void beforeParsing(ParserHelper parser, String id);

}

0 comments on commit 7a67cb3

Please sign in to comment.