Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / scripts / simplebench / table_templater.py
blob950f3b30247351b814002c192872ffa72740f514
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/>.
19 import itertools
20 from lark import Lark
22 grammar = """
23 start: ( text | column_switch | row_switch )+
25 column_switch: "{" text ["|" text]+ "}"
26 row_switch: "[" text ["|" text]+ "]"
27 text: /[^|{}\[\]]+/
28 """
30 parser = Lark(grammar)
32 class Templater:
33 def __init__(self, template):
34 self.tree = parser.parse(template)
36 c_switches = []
37 r_switches = []
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):
48 i = 0
49 j = 0
50 result = []
52 for x in self.tree.children:
53 if x.data == 'text':
54 result.append(x.children[0].value)
55 elif x.data == 'column_switch':
56 result.append(column[i])
57 i += 1
58 elif x.data == 'row_switch':
59 result.append(row[j])
60 j += 1
62 return ''.join(result)