Skip to content

Commit 4e33612

Browse files
committed
Fix to fine-tune URI link for Haskell BCOMMENT
Also, handle that URI ending-pushback may be present before collateral capture.
1 parent 84792f3 commit 4e33612

File tree

4 files changed

+92
-13
lines changed

4 files changed

+92
-13
lines changed

src/org/opensolaris/opengrok/analysis/JFlexXref.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,28 @@ protected void appendLink(String url, boolean doEndingPushback,
270270
throws IOException {
271271

272272
int n = 0;
273-
if (doEndingPushback) {
274-
n = StringUtils.countURIEndingPushback(url);
275-
}
276-
int ccn = StringUtils.countPushback(url, collateralCapture);
277-
if (ccn > n) n = ccn;
278-
// Push back if positive, but not if equal to the current length.
279-
if (n > 0 && n < url.length()) {
280-
yypushback(n);
281-
url = url.substring(0, url.length() - n);
282-
}
273+
int subn;
274+
do {
275+
// An ending-pushback could be present before a collateral capture,
276+
// so detect both in a loop (on a shrinking `url') until no more
277+
// shrinking should occur.
278+
279+
subn = 0;
280+
if (doEndingPushback) {
281+
subn = StringUtils.countURIEndingPushback(url);
282+
}
283+
int ccn = StringUtils.countPushback(url, collateralCapture);
284+
if (ccn > subn) subn = ccn;
285+
286+
// Push back if positive, but not if equal to the current length.
287+
if (subn > 0 && subn < url.length()) {
288+
url = url.substring(0, url.length() - subn);
289+
n += subn;
290+
} else {
291+
subn = 0;
292+
}
293+
} while (subn != 0);
294+
if (n > 0) yypushback(n);
283295

284296
out.write("<a href=\"");
285297
out.write(Util.formQuoteEscape(url));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2017, Chris Fraire <[email protected]>.
22+
*/
23+
24+
package org.opensolaris.opengrok.analysis.haskell;
25+
26+
import java.util.regex.Pattern;
27+
28+
/**
29+
* Represents a container for Haskell-related utility methods
30+
*/
31+
public class HaskellUtils {
32+
33+
/**
34+
* Matches either the end of a Haskell nested comment or a superfluous
35+
* right curly bracket (which is not a valid BrowseableURI character)
36+
* captured in order to be able to detect the end of a Haskell nested
37+
* comment:
38+
* <pre>
39+
* {@code # [ignore the following right brace {]
40+
* \-?\}
41+
* }
42+
* </pre>
43+
* (Edit above and paste below [in NetBeans] for easy String escaping.)
44+
*/
45+
public static final Pattern MAYBE_END_NESTED_COMMENT =
46+
Pattern.compile("\\-?\\}");
47+
48+
/** Private to enforce static. */
49+
private HaskellUtils() {
50+
}
51+
}

src/org/opensolaris/opengrok/analysis/haskell/HaskellXref.lex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,24 @@ import org.opensolaris.opengrok.web.Util;
152152

153153
<STRING, COMMENT, BCOMMENT> {
154154
{FPath} { out.write(Util.breadcrumbPath(urlPrefix + "path=", yytext(), '/')); }
155+
{FNameChar}+ "@" {FNameChar}+ "." {FNameChar}+ { writeEMailAddress(yytext()); }
156+
}
157+
158+
<STRING, COMMENT> {
155159
{BrowseableURI} {
156160
appendLink(yytext(), true);
157161
}
158-
{FNameChar}+ "@" {FNameChar}+ "." {FNameChar}+ { writeEMailAddress(yytext()); }
162+
}
163+
164+
<BCOMMENT> {
165+
/*
166+
* Right curly bracket is not a valid URI character, so it won't be in a
167+
* {BrowseableURI} capture, but a hyphen is valid. Thus a nested comment
168+
* ending token, -}, can hide at the end of a URI. Work around this by
169+
* capturing a possibly-trailing right curly bracket, and match a special,
170+
* Haskell-specific collateral capture pattern.
171+
*/
172+
{BrowseableURI} \}? {
173+
appendLink(yytext(), true, HaskellUtils.MAYBE_END_NESTED_COMMENT);
174+
}
159175
}

test/org/opensolaris/opengrok/analysis/haskell/sample2_xref.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,5 +264,5 @@
264264
<a class="l" name="261" href="#261">261</a><span class="c">{- comment {- comment -}
265265
<a class="l" name="262" href="#262">262</a>comment
266266
<a class="l" name="263" href="#263">263</a>-}</span>
267-
<a class="l" name="264" href="#264">264</a><span class="c">{-<a href="http://example.com.-">http://example.com.-</a>}
268-
<a class="l" name="265" href="#265">265</a></span></pre></div></body></html>
267+
<a class="l" name="264" href="#264">264</a><span class="c">{-<a href="http://example.com">http://example.com</a>.-}</span>
268+
<a class="l" name="265" href="#265">265</a></pre></div></body></html>

0 commit comments

Comments
 (0)