Skip to content

Commit 06e07b0

Browse files
committed
Fix to fine-tune URI link for Ruby QUO,QUOxN
1 parent e3601cd commit 06e07b0

File tree

6 files changed

+64
-8
lines changed

6 files changed

+64
-8
lines changed

src/org/opensolaris/opengrok/analysis/ruby/RubyLexHelper.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 RubyLexHelper}
@@ -97,6 +98,12 @@ class RubyLexHelper implements Resettable {
9798
*/
9899
private int nendbrace;
99100

101+
/**
102+
* When matching a quoting construct, a Pattern to identify collateral
103+
* capture characters is stored.
104+
*/
105+
private Pattern collateralCapture;
106+
100107
public RubyLexHelper(int qUO, int qUOxN, int qUOxL, int qUOxLxN,
101108
RubyLexer lexer,
102109
int hERE, int hERExN, int hEREin, int hEREinxN) {
@@ -119,6 +126,7 @@ public RubyLexHelper(int qUO, int qUOxN, int qUOxL, int qUOxLxN,
119126
*/
120127
@Override
121128
public void reset() {
129+
collateralCapture = null;
122130
endqchar = '\0';
123131
if (hereSettings != null) hereSettings.clear();
124132
nendbrace = 0;
@@ -191,6 +199,7 @@ public void qop(boolean doWrite, String capture, int namelength,
191199
postop = capture.substring(qopname.length());
192200
}
193201
nendqchar = 1;
202+
collateralCapture = null;
194203

195204
char opc = postop.charAt(0);
196205
setEndQuoteChar(opc);
@@ -212,9 +221,9 @@ public void setState(String postop, boolean nointerp) {
212221
boolean nolink = false;
213222

214223
// "no link" for values in the rules for "string links" if `postop'
215-
// is file- or URI-like.
216-
if (StringUtils.startsWithFnameChars(postop) ||
217-
StringUtils.startsWithURIChars(postop)) {
224+
// starts path-like or with the e-mail delimiter.
225+
if (StringUtils.startsWithFpathChar(postop) ||
226+
postop.startsWith("@")) {
218227
nolink = true;
219228
}
220229

@@ -491,6 +500,26 @@ public int nameLength(String capture) {
491500
return len;
492501
}
493502

503+
/**
504+
* Gets a pattern to match the collateral capture for the current quoting
505+
* state or null if there is no active quoting state.
506+
* @return a defined pattern or null
507+
*/
508+
public Pattern getCollateralCapturePattern() {
509+
if (endqchar == '\0') return null;
510+
if (collateralCapture != null) return collateralCapture;
511+
512+
StringBuilder patb = new StringBuilder("[");
513+
patb.append(Pattern.quote(String.valueOf(endqchar)));
514+
if (nestqchar != '\0') {
515+
patb.append(Pattern.quote(String.valueOf(nestqchar)));
516+
}
517+
patb.append("]");
518+
patb.append(RegexUtils.getNotFollowingEscapePattern());
519+
collateralCapture = Pattern.compile(patb.toString());
520+
return collateralCapture;
521+
}
522+
494523
private class HereDocSettings {
495524
private final String terminator;
496525
private final int state;

src/org/opensolaris/opengrok/analysis/ruby/RubyProductions.lexh

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,30 @@ Here_EOF3 = [\`][^\r\n\`]*[\`]
504504
}
505505
}
506506

507+
{FNameChar}+ "@" {FNameChar}+ "." {FNameChar}+ {
508+
if (takeAllContent()) {
509+
writeEMailAddress(yytext());
510+
}
511+
}
512+
}
513+
514+
<SCOMMENT, POD, HERE, HERExN, HEREin, HEREinxN> {
507515
{BrowseableURI} {
508516
if (takeAllContent()) {
509-
appendLink(yytext(), true);
517+
appendLink(yytext(), true, null);
510518
}
519+
// no skipLink() needed except in QUO* states
511520
}
521+
}
512522

513-
{FNameChar}+ "@" {FNameChar}+ "." {FNameChar}+ {
523+
<QUO, QUOxN> {
524+
{BrowseableURI} {
525+
String capture = yytext();
526+
Pattern collateralCapture = h.getCollateralCapturePattern();
514527
if (takeAllContent()) {
515-
writeEMailAddress(yytext());
528+
appendLink(capture, true, collateralCapture);
529+
} else {
530+
skipLink(capture, collateralCapture);
516531
}
517532
}
518533
}

src/org/opensolaris/opengrok/analysis/ruby/RubySymbolTokenizer.lex

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

3131
import java.io.IOException;
3232
import java.util.Stack;
33+
import java.util.regex.Pattern;
3334
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
35+
import org.opensolaris.opengrok.util.StringUtils;
3436
import org.opensolaris.opengrok.web.HtmlConsts;
3537
import org.opensolaris.opengrok.web.Util;
3638

@@ -152,9 +154,14 @@ import org.opensolaris.opengrok.web.Util;
152154

153155
protected void appendProject() { /* noop */ }
154156

155-
protected void appendLink(String s, boolean b) { /* noop */ }
157+
protected void appendLink(String s, boolean b, Pattern p) { /* noop */ }
156158

157159
protected void writeEMailAddress(String s) { /* noop */ }
160+
161+
protected void skipLink(String url, Pattern p) {
162+
int n = StringUtils.countPushback(url, p);
163+
if (n > 0) yypushback(n);
164+
}
158165
%}
159166

160167
%include Common.lexh

src/org/opensolaris/opengrok/analysis/ruby/RubyXref.lex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package org.opensolaris.opengrok.analysis.ruby;
3131
import java.io.IOException;
3232
import java.io.Reader;
3333
import java.util.Stack;
34+
import java.util.regex.Pattern;
3435
import org.opensolaris.opengrok.analysis.JFlexXref;
3536
import org.opensolaris.opengrok.web.HtmlConsts;
3637
import org.opensolaris.opengrok.web.Util;
@@ -138,6 +139,8 @@ import org.opensolaris.opengrok.web.Util;
138139
}
139140

140141
protected String getUrlPrefix() { return urlPrefix; }
142+
143+
protected void skipLink(String s, Pattern p) { /* noop */ }
141144
%}
142145

143146
%include Common.lexh

test/org/opensolaris/opengrok/analysis/ruby/ruby_xrefres.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,6 @@
182182
<a class="l" name="174" href="#174">174</a> <b>system</b>(<span class="s">&quot;stty -raw echo&quot;</span>)
183183
<a class="l" name="175" href="#175">175</a><b>end</b>
184184
<a class="l" name="176" href="#176">176</a><b>print</b> <span class="s">&quot;\n&quot;</span>
185-
<a class="l" name="177" href="#177">177</a></body>
185+
<a class="l" name="177" href="#177">177</a><b>print</b> <span class="s">&apos;<a href="http://example.com">http://example.com</a>&apos;</span>
186+
<a class="l" name="178" href="#178">178</a></body>
186187
</html>

test/org/opensolaris/opengrok/analysis/ruby/sample.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,4 @@ def right
174174
system("stty -raw echo")
175175
end
176176
print "\n"
177+
print 'http://example.com'

0 commit comments

Comments
 (0)