Skip to content

Commit e3601cd

Browse files
committed
Fix to fine-tune URI link for Perl QUO,QUOxN
1 parent 2b1decd commit e3601cd

File tree

6 files changed

+64
-8
lines changed

6 files changed

+64
-8
lines changed

src/org/opensolaris/opengrok/analysis/perl/PerlLexHelper.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.opensolaris.opengrok.util.StringUtils;
3333
import org.opensolaris.opengrok.web.HtmlConsts;
3434
import org.opensolaris.opengrok.analysis.JFlexJointLexer;
35+
import org.opensolaris.opengrok.util.RegexUtils;
3536

3637
/**
3738
* Represents an API for object's using {@link PerlLexHelper}
@@ -113,6 +114,12 @@ class PerlLexHelper implements Resettable {
113114
*/
114115
private boolean waitq;
115116

117+
/**
118+
* When matching a quoting construct, a Pattern to identify collateral
119+
* capture characters is stored.
120+
*/
121+
private Pattern collateralCapture;
122+
116123
public PerlLexHelper(int qUO, int qUOxN, int qUOxL, int qUOxLxN,
117124
PerlLexer lexer,
118125
int hERE, int hERExN, int hEREin, int hEREinxN) {
@@ -135,6 +142,7 @@ public PerlLexHelper(int qUO, int qUOxN, int qUOxL, int qUOxLxN,
135142
*/
136143
@Override
137144
public void reset() {
145+
collateralCapture = null;
138146
endqchar = '\0';
139147
if (hereSettings != null) hereSettings.clear();
140148
nendqchar = 0;
@@ -227,6 +235,7 @@ public void qop(boolean doWrite, String capture, int namelength,
227235
}
228236
waitq = false;
229237
nendqchar = 1;
238+
collateralCapture = null;
230239

231240
switch (qopname) {
232241
case "tr":
@@ -265,9 +274,9 @@ public void setState(String ltpostop, boolean nointerp) {
265274
boolean nolink = false;
266275

267276
// "no link" for values in the rules for "string links" if `ltpostop'
268-
// is file- or URI-like.
269-
if (StringUtils.startsWithFnameChars(ltpostop) ||
270-
StringUtils.startsWithURIChars(ltpostop)) {
277+
// starts path-like or with the e-mail delimiter.
278+
if (StringUtils.startsWithFpathChar(ltpostop) ||
279+
ltpostop.startsWith("@")) {
271280
nolink = true;
272281
}
273282

@@ -604,6 +613,26 @@ public void specialID(String capture) throws IOException {
604613
}
605614
}
606615

616+
/**
617+
* Gets a pattern to match the collateral capture for the current quoting
618+
* state or null if there is no active quoting state.
619+
* @return a defined pattern or null
620+
*/
621+
public Pattern getCollateralCapturePattern() {
622+
if (endqchar == '\0') return null;
623+
if (collateralCapture != null) return collateralCapture;
624+
625+
StringBuilder patb = new StringBuilder("[");
626+
patb.append(Pattern.quote(String.valueOf(endqchar)));
627+
if (nestqchar != '\0') {
628+
patb.append(Pattern.quote(String.valueOf(nestqchar)));
629+
}
630+
patb.append("]");
631+
patb.append(RegexUtils.getNotFollowingEscapePattern());
632+
collateralCapture = Pattern.compile(patb.toString());
633+
return collateralCapture;
634+
}
635+
607636
class HereDocSettings {
608637
private final String terminator;
609638
private final int state;

src/org/opensolaris/opengrok/analysis/perl/PerlProductions.lexh

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,15 +525,30 @@ Mpunc2IN = ([!=]"~" | [\:\?\=\+\-\<\>] | "=="|"!="|"<="|">="|"<=>"|"=>")
525525
}
526526
}
527527

528+
{FNameChar}+ "@" {FNameChar}+ "." {FNameChar}+ {
529+
if (takeAllContent()) {
530+
writeEMailAddress(yytext());
531+
}
532+
}
533+
}
534+
535+
<SCOMMENT, POD, FMT, HERE, HERExN, HEREin, HEREinxN> {
528536
{BrowseableURI} {
529537
if (takeAllContent()) {
530-
appendLink(yytext(), true);
538+
appendLink(yytext(), true, null);
531539
}
540+
// no skipLink() needed except in QUO* states
532541
}
542+
}
533543

534-
{FNameChar}+ "@" {FNameChar}+ "." {FNameChar}+ {
544+
<QUO, QUOxN> {
545+
{BrowseableURI} {
546+
String capture = yytext();
547+
Pattern collateralCapture = h.getCollateralCapturePattern();
535548
if (takeAllContent()) {
536-
writeEMailAddress(yytext());
549+
appendLink(capture, true, collateralCapture);
550+
} else {
551+
skipLink(yytext(), collateralCapture);
537552
}
538553
}
539554
}

src/org/opensolaris/opengrok/analysis/perl/PerlSymbolTokenizer.lex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
package org.opensolaris.opengrok.analysis.perl;
3030

3131
import java.io.IOException;
32+
import java.util.regex.Pattern;
3233
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
34+
import org.opensolaris.opengrok.util.StringUtils;
3335
import org.opensolaris.opengrok.web.HtmlConsts;
3436
import org.opensolaris.opengrok.web.Util;
3537

@@ -137,9 +139,14 @@ import org.opensolaris.opengrok.web.Util;
137139

138140
protected void appendProject() { /* noop */ }
139141

140-
protected void appendLink(String s, boolean b) { /* noop */ }
142+
protected void appendLink(String s, boolean b, Pattern p) { /* noop */ }
141143

142144
protected void writeEMailAddress(String s) { /* noop */ }
145+
146+
protected void skipLink(String url, Pattern p) {
147+
int n = StringUtils.countPushback(url, p);
148+
if (n > 0) yypushback(n);
149+
}
143150
%}
144151

145152
%include Common.lexh

src/org/opensolaris/opengrok/analysis/perl/PerlXref.lex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ package org.opensolaris.opengrok.analysis.perl;
3030

3131
import java.io.IOException;
3232
import java.io.Reader;
33+
import java.util.regex.Pattern;
3334
import org.opensolaris.opengrok.analysis.JFlexXref;
3435
import org.opensolaris.opengrok.web.HtmlConsts;
3536
import org.opensolaris.opengrok.web.Util;
@@ -127,6 +128,8 @@ import org.opensolaris.opengrok.web.Util;
127128
}
128129

129130
protected String getUrlPrefix() { return urlPrefix; }
131+
132+
protected void skipLink(String s, Pattern p) { /* noop */ }
130133
%}
131134

132135
%include Common.lexh

test/org/opensolaris/opengrok/analysis/perl/sample.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,4 @@ ($)
382382
"left", "middle", "right"
383383
.
384384
print "1\n";
385+
print 'http://example.com';

test/org/opensolaris/opengrok/analysis/perl/samplexrefres.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,5 +389,6 @@
389389
<a class="l" name="382" href="#382">382</a><span class="s">&quot;left&quot;, &quot;middle&quot;, &quot;right&quot;</span>
390390
<a class="l" name="383" href="#383">383</a><span class="s">.</span>
391391
<a class="l" name="384" href="#384">384</a><b>print</b> <span class="s">&quot;1\n&quot;</span>;
392-
<a class="l" name="385" href="#385">385</a></body>
392+
<a class="l" name="385" href="#385">385</a><b>print</b> <span class="s">&apos;<a href="http://example.com">http://example.com</a>&apos;</span>;
393+
<a class="l" name="386" href="#386">386</a></body>
393394
</html>

0 commit comments

Comments
 (0)