File tree Expand file tree Collapse file tree 4 files changed +38
-1
lines changed
main/java/emailvalidator4j/parser
test/java/emailvalidator4j Expand file tree Collapse file tree 4 files changed +38
-1
lines changed Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff line change 1212abstract 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
Original file line number Diff line number Diff line change @@ -52,7 +52,13 @@ public static Object[][] invalidEmailProvider() {
52525353 {"test@iana/icann.org" },
5454 {"test@foo;bar.com" },
55- 55+ 56+ 57+ {"comment(example))@example.com" },
58+ {"example@example)comment.com" },
59+ {"example@example(comment)).com" },
60+ {"example@(example))comment.com" }
61+
5662 };
5763 }
5864
You can’t perform that action at this time.
0 commit comments