diff options
author | Ben Smith <binjimin@gmail.com> | 2018-01-10 16:32:10 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-10 16:32:10 -0800 |
commit | afc3f3a84d138b64e5d259ed61c70c51d4d0abe7 (patch) | |
tree | 4cf605b53e5a064a8eee8d435531c752fecb66f0 /src/wasm2c_tmpl.py | |
parent | ed1e3977f936ac4056a28afd0b5136ba054762b2 (diff) | |
download | wabt-afc3f3a84d138b64e5d259ed61c70c51d4d0abe7.tar.gz wabt-afc3f3a84d138b64e5d259ed61c70c51d4d0abe7.tar.bz2 wabt-afc3f3a84d138b64e5d259ed61c70c51d4d0abe7.zip |
Use templates for generating wasm2c source (#712)
This uses a very simple template syntax, without any advanced features:
```
%%top
...
%%bottom
...
```
This template will generate output with two C strings:
```
const char SECTION_NAME(top)[] = ...
...
const char SECTION_NAME(bottom)[] = ...
...
```
To update the generated files, run `make update-wasm2c`.
Diffstat (limited to 'src/wasm2c_tmpl.py')
-rwxr-xr-x | src/wasm2c_tmpl.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/wasm2c_tmpl.py b/src/wasm2c_tmpl.py new file mode 100755 index 00000000..d43c2065 --- /dev/null +++ b/src/wasm2c_tmpl.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +# +# Copyright 2018 WebAssembly Community Group participants +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import print_function +import argparse +try: + from cStringIO import StringIO +except ImportError: + from io import StringIO +import os +import sys + +def EscapeCString(s): + out = '' + for b in bytearray(s.encode('utf-8')): + if b in (34, 92): + # " or \ + out += '\\' + chr(b) + elif b == 10: + # newline + out += '\\n' + elif 32 <= b <= 127: + # printable char + out += chr(b) + else: + # non-printable; write as \xab + out += '\\x%02x' % b + + return out + + +def main(args): + arg_parser = argparse.ArgumentParser() + arg_parser.add_argument('-o', '--output', metavar='PATH', + help='output file.') + arg_parser.add_argument('file', help='input file.') + options = arg_parser.parse_args(args) + + section_name = None + output = StringIO() + + output.write('/* Generated from \'%s\' by wasm2c_tmpl.py, do not edit! */\n' % + os.path.basename(options.file)) + + with open(options.file) as f: + for line in f.readlines(): + if line.startswith('%%'): + if section_name is not None: + output.write(';\n\n'); + section_name = line[2:-1] + output.write('const char SECTION_NAME(%s)[] =\n' % section_name) + else: + output.write('"%s"\n' % EscapeCString(line)) + + output.write(';\n'); + if options.output: + with open(options.output, 'w') as outf: + outf.write(output.getvalue()) + else: + sys.stdout.write(output.getvalue()) + + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) |