barectf: reflow licence headers for 72 columns
[deliverable/barectf.git] / barectf / codegen.py
CommitLineData
e5aa0be3
PP
1# The MIT License (MIT)
2#
4a90140d 3# Copyright (c) 2015-2020 Philippe Proulx <pproulx@efficios.com>
e5aa0be3 4#
1378f213
PP
5# Permission is hereby granted, free of charge, to any person obtaining
6# a copy of this software and associated documentation files (the
7# "Software"), to deal in the Software without restriction, including
8# without limitation the rights to use, copy, modify, merge, publish,
9# distribute, sublicense, and/or sell copies of the Software, and to
10# permit persons to whom the Software is furnished to do so, subject to
11# the following conditions:
e5aa0be3 12#
1378f213
PP
13# The above copyright notice and this permission notice shall be
14# included in all copies or substantial portions of the Software.
e5aa0be3 15#
1378f213
PP
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
e5aa0be3
PP
23
24
25class CodeGenerator:
26 def __init__(self, indent_string):
27 self._indent_string = indent_string
28 self.reset()
29
30 @property
31 def code(self):
32 return '\n'.join(self._lines)
33
34 def reset(self):
35 self._lines = []
36 self._indent = 0
37 self._glue = False
38
39 def add_line(self, line):
40 if self._glue:
41 self.append_to_last_line(line)
42 self._glue = False
43 return
44
45 indent_string = self._get_indent_string()
46 self._lines.append(indent_string + str(line))
47
48 def add_lines(self, lines):
49 if type(lines) is str:
50 lines = lines.split('\n')
51
52 for line in lines:
53 self.add_line(line)
54
55 def add_glue(self):
56 self._glue = True
57
58 def append_to_last_line(self, s):
59 if self._lines:
60 self._lines[-1] += str(s)
61
62 def add_empty_line(self):
63 self._lines.append('')
64
65 def add_cc_line(self, comment):
66 self.add_line('/* {} */'.format(comment))
67
68 def append_cc_to_last_line(self, comment, with_space=True):
69 if with_space:
70 sp = ' '
71 else:
72 sp = ''
73
74 self.append_to_last_line('{}/* {} */'.format(sp, comment))
75
76 def indent(self):
77 self._indent += 1
78
79 def unindent(self):
80 self._indent = max(self._indent - 1, 0)
81
82 def _get_indent_string(self):
83 return self._indent_string * self._indent
This page took 0.054095 seconds and 4 git commands to generate.