| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2013 the V8 project authors. All rights reserved. |
| 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are |
| 5 # met: |
| 6 # |
| 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following |
| 11 # disclaimer in the documentation and/or other materials provided |
| 12 # with the distribution. |
| 13 # * Neither the name of Google Inc. nor the names of its |
| 14 # contributors may be used to endorse or promote products derived |
| 15 # from this software without specific prior written permission. |
| 16 # |
| 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
| 29 import codecs |
| 30 import optparse |
| 31 import pystache |
| 32 import re |
| 33 import sys |
| 34 |
| 35 # List of all available keywords. |
| 36 BASE = "base" |
| 37 BITFIELD = "bitfield" |
| 38 COMMENT = "comment" |
| 39 FIELDS = "fields" |
| 40 NAME = "name" |
| 41 TYPE = "type" |
| 42 TYPES = "types" |
| 43 KEYWORDS = {} |
| 44 for key in [BASE, BITFIELD, COMMENT, FIELDS, NAME, TYPE, TYPES]: |
| 45 KEYWORDS[key] = key |
| 46 |
| 47 # List of all available primitive types. |
| 48 T_BOOL = "tBool" |
| 49 T_SMI = "tSmi" |
| 50 for key in [T_BOOL, T_SMI]: |
| 51 KEYWORDS[key] = key |
| 52 |
| 53 # Converts "camel_case" to "CamelCase". |
| 54 def ConvertLowerToPascal(name): |
| 55 return ''.join(x.capitalize() or '_' for x in name.split('_')) |
| 56 |
| 57 # Converts "CamelCase" to "CAMEL_CASE". |
| 58 def ConvertPascalToUpper(name): |
| 59 return '_'.join(x.upper() for x in re.findall('[A-Z][a-z]*', name)) |
| 60 |
| 61 def ParseLayout(layout): |
| 62 dictionary = eval(layout, KEYWORDS) |
| 63 for t in dictionary[TYPES]: |
| 64 t[COMMENT] = t.get(COMMENT, '%s is a %s.' % (t[NAME], t[BASE])) |
| 65 t['type_name_upper'] = ConvertPascalToUpper(t[NAME]) |
| 66 for f in t[FIELDS]: |
| 67 f[TYPE] = f.get(TYPE, 'Object') |
| 68 f['field_name_lower'] = f[NAME] |
| 69 f['field_name_pascal'] = ConvertLowerToPascal(f[NAME]) |
| 70 f['field_comment'] = f.get(COMMENT) |
| 71 |
| 72 # Pre-render multi-line comments. |
| 73 comment = t[COMMENT] |
| 74 if type(comment) == list: |
| 75 comment = '\n'.join(map(lambda l : '// ' + l, comment)) |
| 76 else: |
| 77 comment = '// %s' % comment |
| 78 t[COMMENT] = comment |
| 79 |
| 80 # Determine primitive fileds. |
| 81 for f in t[FIELDS]: |
| 82 if f[TYPE] == T_SMI: |
| 83 f['is_primitive'] = True |
| 84 f['is_smi'] = True |
| 85 |
| 86 # Determine bitfields. |
| 87 bitfields = [] |
| 88 for f in t[FIELDS]: |
| 89 if not f.has_key(BITFIELD): continue |
| 90 bitfield_index = 0 |
| 91 for bf in f[BITFIELD]: |
| 92 bf['bitfield_name_pascal'] = ConvertLowerToPascal(bf[NAME]) |
| 93 bf['bitfield_start'] = bitfield_index |
| 94 bitfield_index += 1 |
| 95 bitfields.append(f) |
| 96 if bitfields: t['bitfields'] = bitfields |
| 97 |
| 98 # Compute field offsets. |
| 99 prev_f = None |
| 100 for f in t[FIELDS]: |
| 101 if prev_f == None: |
| 102 field_offset = '%s::kHeaderSize' % t[BASE] |
| 103 else: |
| 104 field_offset = 'k%sOffset + %s' % (prev_f['field_name_pascal'], prev_f['
field_size']) |
| 105 f['field_size'] = 'kPointerSize' |
| 106 f['field_offset'] = field_offset |
| 107 prev_f = f; |
| 108 |
| 109 # Compute type sizes. |
| 110 t['type_size'] = 'k%sOffset + %s' % (prev_f['field_name_pascal'], prev_f['fi
eld_size']) |
| 111 |
| 112 return dictionary |
| 113 |
| 114 def RenderOutput(template, dictionary): |
| 115 renderer = pystache.Renderer() |
| 116 parsed = pystache.parse(template) |
| 117 output = renderer.render(parsed, dictionary) |
| 118 return output |
| 119 |
| 120 def Main(): |
| 121 usage = 'Usage: %prog [options] template layout-file' |
| 122 parser = optparse.OptionParser(usage) |
| 123 (options, args) = parser.parse_args() |
| 124 if len(args) != 2: |
| 125 parser.print_help() |
| 126 return 1 |
| 127 |
| 128 with codecs.open(args[0], encoding='utf-8') as f: |
| 129 template = f.read() |
| 130 with codecs.open(args[1], encoding='utf-8') as f: |
| 131 layout = f.read() |
| 132 dictionary = ParseLayout(layout) |
| 133 output = RenderOutput(template, dictionary) |
| 134 sys.stdout.write(output) |
| 135 |
| 136 if __name__ == "__main__": |
| 137 sys.exit(Main()) |
| OLD | NEW |