Skip to content

Commit 07a41d5

Browse files
authored
Merge pull request #1945 from idodeclare/feature/swift_tests
Feature/swift tests
2 parents bd3f549 + 168e6aa commit 07a41d5

File tree

13 files changed

+1283
-65
lines changed

13 files changed

+1283
-65
lines changed

opengrok-indexer/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
228228
<exclude>*.java</exclude>
229229
</excludes>
230230
</testResource>
231+
<testResource>
232+
<targetPath>org/opensolaris/opengrok/analysis/swift/</targetPath>
233+
<directory>../test/org/opensolaris/opengrok/analysis/swift/</directory>
234+
<excludes>
235+
<exclude>*.java</exclude>
236+
</excludes>
237+
</testResource>
231238
<testResource>
232239
<targetPath>org/opensolaris/opengrok/analysis/vb/</targetPath>
233240
<directory>../test/org/opensolaris/opengrok/analysis/vb/</directory>

src/org/opensolaris/opengrok/analysis/swift/Consts.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
2223
*/
2324
package org.opensolaris.opengrok.analysis.swift;
2425

@@ -127,5 +128,7 @@ public class Consts {
127128
kwd.add("unowned");
128129
kwd.add("weak");
129130
kwd.add("willSet");
131+
132+
kwd.add("_"); // 4.0.3 "Keywords ... used in patterns"
130133
}
131134
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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) 2017, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
*/
24+
25+
Identifier = {identifier0} {identifier_n}*
26+
27+
identifier0 = ([a-zA-Z_] | [\u{00A8}\u{00AA}\u{00AD}\u{00AF}] |
28+
[\u{00B2}–\u{00B5}] | [\u{00B7}–\u{00BA}] | [\u{00BC}–\u{00BE}] |
29+
[\u{00C0}–\u{00D6}] | [\u{00D8}–\u{00F6}] | [\u{00F8}–\u{00FF}] |
30+
[\u{0100}–\u{02FF}] | [\u{0370}–\u{167F}] | [\u{1681}–\u{180D}] |
31+
[\u{180F}–\u{1DBF}] | [\u{1E00}–\u{1FFF}] | [\u{200B}–\u{200D}] |
32+
[\u{202A}–\u{202E}] | [\u{203F}–\u{2040}] | [\u{2054}] |
33+
[\u{2060}–\u{206F}] | [\u{2070}–\u{20CF}] | [\u{2100}–\u{218F}] |
34+
[\u{2460}–\u{24FF}] | [\u{2776}–\u{2793}] | [\u{2C00}–\u{2DFF}] |
35+
[\u{2E80}–\u{2FFF}] | [\u{3004}–\u{3007}] | [\u{3021}–\u{302F}] |
36+
[\u{3031}–\u{303F}] | [\u{3040}–\u{D7FF}] | [\u{F900}–\u{FD3D}] |
37+
[\u{FD40}–\u{FDCF}] | [\u{FDF0}–\u{FE1F}] | [\u{FE30}–\u{FE44}] |
38+
[\u{FE47}–\u{FFFD}] | [\u{10000}–\u{1FFFD}] | [\u{20000}–\u{2FFFD}] |
39+
[\u{30000}–\u{3FFFD}] | [\u{40000}–\u{4FFFD}] | [\u{50000}–\u{5FFFD}] |
40+
[\u{60000}–\u{6FFFD}] | [\u{70000}–\u{7FFFD}] | [\u{80000}–\u{8FFFD}] |
41+
[\u{90000}–\u{9FFFD}] | [\u{A0000}–\u{AFFFD}] | [\u{B0000}–\u{BFFFD}] |
42+
[\u{C0000}–\u{CFFFD}] | [\u{D0000}–\u{DFFFD}] | [\u{E0000}–\u{EFFFD}])
43+
44+
identifier_n = ([0-9] | [\u{0300}–\u{036F}] | [\u{1DC0}–\u{1DFF}] |
45+
[\u{20D0}–\u{20FF}] | [\u{FE20}–\u{FE2F}] | {identifier0})
46+
47+
ImplicitIdentifier = \$ [0-9]+
48+
49+
Number = ({binary} | {octal} | {hexadecimal} | \-?({decimal} | {floating}))
50+
binary = 0[bB][01_][01_]*
51+
octal = 0[oO][0-7][0-7_]*
52+
hexadecimal = 0[xX] {hexadecimal0}
53+
hexadecimal0 = [0-9a-fA-F][0-9a-fA-F_]*
54+
decimal = [0-9][0-9_]*
55+
floating = ({decimal} ("." {decimal})? ([eE] [\+\-]? {decimal})? |
56+
{hexadecimal} ("." {hexadecimal0})? ([pP] [\+\-]? {decimal})?)

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

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
*/
2424

