test262/tools/generation/lib/util/parse_yaml.py
Richard Gibson d15066ec39
Cleanup generation code (#3041)
* Simplify find_cases

* Improve help text

* Improve YAML-capturing regex

* Use built-in dedenting

* Fix use of built-in dedenting

* Fix use of built-in dedenting for Python 3
2021-07-16 09:39:57 -04:00

24 lines
761 B
Python

# Copyright (C) 2016 the V8 project authors. All rights reserved.
# This code is governed by the BSD license found in the LICENSE file.
import yaml, re, textwrap
yamlPattern = re.compile(
r'^\s*---\n(.*?)(?:\n[^\n\S]*)?---\s*$',
flags=re.DOTALL)
endOfLine = re.compile(r'(^|.)$', flags=re.MULTILINE)
def parse_yaml(string):
match = yamlPattern.match(string)
if not match:
return False
# dedent truncates only-whitespace lines,
# so run it against a transformed string
# in which every line is terminated by a dot
terminated = endOfLine.sub(r'\1~', match.group(1))
dedented_terminated = textwrap.dedent(terminated)
dedented = endOfLine.sub('', dedented_terminated)
return yaml.safe_load(dedented)