summaryrefslogtreecommitdiff
path: root/tools/generation/lib/case.py
blob: f43d4f664ba24c0965aa8f8d14f5e6308aeb7468 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Copyright (C) 2016 the V8 project authors. All rights reserved.
# This code is governed by the BSD license found in the LICENSE file.

import re

from util.find_comments import find_comments
from util.parse_yaml import parse_yaml

regionStartPattern = re.compile(r'-\s+(\S+)')

class Case:
    def __init__(self, file_name):
        self.attribs = dict(meta=None, regions=dict())

        with open(file_name) as handle:
            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