Skip to content

Commit 59aade2

Browse files
committed
Bugfix for Lua comments with apostrophe
1 parent 2336ca9 commit 59aade2

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Contributors
1414
* Ivan Poluyanov <[email protected]> `@poluyanov <https://github.com/poluyanov>`_
1515
* Raymond Lau <[email protected]> `@Raymond26 <https://github.com/Raymond26>`_
1616
* Luca Comellini <[email protected]> `@lucacome <https://github.com/lucacome>`_
17+
* Ron Vider <[email protected]> `@RonVider <https://github.com/RonVider>`_

crossplane/ext/lua.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66
from crossplane.ext.abstract import CrossplaneExtension
77

88

9+
class EmplaceIter:
10+
def __init__(self, it):
11+
self.it = it
12+
self.ret = []
13+
14+
def __iter__(self):
15+
return self
16+
17+
def __next__(self):
18+
if len(self.ret) > 0:
19+
v = self.ret.pop()
20+
return v
21+
return next(self.it)
22+
23+
def put_back(self, v):
24+
self.ret.append(v)
25+
26+
27+
928
class LuaBlockPlugin(CrossplaneExtension):
1029
"""
1130
This plugin adds special handling for Lua code block directives (*_by_lua_block)
@@ -64,10 +83,23 @@ def lex(self, char_iterator, directive):
6483

6584
depth += 1
6685

86+
char_iterator = EmplaceIter(char_iterator)
87+
6788
# Grab everything in Lua block as a single token
6889
# and watch for curly brace '{' in strings
6990
for char, line in char_iterator:
70-
if char == '{':
91+
if char == '-':
92+
prev_char, prev_line = char, line
93+
char, comment_line = next(char_iterator)
94+
if char == '-':
95+
token += '-'
96+
while char != '\n':
97+
token += char
98+
char, line = next(char_iterator)
99+
else:
100+
char_iterator.put_back((char, comment_line))
101+
char, line = prev_char, prev_line
102+
elif char == '{':
71103
depth += 1
72104
elif char == '}':
73105
depth -= 1

tests/configs/lua-block-simple/nginx.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ http {
3131
upstream foo {
3232
server 127.0.0.1;
3333
balancer_by_lua_block {
34-
-- use Lua to do something interesting here
34+
-- use Lua that'll do something interesting here
3535
}
3636
log_by_lua_block {
3737
print("I need no extra escaping here, for example: \r\nblah")

tests/ext/test_lua.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_lex_lua_block_simple():
6161
('127.0.0.1', 32),
6262
(';', 32),
6363
('balancer_by_lua_block', 33),
64-
('\n -- use Lua to do something interesting here\n ', 35),
64+
('\n -- use Lua that\'ll do something interesting here\n ', 35),
6565
(';', 35),
6666
('log_by_lua_block', 36),
6767
('\n print("I need no extra escaping here, for example: \\r\\nblah")\n ', 38),
@@ -277,7 +277,7 @@ def test_parse_lua_block_simple():
277277
{
278278
'line': 33,
279279
'args': [
280-
'\n -- use Lua to do something interesting here'
280+
'\n -- use Lua that\'ll do something interesting here'
281281
'\n '
282282
],
283283
'directive': 'balancer_by_lua_block'

0 commit comments

Comments
 (0)