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