Skip to content

Commit

Permalink
Merge pull request #68 from nginxinc/comments-between-args-support
Browse files Browse the repository at this point in the history
Added support for comments between arguments
  • Loading branch information
aluttik authored Apr 8, 2019
2 parents 8300aaa + 122da4d commit c035082
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
18 changes: 17 additions & 1 deletion crossplane/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def _parse(parsing, tokens, ctx=(), consume=False):

# parse recursively by pulling from a flat stream of tokens
for token, lineno, quoted in tokens:
comments_in_args = []

# we are parsing a block, so break if it's closing
if token == '}' and not quoted:
break
Expand Down Expand Up @@ -118,7 +120,11 @@ def _parse(parsing, tokens, ctx=(), consume=False):
args = stmt['args']
token, __, quoted = next(tokens) # disregard line numbers of args
while token not in ('{', ';', '}') or quoted:
stmt['args'].append(token)
if token.startswith('#') and not quoted:
comments_in_args.append(token[1:])
else:
stmt['args'].append(token)

token, __, quoted = next(tokens)

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

parsed.append(stmt)

# add all comments found inside args after stmt is added
for comment in comments_in_args:
comment_stmt = {
'directive': '#',
'line': stmt['line'],
'args': [],
'comment': comment
}
parsed.append(comment_stmt)

return parsed

# the includes list grows as "include" directives are found in _parse
Expand Down
7 changes: 7 additions & 0 deletions tests/configs/comments-between-args/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
http { #comment 1
log_format #comment 2
\#arg\ 1 #comment 3
'#arg 2' #comment 4
#comment 5
;
}
31 changes: 31 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,37 @@ def test_build_with_quoted_unicode():
assert built == u"env 'русский текст';"


def test_build_multiple_comments_on_one_line():
payload = [
{
"directive": "#",
"line": 1,
"args": [],
"comment": "comment1"
},
{
"directive": "user",
"line": 2,
"args": ["root"]
},
{
"directive": "#",
"line": 2,
"args": [],
"comment": "comment2"
},
{
"directive": "#",
"line": 2,
"args": [],
"comment": "comment3"
}
]
built = crossplane.build(payload, indent=4, tabs=False)
assert built == '#comment1\nuser root; #comment2 #comment3'



def test_build_files_with_missing_status_and_errors(tmpdir):
assert len(tmpdir.listdir()) == 0
payload = {
Expand Down
63 changes: 62 additions & 1 deletion tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def test_config_with_comments():
}
],
"status" : "ok",
"file" : os.path.join(dirname, 'nginx.conf')
"file" : config
}
]
}
Expand Down Expand Up @@ -914,3 +914,64 @@ def test_combine_parsed_missing_values():
}
]
}


def test_comments_between_args():
dirname = os.path.join(here, 'configs', 'comments-between-args')
config = os.path.join(dirname, 'nginx.conf')
payload = crossplane.parse(config, comments=True)
assert payload == {
'status': 'ok',
'errors': [],
'config': [
{
'file': config,
'status': 'ok',
'errors': [],
'parsed': [
{
'directive': 'http',
'line': 1,
'args': [],
'block': [
{
'directive': '#',
'line': 1,
'args': [],
'comment': 'comment 1'
},
{
'directive': 'log_format',
'line': 2,
'args': ['\\#arg\\ 1', '#arg 2']
},
{
'directive': '#',
'line': 2,
'args': [],
'comment': 'comment 2'
},
{
'directive': '#',
'line': 2,
'args': [],
'comment': 'comment 3'
},
{
'directive': '#',
'line': 2,
'args': [],
'comment': 'comment 4'
},
{
'directive': '#',
'line': 2,
'args': [],
'comment': 'comment 5'
}
]
}
]
}
]
}

0 comments on commit c035082

Please sign in to comment.