summaryrefslogtreecommitdiff
path: root/perf2cfg/perf2cfg/parse.py
blob: 9e211bef3c1435be46a9ba8c0e81b6e8636afcf0 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright (C) 2020 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Functions to build and parse directives from CFG files."""

import re

from typing import Iterable, List

from perf2cfg import exceptions


def build_flags(flags: Iterable[str]) -> str:
    """Builds a flags directive from a list of arguments.

    Args:
        flags (Iterable[str]): An iterable of flags.

    Returns:
        str: A flags directive with the given arguments.

    Examples:
        >>> parse_flags(['catch_block', 'critical'])
        '    flags "catch_block" "critical"'
    """
    if not flags:
        return '    flags'

    args = ' '.join(f'"{flag}"' for flag in flags)
    return f'    flags {args}'


def build_name(name: str) -> str:
    """Builds a name directive from an argument.

    Args:
        name (str): An argument.

    Returns:
        str: A name directive with the given argument.
    """
    return f'  name "{name}"'


def parse_address(line: str) -> int:
    """Parses an address from a line.

    Args:
        line (str): A line to parse an address from.

    Returns:
        int: An instruction address.

    Raises:
        exceptions.ParseError: An error occurred during parsing.

    Examples:
        >>> parse_address('0x0000001c: d503201f nop')
        28
    """
    parts = line.split(':', 1)
    addr = parts[0]

    try:
        return int(addr, 16)
    except ValueError:
        raise exceptions.ParseError('Expected an address')


def parse_flags(line: str) -> List[str]:
    """Parses a flags directive from a line.

    Args:
        line (str): A line to parse a flags directive from.

    Returns:
        List[str]: A list of unquoted arguments from a flags directive, or an
            empty list if no arguments were found.

    Raises:
        exceptions.ParseError: An error occurred during parsing.

    Example:
        >>> parse_flags('flags "catch_block" "critical"')
        ['catch_block', 'critical']
    """
    parts = line.split(None, 1)
    if parts[0] != 'flags':
        raise exceptions.ParseError('Expected a `flags` directive')

    if len(parts) < 2:
        return []

    return re.findall(r'\"([^\"]+)\"', parts[1])


def parse_name(line: str) -> str:
    """Parses a name directive from a line.

    Args:
        line (str): A line to parse a name directive from.

    Returns:
        str: The unquoted argument of a name directive.

    Raises:
        exceptions.ParseError: An error occurred during parsing.

    Examples:
        >>> parse_name('name "disassembly (after)"')
        'disassembly (after)'
    """
    parts = line.split(None, 1)
    if parts[0] != 'name':
        raise exceptions.ParseError('Expected a `name` directive')

    if len(parts) < 2:
        raise exceptions.ParseError(
            'Expected an argument to the `name` directive')

    return parts[1].strip('"')