Commit 9b24d850 authored by Ben Pastene's avatar Ben Pastene Committed by Commit Bot

Make gn_helpers.py safely ignore comments.

I'd like to add comments to the GN args of some bots. This will let us
do that without mb or simplechrome SDK crashing.

Bug: 901471
Change-Id: I7f49434e02591639a02ef9cfd0f026680f6b9de1
Reviewed-on: https://chromium-review.googlesource.com/c/1318188Reviewed-by: default avatarDirk Pranke <dpranke@chromium.org>
Commit-Queue: Ben Pastene <bpastene@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605544}
parent e0e48885
......@@ -171,6 +171,19 @@ class GNValueParser(object):
while not self.IsDone() and self.input[self.cur] in ' \t\n':
self.cur += 1
def ConsumeComment(self):
if self.IsDone() or self.input[self.cur] != '#':
return
# Consume each comment, line by line.
while not self.IsDone() and self.input[self.cur] == '#':
# Consume the rest of the comment, up until the end of the line.
while not self.IsDone() and self.input[self.cur] != '\n':
self.cur += 1
# Move the cursor to the next line (if there is one).
if not self.IsDone():
self.cur += 1
def Parse(self):
"""Converts a string representing a printed GN value to the Python type.
......@@ -203,6 +216,7 @@ class GNValueParser(object):
d = {}
self.ConsumeWhitespace()
self.ConsumeComment()
while not self.IsDone():
ident = self._ParseIdent()
self.ConsumeWhitespace()
......@@ -212,6 +226,7 @@ class GNValueParser(object):
self.ConsumeWhitespace()
val = self._ParseAllowTrailing()
self.ConsumeWhitespace()
self.ConsumeComment()
d[ident] = val
return d
......
......@@ -85,6 +85,15 @@ class UnitTest(unittest.TestCase):
self.assertEqual(gn_helpers.FromGNArgs('foo="bar baz"'),
{'foo': 'bar baz'})
# Comments should work (and be ignored).
gn_args_lines = [
'# Top-level comment.',
'foo = true',
'bar = 1 # In-line comment.',
]
self.assertEqual(gn_helpers.FromGNArgs('\n'.join(gn_args_lines)),
{'foo': True, 'bar': 1})
# Lists should work.
self.assertEqual(gn_helpers.FromGNArgs('foo=[1, 2, 3]'),
{'foo': [1, 2, 3]})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment