Skip to content

Bugfix for Lua comments with apostrophe #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Contributors
* Ivan Poluyanov <i.poluyanov@icloud.com> `@poluyanov <https://github.com/poluyanov>`_
* Raymond Lau <raymond.lau.ca@gmail.com> `@Raymond26 <https://github.com/Raymond26>`_
* Luca Comellini <luca.com@gmail.com> `@lucacome <https://github.com/lucacome>`_
* Ron Vider <viderron@gmail.com> `@RonVider <https://github.com/RonVider>`_
36 changes: 35 additions & 1 deletion crossplane/ext/lua.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@
from crossplane.ext.abstract import CrossplaneExtension


class EmplaceIter:
def __init__(self, it):
self.it = it
self.ret = []

def __iter__(self):
return self

def __next__(self):
if len(self.ret) > 0:
v = self.ret.pop()
return v
return next(self.it)

next = __next__

def put_back(self, v):
self.ret.append(v)



class LuaBlockPlugin(CrossplaneExtension):
"""
This plugin adds special handling for Lua code block directives (*_by_lua_block)
Expand Down Expand Up @@ -64,10 +85,23 @@ def lex(self, char_iterator, directive):

depth += 1

char_iterator = EmplaceIter(char_iterator)

# Grab everything in Lua block as a single token
# and watch for curly brace '{' in strings
for char, line in char_iterator:
if char == '{':
if char == '-':
prev_char, prev_line = char, line
char, comment_line = next(char_iterator)
if char == '-':
token += '-'
while char != '\n':
token += char
char, line = next(char_iterator)
else:
char_iterator.put_back((char, comment_line))
char, line = prev_char, prev_line
elif char == '{':
depth += 1
elif char == '}':
depth -= 1
Expand Down
2 changes: 1 addition & 1 deletion tests/configs/lua-block-simple/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ http {
upstream foo {
server 127.0.0.1;
balancer_by_lua_block {
-- use Lua to do something interesting here
-- use Lua that'll do something interesting here with external bracket for testing {
}
log_by_lua_block {
print("I need no extra escaping here, for example: \r\nblah")
Expand Down
4 changes: 2 additions & 2 deletions tests/ext/test_lua.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_lex_lua_block_simple():
('127.0.0.1', 32),
(';', 32),
('balancer_by_lua_block', 33),
('\n -- use Lua to do something interesting here\n ', 35),
('\n -- use Lua that\'ll do something interesting here with external bracket for testing {\n ', 35),
(';', 35),
('log_by_lua_block', 36),
('\n print("I need no extra escaping here, for example: \\r\\nblah")\n ', 38),
Expand Down Expand Up @@ -277,7 +277,7 @@ def test_parse_lua_block_simple():
{
'line': 33,
'args': [
'\n -- use Lua to do something interesting here'
'\n -- use Lua that\'ll do something interesting here with external bracket for testing {'
'\n '
],
'directive': 'balancer_by_lua_block'
Expand Down