Skip to content

Commit 1500ff0

Browse files
author
Eduardo Gulias Davis
committed
parenthesis control
1 parent 702bcde commit 1500ff0

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

src/main/java/emailvalidator4j/parser/DomainPart.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void parse(String domainPart) throws InvalidEmail {
5252
}
5353

5454
private void doParseDomainPart() throws InvalidEmail {
55+
int domainPartOpenedParenthesis = 0;
5556
do {
5657
if (this.lexer.getCurrent().equals(Tokens.SEMICOLON)) {
5758
throw new ExpectedATEXT("Expected ATEXT");
@@ -65,7 +66,19 @@ private void doParseDomainPart() throws InvalidEmail {
6566
}
6667

6768
this.parseComment();
69+
domainPartOpenedParenthesis += getOpenedParenthesis();
6870
this.lexer.next();
71+
if (this.lexer.getPrevious().equals(Tokens.CLOSEPARENTHESIS)) {
72+
domainPartOpenedParenthesis--;
73+
}
74+
75+
}
76+
77+
if (this.lexer.getCurrent().equals(Tokens.CLOSEPARENTHESIS)) {
78+
if (domainPartOpenedParenthesis <= 0) {
79+
throw new UnclosedComment("Missing closing parenthesis");
80+
}
81+
domainPartOpenedParenthesis--;
6982
}
7083

7184
this.checkConsecutiveDots();

src/main/java/emailvalidator4j/parser/LocalPart.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ final class LocalPart extends Parser {
2222
@Override
2323
public void parse(String localpart) throws InvalidEmail {
2424
lexer.lex(localpart);
25+
int localPartOpenedParenthesis = 0;
2526
if (this.lexer.getCurrent().equals(Tokens.DOT)) {
2627
throw new DotAtStart("Found DOT at start");
2728
}
@@ -35,6 +36,14 @@ public void parse(String localpart) throws InvalidEmail {
3536

3637
if (this.lexer.getCurrent().equals(Tokens.OPENPARETHESIS)) {
3738
this.parseComment();
39+
localPartOpenedParenthesis += getOpenedParenthesis();
40+
}
41+
42+
if (this.lexer.getCurrent().equals(Tokens.CLOSEPARENTHESIS)) {
43+
if(localPartOpenedParenthesis == 0) {
44+
throw new UnclosedComment("Missing closing parenthesis");
45+
}
46+
localPartOpenedParenthesis--;
3847
}
3948

4049
this.checkConsecutiveDots();

src/main/java/emailvalidator4j/parser/Parser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
abstract class Parser {
1313
protected List<Warnings> warnings = new ArrayList<Warnings>();
1414
protected EmailLexer lexer;
15+
protected int openedParenthesis = 0;
1516

1617
public Parser(EmailLexer lexer) {
1718
this.lexer = lexer;
@@ -32,11 +33,19 @@ protected boolean escaped() {
3233
return this.lexer.getPrevious().equals(Tokens.BACKSLASH) && !this.lexer.getCurrent().equals(Tokens.get("GENERIC"));
3334
}
3435

36+
protected int getOpenedParenthesis() {
37+
return this.openedParenthesis;
38+
}
39+
3540
protected void parseComment() throws InvalidEmail {
41+
this.openedParenthesis = 1;
3642
this.checkUnclosedComment();
3743
this.warnings.add(Warnings.COMMENT);
3844

3945
while (!this.lexer.getCurrent().equals(Tokens.CLOSEPARENTHESIS)) {
46+
if (this.lexer.isNextToken(Tokens.OPENPARETHESIS)) {
47+
this.openedParenthesis++;
48+
}
4049
this.lexer.next();
4150
}
4251

src/test/java/emailvalidator4j/EmailValidatorTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ public static Object[][] invalidEmailProvider() {
5252
{"[email protected] \r\n\r\n "},
5353
{"test@iana/icann.org"},
5454
{"test@foo;bar.com"},
55-
{(char) 1 + "[email protected]"}
55+
{(char) 1 + "[email protected]"},
56+
{"comment)[email protected]"},
57+
{"comment(example))@example.com"},
58+
{"example@example)comment.com"},
59+
{"example@example(comment)).com"},
60+
{"example@(example))comment.com"}
61+
5662
};
5763
}
5864

0 commit comments

Comments
 (0)