Skip to content

Commit e6b499e

Browse files
committed
Fix to support Swift nested comments
Also: - Avoid overlapping spans for comments,strings and scopes for readability while debugging.
1 parent a8c60cf commit e6b499e

File tree

4 files changed

+81
-24
lines changed

4 files changed

+81
-24
lines changed

src/org/opensolaris/opengrok/analysis/swift/SwiftSymbolTokenizer.lex

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

2929
package org.opensolaris.opengrok.analysis.swift;
3030

31+
import java.io.IOException;
3132
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
3233
%%
3334
%public
@@ -41,6 +42,15 @@ super(in);
4142
%int
4243
%include CommonTokenizer.lexh
4344
%char
45+
%{
46+
private int nestedComment;
47+
48+
@Override
49+
public void reset() throws IOException {
50+
super.reset();
51+
nestedComment = 0;
52+
}
53+
%}
4454

4555
%state STRING COMMENT SCOMMENT TSTRING
4656

@@ -71,7 +81,6 @@ super(in);
7181

7282
\" { yybegin(STRING); }
7383
\"\"\" { yybegin(TSTRING); }
74-
"/*" { yybegin(COMMENT); }
7584
"//" { yybegin(SCOMMENT); }
7685

7786
}
@@ -85,8 +94,20 @@ super(in);
8594
\"\"\" { yybegin(YYINITIAL); }
8695
}
8796

97+
<YYINITIAL, COMMENT> {
98+
"/*" {
99+
if (nestedComment++ == 0) {
100+
yybegin(COMMENT);
101+
}
102+
}
103+
}
104+
88105
<COMMENT> {
89-
"*/" { yybegin(YYINITIAL);}
106+
"*/" {
107+
if (--nestedComment == 0) {
108+
yybegin(YYINITIAL);
109+
}
110+
}
90111
}
91112

92113
<SCOMMENT> {

src/org/opensolaris/opengrok/analysis/swift/SwiftXref.lex

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

42+
private int nestedComment;
43+
44+
@Override
45+
public void reset() {
46+
super.reset();
47+
nestedComment = 0;
48+
}
49+
4250
// TODO move this into an include file when bug #16053 is fixed
4351
@Override
4452
protected int getLineNumber() { return yyline; }
@@ -120,11 +128,9 @@ ParamName = {Identifier} | "<" {Identifier} ">"
120128
out.write(htmlize(yytext()));
121129
}
122130
"/**" / [^/] {
123-
pushSpan(SDOC, HtmlConsts.COMMENT_CLASS);
124-
out.write(yytext());
125-
}
126-
"/*" {
127-
pushSpan(COMMENT, HtmlConsts.COMMENT_CLASS);
131+
if (nestedComment++ == 0) {
132+
pushSpan(SDOC, HtmlConsts.COMMENT_CLASS);
133+
}
128134
out.write(yytext());
129135
}
130136
"//" {
@@ -148,10 +154,34 @@ ParamName = {Identifier} | "<" {Identifier} ">"
148154
}
149155
}
150156

157+
<STRING, TSTRING> {
158+
{WhspChar}*{EOL} {
159+
disjointSpan(null);
160+
startNewLine();
161+
disjointSpan(HtmlConsts.STRING_CLASS);
162+
}
163+
}
164+
165+
<YYINITIAL, COMMENT, SDOC> {
166+
"/*" {
167+
if (nestedComment++ == 0) {
168+
pushSpan(COMMENT, HtmlConsts.COMMENT_CLASS);
169+
}
170+
out.write(yytext());
171+
}
172+
}
173+
151174
<COMMENT, SDOC> {
152175
"*/" {
153176
out.write(yytext());
154-
yypop();
177+
if (--nestedComment == 0) {
178+
yypop();
179+
}
180+
}
181+
{WhspChar}*{EOL} {
182+
disjointSpan(null);
183+
startNewLine();
184+
disjointSpan(HtmlConsts.COMMENT_CLASS);
155185
}
156186
}
157187

test/org/opensolaris/opengrok/analysis/swift/sample.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,6 @@ public class D2PDatePicker: UIView {
215215
private Int `class`
216216
}
217217
/*http://example.com.*/
218+
/* comment /* comment */
219+
comment
220+
*/

