|
14 | 14 | from loki.ir.find import FindNodes |
15 | 15 | from loki.ir.transformer import Transformer |
16 | 16 | from loki.ir.visitor import Visitor |
17 | | -# from loki.expression.parser import parse_expr |
18 | 17 | from loki.tools.util import as_tuple, replace_windowed |
19 | 18 | from loki.logging import debug, warning |
20 | 19 |
|
@@ -51,13 +50,26 @@ def is_loki_pragma(pragma, starts_with=None): |
51 | 50 |
|
52 | 51 |
|
53 | 52 | class PragmaParameters: |
| 53 | + """ |
| 54 | + Utility class to parse strings for parameters in the form ``<command>[(<arg>)]`` and |
| 55 | + return them as a map ``{<command>: <arg> or None}``. |
| 56 | + """ |
54 | 57 |
|
55 | 58 | _pattern_opening_parenthesis = re.compile(r'\(') |
56 | 59 | _pattern_closing_parenthesis = re.compile(r'\)') |
57 | 60 | _pattern_quoted_string = re.compile(r'(?:\'.*?\')|(?:".*?")') |
58 | 61 |
|
59 | 62 | @classmethod |
60 | 63 | def find(cls, string): |
| 64 | + """ |
| 65 | + Find parameters in the form ``<command>[(<arg>)]`` and |
| 66 | + return them as a map ``{<command>: <arg> or None}``. |
| 67 | +
|
| 68 | + .. note:: |
| 69 | + This allows nested parenthesis by matching pairs of |
| 70 | + parantheses starting at the end by pushing and popping |
| 71 | + from a stack. |
| 72 | + """ |
61 | 73 | string = cls._pattern_quoted_string.sub('', string) |
62 | 74 | p_open = [match.start() for match in cls._pattern_opening_parenthesis.finditer(string)] |
63 | 75 | p_close = [match.start() for match in cls._pattern_closing_parenthesis.finditer(string)] |
@@ -91,7 +103,7 @@ def _match_spans(open_, close_): |
91 | 103 | spans += p_spans[::-1] |
92 | 104 | parameters = defaultdict(list) |
93 | 105 | if not spans and string.strip(): |
94 | | - for key in string.strip().split(' '): # keys[:-1]: |
| 106 | + for key in string.strip().split(' '): |
95 | 107 | if key != '': |
96 | 108 | parameters[key].append(None) |
97 | 109 | for i, span in enumerate(spans): |
@@ -134,7 +146,6 @@ def get_pragma_parameters(pragma, starts_with=None, only_loki_pragmas=True): |
134 | 146 | pragma = as_tuple(pragma) |
135 | 147 | parameters = defaultdict(list) |
136 | 148 | for p in pragma: |
137 | | - parameter = None |
138 | 149 | if only_loki_pragmas and p.keyword.lower() != 'loki': |
139 | 150 | continue |
140 | 151 | content = p.content or '' |
|
0 commit comments