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.
|
|
|
|
|
2018-02-09 17:27:33 +01:00
|
|
|
import codecs
|
2016-03-16 18:58:10 +01:00
|
|
|
import re
|
|
|
|
|
2019-08-14 18:46:24 +02:00
|
|
|
from .util.find_comments import find_comments
|
|
|
|
from .util.parse_yaml import parse_yaml
|
2016-03-16 18:58:10 +01:00
|
|
|
|
|
|
|
regionStartPattern = re.compile(r'-\s+(\S+)')
|
|
|
|
|
|
|
|
class Case:
|
2018-02-09 17:27:33 +01:00
|
|
|
def __init__(self, file_name, encoding):
|
2016-03-16 18:58:10 +01:00
|
|
|
self.attribs = dict(meta=None, regions=dict())
|
|
|
|
|
2018-02-09 17:27:33 +01:00
|
|
|
with codecs.open(file_name, 'r', encoding) as handle:
|
2016-03-16 18:58:10 +01:00
|
|
|
self.attribs = self._parse(handle.read())
|
|
|
|
|
|
|
|
def _parse(self, source):
|
|
|
|
case = dict(meta=None, regions=dict())
|
|
|
|
region_name = None
|
|
|
|
region_start = 0
|
|
|
|
lines = source.split('\n')
|
|
|
|
|
|
|
|
for comment in find_comments(source):
|
|
|
|
meta = parse_yaml(comment['source'])
|
|
|
|
if meta:
|
|
|
|
case['meta'] = meta
|
|
|
|
continue
|
|
|
|
|
|
|
|
match = regionStartPattern.match(comment['source'])
|
|
|
|
if match:
|
|
|
|
if region_name:
|
|
|
|
case['regions'][region_name] = \
|
|
|
|
'\n'.join(lines[region_start:comment['lineno'] - 1])
|
|
|
|
|
|
|
|
region_name = match.group(1)
|
|
|
|
region_start = comment['lineno']
|
|
|
|
continue
|
|
|
|
|
|
|
|
if region_name:
|
|
|
|
case['regions'][region_name] = \
|
|
|
|
'\n'.join(lines[region_start:-1])
|
|
|
|
|
|
|
|
return case
|