def runScanner(data, scanner_class, lexicon=None): info = UStringIO(data) outfo = UStringIO() if lexicon is not None: scanner = scanner_class(lexicon, info) else: scanner = scanner_class(info) while True: value, text = scanner.read() if value is None: break elif value is IGNORE: pass else: outfo.write(value) return outfo.getvalue(), scanner
def removeHeader(source): lines = LineQueue() def LookingForHeader(line): m = re.match(r'/\*[^\n]*-- translated', line) if m: return InHeader else: lines.add(line) return LookingForHeader def InHeader(line): if line.startswith('*/'): return OutOfHeader else: return InHeader def OutOfHeader(line): if line.startswith('#include "f2c.h"'): pass else: lines.add(line) return OutOfHeader state = LookingForHeader for line in UStringIO(source): state = state(line) return lines.getValue()
def compute_content(self, layout): """trick to compute the formatting of children layout before actually writing it return an iterator on strings (one for each child element) """ # use cells ! def write(data): try: stream.write(data) except UnicodeEncodeError: stream.write(data.encode(self.encoding)) def writeln(data=''): try: stream.write(data+linesep) except UnicodeEncodeError: stream.write(data.encode(self.encoding)+linesep) self.write = write self.writeln = writeln self.__compute_funcs.append((write, writeln)) for child in layout.children: stream = UStringIO() child.accept(self) yield stream.getvalue() self.__compute_funcs.pop() try: self.write, self.writeln = self.__compute_funcs[-1] except IndexError: del self.write del self.writeln
def removeSubroutinePrototypes(source): expression = re.compile( r'/[*] Subroutine [*]/^\s*(?:(?:inline|static)\s+){0,2}(?!else|typedef|return)\w+\s+\*?\s*(\w+)\s*\([^0]+\)\s*;?' ) lines = LineQueue() for line in UStringIO(source): if not expression.match(line): lines.add(line) return lines.getValue()
def cleanComments(source): lines = LineQueue() comments = CommentQueue() def isCommentLine(line): return line.startswith('/*') and line.endswith('*/\n') blanks = LineQueue() def isBlank(line): return line.strip() == '' def SourceLines(line): if isCommentLine(line): comments.add(line) return HaveCommentLines else: lines.add(line) return SourceLines def HaveCommentLines(line): if isBlank(line): blanks.add('\n') return HaveBlankLines elif isCommentLine(line): comments.add(line) return HaveCommentLines else: comments.flushTo(lines) lines.add(line) return SourceLines def HaveBlankLines(line): if isBlank(line): blanks.add('\n') return HaveBlankLines elif isCommentLine(line): blanks.flushTo(comments) comments.add(line) return HaveCommentLines else: comments.flushTo(lines) blanks.flushTo(lines) lines.add(line) return SourceLines state = SourceLines for line in UStringIO(source): state = state(line) comments.flushTo(lines) return lines.getValue()
def removeBuiltinFunctions(source): lines = LineQueue() def LookingForBuiltinFunctions(line): if line.strip() == '/* Builtin functions */': return InBuiltInFunctions else: lines.add(line) return LookingForBuiltinFunctions def InBuiltInFunctions(line): if line.strip() == '': return LookingForBuiltinFunctions else: return InBuiltInFunctions state = LookingForBuiltinFunctions for line in UStringIO(source): state = state(line) return lines.getValue()