Chromium Code Reviews| Index: build/gn_helpers.py |
| diff --git a/build/gn_helpers.py b/build/gn_helpers.py |
| index f1037720bbf878ac8cbaa56f0896aaec04ea5a62..b5fa1f8e6ab00d2a028e8e27822b025be44e8007 100644 |
| --- a/build/gn_helpers.py |
| +++ b/build/gn_helpers.py |
| @@ -100,6 +100,27 @@ def FromGNString(input): |
| return parser.Parse() |
| +def FromGNArgs(input): |
| + """Converts a string with a bunch of gn arg assignments into a Python dict. |
| + |
| + Given a whitespace-separated list of |
| + |
| + <ident> = (integer | string | boolean) |
| + |
| + gn assignments, this returns a Python dict, i.e.: |
| + |
| + FromGNArgs("foo=true\nbar=1\n") -> { 'foo': True, 'bar': 1 }. |
| + |
| + Only simple types are supported; variables, complex types, calls |
| + and other, more complicated things are not. |
| + |
| + This routine is meant to handle only the simple sorts of values that |
| + arise in parsing --args. |
| + """ |
| + parser = GNValueParser(input) |
| + return parser.ParseArgs() |
| + |
| + |
| def UnescapeGNString(value): |
| """Given a string with GN escaping, returns the unescaped string. |
| @@ -171,14 +192,35 @@ class GNValueParser(object): |
| self.input[self.cur:]) |
| return result |
| - def _ParseAllowTrailing(self): |
| + def ParseArgs(self): |
| + """Converts a whitespace-separated list of ident=literals to a dict. |
| + |
| + See additional usage notes on FromGNArgs, above. |
| + """ |
| + d = {} |
| + |
| + self.ConsumeWhitespace() |
| + while not self.IsDone(): |
| + ident = self._ParseIdent() |
| + self.ConsumeWhitespace() |
| + if self.input[self.cur] != '=': |
| + raise GNException("Unexpected token: " + self.input[self.cur:]) |
| + self.cur += 1 |
| + self.ConsumeWhitespace() |
| + val = self._ParseAllowTrailing(lists_are_allowed=False) |
|
brettw
2016/04/05 19:25:27
It seems like you go through some effort here to d
Dirk Pranke
2016/04/05 19:28:42
I don't remember exactly now, but I have a vague m
|
| + self.ConsumeWhitespace() |
| + d[ident] = val |
| + |
| + return d |
| + |
| + def _ParseAllowTrailing(self, lists_are_allowed=True): |
| """Internal version of Parse that doesn't check for trailing stuff.""" |
| self.ConsumeWhitespace() |
| if self.IsDone(): |
| raise GNException("Expected input to parse.") |
| next_char = self.input[self.cur] |
| - if next_char == '[': |
| + if lists_are_allowed and next_char == '[': |
| return self.ParseList() |
| elif _IsDigitOrMinus(next_char): |
| return self.ParseNumber() |
| @@ -191,6 +233,24 @@ class GNValueParser(object): |
| else: |
| raise GNException("Unexpected token: " + self.input[self.cur:]) |
| + def _ParseIdent(self): |
| + id = '' |
| + |
| + next_char = self.input[self.cur] |
| + if not next_char.isalpha() and not next_char=='_': |
| + raise GNException("Expected an identifier: " + self.input[self.cur:]) |
| + |
| + id += next_char |
| + self.cur += 1 |
| + |
| + next_char = self.input[self.cur] |
| + while next_char.isalpha() or next_char.isdigit() or next_char=='_': |
| + id += next_char |
| + self.cur += 1 |
| + next_char = self.input[self.cur] |
| + |
| + return id |
| + |
| def ParseNumber(self): |
| self.ConsumeWhitespace() |
| if self.IsDone(): |