Skip to content

Commit 1bfa324

Browse files
committed
normalize fortran identifiers like gfortran
1 parent f6c4ed0 commit 1bfa324

File tree

11 files changed

+114
-32
lines changed

11 files changed

+114
-32
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,8 @@ protected TokenStream normalize(String fieldName, TokenStream in) {
360360
return new LowerCaseFilter(in);
361361
}
362362
}
363+
364+
public Definitions normalizeDefinitions(Definitions defs) {
365+
return defs;
366+
}
363367
}

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

+42-4
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,24 @@ protected void onScopeChanged(ScopeAction action, String str, int start) {
463463
*/
464464
protected boolean onFilteredSymbolMatched(String str, int start,
465465
Set<String> keywords) {
466-
return onFilteredSymbolMatched(str, start, keywords, true);
466+
return onFilteredSymbolMatched(str, str, start, keywords, true);
467+
}
468+
469+
/**
470+
* Calls
471+
* {@link #onFilteredSymbolMatched(java.lang.String, int, java.util.Set, boolean)}
472+
* with {@code str}, {@code start}, {@code keywords}, and {@code true}.
473+
* @param literal the literal representation of the symbol
474+
* @param str the text string
475+
* @param start the text start position
476+
* @param keywords an optional set to search for {@code str} as a member to
477+
* indicate a keyword
478+
* @return true if the {@code str} was not in {@code keywords} or if
479+
* {@code keywords} was null
480+
*/
481+
protected boolean onFilteredSymbolMatched(String literal, String str, int start,
482+
Set<String> keywords) {
483+
return onFilteredSymbolMatched(literal, str, start, keywords, true);
467484
}
468485

469486
/**
@@ -483,15 +500,36 @@ protected boolean onFilteredSymbolMatched(String str, int start,
483500
*/
484501
protected boolean onFilteredSymbolMatched(String str, int start,
485502
Set<String> keywords, boolean caseSensitive) {
503+
return onFilteredSymbolMatched(str, str, start, keywords, caseSensitive);
504+
}
505+
506+
/**
507+
* Raises {@link #onKeywordMatched(java.lang.String, int)} if
508+
* {@code keywords} is not null and {@code str} is found as a member (in a
509+
* case-sensitive or case-less search per {@code caseSensitive}); otherwise
510+
* raises {@link #onSymbolMatched(java.lang.String, int)}.
511+
* @param literal the literal representation of the symbol
512+
* @param str the text string
513+
* @param start the text start position
514+
* @param keywords an optional set to search for {@code str} as a member to
515+
* indicate a keyword
516+
* @param caseSensitive a value indicating if {@code keywords} should be
517+
* searched for {@code str} as-is ({@code true}) or if the lower-case
518+
* equivalent of {@code str} should be used ({@code false}).
519+
* @return true if the {@code str} was not in {@code keywords} or if
520+
* {@code keywords} was null
521+
*/
522+
protected boolean onFilteredSymbolMatched(String literal, String str, int start,
523+
Set<String> keywords, boolean caseSensitive) {
486524

487525
if (keywords != null) {
488-
String check = caseSensitive ? str : str.toLowerCase(Locale.ROOT);
526+
String check = caseSensitive ? literal : literal.toLowerCase(Locale.ROOT);
489527
if (keywords.contains(check)) {
490-
onKeywordMatched(str, start);
528+
onKeywordMatched(literal, start);
491529
return false;
492530
}
493531
}
494-
onSymbolMatched(str, start);
532+
onSymbolMatched(literal, str, start);
495533
return true;
496534
}
497535

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public static boolean writeSymbol(Writer out, Definitions defs,
277277

278278
String[] strs = new String[1];
279279
strs[0] = "";
280-
String check = caseSensitive ? symbol : symbol.toLowerCase(Locale.ROOT);
280+
String check = caseSensitive ? literal : literal.toLowerCase(Locale.ROOT);
281281
if (isKeyword || (keywords != null && keywords.contains( check ))) {
282282
// This is a keyword, so we don't create a link.
283283
out.append("<b>");

src/org/opensolaris/opengrok/analysis/fortran/FortranAnalyzer.java

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
package org.opensolaris.opengrok.analysis.fortran;
2525

2626
import java.io.Reader;
27+
import java.util.function.Function;
28+
import org.opensolaris.opengrok.analysis.Definitions;
2729
import org.opensolaris.opengrok.analysis.FileAnalyzer;
2830
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
2931
import org.opensolaris.opengrok.analysis.JFlexXref;
@@ -36,6 +38,10 @@
3638
*/
3739
public class FortranAnalyzer extends AbstractSourceCodeAnalyzer {
3840

41+
public static final Function<String, String> NORMALIZE = (id) -> {
42+
return id.toLowerCase() + "_";
43+
};
44+
3945
FortranAnalyzer(FortranAnalyzerFactory factory) {
4046
super(factory, new JFlexTokenizer(new FortranSymbolTokenizer(
4147
FileAnalyzer.dummyReader)));
@@ -50,4 +56,15 @@ public class FortranAnalyzer extends AbstractSourceCodeAnalyzer {
5056
protected JFlexXref newXref(Reader reader) {
5157
return new JFlexXref(new FortranXref(reader), getFactory().getEnv());
5258
}
59+
60+
@Override
61+
public Definitions normalizeDefinitions(Definitions oldDefs) {
62+
Definitions defs = new Definitions();
63+
for (Definitions.Tag tag : oldDefs.getTags()) {
64+
defs.addTag(tag.line, NORMALIZE.apply(tag.symbol).intern(),
65+
tag.type, tag.text, tag.namespace, tag.signature,
66+
tag.lineStart, tag.lineEnd);
67+
}
68+
return defs;
69+
}
5370
}

src/org/opensolaris/opengrok/analysis/fortran/FortranSymbolTokenizer.lex

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import org.opensolaris.opengrok.analysis.JFlexSymbolMatcher;
5151
^[^ \t\f\r\n]+ { yybegin(SCOMMENT); }
5252
{Identifier} {String id = yytext();
5353
if (!Consts.kwd.contains(id.toLowerCase(Locale.ROOT))) {
54-
onSymbolMatched(id, yychar);
54+
onSymbolMatched(id, FortranAnalyzer.NORMALIZE.apply(id), yychar);
5555
return yystate(); }
5656
}
5757

src/org/opensolaris/opengrok/analysis/fortran/FortranXref.lex

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ File = [a-zA-Z]{FNameChar}* ".inc"
8989
String id = yytext();
9090
// For historical reasons, FortranXref doesn't link identifiers of length=1
9191
if (id.length() > 1) {
92-
onFilteredSymbolMatched(id, yychar, Consts.kwd, false);
92+
onFilteredSymbolMatched(id, FortranAnalyzer.NORMALIZE.apply(id), yychar, Consts.kwd, false);
9393
} else {
9494
onNonSymbolMatched(id, yychar);
9595
}

src/org/opensolaris/opengrok/analysis/plain/PlainAnalyzer.java

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut)
109109
if (fullpath != null && ctags != null) {
110110
defs = ctags.doCtags(fullpath);
111111
if (defs != null && defs.numberOfSymbols() > 0) {
112+
defs = normalizeDefinitions(defs);
112113
tryAddingDefs(doc, defs, src, fullpath);
113114
byte[] tags = defs.serialize();
114115
doc.add(new StoredField(QueryBuilder.TAGS, tags));

test/org/opensolaris/opengrok/analysis/JFlexTokenizerTest.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public void testOffsetAttribute() throws Exception {
7676
// create a text fragment that it understands
7777
testOffsetAttribute(FortranSymbolTokenizer.class,
7878
"1 token1 = token2 + token3",
79-
new String[]{"token1", "token2", "token3"});
79+
new String[]{"token1", "token2", "token3"},
80+
true);
8081
}
8182

8283
/**
@@ -98,6 +99,11 @@ private void testOffsetAttribute(Class<? extends JFlexSymbolMatcher> klass)
9899
private void testOffsetAttribute(Class<? extends JFlexSymbolMatcher> klass,
99100
String inputText, String[] expectedTokens)
100101
throws Exception {
102+
testOffsetAttribute(klass, inputText, expectedTokens, false);
103+
}
104+
private void testOffsetAttribute(Class<? extends JFlexSymbolMatcher> klass,
105+
String inputText, String[] expectedTokens, Boolean matchPrefix)
106+
throws Exception {
101107
JFlexSymbolMatcher matcher = klass.getConstructor(Reader.class).
102108
newInstance(new StringReader(inputText));
103109
JFlexTokenizer tokenizer = new JFlexTokenizer(matcher);
@@ -109,7 +115,11 @@ private void testOffsetAttribute(Class<? extends JFlexSymbolMatcher> klass,
109115
while (tokenizer.incrementToken()) {
110116
assertTrue("too many tokens", count < expectedTokens.length);
111117
String expected = expectedTokens[count];
112-
assertEquals("term", expected, term.toString());
118+
if (matchPrefix) {
119+
assertEquals("term", term.toString().indexOf(expected), 0);
120+
} else {
121+
assertEquals("term", expected, term.toString());
122+
}
113123
assertEquals("start",
114124
inputText.indexOf(expected), offset.startOffset());
115125
assertEquals("end",

test/org/opensolaris/opengrok/analysis/fortran/FortranSymbolTokenizerTest.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727
import java.io.BufferedReader;
2828
import java.io.InputStream;
2929
import java.io.InputStreamReader;
30+
import java.util.AbstractMap.SimpleEntry;
3031
import java.util.ArrayList;
32+
import java.util.HashMap;
3133
import java.util.List;
34+
import java.util.Map;
3235
import static org.junit.Assert.assertNotNull;
3336
import org.junit.Test;
3437
import static org.opensolaris.opengrok.util.CustomAssertions.assertSymbolStream;
@@ -52,17 +55,23 @@ public void testFortranSymbolStream() throws Exception {
5255
assertNotNull("despite samplesymbols.txt as resource,", wdsres);
5356

5457
List<String> expectedSymbols = new ArrayList<>();
58+
Map<Integer, SimpleEntry<String, String>> overrides = new HashMap<>();
59+
int i = 1;
5560
try (BufferedReader wdsr = new BufferedReader(new InputStreamReader(
5661
wdsres, "UTF-8"))) {
5762
String line;
5863
while ((line = wdsr.readLine()) != null) {
5964
int hasho = line.indexOf('#');
6065
if (hasho != -1) line = line.substring(0, hasho);
61-
expectedSymbols.add(line.trim());
66+
String literal = line.trim();
67+
String symbol = FortranAnalyzer.NORMALIZE.apply(literal);
68+
expectedSymbols.add(symbol);
69+
overrides.put(i, new SimpleEntry(literal, symbol));
70+
i++;
6271
}
6372
}
6473

6574
assertSymbolStream(FortranSymbolTokenizer.class, fres,
66-
expectedSymbols);
75+
overrides, expectedSymbols);
6776
}
6877
}

test/org/opensolaris/opengrok/analysis/fortran/FortranXrefTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ private int writeFortranXref(PrintStream oss, InputStream iss,
108108
analyzer.setFoldingEnabled(true);
109109
WriteXrefArgs wargs = new WriteXrefArgs(
110110
new InputStreamReader(iss, "UTF-8"), sw);
111+
if (defs != null) {
112+
defs = analyzer.normalizeDefinitions(defs);
113+
}
111114
wargs.setDefs(defs);
112115
Xrefer xref = analyzer.writeXref(wargs);
113116
oss.print(sw.toString());

0 commit comments

Comments
 (0)