org.yamcs.templating.TemplateParser.jj Maven / Gradle / Ivy
options {
STATIC = false;
IGNORE_CASE = false;
}
PARSER_BEGIN(TemplateParser)
package org.yamcs.templating;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class TemplateParser {
}
PARSER_END(TemplateParser)
< DEFAULT > SKIP: {
"{% comment %}": IN_COMMENT
}
< DEFAULT > TOKEN: {
< OPEN_TAG: "{%" (" ")? >
| < CLOSE_TAG: (" ")? "%}" >
| < ELIF: "elif" >
| < ENDIF: "endif" >
| < ELSE: "else" >
}
< DEFAULT > TOKEN: {
< OPEN_VAR: "{{" (" ")? > : IN_VAR
}
< DEFAULT > TOKEN: {
< OPEN_IF_TAG: "{% if " > : IN_CONDITION
| < OPEN_ELIF_TAG: "{% elif " > : IN_CONDITION
}
< DEFAULT > TOKEN: {
< TEXT: ~[] >
}
SKIP: {
"{% endcomment %}" : DEFAULT
}
MORE: {
< ~[] >
}
TOKEN: {
< VAR: (["a"-"z","A"-"Z","0"-"9","_","."])+ >
}
SKIP: {
" "
}
TOKEN: {
< PIPE: "|" >
| < CLOSE_VAR: "}}" > : DEFAULT
}
TOKEN: {
< CLOSE_IF_ELIF_TAG: (" ")? "%}" > : DEFAULT
}
public TemplateBody parse():
{
TemplateBody r;
}
{
r = body()
{
return r;
}
}
TemplateBody body():
{
TemplateBody result = new TemplateBody();
Statement stmt;
}
{
(
stmt = statement() { result.addStatement(stmt); }
)*
{
return result;
}
}
Statement statement():
{
Statement stmt;
}
{
(
stmt = varStatement()
| stmt = textStatement()
| stmt = ifStatement()
)
{
return stmt;
}
}
VarStatement varStatement():
{
String id;
String filter;
List filters = new ArrayList();
}
{
id=identifier() ( filter=identifier() { filters.add(filter); } )* {
return new VarStatement(id, filters);
}
}
String identifier():
{}
{
{
return token.image;
}
}
TextStatement textStatement():
{}
{
{
return new TextStatement(token.image);
}
}
IfStatement ifStatement():
{
String condition;
TemplateBody ifBody;
String elifCondition = null;
TemplateBody elifBody = null;
LinkedHashMap elifConditions = new LinkedHashMap();
TemplateBody elseBody = null;
}
{
condition=identifier()
ifBody = body()
(
elifCondition=identifier()
elifBody=body()
{
elifConditions.put(elifCondition, elifBody);
}
)*
(
LOOKAHEAD(2)
elseBody = body()
)?
{
return new IfStatement(condition, ifBody, elifConditions, elseBody);
}
}