Skip to content

Commit b265abf

Browse files
authored
Merge pull request #1921 from idodeclare/feature/joint_lexer_tests
Feature/joint lexer tests
2 parents 22799a1 + 06e07b0 commit b265abf

24 files changed

+634
-426
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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;
25+
26+
import java.io.IOException;
27+
28+
/**
29+
* Represents an API for JFlex lexers that produce multiple types of derived
30+
* OpenGrok documents (e.g., cross-reference documents [xrefs] or Lucene search
31+
* documents [tokenizers]) from the same JFlex productions.
32+
*/
33+
public interface JFlexJointLexer extends JFlexStackingLexer {
34+
35+
/**
36+
* Passes non-symbolic fragment for processing.
37+
* @param value the excised fragment
38+
* @throws IOException if an error occurs while accepting
39+
*/
40+
void offer(String value) throws IOException;
41+
42+
/**
43+
* Passes non-symbolic fragment for processing, with a hint that it
44+
* contains non-word-like characters.
45+
* @param value the excised fragment
46+
* @throws IOException if an error occurs while accepting
47+
*/
48+
void offerNonword(String value) throws IOException;
49+
50+
/**
51+
* Passes a text fragment that is syntactically a symbol for processing.
52+
* @param value the excised symbol
53+
* @param captureOffset the offset from yychar where {@code value} began
54+
* @param ignoreKwd a value indicating whether keywords should be ignored
55+
* @return true if the {@code value} was not in keywords or if the
56+
* {@code ignoreKwd} was true
57+
* @throws IOException if an error occurs while accepting
58+
*/
59+
boolean offerSymbol(String value, int captureOffset, boolean ignoreKwd)
60+
throws IOException;
61+
62+
/**
63+
* Indicates that something unusual happened where normally a symbol would
64+
* have been offered.
65+
*/
66+
void skipSymbol();
67+
68+
/**
69+
* Passes a text fragment that is syntactically a keyword symbol for
70+
* processing.
71+
* @param value the excised symbol
72+
* @throws IOException if an error occurs while accepting
73+
*/
74+
void offerKeyword(String value) throws IOException;
75+
76+
/**
77+
* Indicates that the current line is ended.
78+
* @throws IOException if an error occurs when handling the EOL
79+
*/
80+
void startNewLine() throws IOException;
81+
82+
/**
83+
* Indicates the closing of an open tag and the opening -- if
84+
* {@code className} is non-null -- of a new one.
85+
* @param className the class name for the new tag or {@code null} just to
86+
* close an open tag.
87+
* @throws IOException if an output error occurs
88+
*/
89+
void disjointSpan(String className) throws IOException;
90+
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,8 @@ protected void appendLink(String url, boolean doEndingPushback,
273273
if (doEndingPushback) {
274274
n = StringUtils.countURIEndingPushback(url);
275275
}
276-
if (collateralCapture != null) {
277-
int o = StringUtils.patindexOf(url, collateralCapture);
278-
if (o > 0) {
279-
int ccn = url.length() - o;
280-
if (ccn > n) n = ccn;
281-
}
282-
}
276+
int ccn = StringUtils.countPushback(url, collateralCapture);
277+
if (ccn > n) n = ccn;
283278
// Push back if positive, but not if equal to the current length.
284279
if (n > 0 && n < url.length()) {
285280
yypushback(n);

src/org/opensolaris/opengrok/analysis/ada/AdaLexHelper.java

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,58 +25,26 @@
2525

2626
import java.io.IOException;
2727
import org.opensolaris.opengrok.analysis.Resettable;
28+
import org.opensolaris.opengrok.analysis.JFlexJointLexer;
2829

2930
/**
3031
* Represents an API for object's using {@link AdaLexHelper}
3132
*/
32-
interface AdaLexListener {
33-
void take(String value) throws IOException;
34-
void takeNonword(String value) throws IOException;
35-
36-
/**
37-
* Passes a text fragment that is syntactically a symbol for processing.
38-
* @param value the excised symbol
39-
* @param captureOffset the offset from yychar where {@code value} began
40-
* @param ignoreKwd a value indicating whether keywords should be ignored
41-
* @return true if the {@code value} was not in keywords or if the
42-
* {@code ignoreKwd} was true
43-
*/
44-
boolean takeSymbol(String value, int captureOffset, boolean ignoreKwd)
45-
throws IOException;
46-
47-
/**
48-
* Indicates that something unusual happened where normally a symbol would
49-
* have been written.
50-
*/
51-
void skipSymbol();
52-
53-
/**
54-
* Passes a text fragment that is syntactically a keyword symbol for
55-
* processing
56-
* @param value the excised symbol
57-
*/
58-
void takeKeyword(String value) throws IOException;
59-
60-
/**
61-
* Indicates that the current line is ended.
62-
*
63-
* @throws IOException thrown on error when handling the EOL
64-
*/
65-
void startNewLine() throws IOException;
33+
interface AdaLexer extends JFlexJointLexer {
6634
}
6735

6836
/**
6937
* Represents a helper for Ada lexers
7038
*/
7139
class AdaLexHelper implements Resettable {
7240

73-
private final AdaLexListener listener;
41+
private final AdaLexer lexer;
7442

75-
public AdaLexHelper(AdaLexListener listener) {
76-
if (listener == null) {
77-
throw new IllegalArgumentException("`listener' is null");
43+
public AdaLexHelper(AdaLexer lexer) {
44+
if (lexer == null) {
45+
throw new IllegalArgumentException("`lexer' is null");
7846
}
79-
this.listener = listener;
47+
this.lexer = lexer;
8048
}
8149

8250
/**
@@ -91,10 +59,10 @@ public void reset() {
9159
* Write {@code value} to output -- if it contains any EOLs then the
9260
* {@code startNewLine()} is called in lieu of outputting EOL.
9361
*/
94-
public void takeLiteral(String value, String linePrefix, String lineSuffix)
62+
public void takeLiteral(String value, String className)
9563
throws IOException {
9664

97-
if (linePrefix != null) listener.take(linePrefix);
65+
lexer.disjointSpan(className);
9866

9967
int off = 0;
10068
do {
@@ -103,7 +71,7 @@ public void takeLiteral(String value, String linePrefix, String lineSuffix)
10371
ni = value.indexOf("\n", off);
10472
if (ri == -1 && ni == -1) {
10573
String sub = value.substring(off);
106-
listener.takeNonword(sub);
74+
lexer.offerNonword(sub);
10775
break;
10876
}
10977
if (ri != -1 && ni != -1) {
@@ -122,13 +90,13 @@ public void takeLiteral(String value, String linePrefix, String lineSuffix)
12290
}
12391

12492
String sub = value.substring(off, i);
125-
listener.takeNonword(sub);
126-
if (lineSuffix != null) listener.take(lineSuffix);
127-
listener.startNewLine();
128-
if (linePrefix != null) listener.take(linePrefix);
93+
lexer.offerNonword(sub);
94+
lexer.disjointSpan(null);
95+
lexer.startNewLine();
96+
lexer.disjointSpan(className);
12997
off = i + w;
13098
} while (off < value.length());
13199

132-
if (lineSuffix != null) listener.take(lineSuffix);
100+
lexer.disjointSpan(null);
133101
}
134102
}

src/org/opensolaris/opengrok/analysis/ada/AdaProductions.lexh

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,27 +141,27 @@ File = [a-zA-Z]{FNameChar}* "." {FileExt}
141141
<YYINITIAL> {
142142
{Identifier} {
143143
String id = yytext();
144-
if (takeSymbol(id, 0, false) && returnOnSymbol()) {
144+
if (offerSymbol(id, 0, false) && returnOnSymbol()) {
145145
return yystate();
146146
}
147147
}
148148

149149
{Character_literal} {
150-
h.takeLiteral(yytext(), HtmlConsts.SPAN_S, HtmlConsts.ZSPAN);
150+
h.takeLiteral(yytext(), HtmlConsts.STRING_CLASS);
151151
}
152152

153153
{Numeric_literal} {
154-
h.takeLiteral(yytext(), HtmlConsts.SPAN_N, HtmlConsts.ZSPAN);
154+
h.takeLiteral(yytext(), HtmlConsts.NUMBER_CLASS);
155155
}
156156

157157
{String_literal} {
158-
h.takeLiteral(yytext(), HtmlConsts.SPAN_S, HtmlConsts.ZSPAN);
158+
h.takeLiteral(yytext(), HtmlConsts.STRING_CLASS);
159159
}
160160

161161
{Comment_token} {
162162
yypush(SCOMMENT);
163-
take(HtmlConsts.SPAN_C);
164-
takeNonword(yytext());
163+
disjointSpan(HtmlConsts.COMMENT_CLASS);
164+
offerNonword(yytext());
165165
}
166166
}
167167

@@ -171,7 +171,7 @@ File = [a-zA-Z]{FNameChar}* "." {FileExt}
171171
String capture = yytext();
172172
yypushback(capture.length());
173173
yypop();
174-
take(HtmlConsts.ZSPAN);
174+
disjointSpan(null);
175175
}
176176
}
177177

@@ -184,16 +184,16 @@ File = [a-zA-Z]{FNameChar}* "." {FileExt}
184184

185185
<YYINITIAL, SCOMMENT> {
186186
[\&\<\>\"\'] {
187-
takeNonword(yytext());
187+
offerNonword(yytext());
188188
}
189189

190190
// Only one whitespace char at a time
191191
{WhspChar} {
192-
take(yytext());
192+
offer(yytext());
193193
}
194194

195195
[!-~] {
196-
take(yytext());
196+
offer(yytext());
197197
}
198198

199199
[^\n\r] {
@@ -205,19 +205,19 @@ File = [a-zA-Z]{FNameChar}* "." {FileExt}
205205
<SCOMMENT> {
206206
{FPath} {
207207
if (takeAllContent()) {
208-
take(Util.breadcrumbPath(getUrlPrefix() + "path=",yytext(),'/'));
208+
offer(Util.breadcrumbPath(getUrlPrefix() + "path=",yytext(),'/'));
209209
}
210210
}
211211

212212
{File} {
213213
if (takeAllContent()) {
214214
String path = yytext();
215-
take("<a href=\"" + getUrlPrefix() + "path=");
216-
take(path);
215+
offer("<a href=\"" + getUrlPrefix() + "path=");
216+
offer(path);
217217
appendProject();
218-
take("\">");
219-
take(path);
220-
take("</a>");
218+
offer("\">");
219+
offer(path);
220+
offer("</a>");
221221
}
222222
}
223223

src/org/opensolaris/opengrok/analysis/ada/AdaSymbolTokenizer.lex

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import org.opensolaris.opengrok.web.Util;
3737
%public
3838
%class AdaSymbolTokenizer
3939
%extends JFlexTokenizer
40-
%implements AdaLexListener
40+
%implements AdaLexer
4141
%unicode
4242
%ignorecase
4343
%int
@@ -63,12 +63,12 @@ import org.opensolaris.opengrok.web.Util;
6363
}
6464

6565
@Override
66-
public void take(String value) throws IOException {
66+
public void offer(String value) throws IOException {
6767
// noop
6868
}
6969

7070
@Override
71-
public void takeNonword(String value) throws IOException {
71+
public void offerNonword(String value) throws IOException {
7272
// noop
7373
}
7474

@@ -77,7 +77,7 @@ import org.opensolaris.opengrok.web.Util;
7777
}
7878

7979
@Override
80-
public boolean takeSymbol(String value, int captureOffset,
80+
public boolean offerSymbol(String value, int captureOffset,
8181
boolean ignoreKwd)
8282
throws IOException {
8383
if (ignoreKwd || !Consts.kwd.contains(value.toLowerCase())) {
@@ -97,7 +97,7 @@ import org.opensolaris.opengrok.web.Util;
9797
}
9898

9999
@Override
100-
public void takeKeyword(String value) throws IOException {
100+
public void offerKeyword(String value) throws IOException {
101101
lastSymbol = null;
102102
}
103103

@@ -106,6 +106,11 @@ import org.opensolaris.opengrok.web.Util;
106106
// noop
107107
}
108108

109+
@Override
110+
public void disjointSpan(String className) throws IOException {
111+
// noop
112+
}
113+
109114
protected boolean takeAllContent() {
110115
return false;
111116
}

src/org/opensolaris/opengrok/analysis/ada/AdaXref.lex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import org.opensolaris.opengrok.web.Util;
3838
%public
3939
%class AdaXref
4040
%extends JFlexXref
41-
%implements AdaLexListener
41+
%implements AdaLexer
4242
%unicode
4343
%ignorecase
4444
%int
@@ -63,12 +63,12 @@ import org.opensolaris.opengrok.web.Util;
6363
}
6464

6565
@Override
66-
public void take(String value) throws IOException {
66+
public void offer(String value) throws IOException {
6767
out.write(value);
6868
}
6969

7070
@Override
71-
public void takeNonword(String value) throws IOException {
71+
public void offerNonword(String value) throws IOException {
7272
out.write(htmlize(value));
7373
}
7474

@@ -80,7 +80,7 @@ import org.opensolaris.opengrok.web.Util;
8080
}
8181

8282
@Override
83-
public boolean takeSymbol(String value, int captureOffset,
83+
public boolean offerSymbol(String value, int captureOffset,
8484
boolean ignoreKwd)
8585
throws IOException {
8686
if (ignoreKwd) {
@@ -101,7 +101,7 @@ import org.opensolaris.opengrok.web.Util;
101101
}
102102

103103
@Override
104-
public void takeKeyword(String value) throws IOException {
104+
public void offerKeyword(String value) throws IOException {
105105
writeKeyword(value, yyline);
106106
}
107107

0 commit comments

Comments
 (0)