def lambda_handler(event, context): cpp = Preprocessor() cpp.add_path(os.getcwd() + "/lvmxlib") tmpf = io.StringIO("") with open("/tmp/main.c", mode='w') as f: f.write(event['body']) with open("/tmp/main.c", mode="r") as f: cpp.parse(f) cpp.write(tmpf) g.init("/tmp/main.c", tmpf.getvalue()) try: dumps = compile(g.source) except Exception as e: return {'statusCode': 400, 'body': g.r.report()} if dumps is None: return {'statusCode': 400, 'body': g.r.report()} bytecode = f".data {len(dumps['data'])}" + '\n' for elem in dumps['data']: bytecode += value2hex(elem) + '\n' bytecode += f".code {len(dumps['code'])}" + '\n' for elem in dumps['code']: bytecode += elem.serialize() + '\n' if g.r.hasError(): return {'statusCode': 400, 'body': g.r.report()} else: return {'statusCode': 200, 'body': bytecode}
def preprocess(source): preprocessor = Preprocessor() preprocessor.line_directive = None preprocessor.parse(source) stream = StringIO() preprocessor.write(stream) return stream.getvalue()
def preprocess_sources(self, sources): cpp = Preprocessor() cpp.parse(StringIO(sources), source="proto.hpp") sstr = StringIO() cpp.write(sstr) src = sstr.getvalue() sstr.close() return src
def runTest(self): from pcpp import Preprocessor output = r''' a ''' p = Preprocessor() p.define('BAR FOO') p.parse(r'''#define FOO 1 #if FOO == BAR a #endif ''') oh = StringIO() p.write(oh) if oh.getvalue() != output: print("Should be:\n" + output, file = sys.stderr) print("\n\nWas:\n" + oh.getvalue(), file = sys.stderr) self.assertEqual(p.return_code, 0) self.assertEqual(oh.getvalue(), output)
def runTest(self): from pcpp import Preprocessor import os, sys start = clock() p = Preprocessor() p.parse(self.input) oh = StringIO() p.write(oh) end = clock() print("Preprocessed test in", end - start, "seconds") if oh.getvalue() != self.output: print("Should be:\n" + self.output, file=sys.stderr) print("\n\nWas:\n" + oh.getvalue(), file=sys.stderr) self.assertEqual(p.return_code, 0) self.assertEqual(oh.getvalue(), self.output)
def buildShader(dirName): outputFile = None inputFile = None for root, dirs, files in os.walk(dirName): for file in files: if needBuildShader(file): outputFile = file outputFile = outputFile.replace(".d.hlsl", ".hlsl") outputFile = outputFile.replace(".d.glsl", ".glsl") print("%s <- %s" % (outputFile, file)) outputFile = root + "/" + outputFile inputFile = root + "/" + file p = Preprocessor() p.compress = 2 p.line_directive = None with open(inputFile, 'rt') as finput: p.parse(finput.read(), inputFile) with open(outputFile, 'w') as foutput: p.write(foutput)
def runTest(self): from pcpp import Preprocessor import os, sys p = Preprocessor() path = 'tests/issue0017/issue0017.c' with open(path, 'rt') as ih: p.parse(ih.read(), path) with open('tests/issue0017.i', 'w') as oh: p.write(oh) with open('tests/issue0017.i', 'r') as ih: was = ih.read() with open('tests/issue0017-ref.i', 'r') as ih: shouldbe = ih.read() if was != shouldbe: print("Should be:\n" + shouldbe, file=sys.stderr) print("\n\nWas:\n" + was, file=sys.stderr) self.assertEqual(p.return_code, 0) self.assertEqual(was, shouldbe)
def buildShader(shaderXml, root): for shaderInfo in shaderXml: fs = shaderInfo.get('fs') fs_source = shaderInfo.get('fs_source') vs = shaderInfo.get('vs') vs_source = shaderInfo.get('vs_source') define = shaderInfo.get('define') listDefine = None if define != None: listDefine = define.split(',') else: define = "_" if fs_source != None and fs != None: outputFile = root + "/" + fs inputFile = root + "/" + fs_source print(" + %s <- %s : %s" % (fs, fs_source, define)) p = Preprocessor() p.compress = 2 p.line_directive = None if listDefine != None: for d in listDefine: p.define(d.strip()) with open(inputFile, 'rt') as finput: p.parse(finput.read(), inputFile) with open(outputFile, 'w') as foutput: foutput.writelines( "// File Generated by Assets/BuildShader.py - source: [" + os.path.basename(fs_source) + " : " + define + "]\n") p.write(foutput) if vs_source != None and vs != None: outputFile = root + "/" + vs inputFile = root + "/" + vs_source print(" + %s <- %s : %s" % (vs, vs_source, define)) p = Preprocessor() p.compress = 2 p.line_directive = None if listDefine != None: for d in listDefine: p.define(d.strip()) with open(inputFile, 'rt') as finput: p.parse(finput.read(), inputFile) with open(outputFile, 'w') as foutput: foutput.writelines( "// File Generated by Assets/BuildShader.py - source: [" + os.path.basename(vs_source) + " : " + define + "]\n") p.write(foutput) return
def runTest(self): from pcpp import Preprocessor import os start = clock() p = Preprocessor() p.compress = 1 p.line_directive = '#' p.define('__STDC__ 1') p.define('__STDC_VERSION__ 199901L') p.define('__DATE__ "Jan 13 2020"') p.define('__TIME__ "10:47:38"') p.define('NO_SYSTEM_HEADERS') path = 'tests/test-c/n_std.c' with open(path, 'rt') as ih: p.parse(ih.read(), path) with open('tests/n_std.i', 'w') as oh: p.write(oh) end = clock() print("Preprocessed", path, "in", end - start, "seconds") self.assertEqual(p.return_code, 0) with open('tests/n_std.i', 'rt') as ih: written = ih.readlines() with open('tests/n_std-pcpp.i', 'rt') as ih: reference = ih.readlines() if written != reference: print("pcpp is not emitting its reference output! Differences:") for line in difflib.unified_diff(reference, written, fromfile='n_std-pcpp.i', tofile='n_std.i'): print(line, end='') self.assertTrue(False)
def runTest(self): from pcpp import Preprocessor import os start = clock() p = Preprocessor() p.compress = 1 p.define('__STDC__ 1') p.define('__STDC_VERSION__ 199901L') p.define('NO_SYSTEM_HEADERS') path = 'tests/test-c/n_std.c' with open(path, 'rt') as ih: p.parse(ih.read(), path) with open('tests/n_std.i', 'w') as oh: p.write(oh) end = clock() print("Preprocessed", path, "in", end-start, "seconds") self.assertEqual(p.return_code, 0)
return body if __name__ == '__main__': argparser = ArgumentParser() argparser.add_argument('filename', type=str, help='target source file') argparser.add_argument('-j', '--json', action='store_true', help='output as json') args = argparser.parse_args() cpp = Preprocessor() cpp.add_path(os.getcwd() + "/lvmxlib") tmpf = io.StringIO("") with open(args.filename, mode="r") as f: cpp.parse(f) cpp.write(tmpf) g.init(args.filename, tmpf.getvalue()) try: dumps = compile(g.source) except Exception as e: g.r.report() raise