OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Code shared by the various language-specific code generators.""" | 5 """Code shared by the various language-specific code generators.""" |
6 | 6 |
7 import os | 7 import os |
8 import mojom | 8 import mojom |
9 import mojom_pack | 9 import mojom_pack |
10 import re | 10 import re |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 values. Arrays are tokens that look like ('ARRAY', element0, element1...). | 46 values. Arrays are tokens that look like ('ARRAY', element0, element1...). |
47 See mojom_parser.py for their representation. | 47 See mojom_parser.py for their representation. |
48 """ | 48 """ |
49 if not isinstance(token, tuple): | 49 if not isinstance(token, tuple): |
50 raise Exception("Expected token type '%s'. Invalid token '%s'." % | 50 raise Exception("Expected token type '%s'. Invalid token '%s'." % |
51 (expected, token)) | 51 (expected, token)) |
52 if token[0] != expected: | 52 if token[0] != expected: |
53 raise Exception("Expected token type '%s'. Got '%s'." % | 53 raise Exception("Expected token type '%s'. Got '%s'." % |
54 (expected, token[0])) | 54 (expected, token[0])) |
55 | 55 |
| 56 def ExpressionMapper(expression, mapper): |
| 57 if isinstance(expression, tuple) and expression[0] == 'EXPRESSION': |
| 58 result = [] |
| 59 for each in expression[1]: |
| 60 result.extend(ExpressionMapper(each, mapper)) |
| 61 return result |
| 62 return [mapper(expression)] |
| 63 |
56 class Generator(object): | 64 class Generator(object): |
57 # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all | 65 # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all |
58 # files to stdout. | 66 # files to stdout. |
59 def __init__(self, module, header_dir, output_dir=None): | 67 def __init__(self, module, header_dir, output_dir=None): |
60 self.module = module | 68 self.module = module |
61 self.header_dir = header_dir | 69 self.header_dir = header_dir |
62 self.output_dir = output_dir | 70 self.output_dir = output_dir |
63 | 71 |
64 def GetStructsFromMethods(self): | 72 def GetStructsFromMethods(self): |
65 result = [] | 73 result = [] |
66 for interface in self.module.interfaces: | 74 for interface in self.module.interfaces: |
67 for method in interface.methods: | 75 for method in interface.methods: |
68 result.append(GetStructFromMethod(interface, method)) | 76 result.append(GetStructFromMethod(interface, method)) |
69 return map(partial(GetStructInfo, False), result) | 77 return map(partial(GetStructInfo, False), result) |
70 | 78 |
71 def GetStructs(self): | 79 def GetStructs(self): |
72 return map(partial(GetStructInfo, True), self.module.structs) | 80 return map(partial(GetStructInfo, True), self.module.structs) |
73 | 81 |
74 def Write(self, contents, filename): | 82 def Write(self, contents, filename): |
75 if self.output_dir is None: | 83 if self.output_dir is None: |
76 print contents | 84 print contents |
77 return | 85 return |
78 with open(os.path.join(self.output_dir, filename), "w+") as f: | 86 with open(os.path.join(self.output_dir, filename), "w+") as f: |
79 f.write(contents) | 87 f.write(contents) |
80 | 88 |
81 def GenerateFiles(self): | 89 def GenerateFiles(self): |
82 raise NotImplementedError("Subclasses must override/implement this method") | 90 raise NotImplementedError("Subclasses must override/implement this method") |
OLD | NEW |