Skip to content

Commit 3a17e10

Browse files
committed
Fix dangling CSharp span by subclassing JFlexXrefSimple
Also: - Make CSharpXref case-sensitive -- no patterns were affected. - Escape all HTML special characters in CSharpXref. - Use Common.xref in CSharpSymbolTokenizer. - Fix not to continue VSTRING after an end-quote.
1 parent 59849ba commit 3a17e10

File tree

5 files changed

+77
-44
lines changed

5 files changed

+77
-44
lines changed

src/org/opensolaris/opengrok/analysis/csharp/CSharpSymbolTokenizer.lex

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ super(in);
4545

4646
%state STRING COMMENT SCOMMENT QSTRING VSTRING
4747

48+
%include Common.lexh
4849
%include CSharp.lexh
4950
%%
5051

@@ -67,26 +68,31 @@ super(in);
6768
}
6869

6970
<STRING> {
71+
\\[\"\\] {}
7072
\" { yybegin(YYINITIAL); }
71-
\\\\ | \\\" {}
72-
}
73-
74-
<VSTRING> {
75-
"@\"" { yybegin(YYINITIAL);}
7673
}
7774

7875
<QSTRING> {
76+
\\[\'\\] {}
7977
\' { yybegin(YYINITIAL); }
8078
}
8179

80+
<VSTRING> {
81+
\\ |
82+
\"\" {}
83+
\" { yybegin(YYINITIAL);}
84+
}
85+
8286
<COMMENT> {
8387
"*/" { yybegin(YYINITIAL);}
8488
}
8589

8690
<SCOMMENT> {
87-
\n { yybegin(YYINITIAL);}
91+
{CsharpEOL} { yybegin(YYINITIAL);}
8892
}
8993

9094
<YYINITIAL, STRING, COMMENT, SCOMMENT, QSTRING, VSTRING> {
95+
{WhiteSpace} {}
96+
9197
[^] {}
9298
}

src/org/opensolaris/opengrok/analysis/csharp/CSharpXref.lex

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@
2828
*/
2929

3030
package org.opensolaris.opengrok.analysis.csharp;
31-
import org.opensolaris.opengrok.analysis.JFlexXref;
31+
import org.opensolaris.opengrok.analysis.JFlexXrefSimple;
3232
import java.io.IOException;
3333
import java.io.Writer;
3434
import java.io.Reader;
35+
import org.opensolaris.opengrok.web.HtmlConsts;
3536
import org.opensolaris.opengrok.web.Util;
3637

3738
%%
3839
%public
3940
%class CSharpXref
40-
%extends JFlexXref
41+
%extends JFlexXrefSimple
4142
%unicode
42-
%ignorecase
4343
%int
4444
%include CommonXref.lexh
4545
%{
@@ -50,7 +50,7 @@ import org.opensolaris.opengrok.web.Util;
5050
protected void setLineNumber(int x) { yyline = x; }
5151
%}
5252

53-
File = [a-zA-Z]{FNameChar}* "." ([chts]|"cs")
53+
File = [a-zA-Z]{FNameChar}* "." ([cChHtTsS]|[cC][sS])
5454

5555
%state STRING COMMENT SCOMMENT QSTRING VSTRING
5656

@@ -91,51 +91,78 @@ File = [a-zA-Z]{FNameChar}* "." ([chts]|"cs")
9191
/*{Hier}
9292
{ out.write(Util.breadcrumbPath(urlPrefix+"defs=",yytext(),'.'));}
9393
*/
94-
{Number} { out.write("<span class=\"n\">"); out.write(yytext()); out.write("</span>"); }
95-
96-
\" { yybegin(STRING);out.write("<span class=\"s\">\"");}
97-
\' { yybegin(QSTRING);out.write("<span class=\"s\">\'");}
98-
"/*" { yybegin(COMMENT);out.write("<span class=\"c\">/*");}
99-
"//" { yybegin(SCOMMENT);out.write("<span class=\"c\">//");}
100-
"@\"" { yybegin(VSTRING);out.write("<span class=\"s\">@\"");}
94+
{Number} {
95+
disjointSpan(HtmlConsts.NUMBER_CLASS);
96+
out.write(yytext());
97+
disjointSpan(null);
98+
}
99+
100+
\" {
101+
pushSpan(STRING, HtmlConsts.STRING_CLASS);
102+
out.write(htmlize(yytext()));
103+
}
104+
\' {
105+
pushSpan(QSTRING, HtmlConsts.STRING_CLASS);
106+
out.write(htmlize(yytext()));
107+
}
108+
"/*" {
109+
pushSpan(COMMENT, HtmlConsts.COMMENT_CLASS);
110+
out.write(yytext());
111+
}
112+
"//" {
113+
pushSpan(SCOMMENT, HtmlConsts.COMMENT_CLASS);
114+
out.write(yytext());
115+
}
116+
"@\"" {
117+
pushSpan(VSTRING, HtmlConsts.STRING_CLASS);
118+
out.write(htmlize(yytext()));
119+
}
101120
}
102121

