OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 # for details. All rights reserved. Use of this source code is governed by a | |
3 # BSD-style license that can be found in the LICENSE file. | |
4 | |
5 # TODO(jimhug): Move this and other gen files into Dart. | |
6 | |
7 HEADER = '''// Copyright (c) 2012, the Dart project authors. Please see the AUT
HORS file | |
8 // for details. All rights reserved. Use of this source code is governed by a | |
9 // BSD-style license that can be found in the LICENSE file. | |
10 // Generated by %s. | |
11 | |
12 ''' | |
13 | |
14 EXT = '.g.dart' | |
15 | |
16 class CodeWriter: | |
17 '''Helper class for generating source code. The main purpose right now | |
18 is to keep track of indentation for pretty printing. In the future | |
19 much more could be added here - but I'd rather wait until we can write | |
20 this directly in Dart. | |
21 ''' | |
22 def __init__(self, genfile): | |
23 self._text = [] | |
24 self._indent = 0 | |
25 self._genfile = genfile | |
26 | |
27 def enterBlock(self, text=None): | |
28 if text is not None: self.writeln(text) | |
29 self._indent += 1 | |
30 | |
31 def exitBlock(self, text=None): | |
32 assert self._indent > 0 | |
33 self._indent -= 1 | |
34 if text is not None: self.writeln(text) | |
35 | |
36 def writeIndent(self): | |
37 self._text.append(' ' * self._indent) | |
38 | |
39 def writeln(self, s=None, *args): | |
40 if not s: | |
41 self._text.append('\n') | |
42 return | |
43 | |
44 self.writeIndent() | |
45 if args: | |
46 self._text.append(s % args) | |
47 else: | |
48 self._text.append(s) | |
49 self._text.append('\n') | |
50 | |
51 def __str__(self): | |
52 return ''.join(self._text) | |
53 | |
54 def writeToFile(self, filename): | |
55 with open(filename + EXT, 'w') as f: | |
56 f.write(HEADER % self._genfile) | |
57 for line in self._text: | |
58 f.write(line) | |
OLD | NEW |