com.x5.template.CondLexer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of chunk-templates Show documentation
Show all versions of chunk-templates Show documentation
Chunk Template Engine for Java
package com.x5.template;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.x5.template.filters.FilterArgs;
public class CondLexer
{
String conditional;
CondTree parsed;
public CondLexer(String conditional)
{
this.conditional = conditional;
}
public CondTree parse() throws InvalidExpressionException
{
if (parsed != null) {
return parsed;
}
parsed = CondTree.buildBranch(lexLogicTokens());
return parsed;
}
// pick apart high-level logic tokens && || ! () but treat entire
// comparison tests as single token.
private Iterator lexLogicTokens()
{
char c, d;
int len = conditional.length();
List tokens = new ArrayList();
for (int i=0; i 0 && (c == '=' || c == '!')) {
if (i+1 == len) {
return len;
}
d = s.charAt(i+1);
if (d == '=' || d == '~') {
foundOperator = true;
i += 2;
continue;
}
}
if (foundOperator) {
// seeking RHS, ruled out tag, must be string constant or regex.
if (c == '"' || c == '\'' || c == '/') {
String delim = Character.toString(c);
i = FilterArgs.nextUnescapedDelim(delim, s, i+1);
return (i > 0) ? i+1 : len;
}
// unquoted string :( assume next space/close-paren/&&/|| marks the end
Matcher matcher = END_OF_UNQUOTED_STRING.matcher(s);
if (matcher.find(i)) {
return matcher.start();
} else {
return len;
}
}
// falling through to here is okay... eg "if (true)" is permitted.
// advance cursor to avoid infinite loop.
i++;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy