Skip to content

Commit 87b327b

Browse files
authored
Merge pull request #1920 from idodeclare/feature/lua_tests
Feature/lua tests
2 parents 6b5424d + 348ddc2 commit 87b327b

File tree

13 files changed

+1454
-49
lines changed

13 files changed

+1454
-49
lines changed

opengrok-indexer/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
130130
<exclude>*.java</exclude>
131131
</excludes>
132132
</testResource>
133+
<testResource>
134+
<targetPath>org/opensolaris/opengrok/analysis/lua/</targetPath>
135+
<directory>../test/org/opensolaris/opengrok/analysis/lua/</directory>
136+
<excludes>
137+
<exclude>*.java</exclude>
138+
</excludes>
139+
</testResource>
133140
<testResource>
134141
<targetPath>org/opensolaris/opengrok/analysis/perl/</targetPath>
135142
<directory>../test/org/opensolaris/opengrok/analysis/perl/</directory>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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) 2016, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
*/
24+
25+
/*
26+
* Copyright © 1994–2017 Lua.org, PUC-Rio.
27+
*
28+
* Permission is hereby granted, free of charge, to any person obtaining a
29+
* copy of this software and associated documentation files (the "Software"),
30+
* to deal in the Software without restriction, including without limitation
31+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
32+
* and/or sell copies of the Software, and to permit persons to whom the
33+
* Software is furnished to do so, subject to the following conditions:
34+
*
35+
* The above copyright notice and this permission notice shall be included in
36+
* all copies or substantial portions of the Software.
37+
*
38+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
41+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
43+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
44+
* DEALINGS IN THE SOFTWARE.
45+
*/
46+
47+
/*
48+
* "Names (also called identifiers) in Lua can be any string of letters,
49+
* digits, and underscores, not beginning with a digit...."
50+
*/
51+
Identifier = [\p{Letter}_] [\p{Letter}\p{Number}_]*
52+
53+
Number = ([0][xX][0-9a-fA-F]+ | [0-9]+\.[0-9]+ |
54+
[0-9][0-9_]*) ([eE][+-]?[0-9]+)?

src/org/opensolaris/opengrok/analysis/lua/LuaSymbolTokenizer.lex

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

2929
package org.opensolaris.opengrok.analysis.lua;
30+
31+
import java.io.IOException;
3032
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
3133

3234
/**
3335
* @author Evan Kinney
3436
*/
35-
3637
%%
3738
%public
3839
%class LuaSymbolTokenizer
@@ -44,11 +45,20 @@ super(in);
4445
%int
4546
%include CommonTokenizer.lexh
4647
%char
48+
%{
49+
int bracketLevel;
4750

48-
Identifier = [a-zA-Z_] [a-zA-Z0-9_']*
51+
@Override
52+
public void reset() throws IOException {
53+
super.reset();
54+
bracketLevel = 0;
55+
}
56+
%}
4957

5058
%state STRING LSTRING COMMENT SCOMMENT QSTRING
5159

60+
%include Common.lexh
61+
%include Lua.lexh
5262
%%
5363

5464
<YYINITIAL> {
@@ -59,34 +69,47 @@ Identifier = [a-zA-Z_] [a-zA-Z0-9_']*
5969
return yystate();
6070
}
6171
}
72+
{Number} {}
6273
\" { yybegin(STRING); }
63-
"[[" { yybegin(LSTRING); }
74+
"[" [=]* "[" {
75+
String capture = yytext();
76+
bracketLevel = LuaUtils.countOpeningLongBracket(capture);
77+
yybegin(LSTRING);
78+
}
6479
\' { yybegin(QSTRING); }
65-
"--[[" { yybegin(COMMENT); }
80+
"--[" [=]* "[" {
81+
String capture = yytext();
82+
String bracket = capture.substring(2);
83+
bracketLevel = LuaUtils.countOpeningLongBracket(bracket);
84+
yybegin(COMMENT);
85+
}
6686
"--" { yybegin(SCOMMENT); }
6787
}
6888

6989
<STRING> {
90+
\\[\"\\] {}
7091
\" { yybegin(YYINITIAL); }
71-
\\\\ | \\\" {}
72-
}
73-
74-
<LSTRING> {
75-
"]]" { yybegin(YYINITIAL); }
7692
}
7793

7894
<QSTRING> {
95+
\\[\'\\] {}
7996
\' { yybegin(YYINITIAL); }
8097
}
8198

82-
<COMMENT> {
83-
"]]" { yybegin(YYINITIAL); }
99+
<LSTRING, COMMENT> {
100+
"]" [=]* "]" {
101+
String capture = yytext();
102+
if (LuaUtils.isClosingLongBracket(capture, bracketLevel)) {
103+
yybegin(YYINITIAL);
104+
}
105+
}
84106
}
85107

86108
<SCOMMENT> {
87-
\n { yybegin(YYINITIAL); }
109+
{WhspChar}*{EOL} { yybegin(YYINITIAL); }
88110
}
89111

90112
<YYINITIAL, STRING, LSTRING, COMMENT, SCOMMENT, QSTRING> {
113+
{WhiteSpace} {}
91114
[^] {}
92115
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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, Chris Fraire <[email protected]>.
22+
*/
23+
24+
package org.opensolaris.opengrok.analysis.lua;
25+
26+
/**
27+
* Represents a container for Lua-related utility methods.
28+
*/
29+
public class LuaUtils {
30+
31+
/**
32+
* private to enforce singleton
33+
*/
34+
private LuaUtils() {
35+
}
36+
37+
/**
38+
* Counts the level of a Lua opening long bracket specified in
39+
* {@code capture}.
40+
* @param capture the opening long bracket
41+
* @return the bracket level
42+
*/
43+
public static int countOpeningLongBracket(String capture) {
44+
if (!capture.startsWith("[") || !capture.endsWith("[")) {
45+
throw new IllegalArgumentException(
46+
"Invalid opening long bracket: " + capture);
47+
}
48+
49+
int n = 0;
50+
for (int i = 1; i + 1 < capture.length(); ++i) {
51+
if (capture.charAt(i) != '=') {
52+
throw new IllegalArgumentException(
53+
"Invalid opening long bracket: " + capture);
54+
}
55+
++n;
56+
}
57+
return n;
58+
}
59+
60+
/**
61+
* Determines if the specified {@code capture} is a closing long bracket of
62+
* the specified {@code level}.
63+
* @param capture the possible Lua closing long bracket
64+
* @param level the required level of closing long bracket
65+
* @return true if the {@code capture} is determined to be the required
66+
* closing long bracket or false otherwise.
67+
*/
68+
public static boolean isClosingLongBracket(String capture, int level) {
69+
if (!capture.startsWith("]") || !capture.endsWith("]")) {
70+
throw new IllegalArgumentException(
71+
"Invalid opening long bracket: " + capture);
72+
}
73+
74+
int n = 0;
75+
for (int i = 1; i + 1 < capture.length(); ++i) {
76+
if (capture.charAt(i) != '=') {
77+
throw new IllegalArgumentException(
78+
"Invalid opening long bracket: " + capture);
79+
}
80+
++n;
81+
}
82+
return n == level;
83+
}
84+
}

0 commit comments

Comments
 (0)