6 # ------------------ Create typecodes from typenames ---------
8 _upperstr_pat1 = re.compile(r'([^A-Z])([A-Z])')
9 _upperstr_pat2 = re.compile(r'([A-Z][A-Z])([A-Z][0-9a-z])')
10 _upperstr_pat3 = re.compile(r'^([A-Z])([A-Z])')
12 def to_upper_str(name):
13 """Converts a typename to the equivalent upercase and underscores
14 name. This is used to form the type conversion macros and enum/flag
16 name = _upperstr_pat1.sub(r'\1_\2', name)
17 name = _upperstr_pat2.sub(r'\1_\2', name)
18 name = _upperstr_pat3.sub(r'\1_\2', name, count=1)
19 return string.upper(name)
21 def typecode(typename):
22 """create a typecode (eg. GTK_TYPE_WIDGET) from a typename"""
23 return string.replace(to_upper_str(typename), '_', '_TYPE_', 1)
35 def convert(infp=sys.stdin, outfp=sys.stdout):
39 line = infp.readline()
41 if line[:8] == '(object ':
44 outfp.write('(define-object ' + line[8:])
45 elif line[:11] == '(interface ':
46 state = STATE_INTERFACE
48 outfp.write('(define-interface ' + line[11:])
49 elif line[:7] == '(boxed ':
52 outfp.write('(define-boxed ' + line[7:])
53 elif line[:6] == '(enum ':
56 outfp.write('(define-enum ' + line[6:])
57 elif line[:7] == '(flags ':
60 outfp.write('(define-flags ' + line[7:])
61 elif line[:8] == '(method ':
64 outfp.write('(define-method ' + line[8:])
65 elif line[:10] == '(function ':
66 state = STATE_FUNCTION
68 outfp.write('(define-function ' + line[10:])
69 elif line[:13] == ' (in-module ':
70 outfp.write(re.sub(r'^(\s+\(in-module\s+)(\w+)(.*)$',
72 elif line[:10] == ' (parent ':
73 outfp.write(re.sub(r'^(\s+\(parent\s+)(\w+)(\s+\((\w+)\))?(.*)$',
75 elif line[:14] == ' (implements ':
76 outfp.write(re.sub(r'^(\s+\(implements\s+)([^\s]+)(\s*\))$',
78 elif line[:13] == ' (of-object ':
79 outfp.write(re.sub(r'^(\s+\(of-object\s+)(\w+)(\s+\((\w+)\))?(.*)$',
81 elif line[:10] == ' (c-name ':
82 outfp.write(re.sub(r'^(\s+\(c-name\s+)([^\s]+)(\s*\))$',
84 if state in (STATE_OBJECT, STATE_INTERFACE, STATE_BOXED,
85 STATE_ENUM, STATE_FLAGS):
86 c_name = re.match(r'^\s+\(c-name\s+([^\s]+)\s*\)$',
88 outfp.write(' (gtype-id "%s")\n' % typecode(c_name))
89 elif line[:15] == ' (return-type ':
90 outfp.write(re.sub(r'^(\s+\(return-type\s+)([^\s]+)(\s*\))$',
92 elif line[:13] == ' (copy-func ':
93 outfp.write(re.sub(r'^(\s+\(copy-func\s+)(\w+)(.*)$',
95 elif line[:16] == ' (release-func ':
96 outfp.write(re.sub(r'^(\s+\(release-func\s+)(\w+)(.*)$',
98 elif line[:9] == ' (field ':
100 outfp.write(' (fields\n')
102 outfp.write(re.sub(r'^\s+\(field\s+\(type-and-name\s+([^\s]+)\s+([^\s]+)\s*\)\s*\)$',
103 ' \'("\\1" "\\2")', line))
104 elif line[:9] == ' (value ':
106 outfp.write(' (values\n')
108 outfp.write(re.sub(r'^\s+\(value\s+\(name\s+([^\s]+)\)\s+\(c-name\s+([^\s]+)\s*\)\s*\)$',
109 ' \'("\\1" "\\2")', line))
110 elif line[:13] == ' (parameter ':
112 outfp.write(' (parameters\n')
114 outfp.write(re.sub(r'^\s+\(parameter\s+\(type-and-name\s+([^\s]+)\s+([^\s]+)\s*\)(\s*.*)\)$',
115 ' \'("\\1" "\\2"\\3)', line))
116 elif line[:11] == ' (varargs ':
120 outfp.write(' (varargs #t)\n')
129 line = infp.readline()
131 if __name__ == '__main__':