2525
/*
26-
* Gets Java symbols - ignores comments, strings, keywords
26+
* Gets Swift symbols - ignores comments, strings, keywords
2727
*/
2828

29-
// comments can be nested in kotlin, so below logic doesn't allow that with yybegin we save only one nesting
30-
// same for strings
31-
3229
package org.opensolaris.opengrok.analysis.swift;
33-
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
3430

31+
import java.io.IOException;
32+
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
3533
%%
3634
%public
3735
%class SwiftSymbolTokenizer
@@ -44,12 +42,20 @@ super(in);
4442
%int
4543
%include CommonTokenizer.lexh
4644
%char
45+
%{
46+
private int nestedComment;
4747

48-
/* TODO add unicode as stated in https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head */
49-
Identifier = [:jletter:] [:jletterdigit:]*
48+
@Override
49+
public void reset() throws IOException {
50+
super.reset();
51+
nestedComment = 0;
52+
}
53+
%}
5054

51-
%state STRING COMMENT SCOMMENT QSTRING TSTRING
55+
%state STRING COMMENT SCOMMENT TSTRING
5256

57+
%include Common.lexh
58+
%include Swift.lexh
5359
%%
5460

5561
/* TODO : support identifiers escaped by ` `*/
@@ -60,36 +66,55 @@ Identifier = [:jletter:] [:jletterdigit:]*
6066
return yystate(); }
6167
}
6268

69+
[`] {Identifier} [`] {
70+
String capture = yytext();
71+
String id = capture.substring(1, capture.length() - 1);
72+
setAttribs(id, yychar + 1, yychar + 1 + id.length());
73+
return yystate();
74+
}
75+
76+
{ImplicitIdentifier} {
77+
// noop
78+
}
79+
80+
{Number} {}
81+
6382
\" { yybegin(STRING); }
64-
\' { yybegin(QSTRING); }
6583
\"\"\" { yybegin(TSTRING); }
66-
"/*" { yybegin(COMMENT); }
6784
"//" { yybegin(SCOMMENT); }
6885

6986
}
7087

71-
/* TODO : support raw """ strings */
7288
<STRING> {
89+
\\[\"\\] {}
7390
\" { yybegin(YYINITIAL); }
74-
\\\\ | \\\" {}
75-
}
76-
77-
<QSTRING> {
78-
\' { yybegin(YYINITIAL); }
7991
}
8092

8193
<TSTRING> {
8294
\"\"\" { yybegin(YYINITIAL); }
8395
}
8496

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

89113
<SCOMMENT> {
90-
\n { yybegin(YYINITIAL);}
114+
{WhspChar}*{EOL} { yybegin(YYINITIAL);}
91115
}
92116

93-
<YYINITIAL, STRING, COMMENT, SCOMMENT, QSTRING, TSTRING> {
117+
<YYINITIAL, STRING, COMMENT, SCOMMENT, TSTRING> {
118+
{WhiteSpace} |
94119
[^] {}
95120
}

0 commit comments

Comments
 (0)