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
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 operands to ensure no oprerand has ',' in them later can be customized further
private final static String VALID_OPERAND = "^[^,]*$";
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) {
isOperandValid(token,expr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the method isOperandValid() is actually wrong here.
Correct, that is your intention but also operator-strings like and, but and not are checked by this method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @jenisys , for your review and feedback. I have made the necessary adjustment by updating the method name. As you rightly mentioned, apart from operands, this method will also handle validations for operators. Please confirm if the changes meet the requirements. Your continued assistance is appreciated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method-name is now OK for me 👍

tokens.add(token.toString());
token = new StringBuilder();
}
Expand All @@ -116,12 +119,27 @@ private static List<String> tokenize(String expr) {
token.append(c);
}
}
if (token.length() > 0) {
isOperandValid(token,expr);
if (token.length() > 0) {
tokens.add(token.toString());
}
return tokens;
}

/**
* this method checks if the operand comply with the req
* regex if not throws exception
* @param token supposed tag of token
* @param expr entire expression
*/
private static void isOperandValid(StringBuilder token,String expr){
if(!String.valueOf(token).matches(VALID_OPERAND)){
throw new TagExpressionException("Tag expression \"%s\" could not be parsed because of syntax error: Tag names should follow this pattern \"%s\"",
expr, VALID_OPERAND);
}

}

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