Skip to content

Commit 9a7b508

Browse files
Code re-factor
1 parent 38a2d66 commit 9a7b508

File tree

4 files changed

+81
-50
lines changed

4 files changed

+81
-50
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,43 +1394,58 @@ protected boolean parseReference() throws InvalidInputException {
13941394
}
13951395

13961396
// Parses a complete URL reference starting from current position
1397-
private Object parseURLReference() throws InvalidInputException {
1398-
// httpFlag is used to check wheather URL contains http or not
1397+
protected boolean parseURLReference() throws InvalidInputException {
1398+
int pos = 0;
1399+
char[]fullURL = null;
1400+
int firstTokenStartPos;
13991401
StringBuilder urlBuilder = new StringBuilder();
1400-
int firstTokenStartPos = this.scanner.startPosition;
1401-
//start with the current identifier
1402-
if (this.currentTokenType == TerminalToken.TokenNameIdentifier) {
1403-
urlBuilder.append(this.scanner.getCurrentTokenSource());
1404-
consumeToken();
1405-
}
1406-
char[] fullURL;
1407-
int pos;
1408-
1409-
TerminalToken token1 = readTokenSafely();
1410-
if (token1 == TerminalToken.TokenNameCOLON) {
1411-
urlBuilder.append(this.scanner.getCurrentTokenSource());
1412-
consumeToken();
1413-
}
1414-
1415-
pos = this.scanner.getCurrentTokenEndPosition() + 1;
14161402
char c;
1417-
while (pos < this.source.length) {
1418-
c = (this.source[pos] == ')') ? this.source[pos] : readChar();
1419-
1420-
if (c == ':') continue;
1421-
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '*' || c == ')' || c == '(' || c == '[' || c == ']' ) {
1422-
break;
1423-
}
1424-
urlBuilder.append(c);
1425-
pos++;
1403+
if (this.source[this.index] == '(') {
1404+
firstTokenStartPos = this.index;
1405+
while (pos < this.source.length) {
1406+
c = readChar();
1407+
if (c == '(' || c == '[' || c == ' ')
1408+
continue;
1409+
1410+
if (c == '\t' || c == '\n' || c == '\r' || c == '*' || c == ')' || c == '[' || c == ']' ) {
1411+
break;
1412+
}
1413+
urlBuilder.append(c);
1414+
}
1415+
pos = this.index - 1;
1416+
if (validateMarkdownSyntax(this.source[firstTokenStartPos], this.source[pos]))
1417+
fullURL = urlBuilder.toString().toCharArray();
1418+
} else {
1419+
pos = this.scanner.startPosition - 1;
1420+
firstTokenStartPos = pos;
1421+
while (pos < this.source.length) {
1422+
c = this.source[pos];
1423+
if (c == '(' || c == '[' || c == ' ') {
1424+
pos++;
1425+
continue;
1426+
}
1427+
if (c == '\t' || c == '\n' || c == '\r' || c == '*' || c == ')' || c == '[' || c == ']' ) {
1428+
break;
1429+
}
1430+
urlBuilder.append(c);
1431+
pos++;
1432+
}
1433+
if (validateMarkdownSyntax(this.source[firstTokenStartPos], this.source[pos]))
1434+
fullURL = urlBuilder.toString().toCharArray();
14261435
}
1427-
fullURL = urlBuilder.toString().toCharArray();
14281436

14291437
this.identifierPtr = 0;
14301438
this.identifierStack[this.identifierPtr] = fullURL;
14311439
this.identifierPositionStack[this.identifierPtr] = (((long) firstTokenStartPos) << 32) + (pos - 1);
14321440
this.identifierLengthStack[this.identifierLengthPtr] = 1;
1433-
return createTypeReference(TokenNameInvalid, true);
1441+
Object typeRef = createTypeReference(TerminalToken.TokenNameInvalid, true);
1442+
pushSeeRef(typeRef);
1443+
return true;
1444+
}
1445+
1446+
/// validate markdown syntax check
1447+
private boolean validateMarkdownSyntax(char start, char end) {
1448+
return (start == '[' && end == ']') || (start == '(' && end == ')');
14341449
}
14351450

14361451
/*
@@ -1528,19 +1543,6 @@ else if (this.tagValue == TAG_VALUE_VALUE) {
15281543
break nextToken;
15291544
case TokenNameUNDERSCORE:
15301545
case TokenNameIdentifier :
1531-
if (this.markdown) {
1532-
int pos1 = this.scanner.getCurrentTokenStartPosition() - 1;
1533-
while (pos1 >= 0 && Character.isWhitespace(this.source[pos1])) {
1534-
pos1--;
1535-
}
1536-
if (pos1 >= 0 && this.source[pos1] == '(') {
1537-
if (typeRef == null) {
1538-
typeRefStartPosition = this.scanner.getCurrentTokenStartPosition();
1539-
typeRef = parseURLReference();
1540-
if (this.abort) return false;
1541-
}
1542-
}
1543-
}
15441546
if (typeRef == null) {
15451547
typeRefStartPosition = this.scanner.getCurrentTokenStartPosition();
15461548
typeRef = parseQualifiedName(true, allowModule);

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/JavadocParser.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,16 @@ protected boolean parseMarkdownLinks(int previousPosition) throws InvalidInputEx
573573
}
574574
break;
575575
case ']':
576-
if (peekChar() == '[' || peekChar() == '(') {
576+
if (peekChar() == '[') {
577577
// We might want to store the description in case of DOM parser
578578
// but the compiler does not need it
579579
//int length = this.index - start - 1;
580580
//System.arraycopy(this.scanner.source, start, desc = new char[length], 0, length);
581581
// move it past '['
582582
currentChar = readChar();
583583
start = this.index;
584+
} else if (peekChar() == '(') {
585+
valid = parseURLReference();
584586
} else {
585587
break loop;
586588
}
@@ -602,7 +604,8 @@ protected boolean parseMarkdownLinks(int previousPosition) throws InvalidInputEx
602604
int eofBkup = this.scanner.eofPosition;
603605
this.scanner.resetTo(start, Math.max(this.javadocEnd, this.index));
604606
this.tagValue = TAG_LINK_VALUE;
605-
valid = parseReference(true);
607+
if (!valid)
608+
valid = parseReference(true);
606609
this.tagValue = NO_TAG_VALUE;
607610
this.scanner.eofPosition = eofBkup;
608611
this.markdownHelper.resetLineStart();

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterMarkdownTest.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,11 +2027,7 @@ public class Markdown() {}
20272027
CompilationUnit compilUnit = (CompilationUnit) runConversion(this.workingCopies[0], true);
20282028
TypeDeclaration typedeclaration = (TypeDeclaration) compilUnit.types().get(0);
20292029
Javadoc javadoc = typedeclaration.getJavadoc();
2030-
TagElement tags = (TagElement) javadoc.tags().get(0);
2031-
assertEquals("fragments count does not match", 3, tags.fragments().size());
2032-
assertTrue(tags.fragments().get(0) instanceof TextElement);
2033-
assertTrue(tags.fragments().get(1) instanceof TextElement);
2034-
assertTrue(tags.fragments().get(2) instanceof TextElement);
2030+
assertEquals("Tags count does not match", 0, javadoc.tags().size());
20352031
}
20362032
}
20372033

@@ -2136,4 +2132,31 @@ public class Markdown() {}
21362132
assertTrue(fragTag.fragments().get(1) instanceof QualifiedName);
21372133
}
21382134
}
2135+
2136+
public void testMarkdownURLs4531_12() throws JavaModelException {
2137+
String source = """
2138+
/// @see [Ex Si)(http://ex.com ) [Ex Si]( https://www.ex.com)
2139+
public class Markdown() {}
2140+
""";
2141+
2142+
this.workingCopies = new ICompilationUnit[1];
2143+
this.workingCopies[0] = getWorkingCopy("/Converter_25/src/markdown/gh3761/Markdown.java", source, null);
2144+
if (this.docCommentSupport.equals(JavaCore.ENABLED)) {
2145+
CompilationUnit compilUnit = (CompilationUnit) runConversion(this.workingCopies[0], true);
2146+
TypeDeclaration typedeclaration = (TypeDeclaration) compilUnit.types().get(0);
2147+
Javadoc javadoc = typedeclaration.getJavadoc();
2148+
TagElement tags = (TagElement) javadoc.tags().get(0);
2149+
assertEquals("fragments count does not match", 4, tags.fragments().size());
2150+
assertTrue(tags.fragments().get(0) instanceof TextElement);
2151+
assertTrue(tags.fragments().get(1) instanceof TagElement);
2152+
TagElement tagFrag1 = (TagElement) tags.fragments().get(1);
2153+
SimpleName simplName1 = (SimpleName) tagFrag1.fragments().get(1);
2154+
assertEquals("SimpleName1 value not match", "http://ex.com", simplName1.getIdentifier());
2155+
assertTrue(tags.fragments().get(0) instanceof TextElement);
2156+
assertTrue(tags.fragments().get(1) instanceof TagElement);
2157+
TagElement tagFrag2 = (TagElement) tags.fragments().get(3);
2158+
SimpleName simplName2 = (SimpleName) tagFrag2.fragments().get(1);
2159+
assertEquals("SimpleName2 value not match", "https://www.ex.com", simplName2.getIdentifier());
2160+
}
2161+
}
21392162
}

org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DocCommentParser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,10 @@ protected boolean parseMarkdownLinks(int previousPosition) throws InvalidInputEx
748748
this.inlineTagStart = previousPosition;
749749
this.tagValue = TAG_LINK_VALUE;
750750
int indexBkup = this.index;
751-
valid = parseReference(true);
751+
if(currentChar != ']')
752+
valid = parseURLReference();
753+
else
754+
valid = parseReference(true);
752755
this.index = indexBkup;
753756
// This creates a two level structure. The @link tag is added to
754757
// another tag element, which gets added to the astStack

0 commit comments

Comments
 (0)