Skip to content

Commit e506a23

Browse files
committed
Simplify ShXref by extending JFlexXrefSimple
... Avoids no-class <span> elements but otherwise keeps the Sh-specific extra styleStack.
1 parent c2598dd commit e506a23

File tree

4 files changed

+98
-87
lines changed

4 files changed

+98
-87
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,15 @@ public void disjointSpan(String className) throws IOException {
420420
disjointSpanClassName = className;
421421
}
422422

423+
/**
424+
* Gets the argument from the last call to
425+
* {@link #disjointSpan(java.lang.String)}.
426+
* @return a defined value or null
427+
*/
428+
public String getDisjointSpanClassName() {
429+
return disjointSpanClassName;
430+
}
431+
423432
/**
424433
* Write xref to the specified {@code Writer}.
425434
*

src/org/opensolaris/opengrok/analysis/sh/ShSymbolTokenizer.lex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
*/
2424

2525
package org.opensolaris.opengrok.analysis.sh;
26-
import java.io.IOException;
27-
import java.io.Reader;
26+
2827
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
2928
%%
3029
%public

src/org/opensolaris/opengrok/analysis/sh/ShXref.lex

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
*/
2424

2525
package org.opensolaris.opengrok.analysis.sh;
26-
import org.opensolaris.opengrok.analysis.JFlexXref;
26+
2727
import java.io.IOException;
28-
import org.opensolaris.opengrok.web.Util;
2928
import java.util.Stack;
30-
29+
import org.opensolaris.opengrok.analysis.JFlexXrefSimple;
30+
import org.opensolaris.opengrok.web.HtmlConsts;
31+
import org.opensolaris.opengrok.web.Util;
3132
%%
3233
%public
3334
%class ShXref
34-
%extends JFlexXref
35+
%extends JFlexXrefSimple
3536
%unicode
3637
%int
3738
%include CommonXref.lexh
3839
%{
39-
private final Stack<Integer> stateStack = new Stack<Integer>();
4040
private final Stack<String> styleStack = new Stack<String>();
4141

4242
// State variables for the HEREDOC state. They tell what the stop word is,
@@ -46,9 +46,8 @@ import java.util.Stack;
4646
private boolean heredocStripLeadingTabs;
4747

4848
@Override
49-
public void reset() {
50-
super.reset();
51-
stateStack.clear();
49+
protected void clearStack() {
50+
super.clearStack();
5251
styleStack.clear();
5352
}
5453

@@ -58,32 +57,21 @@ import java.util.Stack;
5857
@Override
5958
protected void setLineNumber(int x) { yyline = x; }
6059

61-
private void pushstate(int state, String style) throws IOException {
62-
if (!styleStack.empty()) {
63-
out.write("</span>");
64-
}
65-
if (style == null) {
66-
out.write("<span>");
67-
} else {
68-
out.write("<span class=\"" + style + "\">");
69-
}
70-
stateStack.push(yystate());
71-
styleStack.push(style);
72-
yybegin(state);
60+
@Override
61+
public void pushSpan(int newState, String className) throws IOException {
62+
super.pushSpan(newState, className);
63+
styleStack.push(className);
7364
}
7465

75-
private void popstate() throws IOException {
76-
out.write("</span>");
77-
yybegin(stateStack.pop());
78-
styleStack.pop();
79-
if (!styleStack.empty()) {
80-
String style = styleStack.peek();
81-
if (style == null) {
82-
out.write("<span>");
83-
} else {
84-
out.write("<span class=\"" + style + "\">");
66+
@Override
67+
public void yypop() throws IOException {
68+
super.yypop();
69+
styleStack.pop();
70+
71+
if (!styleStack.empty()) {
72+
String style = styleStack.peek();
73+
disjointSpan(style);
8574
}
86-
}
8775
}
8876

8977
/**
@@ -150,7 +138,8 @@ File = {FNameChar}+ "." ([a-zA-Z]+)
150138
state on the stack to prevent premature exit from the
151139
STRING state. */
152140
\$\{ {Identifier} \[\" {
153-
out.write(yytext()); pushstate(STRING, "s");
141+
out.write(yytext());
142+
pushSpan(STRING, HtmlConsts.STRING_CLASS);
154143
}
155144
}
156145

@@ -160,11 +149,25 @@ File = {FNameChar}+ "." ([a-zA-Z]+)
160149
writeSymbol(id, Consts.shkwd, yyline);
161150
}
162151

163-
{Number} { out.write("<span class=\"n\">"); out.write(yytext()); out.write("</span>"); }
152+
{Number} {
153+
String lastClassName = getDisjointSpanClassName();
154+
disjointSpan(HtmlConsts.NUMBER_CLASS);
155+
out.write(yytext());
156+
disjointSpan(lastClassName);
157+
}
164158

165-
\$ ? \" { pushstate(STRING, "s"); out.write(yytext()); }
166-
\$ ? \' { pushstate(QSTRING, "s"); out.write(yytext()); }
167-
"#" { pushstate(SCOMMENT, "c"); out.write(yytext()); }
159+
\$ ? \" {
160+
pushSpan(STRING, HtmlConsts.STRING_CLASS);
161+
out.write(yytext());
162+
}
163+
\$ ? \' {
164+
pushSpan(QSTRING, HtmlConsts.STRING_CLASS);
165+
out.write(yytext());
166+
}
167+
"#" {
168+
pushSpan(SCOMMENT, HtmlConsts.COMMENT_CLASS);
169+
out.write(yytext());
170+
}
168171

169172
// Recognize here-documents. At least a subset of them.
170173
"<<" "-"? {WhspChar}* {Identifier} {WhspChar}* {
@@ -173,7 +176,7 @@ File = {FNameChar}+ "." ([a-zA-Z]+)
173176

174177
heredocStripLeadingTabs = (text.charAt(2) == '-');
175178
heredocStopWord = text.substring(heredocStripLeadingTabs ? 3 : 2).trim();
176-
pushstate(HEREDOC, "s");
179+
pushSpan(HEREDOC, HtmlConsts.STRING_CLASS);
177180
}
178181

179182
// Any sequence of more than two < characters should not start HEREDOC. Use
@@ -186,37 +189,46 @@ File = {FNameChar}+ "." ([a-zA-Z]+)
186189

187190
<STRING> {
188191
\" {WhspChar}* \" { out.write(yytext()); }
189-
\" { out.write(yytext()); popstate(); }
192+
\" { out.write(yytext()); yypop(); }
190193
\\\\ | \\\" | \\\$ | \\` { out.write(yytext()); }
191-
\$\( { pushstate(SUBSHELL, null); out.write(yytext()); }
192-
` { pushstate(BACKQUOTE, null); out.write(yytext()); }
194+
\$\( {
195+
pushSpan(SUBSHELL, null);
196+
out.write(yytext());
197+
}
198+
` {
199+
pushSpan(BACKQUOTE, null);
200+
out.write(yytext());
201+
}
193202

194203
/* Bug #15661: Recognize ksh command substitution within strings. According
195204
* to ksh man page http://www2.research.att.com/~gsf/man/man1/ksh-man.html#Command%20Substitution
196205
* the opening brace must be followed by a blank.
197206
*/
198207
"${" / {WhspChar} | {EOL} {
199-
pushstate(BRACEGROUP, null); out.write(yytext());
208+
pushSpan(BRACEGROUP, null);
209+
out.write(yytext());
200210
}
201211
}
202212

203213
<QSTRING> {
204214
\' {WhspChar}* \' { out.write(yytext()); }
205215
\\' { out.write("\\'"); }
206-
\' { out.write(yytext()); popstate(); }
216+
\' { out.write(yytext()); yypop(); }
207217
}
208218

209219
<SCOMMENT> {
210-
{EOL} { popstate();
211-
startNewLine();}
220+
{EOL} {
221+
yypop();
222+
startNewLine();
223+
}
212224
}
213225

214226
<SUBSHELL> {
215-
\) { out.write(yytext()); popstate(); }
227+
\) { out.write(yytext()); yypop(); }
216228
}
217229

218230
<BACKQUOTE> {
219-
` { out.write(yytext()); popstate(); }
231+
` { out.write(yytext()); yypop(); }
220232
}
221233

222234
<BRACEGROUP> {
@@ -225,15 +237,15 @@ File = {FNameChar}+ "." ([a-zA-Z]+)
225237
* the closing brace must be on beginning of line, or it must be preceded by
226238
* a semi-colon and (optionally) whitespace.
227239
*/
228-
^ {WhspChar}* \} { out.write(yytext()); popstate(); }
229-
; {WhspChar}* \} { out.write(yytext()); popstate(); }
240+
^ {WhspChar}* \} { out.write(yytext()); yypop(); }
241+
; {WhspChar}* \} { out.write(yytext()); yypop(); }
230242
}
231243

