test262/tools/generation/lib/util/parse_yaml.py

24 lines
763 B
Python
Raw Normal View History

2016-03-16 18:58:10 +01:00
# 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
2016-03-16 18:58:10 +01:00
yamlPattern = re.compile(
r'^\s*---\n(.*?)(?:\n[^\n\S]*)?---\s*$',
flags=re.DOTALL)
endOfLine = re.compile(r'(^|.)$', flags=re.MULTILINE)
2016-03-16 18:58:10 +01:00
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
2021-07-17 00:53:59 +02:00
# in which every line is terminated by a tilde
terminated = endOfLine.sub(r'\1~', match.group(1))
dedented_terminated = textwrap.dedent(terminated)
dedented = endOfLine.sub('', dedented_terminated)
2016-03-16 18:58:10 +01:00
return yaml.safe_load(dedented)