1 # Parser for test templates
3 # Copyright (c) 2021 Virtuozzo International GmbH.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 start: ( text | column_switch | row_switch )+
25 column_switch: "{" text ["|" text]+ "}"
26 row_switch: "[" text ["|" text]+ "]"
30 parser
= Lark(grammar
)
33 def __init__(self
, template
):
34 self
.tree
= parser
.parse(template
)
38 for x
in self
.tree
.children
:
39 if x
.data
== 'column_switch':
40 c_switches
.append([el
.children
[0].value
for el
in x
.children
])
41 elif x
.data
== 'row_switch':
42 r_switches
.append([el
.children
[0].value
for el
in x
.children
])
44 self
.columns
= list(itertools
.product(*c_switches
))
45 self
.rows
= list(itertools
.product(*r_switches
))
47 def gen(self
, column
, row
):
52 for x
in self
.tree
.children
:
54 result
.append(x
.children
[0].value
)
55 elif x
.data
== 'column_switch':
56 result
.append(column
[i
])
58 elif x
.data
== 'row_switch':
62 return ''.join(result
)