Skip to content

Commit 22799a1

Browse files
authored
Merge pull request #1919 from idodeclare/feature/csharp_tests
Feature/csharp tests
2 parents 87b327b + 316fe9b commit 22799a1

File tree

13 files changed

+1313
-58
lines changed

13 files changed

+1313
-58
lines changed

opengrok-indexer/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
102102
<exclude>*.java</exclude>
103103
</excludes>
104104
</testResource>
105+
<testResource>
106+
<targetPath>org/opensolaris/opengrok/analysis/csharp/</targetPath>
107+
<directory>../test/org/opensolaris/opengrok/analysis/csharp/</directory>
108+
<excludes>
109+
<exclude>*.java</exclude>
110+
</excludes>
111+
</testResource>
105112
<testResource>
106113
<targetPath>org/opensolaris/opengrok/analysis/document/</targetPath>
107114
<directory>../test/org/opensolaris/opengrok/analysis/document/</directory>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
*/
24+
25+
CsharpEOL = {EOL}|\u2028|\u2029|\u000B|\u000C|\u0085
26+
27+
Identifier = [a-zA-Z_] [a-zA-Z0-9_]*
28+
29+
Number = (0[xX][0-9a-fA-F]+|[0-9]+\.[0-9]+|[0-9]+)(([eE][+-]?[0-9]+)?[ufdlUFDL]*)?

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@
2727
*/
2828

2929
package org.opensolaris.opengrok.analysis.csharp;
30-
import java.io.IOException;
31-
import java.io.Reader;
32-
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
3330

31+
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
3432
%%
3533
%public
3634
%class CSharpSymbolTokenizer
@@ -42,18 +40,24 @@ super(in);
4240
%int
4341
%include CommonTokenizer.lexh
4442
%char
45-
Identifier = [a-zA-Z_] [a-zA-Z0-9_]*
4643

4744
%state STRING COMMENT SCOMMENT QSTRING VSTRING
4845

46+
%include Common.lexh
47+
%include CSharp.lexh
4948
%%
5049

5150
<YYINITIAL> {
52-
{Identifier} {String id = yytext();
51+
{Identifier} {
52+
String id = yytext();
5353
if(!Consts.kwd.contains(id)){
5454
setAttribs(id, yychar, yychar + yylength());
55-
return yystate(); }
56-
}
55+
return yystate();
56+
}
57+
}
58+
59+
{Number} {}
60+
5761
\" { yybegin(STRING); }
5862
\' { yybegin(QSTRING); }
5963
"/*" { yybegin(COMMENT); }
@@ -62,26 +66,31 @@ Identifier = [a-zA-Z_] [a-zA-Z0-9_]*
6266
}
6367