232244
<HEREDOC> {
233245
[^\n]+ {
234246
String line = yytext();
235247
if (isHeredocStopWord(line)) {
236-
popstate();
248+
yypop();
237249
}
238250
out.write(Util.htmlize(line));
239251
}
@@ -249,8 +261,8 @@ File = {FNameChar}+ "." ([a-zA-Z]+)
249261
/* $# should not start a comment. */
250262
"$#" { out.write(yytext()); }
251263

252-
\$ ? \( { pushstate(SUBSHELL, null); out.write(yytext()); }
253-
` { pushstate(BACKQUOTE, null); out.write(yytext()); }
264+
\$ ? \( { pushSpan(SUBSHELL, null); out.write(yytext()); }
265+
` { pushSpan(BACKQUOTE, null); out.write(yytext()); }
254266

255267
/* Bug #15661: Recognize ksh command substitution within strings. According
256268
* to ksh man page http://www2.research.att.com/~gsf/man/man1/ksh-man.html#Command%20Substitution
@@ -259,7 +271,7 @@ File = {FNameChar}+ "." ([a-zA-Z]+)
259271
* group too early if the ${ cmd; } expression contains nested { cmd; } groups.
260272
*/
261273
\$ ? \{ / {WhspChar} | {EOL} {
262-
pushstate(BRACEGROUP, null); out.write(yytext());
274+
pushSpan(BRACEGROUP, null); out.write(yytext());
263275
}
264276
}
265277

@@ -297,12 +309,3 @@ File = {FNameChar}+ "." ([a-zA-Z]+)
297309
writeEMailAddress(yytext());
298310
}
299311
}
300-
301-
<<EOF>> {
302-
// If we reach EOF while being in a nested state, pop all the way up
303-
// the initial state so that we close open HTML tags.
304-
while (!stateStack.isEmpty()) {
305-
popstate();
306-
}
307-
return YYEOF;
308-
}

0 commit comments

Comments
 (0)