/**
* Copyright (C) 2013-2016 The Rythm Engine project
* for LICENSE and other details see:
* https://github.com/rythmengine/rythmengine
*/
package org.rythmengine.internal.parser.build_in;
import com.stevesoft.pat.Regex;
import org.rythmengine.internal.IContext;
import org.rythmengine.internal.IParser;
import org.rythmengine.internal.Keyword;
import org.rythmengine.internal.Token;
import org.rythmengine.internal.parser.ParserBase;
import org.rythmengine.utils.S;
/**
* Parse @returnIf(expression) statement. Which break the current
* template execution and return to caller if the expression
* evaluated to {@code true}
*/
public class ReturnIfParser extends KeywordParserFactory {
@Override
public Keyword keyword() {
return Keyword.RETURN_IF;
}
public IParser create(final IContext ctx) {
return new ParserBase(ctx) {
public Token go() {
Regex r = reg(dialect());
if (!r.search(remain())) {
raiseParseException("error parsing @returnIf, correct usage: @returnIf(<expression>)");
}
final String matched = r.stringMatched();
if (matched.startsWith("\n") || matched.endsWith("\n")) {
//ctx.getCodeBuilder().addBuilder(new Token.StringToken("\n", ctx));
Regex r0 = new Regex("\\n([ \\t\\x0B\\f]*).*");
if (r0.search(matched)) {
String blank = r0.stringMatched(1);
if (blank.length() > 0) {
ctx.getCodeBuilder().addBuilder(new Token.StringToken(blank, ctx));
}
}
} else {
Regex r0 = new Regex("([ \\t\\x0B\\f]*).*");
if (r0.search(matched)) {
String blank = r0.stringMatched(1);
if (blank.length() > 0) {
ctx.getCodeBuilder().addBuilder(new Token.StringToken(blank, ctx));
}
}
}
step(matched.length());
String condition = r.stringMatched(2);
if (null != condition) {
condition = S.stripBrace(condition);
}
if (S.empty(condition)) {
raiseParseException("expression required by @returnIf directive");
}
return new IfThenToken(condition, "return this", ctx());
}
};
}
@Override
protected String patternStr() {
return "^(\\n?[ \\t\\x0B\\f]*%s%s\\s*((?@()))[\\s;]*)";
}
}