6468
<STRING> {
69+
\\[\"\\] {}
6570
\" { yybegin(YYINITIAL); }
66-
\\\\ | \\\" {}
67-
}
68-
69-
<VSTRING> {
70-
"@\"" { yybegin(YYINITIAL);}
7171
}
7272

7373
<QSTRING> {
74+
\\[\'\\] {}
7475
\' { yybegin(YYINITIAL); }
7576
}
7677

78+
<VSTRING> {
79+
\\ |
80+
\"\" {}
81+
\" { yybegin(YYINITIAL);}
82+
}
83+
7784
<COMMENT> {
7885
"*/" { yybegin(YYINITIAL);}
7986
}
8087

8188
<SCOMMENT> {
82-
\n { yybegin(YYINITIAL);}
89+
{CsharpEOL} { yybegin(YYINITIAL);}
8390
}
8491

8592
<YYINITIAL, STRING, COMMENT, SCOMMENT, QSTRING, VSTRING> {
93+
{WhiteSpace} {}
94+
8695
[^] {}
8796
}

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

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

3030
package org.opensolaris.opengrok.analysis.csharp;
31-
import org.opensolaris.opengrok.analysis.JFlexXref;
32-
import java.io.IOException;
33-
import java.io.Writer;
34-
import java.io.Reader;
35-
import org.opensolaris.opengrok.web.Util;
3631

32+
import org.opensolaris.opengrok.analysis.JFlexXrefSimple;
33+
import org.opensolaris.opengrok.util.StringUtils;
34+
import org.opensolaris.opengrok.web.HtmlConsts;
35+
import org.opensolaris.opengrok.web.Util;
3736
%%
3837
%public
3938
%class CSharpXref
40-
%extends JFlexXref
39+
%extends JFlexXrefSimple
4140
%unicode
42-
%ignorecase
4341
%int
4442
%include CommonXref.lexh
4543
%{
@@ -50,21 +48,14 @@ import org.opensolaris.opengrok.web.Util;
5048
protected void setLineNumber(int x) { yyline = x; }
5149
%}
5250

53-
CsharpEOL = {EOL}|\u2028|\u2029|\u000B|\u000C|\u0085
54-
Identifier = [a-zA-Z_] [a-zA-Z0-9_]+
55-
56-
File = [a-zA-Z]{FNameChar}* "." ([chts]|"cs")
57-
58-
Number = (0[xX][0-9a-fA-F]+|[0-9]+\.[0-9]+|[0-9]+)(([eE][+-]?[0-9]+)?[ufdlUFDL]*)?
59-
60-
//ClassName = ({Identifier} ".")* {Identifier}
61-
//ParamName = {Identifier} | "<" {Identifier} ">"
51+
File = [a-zA-Z]{FNameChar}* "." ([cChHtTsS]|[cC][sS])
6252

6353
%state STRING COMMENT SCOMMENT QSTRING VSTRING
6454

6555
%include Common.lexh
6656
%include CommonURI.lexh
6757
%include CommonPath.lexh
58+
%include CSharp.lexh
6859
%%
6960
<YYINITIAL>{
7061
\{ { incScope(); writeUnicodeChar(yycharat(0)); }
@@ -92,51 +83,78 @@ Number = (0[xX][0-9a-fA-F]+|[0-9]+\.[0-9]+|[0-9]+)(([eE][+-]?[0-9]+)?[ufdlUFDL]*
9283
/*{Hier}
9384
{ out.write(Util.breadcrumbPath(urlPrefix+"defs=",yytext(),'.'));}
9485
*/
95-
{Number} { out.write("<span class=\"n\">"); out.write(yytext()); out.write("</span>"); }
96-
97-
\" { yybegin(STRING);out.write("<span class=\"s\">\"");}
98-
\' { yybegin(QSTRING);out.write("<span class=\"s\">\'");}
99-
"/*" { yybegin(COMMENT);out.write("<span class=\"c\">/*");}
100-
"//" { yybegin(SCOMMENT);out.write("<span class=\"c\">//");}
101-
"@\"" { yybegin(VSTRING);out.write("<span class=\"s\">@\"");}
86+
{Number} {
87+
disjointSpan(HtmlConsts.NUMBER_CLASS);
88+
out.write(yytext());
89+
disjointSpan(null);
90+
}
91+
92+
\" {
93+
pushSpan(STRING, HtmlConsts.STRING_CLASS);
94+
out.write(htmlize(yytext()));
95+
}
96+
\' {
97+
pushSpan(QSTRING, HtmlConsts.STRING_CLASS);
98+
out.write(htmlize(yytext()));
99+
}
100+
"/*" {
101+
pushSpan(COMMENT, HtmlConsts.COMMENT_CLASS);
102+
out.write(yytext());
103+
}
104+
"//" {
105+
pushSpan(SCOMMENT, HtmlConsts.COMMENT_CLASS);
106+
out.write(yytext());
107+
}
108+
"@\"" {
109+
pushSpan(VSTRING, HtmlConsts.STRING_CLASS);
110+
out.write(htmlize(yytext()));
111+
}
102112
}
103113

104114
<STRING> {
105-
\" {WhiteSpace} \" { out.write(yytext());}
106-
\" { yybegin(YYINITIAL); out.write("\"</span>"); }
107-
\\\\ { out.write("\\\\"); }
108-
\\\" { out.write("\\\""); }
115+
\\[\"\\] |
116+
\" {WhiteSpace} \" { out.write(htmlize(yytext()));}
117+
\" {
118+
out.write(htmlize(yytext()));
119+
yypop();
120+
}
109121
}
110122

111123
<QSTRING> {
112-
"\\\\" { out.write("\\\\"); }
113-
"\\\'" { out.write("\\\'"); }
114-
\' {WhiteSpace} \' { out.write(yytext()); }
115-
\' { yybegin(YYINITIAL); out.write("'</span>"); }
124+
\\[\'\\] |
125+
\' {WhiteSpace} \' { out.write(htmlize(yytext())); }
126+
\' {
127+
out.write(htmlize(yytext()));
128+
yypop();
129+
}
116130
}
117131

