Skip to content

Commit

Permalink
#99 Use the new TemplateBodyPiece interface in ParsedTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Suseika committed Aug 20, 2015
1 parent 1628bdb commit 728a49e
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/main/java/org/tendiwa/inflectible/ParsedTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ private ImmutableList<ArgumentName> argumentNames() {
* @return Template from markup
*/
private Template delegate() {
return new ParseTreeListener(this.argumentNames()).filledUpText();
return new TemplateBuilder(this.argumentNames()).template();
}

/**
* Walks an ANTLR parse tree and constructs a template.
*/
private final class ParseTreeListener
private final class TemplateBuilder
extends TemplateBundleParserBaseListener {

/**
* Template builder.
*/
private transient TextTemplateBuilder builder;
private transient ImmutableList.Builder<TemplateBodyPiece> pieces;

/**
* Argument names in the order as they appear in markup.
Expand All @@ -108,7 +108,7 @@ private final class ParseTreeListener
* Ctor.
* @param names Argument names in the order as they appear in markup
*/
ParseTreeListener(final ImmutableList<ArgumentName> names) {
TemplateBuilder(final ImmutableList<ArgumentName> names) {
super();
this.arguments = names;
}
Expand All @@ -117,10 +117,12 @@ private final class ParseTreeListener
public void enterTwoPartPlaceholder(
final TemplateBundleParser.TwoPartPlaceholderContext context
) {
this.builder.addPlaceholder(
new ParsedTwoPartVariableConceptPlaceholder(
ParsedTemplate.this.grammar,
context
this.pieces.add(
new PiPlaceholder(
new ParsedTwoPartVariableConceptPlaceholder(
ParsedTemplate.this.grammar,
context
)
)
);
}
Expand All @@ -129,15 +131,17 @@ public void enterTwoPartPlaceholder(
public void enterRawText(
final TemplateBundleParser.RawTextContext context
) {
this.builder.addText(context.getText());
this.pieces.add(new PiPlainText(context.getText()));
}

@Override
public void enterSinglePartPlaceholder(
final TemplateBundleParser.SinglePartPlaceholderContext context
) {
this.builder.addPlaceholder(
new ParsedSinglePartPlaceholder(context)
this.pieces.add(
new PiPlaceholder(
new ParsedSinglePartPlaceholder(context)
)
);
}

Expand All @@ -146,10 +150,13 @@ public void enterSinglePartPlaceholder(
* it.
* @return Template.
*/
private Template filledUpText() {
this.builder = new TextTemplateBuilder(this.arguments);
private Template template() {
this.pieces = ImmutableList.builder();
ParseTreeWalker.DEFAULT.walk(this, ParsedTemplate.this.ctx);
return this.builder.build();
return new BasicTemplate(
this.arguments,
this.pieces.build()
);
}
}
}

0 comments on commit 728a49e

Please sign in to comment.