103122
<STRING> {
104-
\" {WhiteSpace} \" { out.write(yytext());}
105-
\" { yybegin(YYINITIAL); out.write("\"</span>"); }
106-
\\\\ { out.write("\\\\"); }
107-
\\\" { out.write("\\\""); }
123+
\\[\"\\] |
124+
\" {WhiteSpace} \" { out.write(htmlize(yytext()));}
125+
\" {
126+
out.write(htmlize(yytext()));
127+
yypop();
128+
}
108129
}
109130

110131
<QSTRING> {
111-
"\\\\" { out.write("\\\\"); }
112-
"\\\'" { out.write("\\\'"); }
113-
\' {WhiteSpace} \' { out.write(yytext()); }
114-
\' { yybegin(YYINITIAL); out.write("'</span>"); }
132+
\\[\'\\] |
133+
\' {WhiteSpace} \' { out.write(htmlize(yytext())); }
134+
\' {
135+
out.write(htmlize(yytext()));
136+
yypop();
137+
}
115138
}
116139

117140
<VSTRING> {
118-
\" {WhiteSpace} \" { out.write(yytext());}
119-
"\"\"" { out.write("\"\""); }
120-
\" { yybegin(YYINITIAL); out.write("\"</span>"); }
121-
\\ { out.write("\\"); }
141+
\\ |
142+
\"\" { out.write(htmlize(yytext())); }
143+
\" {
144+
out.write(htmlize(yytext()));
145+
yypop();
146+
}
122147
}
123148

124149
<COMMENT> {
125-
"*/" { yybegin(YYINITIAL); out.write("*/</span>"); }
150+
"*/" {
151+
out.write(yytext());
152+
yypop();
153+
}
126154
}
127155

128156
<SCOMMENT> {
129157
{WhspChar}*{CsharpEOL} {
130-
yybegin(YYINITIAL); out.write("</span>");
158+
yypop();
131159
startNewLine();
132160
}
133161
}
134162

