[email protected] | 530bf7e8 | 2014-04-06 04:35:10 | [diff] [blame] | 1 | # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 5 | """Helper functions useful when writing scripts that integrate with GN. |
| 6 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 7 | The main functions are ToGNString() and FromGNString(), to convert between |
brettw | 81687d8 | 2016-01-29 23:23:10 | [diff] [blame] | 8 | serialized GN veriables and Python variables. |
| 9 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 10 | To use in an arbitrary Python file in the build: |
brettw | 81687d8 | 2016-01-29 23:23:10 | [diff] [blame] | 11 | |
| 12 | import os |
| 13 | import sys |
| 14 | |
| 15 | sys.path.append(os.path.join(os.path.dirname(__file__), |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 16 | os.pardir, os.pardir, 'build')) |
brettw | 81687d8 | 2016-01-29 23:23:10 | [diff] [blame] | 17 | import gn_helpers |
| 18 | |
| 19 | Where the sequence of parameters to join is the relative path from your source |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 20 | file to the build directory. |
| 21 | """ |
[email protected] | 530bf7e8 | 2014-04-06 04:35:10 | [diff] [blame] | 22 | |
Andrew Grieve | 169e377d | 2020-08-10 18:12:14 | [diff] [blame] | 23 | import json |
Ben Pastene | 00156a2 | 2020-03-23 18:13:10 | [diff] [blame] | 24 | import os |
| 25 | import re |
Raul Tambre | 4197d3a | 2019-03-19 15:04:20 | [diff] [blame] | 26 | import sys |
| 27 | |
| 28 | |
Ben Pastene | b52227f | 2020-06-01 22:41:30 | [diff] [blame] | 29 | _CHROMIUM_ROOT = os.path.join(os.path.dirname(__file__), os.pardir) |
| 30 | |
Andrew Grieve | 169e377d | 2020-08-10 18:12:14 | [diff] [blame] | 31 | BUILD_VARS_FILENAME = 'build_vars.json' |
Ben Pastene | 00156a2 | 2020-03-23 18:13:10 | [diff] [blame] | 32 | IMPORT_RE = re.compile(r'^import\("//(\S+)"\)') |
| 33 | |
| 34 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 35 | class GNError(Exception): |
[email protected] | 530bf7e8 | 2014-04-06 04:35:10 | [diff] [blame] | 36 | pass |
| 37 | |
| 38 | |
Samuel Huang | 007ab8b8 | 2020-06-05 21:44:34 | [diff] [blame] | 39 | # Computes ASCII code of an element of encoded Python 2 str / Python 3 bytes. |
| 40 | _Ord = ord if sys.version_info.major < 3 else lambda c: c |
| 41 | |
| 42 | |
| 43 | def _TranslateToGnChars(s): |
| 44 | for decoded_ch in s.encode('utf-8'): # str in Python 2, bytes in Python 3. |
| 45 | code = _Ord(decoded_ch) # int |
| 46 | if code in (34, 36, 92): # For '"', '$', or '\\'. |
| 47 | yield '\\' + chr(code) |
| 48 | elif 32 <= code < 127: |
| 49 | yield chr(code) |
| 50 | else: |
| 51 | yield '$0x%02X' % code |
| 52 | |
| 53 | |
| 54 | def ToGNString(value, pretty=False): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 55 | """Returns a stringified GN equivalent of a Python value. |
[email protected] | 530bf7e8 | 2014-04-06 04:35:10 | [diff] [blame] | 56 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 57 | Args: |
| 58 | value: The Python value to convert. |
Samuel Huang | 007ab8b8 | 2020-06-05 21:44:34 | [diff] [blame] | 59 | pretty: Whether to pretty print. If true, then non-empty lists are rendered |
| 60 | recursively with one item per line, with indents. Otherwise lists are |
| 61 | rendered without new line. |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 62 | Returns: |
| 63 | The stringified GN equivalent to |value|. |
| 64 | |
| 65 | Raises: |
| 66 | GNError: |value| cannot be printed to GN. |
| 67 | """ |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 68 | |
Samuel Huang | 007ab8b8 | 2020-06-05 21:44:34 | [diff] [blame] | 69 | if sys.version_info.major < 3: |
| 70 | basestring_compat = basestring |
| 71 | else: |
| 72 | basestring_compat = str |
hashimoto | 7bf1d0a | 2016-04-04 01:01:51 | [diff] [blame] | 73 | |
Samuel Huang | 007ab8b8 | 2020-06-05 21:44:34 | [diff] [blame] | 74 | # Emits all output tokens without intervening whitespaces. |
| 75 | def GenerateTokens(v, level): |
| 76 | if isinstance(v, basestring_compat): |
| 77 | yield '"' + ''.join(_TranslateToGnChars(v)) + '"' |
[email protected] | 530bf7e8 | 2014-04-06 04:35:10 | [diff] [blame] | 78 | |
Samuel Huang | 007ab8b8 | 2020-06-05 21:44:34 | [diff] [blame] | 79 | elif isinstance(v, bool): |
| 80 | yield 'true' if v else 'false' |
[email protected] | 530bf7e8 | 2014-04-06 04:35:10 | [diff] [blame] | 81 | |
Samuel Huang | 007ab8b8 | 2020-06-05 21:44:34 | [diff] [blame] | 82 | elif isinstance(v, int): |
| 83 | yield str(v) |
[email protected] | 530bf7e8 | 2014-04-06 04:35:10 | [diff] [blame] | 84 | |
Samuel Huang | 007ab8b8 | 2020-06-05 21:44:34 | [diff] [blame] | 85 | elif isinstance(v, list): |
| 86 | yield '[' |
| 87 | for i, item in enumerate(v): |
| 88 | if i > 0: |
| 89 | yield ',' |
| 90 | for tok in GenerateTokens(item, level + 1): |
| 91 | yield tok |
| 92 | yield ']' |
[email protected] | 530bf7e8 | 2014-04-06 04:35:10 | [diff] [blame] | 93 | |
Samuel Huang | 007ab8b8 | 2020-06-05 21:44:34 | [diff] [blame] | 94 | elif isinstance(v, dict): |
| 95 | if level > 0: |
| 96 | raise GNError('Attempting to recursively print a dictionary.') |
| 97 | for key in sorted(v): |
| 98 | if not isinstance(key, basestring_compat): |
| 99 | raise GNError('Dictionary key is not a string.') |
| 100 | if not key or key[0].isdigit() or not key.replace('_', '').isalnum(): |
| 101 | raise GNError('Dictionary key is not a valid GN identifier.') |
| 102 | yield key # No quotations. |
| 103 | yield '=' |
| 104 | for tok in GenerateTokens(value[key], level + 1): |
| 105 | yield tok |
| 106 | |
| 107 | else: # Not supporting float: Add only when needed. |
| 108 | raise GNError('Unsupported type when printing to GN.') |
| 109 | |
| 110 | can_start = lambda tok: tok and tok not in ',]=' |
| 111 | can_end = lambda tok: tok and tok not in ',[=' |
| 112 | |
| 113 | # Adds whitespaces, trying to keep everything (except dicts) in 1 line. |
| 114 | def PlainGlue(gen): |
| 115 | prev_tok = None |
| 116 | for i, tok in enumerate(gen): |
| 117 | if i > 0: |
| 118 | if can_end(prev_tok) and can_start(tok): |
| 119 | yield '\n' # New dict item. |
| 120 | elif prev_tok == '[' and tok == ']': |
| 121 | yield ' ' # Special case for []. |
| 122 | elif tok != ',': |
| 123 | yield ' ' |
| 124 | yield tok |
| 125 | prev_tok = tok |
| 126 | |
| 127 | # Adds whitespaces so non-empty lists can span multiple lines, with indent. |
| 128 | def PrettyGlue(gen): |
| 129 | prev_tok = None |
| 130 | level = 0 |
| 131 | for i, tok in enumerate(gen): |
| 132 | if i > 0: |
| 133 | if can_end(prev_tok) and can_start(tok): |
| 134 | yield '\n' + ' ' * level # New dict item. |
| 135 | elif tok == '=' or prev_tok in '=': |
| 136 | yield ' ' # Separator before and after '=', on same line. |
| 137 | if tok == ']': |
| 138 | level -= 1 |
| 139 | if int(prev_tok == '[') + int(tok == ']') == 1: # Exclude '[]' case. |
| 140 | yield '\n' + ' ' * level |
| 141 | yield tok |
| 142 | if tok == '[': |
| 143 | level += 1 |
| 144 | if tok == ',': |
| 145 | yield '\n' + ' ' * level |
| 146 | prev_tok = tok |
| 147 | |
| 148 | token_gen = GenerateTokens(value, 0) |
| 149 | ret = ''.join((PrettyGlue if pretty else PlainGlue)(token_gen)) |
| 150 | # Add terminating '\n' for dict |value| or multi-line output. |
| 151 | if isinstance(value, dict) or '\n' in ret: |
| 152 | return ret + '\n' |
| 153 | return ret |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 154 | |
| 155 | |
yyanagisawa | 18ef930 | 2016-10-07 02:35:17 | [diff] [blame] | 156 | def FromGNString(input_string): |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 157 | """Converts the input string from a GN serialized value to Python values. |
| 158 | |
| 159 | For details on supported types see GNValueParser.Parse() below. |
| 160 | |
| 161 | If your GN script did: |
| 162 | something = [ "file1", "file2" ] |
| 163 | args = [ "--values=$something" ] |
| 164 | The command line would look something like: |
| 165 | --values="[ \"file1\", \"file2\" ]" |
| 166 | Which when interpreted as a command line gives the value: |
| 167 | [ "file1", "file2" ] |
| 168 | |
| 169 | You can parse this into a Python list using GN rules with: |
| 170 | input_values = FromGNValues(options.values) |
| 171 | Although the Python 'ast' module will parse many forms of such input, it |
| 172 | will not handle GN escaping properly, nor GN booleans. You should use this |
| 173 | function instead. |
| 174 | |
| 175 | |
| 176 | A NOTE ON STRING HANDLING: |
| 177 | |
| 178 | If you just pass a string on the command line to your Python script, or use |
| 179 | string interpolation on a string variable, the strings will not be quoted: |
| 180 | str = "asdf" |
| 181 | args = [ str, "--value=$str" ] |
| 182 | Will yield the command line: |
| 183 | asdf --value=asdf |
| 184 | The unquoted asdf string will not be valid input to this function, which |
| 185 | accepts only quoted strings like GN scripts. In such cases, you can just use |
| 186 | the Python string literal directly. |
| 187 | |
| 188 | The main use cases for this is for other types, in particular lists. When |
| 189 | using string interpolation on a list (as in the top example) the embedded |
| 190 | strings will be quoted and escaped according to GN rules so the list can be |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 191 | re-parsed to get the same result. |
| 192 | """ |
yyanagisawa | 18ef930 | 2016-10-07 02:35:17 | [diff] [blame] | 193 | parser = GNValueParser(input_string) |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 194 | return parser.Parse() |
| 195 | |
| 196 | |
yyanagisawa | 18ef930 | 2016-10-07 02:35:17 | [diff] [blame] | 197 | def FromGNArgs(input_string): |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 198 | """Converts a string with a bunch of gn arg assignments into a Python dict. |
| 199 | |
| 200 | Given a whitespace-separated list of |
| 201 | |
| 202 | <ident> = (integer | string | boolean | <list of the former>) |
| 203 | |
| 204 | gn assignments, this returns a Python dict, i.e.: |
| 205 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 206 | FromGNArgs('foo=true\nbar=1\n') -> { 'foo': True, 'bar': 1 }. |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 207 | |
| 208 | Only simple types and lists supported; variables, structs, calls |
| 209 | and other, more complicated things are not. |
| 210 | |
| 211 | This routine is meant to handle only the simple sorts of values that |
| 212 | arise in parsing --args. |
| 213 | """ |
yyanagisawa | 18ef930 | 2016-10-07 02:35:17 | [diff] [blame] | 214 | parser = GNValueParser(input_string) |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 215 | return parser.ParseArgs() |
| 216 | |
| 217 | |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 218 | def UnescapeGNString(value): |
| 219 | """Given a string with GN escaping, returns the unescaped string. |
| 220 | |
| 221 | Be careful not to feed with input from a Python parsing function like |
| 222 | 'ast' because it will do Python unescaping, which will be incorrect when |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 223 | fed into the GN unescaper. |
| 224 | |
| 225 | Args: |
| 226 | value: Input string to unescape. |
| 227 | """ |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 228 | result = '' |
| 229 | i = 0 |
| 230 | while i < len(value): |
| 231 | if value[i] == '\\': |
| 232 | if i < len(value) - 1: |
| 233 | next_char = value[i + 1] |
| 234 | if next_char in ('$', '"', '\\'): |
| 235 | # These are the escaped characters GN supports. |
| 236 | result += next_char |
| 237 | i += 1 |
| 238 | else: |
| 239 | # Any other backslash is a literal. |
| 240 | result += '\\' |
| 241 | else: |
| 242 | result += value[i] |
| 243 | i += 1 |
| 244 | return result |
| 245 | |
| 246 | |
| 247 | def _IsDigitOrMinus(char): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 248 | return char in '-0123456789' |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 249 | |
| 250 | |
| 251 | class GNValueParser(object): |
| 252 | """Duplicates GN parsing of values and converts to Python types. |
| 253 | |
| 254 | Normally you would use the wrapper function FromGNValue() below. |
| 255 | |
| 256 | If you expect input as a specific type, you can also call one of the Parse* |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 257 | functions directly. All functions throw GNError on invalid input. |
| 258 | """ |
Ben Pastene | b52227f | 2020-06-01 22:41:30 | [diff] [blame] | 259 | |
| 260 | def __init__(self, string, checkout_root=_CHROMIUM_ROOT): |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 261 | self.input = string |
| 262 | self.cur = 0 |
Ben Pastene | b52227f | 2020-06-01 22:41:30 | [diff] [blame] | 263 | self.checkout_root = checkout_root |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 264 | |
| 265 | def IsDone(self): |
| 266 | return self.cur == len(self.input) |
| 267 | |
Ben Pastene | 00156a2 | 2020-03-23 18:13:10 | [diff] [blame] | 268 | def ReplaceImports(self): |
| 269 | """Replaces import(...) lines with the contents of the imports. |
| 270 | |
| 271 | Recurses on itself until there are no imports remaining, in the case of |
| 272 | nested imports. |
| 273 | """ |
| 274 | lines = self.input.splitlines() |
| 275 | if not any(line.startswith('import(') for line in lines): |
| 276 | return |
| 277 | for line in lines: |
| 278 | if not line.startswith('import('): |
| 279 | continue |
| 280 | regex_match = IMPORT_RE.match(line) |
| 281 | if not regex_match: |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 282 | raise GNError('Not a valid import string: %s' % line) |
Ben Pastene | b52227f | 2020-06-01 22:41:30 | [diff] [blame] | 283 | import_path = os.path.join(self.checkout_root, regex_match.group(1)) |
Ben Pastene | 00156a2 | 2020-03-23 18:13:10 | [diff] [blame] | 284 | with open(import_path) as f: |
| 285 | imported_args = f.read() |
| 286 | self.input = self.input.replace(line, imported_args) |
| 287 | # Call ourselves again if we've just replaced an import() with additional |
| 288 | # imports. |
| 289 | self.ReplaceImports() |
| 290 | |
| 291 | |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 292 | def ConsumeWhitespace(self): |
| 293 | while not self.IsDone() and self.input[self.cur] in ' \t\n': |
| 294 | self.cur += 1 |
| 295 | |
Ben Pastene | 9b24d85 | 2018-11-06 00:42:09 | [diff] [blame] | 296 | def ConsumeComment(self): |
| 297 | if self.IsDone() or self.input[self.cur] != '#': |
| 298 | return |
| 299 | |
| 300 | # Consume each comment, line by line. |
| 301 | while not self.IsDone() and self.input[self.cur] == '#': |
| 302 | # Consume the rest of the comment, up until the end of the line. |
| 303 | while not self.IsDone() and self.input[self.cur] != '\n': |
| 304 | self.cur += 1 |
| 305 | # Move the cursor to the next line (if there is one). |
| 306 | if not self.IsDone(): |
| 307 | self.cur += 1 |
| 308 | |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 309 | def Parse(self): |
| 310 | """Converts a string representing a printed GN value to the Python type. |
| 311 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 312 | See additional usage notes on FromGNString() above. |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 313 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 314 | * GN booleans ('true', 'false') will be converted to Python booleans. |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 315 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 316 | * GN numbers ('123') will be converted to Python numbers. |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 317 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 318 | * GN strings (double-quoted as in '"asdf"') will be converted to Python |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 319 | strings with GN escaping rules. GN string interpolation (embedded |
Julien Brianceau | 96dfe4d8 | 2017-08-01 09:03:13 | [diff] [blame] | 320 | variables preceded by $) are not supported and will be returned as |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 321 | literals. |
| 322 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 323 | * GN lists ('[1, "asdf", 3]') will be converted to Python lists. |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 324 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 325 | * GN scopes ('{ ... }') are not supported. |
| 326 | |
| 327 | Raises: |
| 328 | GNError: Parse fails. |
| 329 | """ |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 330 | result = self._ParseAllowTrailing() |
| 331 | self.ConsumeWhitespace() |
| 332 | if not self.IsDone(): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 333 | raise GNError("Trailing input after parsing:\n " + self.input[self.cur:]) |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 334 | return result |
| 335 | |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 336 | def ParseArgs(self): |
| 337 | """Converts a whitespace-separated list of ident=literals to a dict. |
| 338 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 339 | See additional usage notes on FromGNArgs(), above. |
| 340 | |
| 341 | Raises: |
| 342 | GNError: Parse fails. |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 343 | """ |
| 344 | d = {} |
| 345 | |
Ben Pastene | 00156a2 | 2020-03-23 18:13:10 | [diff] [blame] | 346 | self.ReplaceImports() |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 347 | self.ConsumeWhitespace() |
Ben Pastene | 9b24d85 | 2018-11-06 00:42:09 | [diff] [blame] | 348 | self.ConsumeComment() |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 349 | while not self.IsDone(): |
| 350 | ident = self._ParseIdent() |
| 351 | self.ConsumeWhitespace() |
| 352 | if self.input[self.cur] != '=': |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 353 | raise GNError("Unexpected token: " + self.input[self.cur:]) |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 354 | self.cur += 1 |
| 355 | self.ConsumeWhitespace() |
| 356 | val = self._ParseAllowTrailing() |
| 357 | self.ConsumeWhitespace() |
Ben Pastene | 9b24d85 | 2018-11-06 00:42:09 | [diff] [blame] | 358 | self.ConsumeComment() |
Ben Pastene | b75f8ec | 2020-06-04 22:09:28 | [diff] [blame] | 359 | self.ConsumeWhitespace() |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 360 | d[ident] = val |
| 361 | |
| 362 | return d |
| 363 | |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 364 | def _ParseAllowTrailing(self): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 365 | """Internal version of Parse() that doesn't check for trailing stuff.""" |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 366 | self.ConsumeWhitespace() |
| 367 | if self.IsDone(): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 368 | raise GNError("Expected input to parse.") |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 369 | |
| 370 | next_char = self.input[self.cur] |
| 371 | if next_char == '[': |
| 372 | return self.ParseList() |
| 373 | elif _IsDigitOrMinus(next_char): |
| 374 | return self.ParseNumber() |
| 375 | elif next_char == '"': |
| 376 | return self.ParseString() |
| 377 | elif self._ConstantFollows('true'): |
| 378 | return True |
| 379 | elif self._ConstantFollows('false'): |
| 380 | return False |
| 381 | else: |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 382 | raise GNError("Unexpected token: " + self.input[self.cur:]) |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 383 | |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 384 | def _ParseIdent(self): |
yyanagisawa | 18ef930 | 2016-10-07 02:35:17 | [diff] [blame] | 385 | ident = '' |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 386 | |
| 387 | next_char = self.input[self.cur] |
| 388 | if not next_char.isalpha() and not next_char=='_': |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 389 | raise GNError("Expected an identifier: " + self.input[self.cur:]) |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 390 | |
yyanagisawa | 18ef930 | 2016-10-07 02:35:17 | [diff] [blame] | 391 | ident += next_char |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 392 | self.cur += 1 |
| 393 | |
| 394 | next_char = self.input[self.cur] |
| 395 | while next_char.isalpha() or next_char.isdigit() or next_char=='_': |
yyanagisawa | 18ef930 | 2016-10-07 02:35:17 | [diff] [blame] | 396 | ident += next_char |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 397 | self.cur += 1 |
| 398 | next_char = self.input[self.cur] |
| 399 | |
yyanagisawa | 18ef930 | 2016-10-07 02:35:17 | [diff] [blame] | 400 | return ident |
dpranke | 65d84dc0 | 2016-04-06 00:07:18 | [diff] [blame] | 401 | |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 402 | def ParseNumber(self): |
| 403 | self.ConsumeWhitespace() |
| 404 | if self.IsDone(): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 405 | raise GNError('Expected number but got nothing.') |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 406 | |
| 407 | begin = self.cur |
| 408 | |
| 409 | # The first character can include a negative sign. |
| 410 | if not self.IsDone() and _IsDigitOrMinus(self.input[self.cur]): |
| 411 | self.cur += 1 |
| 412 | while not self.IsDone() and self.input[self.cur].isdigit(): |
| 413 | self.cur += 1 |
| 414 | |
| 415 | number_string = self.input[begin:self.cur] |
| 416 | if not len(number_string) or number_string == '-': |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 417 | raise GNError('Not a valid number.') |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 418 | return int(number_string) |
| 419 | |
| 420 | def ParseString(self): |
| 421 | self.ConsumeWhitespace() |
| 422 | if self.IsDone(): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 423 | raise GNError('Expected string but got nothing.') |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 424 | |
| 425 | if self.input[self.cur] != '"': |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 426 | raise GNError('Expected string beginning in a " but got:\n ' + |
| 427 | self.input[self.cur:]) |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 428 | self.cur += 1 # Skip over quote. |
| 429 | |
| 430 | begin = self.cur |
| 431 | while not self.IsDone() and self.input[self.cur] != '"': |
| 432 | if self.input[self.cur] == '\\': |
| 433 | self.cur += 1 # Skip over the backslash. |
| 434 | if self.IsDone(): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 435 | raise GNError('String ends in a backslash in:\n ' + self.input) |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 436 | self.cur += 1 |
| 437 | |
| 438 | if self.IsDone(): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 439 | raise GNError('Unterminated string:\n ' + self.input[begin:]) |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 440 | |
| 441 | end = self.cur |
| 442 | self.cur += 1 # Consume trailing ". |
| 443 | |
| 444 | return UnescapeGNString(self.input[begin:end]) |
| 445 | |
| 446 | def ParseList(self): |
| 447 | self.ConsumeWhitespace() |
| 448 | if self.IsDone(): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 449 | raise GNError('Expected list but got nothing.') |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 450 | |
| 451 | # Skip over opening '['. |
| 452 | if self.input[self.cur] != '[': |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 453 | raise GNError('Expected [ for list but got:\n ' + self.input[self.cur:]) |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 454 | self.cur += 1 |
| 455 | self.ConsumeWhitespace() |
| 456 | if self.IsDone(): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 457 | raise GNError('Unterminated list:\n ' + self.input) |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 458 | |
| 459 | list_result = [] |
| 460 | previous_had_trailing_comma = True |
| 461 | while not self.IsDone(): |
| 462 | if self.input[self.cur] == ']': |
| 463 | self.cur += 1 # Skip over ']'. |
| 464 | return list_result |
| 465 | |
| 466 | if not previous_had_trailing_comma: |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 467 | raise GNError('List items not separated by comma.') |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 468 | |
| 469 | list_result += [ self._ParseAllowTrailing() ] |
| 470 | self.ConsumeWhitespace() |
| 471 | if self.IsDone(): |
| 472 | break |
| 473 | |
| 474 | # Consume comma if there is one. |
| 475 | previous_had_trailing_comma = self.input[self.cur] == ',' |
| 476 | if previous_had_trailing_comma: |
| 477 | # Consume comma. |
| 478 | self.cur += 1 |
| 479 | self.ConsumeWhitespace() |
| 480 | |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 481 | raise GNError('Unterminated list:\n ' + self.input) |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 482 | |
| 483 | def _ConstantFollows(self, constant): |
Samuel Huang | 1ea5180e | 2020-05-25 16:29:40 | [diff] [blame] | 484 | """Checks and maybe consumes a string constant at current input location. |
| 485 | |
| 486 | Param: |
| 487 | constant: The string constant to check. |
| 488 | |
| 489 | Returns: |
| 490 | True if |constant| follows immediately at the current location in the |
| 491 | input. In this case, the string is consumed as a side effect. Otherwise, |
| 492 | returns False and the current position is unchanged. |
| 493 | """ |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 494 | end = self.cur + len(constant) |
dpranke | e031ec2 | 2016-04-02 00:17:34 | [diff] [blame] | 495 | if end > len(self.input): |
brettw | 9dffb54 | 2016-01-22 18:40:03 | [diff] [blame] | 496 | return False # Not enough room. |
| 497 | if self.input[self.cur:end] == constant: |
| 498 | self.cur = end |
| 499 | return True |
| 500 | return False |
Andrew Grieve | 169e377d | 2020-08-10 18:12:14 | [diff] [blame] | 501 | |
| 502 | |
| 503 | def ReadBuildVars(output_directory): |
| 504 | """Parses $output_directory/build_vars.json into a dict.""" |
| 505 | with open(os.path.join(output_directory, BUILD_VARS_FILENAME)) as f: |
| 506 | return json.load(f) |