diff --git a/ctypes_generation/com_parser.py b/ctypes_generation/com_parser.py index c06d3b9..edac83c 100644 --- a/ctypes_generation/com_parser.py +++ b/ctypes_generation/com_parser.py @@ -1,4 +1,3 @@ -import StringIO from collections import namedtuple import dummy_wintypes @@ -7,54 +6,6 @@ from winstruct import WinStruct, WinUnion, WinStructType, Ptr, WinEnum from simpleparser import * -def initial_processing( data): - # https://gcc.gnu.org/onlinedocs/cpp/Initial-processing.html#Initial-processing - # Step 1 -> use correct end of line + add last \n if not existing - data = data.replace("\r\n", "\n") - if not data.endswith("\n"): - data = data + "\n" - # Step 2: Trigraph : fuck it - pass - # Step 3: Line merge ! - data = data.replace("\\\n", "") - # Step 4 Remove comments: - - ins = StringIO.StringIO(data) - outs = StringIO.StringIO() - - in_str = False - res = [] - while ins.tell() != len(data): - c = ins.read(1) - if ins.tell() == len(data): - outs.write(c) - break - if not in_str and c == "/": - nc = ins.read(1) - if nc == "/": - while c != "\n": - c = ins.read(1) - outs.write(c) - continue - elif nc == "*": - while c != "*" or nc != "/": - c = nc - nc = ins.read(1) - if not nc: - raise ValueError("Unmatched */") - outs.write(" ") - continue - else: - outs.write(c) - ins.seek(ins.tell() - 1) - continue - # TODO: escape in str - elif c == '"': - in_str = not in_str - outs.write(c) - outs.seek(0) - return outs.read() - class WinComParser(Parser): PARAM_INFO = ["__RPC__deref_out", "__RPC__in", "__RPC__deref_out_opt", "__RPC__out", "__RPC__in_opt", "__RPC__deref_opt_inout_opt", "__in", "__out", "__out_opt", "__in_opt", "__inout", @@ -64,7 +15,7 @@ class WinComParser(Parser): "__in_ecount", "__out_bcount_opt", "__out_bcount", "__in_bcount", "__in_bcount_opt"] def __init__(self, data): - data = initial_processing(data) + # data = self.initial_processing(data) #print(data) super(WinComParser, self).__init__(data) diff --git a/ctypes_generation/simpleparser.py b/ctypes_generation/simpleparser.py index 276fb94..cb19740 100644 --- a/ctypes_generation/simpleparser.py +++ b/ctypes_generation/simpleparser.py @@ -1,4 +1,5 @@ import collections +import StringIO TupleToken = collections.namedtuple('Token', ['value']) @@ -114,7 +115,7 @@ class ParsingError(Exception): class Parser(object): def __init__(self, data): - self.lexer = iter(Lexer(data)) + self.lexer = iter(Lexer(self.initial_processing(data))) self.peek_token = None def assert_keyword(self, expected_keyword, n=None): @@ -163,6 +164,54 @@ class Parser(object): def parse(self): raise NotImplementedError("Parser.parse()") + def initial_processing(self, data): + # https://gcc.gnu.org/onlinedocs/cpp/Initial-processing.html#Initial-processing + # Step 1 -> use correct end of line + add last \n if not existing + data = data.replace("\r\n", "\n") + if not data.endswith("\n"): + data = data + "\n" + # Step 2: Trigraph : fuck it + pass + # Step 3: Line merge ! + data = data.replace("\\\n", "") + # Step 4 Remove comments: + + ins = StringIO.StringIO(data) + outs = StringIO.StringIO() + + in_str = False + res = [] + while ins.tell() != len(data): + c = ins.read(1) + if ins.tell() == len(data): + outs.write(c) + break + if not in_str and c == "/": + nc = ins.read(1) + if nc == "/": + while c != "\n": + c = ins.read(1) + outs.write(c) + continue + elif nc == "*": + while c != "*" or nc != "/": + c = nc + nc = ins.read(1) + if not nc: + raise ValueError("Unmatched */") + outs.write(" ") + continue + else: + outs.write(c) + ins.seek(ins.tell() - 1) + continue + # TODO: escape in str + elif c == '"': + in_str = not in_str + outs.write(c) + outs.seek(0) + return outs.read() + #KNOWN_TYPE = ["BYTE", "USHORT", "DWORD", "PVOID", "ULONG", "HANDLE", "PWSTR"] # diff --git a/ctypes_generation/struct_parser.py b/ctypes_generation/struct_parser.py index 3fffd09..3c2ac26 100644 --- a/ctypes_generation/struct_parser.py +++ b/ctypes_generation/struct_parser.py @@ -4,7 +4,6 @@ from winstruct import WinStruct, WinUnion, WinStructType, Ptr, WinEnum from simpleparser import * class WinStructParser(Parser): - def __init__(self, *args, **kwargs): super(WinStructParser, self).__init__(*args, **kwargs) self.pack = None