135163
<YYINITIAL, STRING, COMMENT, SCOMMENT, QSTRING, VSTRING> {
136-
"&" {out.write( "&amp;");}
137-
"<" {out.write( "&lt;");}
138-
">" {out.write( "&gt;");}
164+
[&<>\'\"] { out.write(htmlize(yytext())); }
165+
139166
{WhspChar}*{CsharpEOL} { startNewLine(); }
140167
{WhiteSpace} { out.write(yytext()); }
141168
[!-~] { out.write(yycharat(0)); }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ public void testCsharpXrefVerbatimString() throws IOException {
415415
CSharpXref xref = new CSharpXref(in);
416416
StringWriter out = new StringWriter();
417417
xref.write(out);
418-
assertTrue(out.toString().contains("<span class=\"s\">@\"\\some_windows_path_in_a_string\\\"</span>"));
418+
assertTrue(out.toString().contains("<span class=\"s\">@&quot;\\some_windows_path_in_a_string\\&quot;</span>"));
419419
}
420420

421421
/**

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<a class="l" name="8" href="#8">8</a><span class='fold-space'>&nbsp;</span>MIT License
1717
<a class="l" name="9" href="#9">9</a><span class='fold-space'>&nbsp;</span>
1818
<a class="hl" name="10" href="#10">10</a><span class='fold-space'>&nbsp;</span>Permission is hereby granted, free of charge, to any person obtaining a copy
19-
<a class="l" name="11" href="#11">11</a><span class='fold-space'>&nbsp;</span>of this software and associated documentation files (the ""Software""), to deal
19+
<a class="l" name="11" href="#11">11</a><span class='fold-space'>&nbsp;</span>of this software and associated documentation files (the &quot;&quot;Software&quot;&quot;), to deal
2020
<a class="l" name="12" href="#12">12</a><span class='fold-space'>&nbsp;</span>in the Software without restriction, including without limitation the rights
2121
<a class="l" name="13" href="#13">13</a><span class='fold-space'>&nbsp;</span>to use, copy, modify, merge, publish, distribute, sublicense, <a href="/source/s?path=and/">and</a>/<a href="/source/s?path=and/or">or</a> sell
2222
<a class="l" name="14" href="#14">14</a><span class='fold-space'>&nbsp;</span>copies of the Software, and to permit persons to whom the Software is
@@ -52,7 +52,7 @@
5252
<a class="l" name="44" href="#44">44</a><span class='fold-space'>&nbsp;</span>&#123;
5353
<a class="l" name="45" href="#45">45</a><span class='fold-space'>&nbsp;</span> <span class="c">/// &lt;summary&gt;</span>
5454
<a class="l" name="46" href="#46">46</a><span class='fold-space'>&nbsp;</span> <span class="c">///</span>
55-
<a class="l" name="47" href="#47">47</a><span class='fold-space'>&nbsp;</span> <span class="c">/// Wraps Hitesh's xml serializer in such a way that it will select the proper serializer based on the data</span>
55+
<a class="l" name="47" href="#47">47</a><span class='fold-space'>&nbsp;</span> <span class="c">/// Wraps Hitesh&apos;s xml serializer in such a way that it will select the proper serializer based on the data</span>
5656
<a class="l" name="48" href="#48">48</a><span class='fold-space'>&nbsp;</span> <span class="c">/// format.</span>
5757
<a class="l" name="49" href="#49">49</a><span class='fold-space'>&nbsp;</span> <span class="c">///</span>
5858
<a class="hl" name="50" href="#50">50</a><span class='fold-space'>&nbsp;</span> <span class="c">/// &lt;/summary&gt;</span>
@@ -97,15 +97,15 @@
9797
<a class="l" name="89" href="#89">89</a><span class='fold-space'>&nbsp;</span> <b>protected</b>
9898
<span id='scope_id_1140b754' class='scope-head'><span class='scope-signature'>Serialization(DataFormat dataFormat, string streamName)</span><a class="hl" name="90" href="#90">90</a><a style='cursor:pointer;' onclick='fold(this.parentNode.id)' id='scope_id_1140b754_fold_icon'><span class='fold-icon'>&nbsp;</span></a> <a class="xmt" name="Serialization"/><a href="/source/s?refs=Serialization" class="xmt intelliWindow-symbol" data-definition-place="def">Serialization</a>(<a class="d intelliWindow-symbol" href="#DataFormat" data-definition-place="defined-in-file">DataFormat</a> <a class="xa" name="dataFormat"/><a href="/source/s?refs=dataFormat" class="xa intelliWindow-symbol" data-definition-place="def">dataFormat</a>, <b>string</b> <a class="xa" name="streamName"/><a href="/source/s?refs=streamName" class="xa intelliWindow-symbol" data-definition-place="def">streamName</a>)</span>
9999
<span id='scope_id_1140b754_fold' class='scope-body'><a class="l" name="91" href="#91">91</a><span class='fold-space'>&nbsp;</span> &#123;
100-
<a class="l" name="92" href="#92">92</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#Dbg" data-definition-place="defined-in-file">Dbg</a>.<a href="/source/s?defs=Assert" class="intelliWindow-symbol" data-definition-place="undefined-in-file">Assert</a>(!<b>string</b>.<a href="/source/s?defs=IsNullOrEmpty" class="intelliWindow-symbol" data-definition-place="undefined-in-file">IsNullOrEmpty</a>(<a href="/source/s?defs=streamName" class="intelliWindow-symbol" data-definition-place="undefined-in-file">streamName</a>), <span class="s">"stream needs a name"</span>)&#59;
100+
<a class="l" name="92" href="#92">92</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#Dbg" data-definition-place="defined-in-file">Dbg</a>.<a href="/source/s?defs=Assert" class="intelliWindow-symbol" data-definition-place="undefined-in-file">Assert</a>(!<b>string</b>.<a href="/source/s?defs=IsNullOrEmpty" class="intelliWindow-symbol" data-definition-place="undefined-in-file">IsNullOrEmpty</a>(<a href="/source/s?defs=streamName" class="intelliWindow-symbol" data-definition-place="undefined-in-file">streamName</a>), <span class="s">&quot;stream needs a name&quot;</span>)&#59;
101101
<a class="l" name="93" href="#93">93</a><span class='fold-space'>&nbsp;</span>
102102
<a class="l" name="94" href="#94">94</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#format" data-definition-place="defined-in-file">format</a> = <a href="/source/s?defs=dataFormat" class="intelliWindow-symbol" data-definition-place="undefined-in-file">dataFormat</a>&#59;
103103
<a class="l" name="95" href="#95">95</a><span class='fold-space'>&nbsp;</span> <b>this</b>.<a href="/source/s?defs=streamName" class="intelliWindow-symbol" data-definition-place="undefined-in-file">streamName</a> = <a href="/source/s?defs=streamName" class="intelliWindow-symbol" data-definition-place="undefined-in-file">streamName</a>&#59;
104104
<a class="l" name="96" href="#96">96</a><span class='fold-space'>&nbsp;</span> &#125;
105105
</span><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>
107107
<a class="l" name="99" href="#99">99</a><span class='fold-space'>&nbsp;</span>
108-
<a class="hl" name="100" href="#100">100</a><span class='fold-space'>&nbsp;</span> <b>protected</b> <b>static</b> <b>string</b> <a class="xfld" name="XmlCliTag"/><a href="/source/s?refs=XmlCliTag" class="xfld intelliWindow-symbol" data-definition-place="def">XmlCliTag</a> = <span class="s">"#&lt; CLIXML"</span>&#59;
108+
<a class="hl" name="100" href="#100">100</a><span class='fold-space'>&nbsp;</span> <b>protected</b> <b>static</b> <b>string</b> <a class="xfld" name="XmlCliTag"/><a href="/source/s?refs=XmlCliTag" class="xfld intelliWindow-symbol" data-definition-place="def">XmlCliTag</a> = <span class="s">&quot;#&lt; CLIXML&quot;</span>&#59;
109109
<a class="l" name="101" href="#101">101</a><span class='fold-space'>&nbsp;</span>
110110
<a class="l" name="102" href="#102">102</a><span class='fold-space'>&nbsp;</span> <b>protected</b> <b>string</b> <a class="xfld" name="streamName"/><a href="/source/s?refs=streamName" class="xfld intelliWindow-symbol" data-definition-place="def">streamName</a>&#59;
111111
<a class="l" name="103" href="#103">103</a><span class='fold-space'>&nbsp;</span> <b>protected</b> <a class="d intelliWindow-symbol" href="#DataFormat" data-definition-place="defined-in-file">DataFormat</a> <a class="xfld" name="format"/><a href="/source/s?refs=format" class="xfld intelliWindow-symbol" data-definition-place="def">format</a>&#59;
@@ -121,7 +121,7 @@
121121
<span id='scope_id_51521845_fold' class='scope-body'><a class="l" name="113" href="#113">113</a><span class='fold-space'>&nbsp;</span> :
122122
<a class="l" name="114" href="#114">114</a><span class='fold-space'>&nbsp;</span> <b>base</b>(<a href="/source/s?defs=dataFormat" class="intelliWindow-symbol" data-definition-place="undefined-in-file">dataFormat</a>, <a href="/source/s?defs=streamName" class="intelliWindow-symbol" data-definition-place="undefined-in-file">streamName</a>)
123123
<a class="l" name="115" href="#115">115</a><span class='fold-space'>&nbsp;</span> &#123;
124-
<a class="l" name="116" href="#116">116</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#Dbg" data-definition-place="defined-in-file">Dbg</a>.<a href="/source/s?defs=Assert" class="intelliWindow-symbol" data-definition-place="undefined-in-file">Assert</a>(<a class="d intelliWindow-symbol" href="#output" data-definition-place="defined-in-file">output</a> != <b>null</b>, <span class="s">"output should have a value"</span>)&#59;
124+
<a class="l" name="116" href="#116">116</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#Dbg" data-definition-place="defined-in-file">Dbg</a>.<a href="/source/s?defs=Assert" class="intelliWindow-symbol" data-definition-place="undefined-in-file">Assert</a>(<a class="d intelliWindow-symbol" href="#output" data-definition-place="defined-in-file">output</a> != <b>null</b>, <span class="s">&quot;output should have a value&quot;</span>)&#59;
125125
<a class="l" name="117" href="#117">117</a><span class='fold-space'>&nbsp;</span>
126126
<a class="l" name="118" href="#118">118</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#textWriter" data-definition-place="defined-in-file">textWriter</a> = <a class="d intelliWindow-symbol" href="#output" data-definition-place="defined-in-file">output</a>&#59;
127127
<a class="l" name="119" href="#119">119</a><span class='fold-space'>&nbsp;</span> <b>switch</b> (<a class="d intelliWindow-symbol" href="#format" data-definition-place="defined-in-file">format</a>)
@@ -135,7 +135,7 @@
135135
<a class="l" name="127" href="#127">127</a><span class='fold-space'>&nbsp;</span> <b>break</b>&#59;
136136
<a class="l" name="128" href="#128">128</a><span class='fold-space'>&nbsp;</span> <b>case</b> <a class="d intelliWindow-symbol" href="#DataFormat" data-definition-place="defined-in-file">DataFormat</a>.<a class="d intelliWindow-symbol" href="#Text" data-definition-place="defined-in-file">Text</a>:
137137
<a class="l" name="129" href="#129">129</a><span class='fold-space'>&nbsp;</span> <b>default</b>:
138-
<a class="hl" name="130" href="#130">130</a><span class='fold-space'>&nbsp;</span> <span class="c">// do nothing; we'll just write to the TextWriter</span>
138+
<a class="hl" name="130" href="#130">130</a><span class='fold-space'>&nbsp;</span> <span class="c">// do nothing; we&apos;ll just write to the TextWriter</span>
139139
<a class="l" name="131" href="#131">131</a><span class='fold-space'>&nbsp;</span> <span class="c">// or discard it.</span>
140140
<a class="l" name="132" href="#132">132</a><span class='fold-space'>&nbsp;</span>
141141
<a class="l" name="133" href="#133">133</a><span class='fold-space'>&nbsp;</span> <b>break</b>&#59;
@@ -215,7 +215,7 @@
215215
<span id='scope_id_961ba94d_fold' class='scope-body'><a class="l" name="207" href="#207">207</a><span class='fold-space'>&nbsp;</span> :
216216
<a class="l" name="208" href="#208">208</a><span class='fold-space'>&nbsp;</span> <b>base</b>(<a href="/source/s?defs=dataFormat" class="intelliWindow-symbol" data-definition-place="undefined-in-file">dataFormat</a>, <a href="/source/s?defs=streamName" class="intelliWindow-symbol" data-definition-place="undefined-in-file">streamName</a>)
217217
<a class="l" name="209" href="#209">209</a><span class='fold-space'>&nbsp;</span> &#123;
218-
<a class="hl" name="210" href="#210">210</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#Dbg" data-definition-place="defined-in-file">Dbg</a>.<a href="/source/s?defs=Assert" class="intelliWindow-symbol" data-definition-place="undefined-in-file">Assert</a>(<a class="d intelliWindow-symbol" href="#input" data-definition-place="defined-in-file">input</a> != <b>null</b>, <span class="s">"input should have a value"</span>)&#59;
218+
<a class="hl" name="210" href="#210">210</a><span class='fold-space'>&nbsp;</span> <a class="d intelliWindow-symbol" href="#Dbg" data-definition-place="defined-in-file">Dbg</a>.<a href="/source/s?defs=Assert" class="intelliWindow-symbol" data-definition-place="undefined-in-file">Assert</a>(<a class="d intelliWindow-symbol" href="#input" data-definition-place="defined-in-file">input</a> != <b>null</b>, <span class="s">&quot;input should have a value&quot;</span>)&#59;
219219
<a class="l" name="211" href="#211">211</a><span class='fold-space'>&nbsp;</span>
220220
<a class="l" name="212" href="#212">212</a><span class='fold-space'>&nbsp;</span> <span class="c">// If the data format is none - do nothing...</span>
221221
<a class="l" name="213" href="#213">213</a><span class='fold-space'>&nbsp;</span> <b>if</b> (<a href="/source/s?defs=dataFormat" class="intelliWindow-symbol" data-definition-place="undefined-in-file">dataFormat</a> == <a class="d intelliWindow-symbol" href="#DataFormat" data-definition-place="defined-in-file">DataFormat</a>.<a class="d intelliWindow-symbol" href="#None" data-definition-place="defined-in-file">None</a>)
@@ -238,7 +238,7 @@
238238
<a class="hl" name="230" href="#230">230</a><span class='fold-space'>&nbsp;</span> <b>break</b>&#59;
239239
<a class="l" name="231" href="#231">231</a><span class='fold-space'>&nbsp;</span> <b>case</b> <a class="d intelliWindow-symbol" href="#DataFormat" data-definition-place="defined-in-file">DataFormat</a>.<a class="d intelliWindow-symbol" href="#Text" data-definition-place="defined-in-file">Text</a>:
240240
<a class="l" name="232" href="#232">232</a><span class='fold-space'>&nbsp;</span> <b>default</b>:
241-
<a class="l" name="233" href="#233">233</a><span class='fold-space'>&nbsp;</span> <span class="c">// do nothing; we'll just read from the TextReader</span>
241+
<a class="l" name="233" href="#233">233</a><span class='fold-space'>&nbsp;</span> <span class="c">// do nothing; we&apos;ll just read from the TextReader</span>
242242
<a class="l" name="234" href="#234">234</a><span class='fold-space'>&nbsp;</span>
243243
<a class="l" name="235" href="#235">235</a><span class='fold-space'>&nbsp;</span> <b>break</b>&#59;
244244
<a class="l" name="236" href="#236">236</a><span class='fold-space'>&nbsp;</span> &#125;

test/org/opensolaris/opengrok/analysis/csharp/truncated_xref.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
class="xref">
66
<head>
77
<title>sampleFile - OpenGrok cross reference for /sampleFile</title></head><body>
8-
<a class="l" name="1" href="#1">1</a><span class='fold-space'>&nbsp;</span> <span class="s">"Oops this string is not ter</span></body>
8+
<a class="l" name="1" href="#1">1</a><span class='fold-space'>&nbsp;</span> <span class="s">&quot;Oops this string is not ter</span></body>
99
</html>

0 commit comments

Comments
 (0)