From f76fd995f26c43b533f961272cdf39cafec63b94 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Thu, 9 Jun 2011 00:22:55 +0100 Subject: [PATCH] A few improvements to the C declaration parser. --- apigen/cdecl.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/apigen/cdecl.py b/apigen/cdecl.py index d591f77..0ef10d3 100755 --- a/apigen/cdecl.py +++ b/apigen/cdecl.py @@ -130,6 +130,8 @@ class Parser: self.match('(') args = [] + if self.tokens[0] == 'void' and self.tokens[1] == ')': + self.tokens.pop(0) while self.tokens[0] != ')': arg = self.parse_arg() args.append(arg) @@ -161,14 +163,62 @@ class Parser: self.match(']') return tag + int_tokens = ('unsigned', 'signed', 'int', 'long', 'short', 'char') + + type_table = { + 'float': 'Float', + 'double': 'Double', + 'int8_t': 'Int8', + 'uint8_t': 'UInt8', + 'int16_t': 'Int16', + 'uint16_t': 'UInt16', + 'int32_t': 'Int32', + 'uint32_t': 'UInt32', + 'int64_t' : 'Int64', + 'uint64_t': 'UInt64', + } + def parse_type(self): token = self.tokens.pop(0) if token == 'const': return 'Const(%s)' % self.parse_type() if token == 'void': type = 'Void' + elif token in self.int_tokens: + unsigned = False + signed = False + long = 0 + short = 0 + char = False + while token in self.int_tokens: + if token == 'unsigned': + unsigned = True + if token == 'signed': + signed = True + if token == 'long': + long += 1 + if token == 'short': + short += 1 + if token == 'char': + char = False + if self.tokens[0] in self.int_tokens: + token = self.tokens.pop(0) + else: + token = None + if char: + type = 'Char' + if signed: + type = 'S' + type + elif short: + type = 'Short' + elif long: + type = 'Long' * long + else: + type = 'Int' + if unsigned: + type = 'U' + type else: - type = token + type = self.type_table.get(token, token) while self.tokens[0] == '*': type = 'OpaquePointer(%s)' % type self.tokens.pop(0) -- 2.45.2