Skip to content

Commit c878e76

Browse files
committed
Fix to support Kotlin nested comments
Also: - Avoid overlapping spans for comments and scopes for readability while debugging.
1 parent 3b7ee0e commit c878e76

File tree

4 files changed

+213
-159
lines changed

4 files changed

+213
-159
lines changed

src/org/opensolaris/opengrok/analysis/kotlin/KotlinSymbolTokenizer.lex

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
package org.opensolaris.opengrok.analysis.kotlin;
3333

34+
import java.io.IOException;
3435
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
3536
%%
3637
%public
@@ -44,6 +45,15 @@ super(in);
4445
%int
4546
%include CommonTokenizer.lexh
4647
%char
48+
%{
49+
private int nestedComment;
50+
51+
@Override
52+
public void reset() throws IOException {
53+
super.reset();
54+
nestedComment = 0;
55+
}
56+
%}
4757

4858
%state STRING COMMENT SCOMMENT QSTRING TSTRING
4959

@@ -62,7 +72,6 @@ super(in);
6272
\" { yybegin(STRING); }
6373
\' { yybegin(QSTRING); }
6474
\"\"\" { yybegin(TSTRING); }
65-
"/*" { yybegin(COMMENT); }
6675
"//" { yybegin(SCOMMENT); }
6776

6877
}
@@ -99,8 +108,20 @@ super(in);
99108
}
100109
}
101110

111+
<YYINITIAL, COMMENT> {
112+
"/*" {
113+
if (nestedComment++ == 0) {
114+
yybegin(COMMENT);
115+
}
116+
}
117+
}
118+
102119
<COMMENT> {
103-
"*/" { yybegin(YYINITIAL);}
120+
"*/" {
121+
if (--nestedComment == 0) {
122+
yybegin(YYINITIAL);
123+
}
124+
}
104125
}
105126

106127
<SCOMMENT> {

src/org/opensolaris/opengrok/analysis/kotlin/KotlinXref.lex

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ import org.opensolaris.opengrok.web.Util;
4040
/* Must match {WhiteSpace} regex */
4141
private final static String WHITE_SPACE = "[ \\t\\f]+";
4242

43+
private int nestedComment;
44+
45+
@Override
46+
public void reset() {
47+
super.reset();
48+
nestedComment = 0;
49+
}
50+
4351
// TODO move this into an include file when bug #16053 is fixed
4452
@Override
4553
protected int getLineNumber() { return yyline; }
@@ -108,11 +116,9 @@ ParamName = {Identifier} | "<" {Identifier} ">"
108116
out.write(htmlize(yytext()));
109117
}
110118
"/**" / [^/] {
111-
pushSpan(KDOC, HtmlConsts.COMMENT_CLASS);
112-
out.write(yytext());
113-
}
114-
"/*" {
115-
pushSpan(COMMENT, HtmlConsts.COMMENT_CLASS);
119+
if (nestedComment++ == 0) {
120+
pushSpan(KDOC, HtmlConsts.COMMENT_CLASS);
121+
}
116122
out.write(yytext());
117123
}
118124
"//" {
@@ -161,12 +167,33 @@ ParamName = {Identifier} | "<" {Identifier} ">"
161167
writeSymbol(id, Consts.kwd, yyline);
162168
disjointSpan(HtmlConsts.STRING_CLASS);
163169
}
170+
{WhspChar}*{EOL} {
171+
disjointSpan(null);
172+
startNewLine();
173+
disjointSpan(HtmlConsts.STRING_CLASS);
174+
}
175+
}
176+
177+
<YYINITIAL, COMMENT, KDOC> {
178+
"/*" {
179+
if (nestedComment++ == 0) {
180+
pushSpan(COMMENT, HtmlConsts.COMMENT_CLASS);
181+
}
182+
out.write(yytext());
183+
}
164184
}
165185

166186
<COMMENT, KDOC> {
167187
"*/" {
168188
out.write(yytext());
169-
yypop();
189+
if (--nestedComment == 0) {
190+
yypop();
191+
}
192+
}
193+
{WhspChar}*{EOL} {
194+
disjointSpan(null);
195+
startNewLine();
196+
disjointSpan(HtmlConsts.COMMENT_CLASS);
170197
}
171198
}
172199

test/org/opensolaris/opengrok/analysis/kotlin/sample.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,6 @@ private fun getTag(clazz: Class<*>): String {
274274
}
275275
}
276276
/*http://example.com.*/
277+
/* comment /* comment */
278+
comment
279+
*/

0 commit comments

Comments
 (0)