test/org/opensolaris/opengrok/analysis/swift/sample_xref.html

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@
9797
<a class="l" name="89" href="#89">89</a><span class='fold-space'>&nbsp;</span> &#125;
9898
<a class="hl" name="90" href="#90">90</a><span class='fold-space'>&nbsp;</span>
9999
<a class="l" name="91" href="#91">91</a><span class='fold-space'>&nbsp;</span>
100-
<a class="l" name="92" href="#92">92</a><span class='fold-space'>&nbsp;</span> <span class="c">/*init() { // 3 - programmatic initializer
101-
<a class="l" name="93" href="#93">93</a><span class='fold-space'>&nbsp;</span> <a href="/source/s?path=super.ini">super.ini</a>t(frame: CGRect()) // 4.
102-
<a class="l" name="94" href="#94">94</a><span class='fold-space'>&nbsp;</span>
103-
<a class="l" name="95" href="#95">95</a><span class='fold-space'>&nbsp;</span>
104-
<a class="l" name="96" href="#96">96</a><span class='fold-space'>&nbsp;</span> }*/</span>
100+
<a class="l" name="92" href="#92">92</a><span class='fold-space'>&nbsp;</span> <span class="c">/*init() { // 3 - programmatic initializer</span>
101+
<a class="l" name="93" href="#93">93</a><span class='fold-space'>&nbsp;</span><span class="c"> <a href="/source/s?path=super.ini">super.ini</a>t(frame: CGRect()) // 4.</span>
102+
<a class="l" name="94" href="#94">94</a><span class='fold-space'>&nbsp;</span><span class="c"></span>
103+
<a class="l" name="95" href="#95">95</a><span class='fold-space'>&nbsp;</span><span class="c"></span>
104+
<a class="l" name="96" href="#96">96</a><span class='fold-space'>&nbsp;</span><span class="c"> }*/</span>
105105
<a class="l" name="97" href="#97">97</a><span class='fold-space'>&nbsp;</span>
106106
<a class="l" name="98" href="#98">98</a><span class='fold-space'>&nbsp;</span> <b>override</b> <b>public</b> <b>func</b> <a class="xf" name="awakeFromNib"/><a href="/source/s?refs=awakeFromNib" class="xf intelliWindow-symbol" data-definition-place="def">awakeFromNib</a>() &#123;
107107
<a class="l" name="99" href="#99">99</a><span class='fold-space'>&nbsp;</span> <b>super</b>.<a class="d intelliWindow-symbol" href="#awakeFromNib" data-definition-place="defined-in-file">awakeFromNib</a>()
@@ -164,9 +164,9 @@
164164
<a class="l" name="156" href="#156">156</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#formatter" data-definition-place="defined-in-file">formatter</a>.<a href="/source/s?defs=dateFormat" class="intelliWindow-symbol" data-definition-place="undefined-in-file">dateFormat</a> = <span class="s">&quot;EEEE&quot;</span>
165165
<a class="l" name="157" href="#157">157</a><span class='fold-space'>&nbsp;</span> <b>self</b>.<a href="/source/s?defs=dayView" class="intelliWindow-symbol" data-definition-place="undefined-in-file">dayView</a>.<a href="/source/s?defs=weekDayLabel" class="intelliWindow-symbol" data-definition-place="undefined-in-file">weekDayLabel</a>.<a href="/source/s?defs=text" class="intelliWindow-symbol" data-definition-place="undefined-in-file">text</a> = <a class="d intelliWindow-symbol" href="#formatter" data-definition-place="defined-in-file">formatter</a>.<a href="/source/s?defs=string" class="intelliWindow-symbol" data-definition-place="undefined-in-file">string</a>(<a href="/source/s?defs=from" class="intelliWindow-symbol" data-definition-place="undefined-in-file">from</a>: <a href="/source/s?defs=date" class="intelliWindow-symbol" data-definition-place="undefined-in-file">date</a>)
166166
<a class="l" name="158" href="#158">158</a><span class='fold-space'>&nbsp;</span>
167-
<a class="l" name="159" href="#159">159</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#formatter" data-definition-place="defined-in-file">formatter</a>.<a href="/source/s?defs=dateFormat" class="intelliWindow-symbol" data-definition-place="undefined-in-file">dateFormat</a> = <span class="s">&quot;&quot;&quot;
168-
<a class="hl" name="160" href="#160">160</a><span class='fold-space'>&nbsp;</span> YYYY
169-
<a class="l" name="161" href="#161">161</a><span class='fold-space'>&nbsp;</span> &quot;&quot;&quot;</span>
167+
<a class="l" name="159" href="#159">159</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#formatter" data-definition-place="defined-in-file">formatter</a>.<a href="/source/s?defs=dateFormat" class="intelliWindow-symbol" data-definition-place="undefined-in-file">dateFormat</a> = <span class="s">&quot;&quot;&quot;</span>
168+
<a class="hl" name="160" href="#160">160</a><span class='fold-space'>&nbsp;</span><span class="s"> YYYY</span>
169+
<a class="l" name="161" href="#161">161</a><span class='fold-space'>&nbsp;</span><span class="s"> &quot;&quot;&quot;</span>
170170
<a class="l" name="162" href="#162">162</a><span class='fold-space'>&nbsp;</span> <b>self</b>.<a href="/source/s?defs=yearView" class="intelliWindow-symbol" data-definition-place="undefined-in-file">yearView</a>.<a href="/source/s?defs=yearLabel" class="intelliWindow-symbol" data-definition-place="undefined-in-file">yearLabel</a>.<a href="/source/s?defs=text" class="intelliWindow-symbol" data-definition-place="undefined-in-file">text</a> = <a class="d intelliWindow-symbol" href="#formatter" data-definition-place="defined-in-file">formatter</a>.<a href="/source/s?defs=string" class="intelliWindow-symbol" data-definition-place="undefined-in-file">string</a>(<a href="/source/s?defs=from" class="intelliWindow-symbol" data-definition-place="undefined-in-file">from</a>: <a href="/source/s?defs=date" class="intelliWindow-symbol" data-definition-place="undefined-in-file">date</a>)
171171
<a class="l" name="163" href="#163">163</a><span class='fold-space'>&nbsp;</span> &#125;
172172
<a class="l" name="164" href="#164">164</a><span class='fold-space'>&nbsp;</span>
@@ -212,16 +212,19 @@
212212
<a class="l" name="204" href="#204">204</a><span class='fold-space'>&nbsp;</span>
213213
<a class="l" name="205" href="#205">205</a><span class='fold-space'>&nbsp;</span> &#125;
214214
<a class="l" name="206" href="#206">206</a><span class='fold-space'>&nbsp;</span>
215-
<a class="l" name="207" href="#207">207</a><span class='fold-space'>&nbsp;</span> <span class="c">/*
216-
<a class="l" name="208" href="#208">208</a><span class='fold-space'>&nbsp;</span> // Only override draw() if you perform custom drawing.
217-
<a class="l" name="209" href="#209">209</a><span class='fold-space'>&nbsp;</span> // An empty implementation adversely affects performance during animation.
218-
<a class="hl" name="210" href="#210">210</a><span class='fold-space'>&nbsp;</span> override func draw(_ rect: CGRect) {
219-
<a class="l" name="211" href="#211">211</a><span class='fold-space'>&nbsp;</span> // Drawing code
220-
<a class="l" name="212" href="#212">212</a><span class='fold-space'>&nbsp;</span> }
221-
<a class="l" name="213" href="#213">213</a><span class='fold-space'>&nbsp;</span> */</span>
215+
<a class="l" name="207" href="#207">207</a><span class='fold-space'>&nbsp;</span> <span class="c">/*</span>
216+
<a class="l" name="208" href="#208">208</a><span class='fold-space'>&nbsp;</span><span class="c"> // Only override draw() if you perform custom drawing.</span>
217+
<a class="l" name="209" href="#209">209</a><span class='fold-space'>&nbsp;</span><span class="c"> // An empty implementation adversely affects performance during animation.</span>
218+
<a class="hl" name="210" href="#210">210</a><span class='fold-space'>&nbsp;</span><span class="c"> override func draw(_ rect: CGRect) {</span>
219+
<a class="l" name="211" href="#211">211</a><span class='fold-space'>&nbsp;</span><span class="c"> // Drawing code</span>
220+
<a class="l" name="212" href="#212">212</a><span class='fold-space'>&nbsp;</span><span class="c"> }</span>
221+
<a class="l" name="213" href="#213">213</a><span class='fold-space'>&nbsp;</span><span class="c"> */</span>
222222
<a class="l" name="214" href="#214">214</a><span class='fold-space'>&nbsp;</span>
223223
<a class="l" name="215" href="#215">215</a><span class='fold-space'>&nbsp;</span> <b>private</b> <a href="/source/s?defs=Int" class="intelliWindow-symbol" data-definition-place="undefined-in-file">Int</a> `<a href="/source/s?defs=class" class="intelliWindow-symbol" data-definition-place="undefined-in-file">class</a>`
224224
<a class="l" name="216" href="#216">216</a><span class='fold-space'>&nbsp;</span>&#125;
225225
</span><a class="l" name="217" href="#217">217</a><span class='fold-space'>&nbsp;</span><span class="c">/*<a href="http://example.com">http://example.com</a>.*/</span>
226-
<a class="l" name="218" href="#218">218</a><span class='fold-space'>&nbsp;</span></body>
226+
<a class="l" name="218" href="#218">218</a><span class='fold-space'>&nbsp;</span><span class="c">/* comment /* comment */</span>
227+
<a class="l" name="219" href="#219">219</a><span class='fold-space'>&nbsp;</span><span class="c">comment</span>
228+
<a class="hl" name="220" href="#220">220</a><span class='fold-space'>&nbsp;</span><span class="c">*/</span>
229+
<a class="l" name="221" href="#221">221</a><span class='fold-space'>&nbsp;</span></body>
227230
</html>

0 commit comments

Comments
 (0)