Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT]Handling error response when a comma is present in the operand name of tags #107

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.regex.Pattern;

public final class TagExpressionParser {
//regex for token to ensure no token has ',' in them later can be customized further
private final static String VALID_TOKEN = "^[^,]*$";
private static final Map<String, Assoc> ASSOC = new HashMap<String, Assoc>() {{
put("or", Assoc.LEFT);
put("and", Assoc.LEFT);
Expand Down Expand Up @@ -106,6 +108,7 @@ private static List<String> tokenize(String expr) {
isEscaped = true;
} else if (c == '(' || c == ')' || Character.isWhitespace(c)) {
if (token.length() > 0) {
isTokenValid(token,expr);
tokens.add(token.toString());
token = new StringBuilder();
}
Expand All @@ -116,12 +119,28 @@ private static List<String> tokenize(String expr) {
token.append(c);
}
}
if (token.length() > 0) {
if (token.length() > 0) {
isTokenValid(token,expr);
tokens.add(token.toString());
}
return tokens;
}

/**
* this method checks if the token comply with the req
* regex if not throws exception
* @param token supposed tag or operator of the expresiion
* @param expr entire expression
*/
private static void isTokenValid(StringBuilder token,String expr){
if(!String.valueOf(token).matches(VALID_TOKEN)){
throw new TagExpressionException("Tag expression \"%s\" could not be parsed because of syntax error: An invalid tag combination operator was detected. The use of a comma (\",\") to combine tags is not supported. Please" +
" replace it with either the \"or\" or \"and\" operators for tag combinations. For example, use \"@tag1 or @tag2\" or \"@tag1 and @tag2\"",
expr);
}

}

private void check(TokenType expectedTokenType, TokenType tokenType) {
if (expectedTokenType != tokenType) {
throw new TagExpressionException("Tag expression \"%s\" could not be parsed because of syntax error: Expected %s.", infix, expectedTokenType.toString().toLowerCase());
Expand Down
4 changes: 4 additions & 0 deletions testdata/errors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
error: 'Tag expression "@a and (@b @c) or" could not be parsed because of syntax error: Expected operator.'
- expression: '@a and or'
error: 'Tag expression "@a and or" could not be parsed because of syntax error: Expected operand.'
- expression: '@a,@b'
error: 'Tag expression "@a,@b" could not be parsed because of syntax error: An invalid tag combination operator was detected. The use of a comma (",") to combine tags is not supported. Please replace it with either the "or" or "and" operators for tag combinations. For example, use "@tag1 or @tag2" or "@tag1 and @tag2"'
- expression: '(@a ,@b)'
error: 'Tag expression "(@a ,@b)" could not be parsed because of syntax error: An invalid tag combination operator was detected. The use of a comma (",") to combine tags is not supported. Please replace it with either the "or" or "and" operators for tag combinations. For example, use "@tag1 or @tag2" or "@tag1 and @tag2"'
- expression: 'or or'
error: 'Tag expression "or or" could not be parsed because of syntax error: Expected operand.'
- expression: 'a and or'
Expand Down