/**
* 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 @return() statement. Which break the current template execution and return to caller
*/
public class ReturnParser extends KeywordParserFactory {
@Override
public Keyword keyword() {
return Keyword.RETURN;
}
public IParser create(final IContext ctx) {
return new ParserBase(ctx) {
public Token go() {
Regex r = reg(dialect());
if (!r.search(remain())) {
raiseParseException("error parsing @return, correct usage: @return()");
}
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)) {
condition = "true";
} else {
logger.warn("@return(<expression>) is deprecated. Please use @returnIf(expression)");
}
return new IfThenToken(condition, "return this", ctx());
}
};
}
@Override
protected String patternStr() {
return "^(\\n?[ \\t\\x0B\\f]*%s%s\\s*((?@()))?[\\s;]*)";
}
}