Skip to content

Commit eecab5c

Browse files
committed
Fix to support complete Lua long bracket syntax
1 parent 27e4c8d commit eecab5c

File tree

5 files changed

+159
-36
lines changed

5 files changed

+159
-36
lines changed

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

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

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

31+
import java.io.IOException;
3132
import org.opensolaris.opengrok.analysis.JFlexTokenizer;
3233

3334
/**
@@ -44,6 +45,15 @@ super(in);
4445
%int
4546
%include CommonTokenizer.lexh
4647
%char
48+
%{
49+
int bracketLevel;
50+
51+
@Override
52+
public void reset() throws IOException {
53+
super.reset();
54+
bracketLevel = 0;
55+
}
56+
%}
4757

4858
%state STRING LSTRING COMMENT SCOMMENT QSTRING
4959

@@ -61,9 +71,18 @@ super(in);
6171
}
6272
{Number} {}
6373
\" { yybegin(STRING); }
64-
"[[" { yybegin(LSTRING); }
74+
"[" [=]* "[" {
75+
String capture = yytext();
76+
bracketLevel = LuaUtils.countOpeningLongBracket(capture);
77+
yybegin(LSTRING);
78+
}
6579
\' { yybegin(QSTRING); }
66-
"--[[" { yybegin(COMMENT); }
80+
"--[" [=]* "[" {
81+
String capture = yytext();
82+
String bracket = capture.substring(2);
83+
bracketLevel = LuaUtils.countOpeningLongBracket(bracket);
84+
yybegin(COMMENT);
85+
}
6786
"--" { yybegin(SCOMMENT); }
6887
}
6988

@@ -77,14 +96,13 @@ super(in);
7796
\' { yybegin(YYINITIAL); }
7897
}
7998

80-
<LSTRING> {
81-
\\[\"\\] {}
82-
83-
"]]" { yybegin(YYINITIAL); }
84-
}
85-
86-
<COMMENT> {
87-
"]]" { yybegin(YYINITIAL); }
99+
<LSTRING, COMMENT> {
100+
"]" [=]* "]" {
101+
String capture = yytext();
102+
if (LuaUtils.isClosingLongBracket(capture, bracketLevel)) {
103+
yybegin(YYINITIAL);
104+
}
105+
}
88106
}
89107

90108
<SCOMMENT> {
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+
}

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

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,19 @@ import org.opensolaris.opengrok.web.Util;
4343
%int
4444
%include CommonXref.lexh
4545
%{
46+
int bracketLevel;
47+
4648
// TODO move this into an include file when bug #16053 is fixed
4749
@Override
4850
protected int getLineNumber() { return yyline; }
4951
@Override
5052
protected void setLineNumber(int x) { yyline = x; }
53+
54+
@Override
55+
public void reset() {
56+
super.reset();
57+
bracketLevel = 0;
58+
}
5159
%}
5260

5361
File = [a-zA-Z]{FNameChar}* "." ([Ll][Uu][Aa] | [Tt][Xx][Tt] |
@@ -74,21 +82,26 @@ File = [a-zA-Z]{FNameChar}* "." ([Ll][Uu][Aa] | [Tt][Xx][Tt] |
7482
pushSpan(STRING, HtmlConsts.STRING_CLASS);
7583
out.write(htmlize(yytext()));
7684
}
77-
"[[" {
85+
"[" [=]* "[" {
86+
String capture = yytext();
87+
bracketLevel = LuaUtils.countOpeningLongBracket(capture);
7888
pushSpan(LSTRING, HtmlConsts.STRING_CLASS);
79-
out.write(yytext());
89+
out.write(capture);
8090
}
8191
\' {
8292
pushSpan(QSTRING, HtmlConsts.STRING_CLASS);
8393
out.write(htmlize(yytext()));
8494
}
85-
"--[[" {
95+
"--[" [=]* "[" {
96+
String capture = yytext();
97+
String bracket = capture.substring(2);
98+
bracketLevel = LuaUtils.countOpeningLongBracket(bracket);
8699
pushSpan(COMMENT, HtmlConsts.COMMENT_CLASS);
87-
out.write(htmlize(yytext()));
100+
out.write(capture);
88101
}
89102
"--" {
90103
pushSpan(SCOMMENT, HtmlConsts.COMMENT_CLASS);
91-
out.write(htmlize(yytext()));
104+
out.write(yytext());
92105
}
93106
}
94107

@@ -107,40 +120,48 @@ File = [a-zA-Z]{FNameChar}* "." ([Ll][Uu][Aa] | [Tt][Xx][Tt] |
107120

108121
<STRING> {
109122
\\[\"\\] |
110-
\" {WhiteSpace} \" { out.write(htmlize(yytext())); }
111-
\" {
123+
\" {WhiteSpace} \" { out.write(htmlize(yytext())); }
124+
\" {
112125
out.write(htmlize(yytext()));
113126
yypop();
114127
}
115128
}
116129

117130
<QSTRING> {
118131
\\[\'\\] |
119-
\' {WhiteSpace} \' { out.write(htmlize(yytext())); }
120-
\' {
132+
\' {WhiteSpace} \' { out.write(htmlize(yytext())); }
133+
\' {
121134
out.write(htmlize(yytext()));
122135
yypop();
123136
}
124137
}
125138

126-
<LSTRING> {
127-
\\[\"\\] |
128-
\" {WhiteSpace} \" { out.write(htmlize(yytext())); }
129-
"]]" {
130-
out.write(htmlize(yytext()));
131-
yypop();
132-
}
139+
<LSTRING, COMMENT> {
140+
"]" [=]* "]" {
141+
String capture = yytext();
142+
out.write(capture);
143+
if (LuaUtils.isClosingLongBracket(capture, bracketLevel)) yypop();
144+
}
145+
}
146+
147+
<STRING, QSTRING, LSTRING> {
148+
{WhspChar}*{EOL} {
149+
disjointSpan(null);
150+
startNewLine();
151+
disjointSpan(HtmlConsts.STRING_CLASS);
152+
}
133153
}
134154

135155
<COMMENT> {
136-
"]]" {
137-
out.write(yytext());
138-
yypop();
156+
{WhspChar}*{EOL} {
157+
disjointSpan(null);
158+
startNewLine();
159+
disjointSpan(HtmlConsts.COMMENT_CLASS);
139160
}
140161
}
141162

142163
<SCOMMENT> {
143-
{WhspChar}*{EOL} {
164+
{WhspChar}*{EOL} {
144165
yypop();
145166
startNewLine();
146167
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ do
273273
return {}
274274
end
275275

276-
-- don't read raw_body if not necessary
277-
-- if we called get_body_args(), we only want the parsed body
276+
--[=[ don't read raw_body if not necessary ]]
277+
-- if we called get_body_args(), we only want the parsed body ]=]
278278
return res
279279
end,
280280
}
@@ -302,7 +302,7 @@ do
302302
then
303303
req_mime = MIME_TYPES.form_url_encoded
304304

305-
elseif str_find(content_type, "text/plain", nil, true) then
305+
elseif str_find(content_type, [=[text/plain]=], nil, true) then
306306
req_mime = MIME_TYPES.text
307307

308308
elseif str_find(content_type, "text/html", nil, true) then

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@
281281
<a class="l" name="273" href="#273">273</a> <b>return</b> {}
282282
<a class="l" name="274" href="#274">274</a> <b>end</b>
283283
<a class="l" name="275" href="#275">275</a>
284-
<a class="l" name="276" href="#276">276</a> <span class="c">-- don&apos;t read raw_body if not necessary</span>
285-
<a class="l" name="277" href="#277">277</a> <span class="c">-- if we called get_body_args(), we only want the parsed body</span>
284+
<a class="l" name="276" href="#276">276</a> <span class="c">--[=[ don&apos;t read raw_body if not necessary ]]</span>
285+
<a class="l" name="277" href="#277">277</a><span class="c"> -- if we called get_body_args(), we only want the parsed body ]=]</span>
286286
<a class="l" name="278" href="#278">278</a> <b>return</b> <a href="/source/s?defs=res" class="intelliWindow-symbol" data-definition-place="undefined-in-file">res</a>
287287
<a class="l" name="279" href="#279">279</a> <b>end</b>,
288288
<a class="hl" name="280" href="#280">280</a> }
@@ -310,7 +310,7 @@
310310
<a class="l" name="302" href="#302">302</a> <b>then</b>
311311
<a class="l" name="303" href="#303">303</a> <a href="/source/s?defs=req_mime" class="intelliWindow-symbol" data-definition-place="undefined-in-file">req_mime</a> = <a href="/source/s?defs=MIME_TYPES" class="intelliWindow-symbol" data-definition-place="undefined-in-file">MIME_TYPES</a>.<a href="/source/s?defs=form_url_encoded" class="intelliWindow-symbol" data-definition-place="undefined-in-file">form_url_encoded</a>
312312
<a class="l" name="304" href="#304">304</a>
313-
<a class="l" name="305" href="#305">305</a> <b>elseif</b> <a href="/source/s?defs=str_find" class="intelliWindow-symbol" data-definition-place="undefined-in-file">str_find</a>(<a href="/source/s?defs=content_type" class="intelliWindow-symbol" data-definition-place="undefined-in-file">content_type</a>, <span class="s">&quot;<a href="/source/s?path=text/">text</a>/<a href="/source/s?path=text/plain">plain</a>&quot;</span>, <b>nil</b>, <b>true</b>) <b>then</b>
313+
<a class="l" name="305" href="#305">305</a> <b>elseif</b> <a href="/source/s?defs=str_find" class="intelliWindow-symbol" data-definition-place="undefined-in-file">str_find</a>(<a href="/source/s?defs=content_type" class="intelliWindow-symbol" data-definition-place="undefined-in-file">content_type</a>, <span class="s">[=[<a href="/source/s?path=text/">text</a>/<a href="/source/s?path=text/plain">plain</a>]=]</span>, <b>nil</b>, <b>true</b>) <b>then</b>
314314
<a class="l" name="306" href="#306">306</a> <a href="/source/s?defs=req_mime" class="intelliWindow-symbol" data-definition-place="undefined-in-file">req_mime</a> = <a href="/source/s?defs=MIME_TYPES" class="intelliWindow-symbol" data-definition-place="undefined-in-file">MIME_TYPES</a>.<a href="/source/s?defs=text" class="intelliWindow-symbol" data-definition-place="undefined-in-file">text</a>
315315
<a class="l" name="307" href="#307">307</a>
316316
<a class="l" name="308" href="#308">308</a> <b>elseif</b> <a href="/source/s?defs=str_find" class="intelliWindow-symbol" data-definition-place="undefined-in-file">str_find</a>(<a href="/source/s?defs=content_type" class="intelliWindow-symbol" data-definition-place="undefined-in-file">content_type</a>, <span class="s">&quot;<a href="/source/s?path=text/">text</a>/<a href="/source/s?path=text/html">html</a>&quot;</span>, <b>nil</b>, <b>true</b>) <b>then</b>

0 commit comments

Comments
 (0)