|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright JS Foundation and other contributors, http://js.foundation |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +LICENSE = """/* Copyright JS Foundation and other contributors, http://js.foundation |
| 18 | + * |
| 19 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 | + * you may not use this file except in compliance with the License. |
| 21 | + * You may obtain a copy of the License at |
| 22 | + * |
| 23 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | + * |
| 25 | + * Unless required by applicable law or agreed to in writing, software |
| 26 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 27 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | + * See the License for the specific language governing permissions and |
| 29 | + * limitations under the License. |
| 30 | + */""" |
| 31 | + |
| 32 | + |
| 33 | +class Source(object): |
| 34 | + def __init__(self, filepath): |
| 35 | + self.__filepath = filepath |
| 36 | + self.__header = [LICENSE, ""] |
| 37 | + self.__data = [] |
| 38 | + |
| 39 | + def complete_header(self, completion): |
| 40 | + self.__header.append(completion) |
| 41 | + self.__header.append("") # for an extra empty line |
| 42 | + |
| 43 | + |
| 44 | + def add_table(self, table, table_name, table_type, table_descr): |
| 45 | + self.__data.append(table_descr) |
| 46 | + self.__data.append("static const %s jerry_%s[] JERRY_CONST_DATA =" % (table_type, table_name)) |
| 47 | + self.__data.append("{") |
| 48 | + self.__data.append(format_code(table, 1)) |
| 49 | + self.__data.append("};") |
| 50 | + self.__data.append("") # for an extra empty line |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + def generate(self): |
| 55 | + with open(self.__filepath, 'w') as genereted_source: |
| 56 | + genereted_source.write("\n".join(self.__header)) |
| 57 | + genereted_source.write("\n".join(self.__data)) |
| 58 | + |
| 59 | + |
| 60 | +def regroup(list_to_group, num): |
| 61 | + return [list_to_group[i:i+num] for i in range(0, len(list_to_group), num)] |
| 62 | + |
| 63 | + |
| 64 | +def hex_format(char): |
| 65 | + if isinstance(char, str): |
| 66 | + char = ord(char) |
| 67 | + |
| 68 | + return "0x{:04x}".format(char) |
| 69 | + |
| 70 | + |
| 71 | +def format_code(code, indent): |
| 72 | + lines = [] |
| 73 | + # convert all characters to hex format |
| 74 | + converted_code = [hex_format(char) for char in code] |
| 75 | + # 10 hex number per line |
| 76 | + for line in regroup(", ".join(converted_code), 10 * 8): |
| 77 | + lines.append((' ' * indent) + line.strip()) |
| 78 | + return "\n".join(lines) |
0 commit comments