Skip to content

Commit 122da4d

Browse files
committed
Added support for comments between arguments
1 parent d2b8b3f commit 122da4d

File tree

4 files changed

+117
-2
lines changed

4 files changed

+117
-2
lines changed

crossplane/parser.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def _parse(parsing, tokens, ctx=(), consume=False):
7676

7777
# parse recursively by pulling from a flat stream of tokens
7878
for token, lineno, quoted in tokens:
79+
comments_in_args = []
80+
7981
# we are parsing a block, so break if it's closing
8082
if token == '}' and not quoted:
8183
break
@@ -118,7 +120,11 @@ def _parse(parsing, tokens, ctx=(), consume=False):
118120
args = stmt['args']
119121
token, __, quoted = next(tokens) # disregard line numbers of args
120122
while token not in ('{', ';', '}') or quoted:
121-
stmt['args'].append(token)
123+
if token.startswith('#') and not quoted:
124+
comments_in_args.append(token[1:])
125+
else:
126+
stmt['args'].append(token)
127+
122128
token, __, quoted = next(tokens)
123129

124130
# consume the directive if it is ignored and move on
@@ -196,6 +202,16 @@ def _parse(parsing, tokens, ctx=(), consume=False):
196202

197203
parsed.append(stmt)
198204

205+
# add all comments found inside args after stmt is added
206+
for comment in comments_in_args:
207+
comment_stmt = {
208+
'directive': '#',
209+
'line': stmt['line'],
210+
'args': [],
211+
'comment': comment
212+
}
213+
parsed.append(comment_stmt)
214+
199215
return parsed
200216

201217
# the includes list grows as "include" directives are found in _parse
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
http { #comment 1
2+
log_format #comment 2
3+
\#arg\ 1 #comment 3
4+
'#arg 2' #comment 4
5+
#comment 5
6+
;
7+
}

tests/test_build.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,37 @@ def test_build_with_quoted_unicode():
196196
assert built == u"env 'русский текст';"
197197

198198

199+
def test_build_multiple_comments_on_one_line():
200+
payload = [
201+
{
202+
"directive": "#",
203+
"line": 1,
204+
"args": [],
205+
"comment": "comment1"
206+
},
207+
{
208+
"directive": "user",
209+
"line": 2,
210+
"args": ["root"]
211+
},
212+
{
213+
"directive": "#",
214+
"line": 2,
215+
"args": [],
216+
"comment": "comment2"
217+
},
218+
{
219+
"directive": "#",
220+
"line": 2,
221+
"args": [],
222+
"comment": "comment3"
223+
}
224+
]
225+
built = crossplane.build(payload, indent=4, tabs=False)
226+
assert built == '#comment1\nuser root; #comment2 #comment3'
227+
228+
229+
199230
def test_build_files_with_missing_status_and_errors(tmpdir):
200231
assert len(tmpdir.listdir()) == 0
201232
payload = {

tests/test_parse.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def test_config_with_comments():
582582
}
583583
],
584584
"status" : "ok",
585-
"file" : os.path.join(dirname, 'nginx.conf')
585+
"file" : config
586586
}
587587
]
588588
}
@@ -914,3 +914,64 @@ def test_combine_parsed_missing_values():
914914
}
915915
]
916916
}
917+
918+
919+
def test_comments_between_args():
920+
dirname = os.path.join(here, 'configs', 'comments-between-args')
921+
config = os.path.join(dirname, 'nginx.conf')
922+
payload = crossplane.parse(config, comments=True)
923+
assert payload == {
924+
'status': 'ok',
925+
'errors': [],
926+
'config': [
927+
{
928+
'file': config,
929+
'status': 'ok',
930+
'errors': [],
931+
'parsed': [
932+
{
933+
'directive': 'http',
934+
'line': 1,
935+
'args': [],
936+
'block': [
937+
{
938+
'directive': '#',
939+
'line': 1,
940+
'args': [],
941+
'comment': 'comment 1'
942+
},
943+
{
944+
'directive': 'log_format',
945+
'line': 2,
946+
'args': ['\\#arg\\ 1', '#arg 2']
947+
},
948+
{
949+
'directive': '#',
950+
'line': 2,
951+
'args': [],
952+
'comment': 'comment 2'
953+
},
954+
{
955+
'directive': '#',
956+
'line': 2,
957+
'args': [],
958+
'comment': 'comment 3'
959+
},
960+
{
961+
'directive': '#',
962+
'line': 2,
963+
'args': [],
964+
'comment': 'comment 4'
965+
},
966+
{
967+
'directive': '#',
968+
'line': 2,
969+
'args': [],
970+
'comment': 'comment 5'
971+
}
972+
]
973+
}
974+
]
975+
}
976+
]
977+
}

0 commit comments

Comments
 (0)