118132
<VSTRING> {
119-
\" {WhiteSpace} \" { out.write(yytext());}
120-
"\"\"" { out.write("\"\""); }
121-
\" { yybegin(YYINITIAL); out.write("\"</span>"); }
122-
\\ { out.write("\\"); }
133+
\\ |
134+
\"\" { out.write(htmlize(yytext())); }
135+
\" {
136+
out.write(htmlize(yytext()));
137+
yypop();
138+
}
123139
}
124140

125141
<COMMENT> {
126-
"*/" { yybegin(YYINITIAL); out.write("*/</span>"); }
142+
"*/" {
143+
out.write(yytext());
144+
yypop();
145+
}
127146
}
128147

129148
<SCOMMENT> {
130149
{WhspChar}*{CsharpEOL} {
131-
yybegin(YYINITIAL); out.write("</span>");
150+
yypop();
132151
startNewLine();
133152
}
134153
}
135154

136155
<YYINITIAL, STRING, COMMENT, SCOMMENT, QSTRING, VSTRING> {
137-
"&" {out.write( "&amp;");}
138-
"<" {out.write( "&lt;");}
139-
">" {out.write( "&gt;");}
156+
[&<>\'\"] { out.write(htmlize(yytext())); }
157+
140158
{WhspChar}*{CsharpEOL} { startNewLine(); }
141159
{WhiteSpace} { out.write(yytext()); }
142160
[!-~] { out.write(yycharat(0)); }
@@ -157,12 +175,26 @@ Number = (0[xX][0-9a-fA-F]+|[0-9]+\.[0-9]+|[0-9]+)(([eE][+-]?[0-9]+)?[ufdlUFDL]*
157175
out.write(path);
158176
out.write("</a>");}
159177

160-
{BrowseableURI} {
161-
appendLink(yytext(), true);
162-
}
163-
164178
{FNameChar}+ "@" {FNameChar}+ "." {FNameChar}+
165179
{
166180
writeEMailAddress(yytext());
167181
}
168182
}
183+
184+
<STRING, SCOMMENT, VSTRING> {
185+
{BrowseableURI} {
186+
appendLink(yytext(), true);
187+
}
188+
}
189+
190+
<COMMENT> {
191+
{BrowseableURI} {
192+
appendLink(yytext(), true, StringUtils.END_C_COMMENT);
193+
}
194+
}
195+
196+
<QSTRING> {
197+
{BrowseableURI} {
198+
appendLink(yytext(), true, StringUtils.APOS_NO_BSESC);
199+
}
200+
}

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
/**
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
*/
24+
25+
package org.opensolaris.opengrok.analysis.csharp;
26+
27+
import java.io.BufferedReader;
28+
import java.io.InputStream;
29+
import java.io.InputStreamReader;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
import static org.junit.Assert.assertNotNull;
33+
import org.junit.Test;
34+
import static org.opensolaris.opengrok.util.CustomAssertions.assertSymbolStream;
35+
36+
/**
37+
* Tests the {@link CSharpSymbolTokenizer} class.
38+
*/
39+
public class CSharpSymbolTokenizerTest {
40+
41+
/**
42+
* Test sample.cs v. samplesymbols.txt
43+
* @throws java.lang.Exception thrown on error
44+
*/
45+
@Test
46+
public void testCSharpSymbolStream() throws Exception {
47+
InputStream csres = getClass().getClassLoader().getResourceAsStream(
48+
"org/opensolaris/opengrok/analysis/csharp/sample.cs");
49+
assertNotNull("despite sample.cs as resource,", csres);
50+
InputStream symres = getClass().getClassLoader().getResourceAsStream(
51+
"org/opensolaris/opengrok/analysis/csharp/samplesymbols.txt");
52+
assertNotNull("despite samplesymbols.txt as resource,", symres);
53+
54+
List<String> expectedSymbols = new ArrayList<>();
55+
try (BufferedReader wdsr = new BufferedReader(new InputStreamReader(
56+
symres, "UTF-8"))) {
57+
String line;
58+
while ((line = wdsr.readLine()) != null) {
59+
int hasho = line.indexOf('#');
60+
if (hasho != -1) line = line.substring(0, hasho);
61+
expectedSymbols.add(line.trim());
62+
}
63+
}
64+
65+
assertSymbolStream(CSharpSymbolTokenizer.class, csres,
66+
expectedSymbols);
67+
}
68+
}

0 commit comments

Comments
 (0)