1#!/usr/bin/env python3 2# Copyright 2024 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Generates macros for encoding tokenizer argument types.""" 16 17import datetime 18import os 19 20FILE_HEADER = """\ 21// Copyright {year} The Pigweed Authors 22// 23// Licensed under the Apache License, Version 2.0 (the "License"); you may not 24// use this file except in compliance with the License. You may obtain a copy of 25// the License at 26// 27// https://www.apache.org/licenses/LICENSE-2.0 28// 29// Unless required by applicable law or agreed to in writing, software 30// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 31// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 32// License for the specific language governing permissions and limitations under 33// the License. 34 35// AUTOGENERATED - DO NOT EDIT 36// 37// This file was generated by {script}. 38// To make changes, update the script and run it to generate new files. 39#pragma once 40 41// clang-format off 42""" 43 44 45def _args(count: int) -> str: 46 return ','.join(f'a{i}' for i in range(0, count)) 47 48 49def generate_tokenized_enum_macro(max_supported_args: int) -> str: 50 """Generates macros to tokenize enums.""" 51 52 output = [ 53 FILE_HEADER.format( 54 script=os.path.basename(__file__), 55 year=datetime.date.today().year, 56 ) 57 ] 58 59 output.append(''.join('#define _PW_APL0(m,s,n)')) 60 61 for count in range(1, max_supported_args + 1): 62 args = [f'\n#define _PW_APL{count}(m,s,n,' f'{_args(count)})'] 63 enumerator = [f'm({i},n,a{i})s({i},n)' for i in range(0, count - 1)] 64 last_enumerator = [f'm({count - 1},n,a{count - 1})'] 65 66 output.append(''.join(args)) 67 output.append(''.join(enumerator)) 68 output.append(''.join(last_enumerator)) 69 70 output.append('\n// clang-format on\n') 71 72 return ''.join(output) 73 74 75def _main() -> None: 76 base_tokenized_enums = os.path.abspath( 77 os.path.join( 78 os.path.dirname(__file__), 79 '..', 80 'public', 81 'pw_preprocessor', 82 ) 83 ) 84 85 with open( 86 os.path.join(base_tokenized_enums, 'internal', 'apply_macros.h'), 87 'w', 88 encoding="utf-8", 89 ) as fd: 90 fd.write(generate_tokenized_enum_macro(128)) 91 92 print('Generated tokenized enum macro headers in', base_tokenized_enums) 93 94 95if __name__ == '__main__': 96 _main() 97