1cdf0e10cSrcweir#!/usr/bin/env python 29f22d7c2SAndrew Rist# ************************************************************* 39f22d7c2SAndrew Rist# 49f22d7c2SAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 59f22d7c2SAndrew Rist# or more contributor license agreements. See the NOTICE file 69f22d7c2SAndrew Rist# distributed with this work for additional information 79f22d7c2SAndrew Rist# regarding copyright ownership. The ASF licenses this file 89f22d7c2SAndrew Rist# to you under the Apache License, Version 2.0 (the 99f22d7c2SAndrew Rist# "License"); you may not use this file except in compliance 109f22d7c2SAndrew Rist# with the License. You may obtain a copy of the License at 119f22d7c2SAndrew Rist# 129f22d7c2SAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 139f22d7c2SAndrew Rist# 149f22d7c2SAndrew Rist# Unless required by applicable law or agreed to in writing, 159f22d7c2SAndrew Rist# software distributed under the License is distributed on an 169f22d7c2SAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 179f22d7c2SAndrew Rist# KIND, either express or implied. See the License for the 189f22d7c2SAndrew Rist# specific language governing permissions and limitations 199f22d7c2SAndrew Rist# under the License. 209f22d7c2SAndrew Rist# 219f22d7c2SAndrew Rist# ************************************************************* 229f22d7c2SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweirimport srclexer, srcparser, globals 25cdf0e10cSrcweir 26cdf0e10cSrcweirclass TestCase: 27cdf0e10cSrcweir 28cdf0e10cSrcweir @staticmethod 29cdf0e10cSrcweir def run (tokens, defines): 30cdf0e10cSrcweir mcExpander = srcparser.MacroExpander(tokens, defines) 31cdf0e10cSrcweir mcExpander.debug = True 32cdf0e10cSrcweir mcExpander.expand() 33cdf0e10cSrcweir tokens = mcExpander.getTokens() 34*7d9fa7c3SPedro Giffuni print(tokens) 35cdf0e10cSrcweir 36cdf0e10cSrcweir @staticmethod 37cdf0e10cSrcweir def simpleNoArgs (): 38cdf0e10cSrcweir tokens = ['FUNC_FOO', '(', 'left', ',', 'right', ')'] 39cdf0e10cSrcweir defines = {} 40cdf0e10cSrcweir macro = globals.Macro('FUNC_FOO') 41cdf0e10cSrcweir macro.tokens = ['Here', 'comes', 'X', 'and', 'Y'] 42cdf0e10cSrcweir defines['FUNC_FOO'] = macro 43cdf0e10cSrcweir TestCase.run(tokens, defines) 44cdf0e10cSrcweir 45cdf0e10cSrcweir @staticmethod 46cdf0e10cSrcweir def simpleArgs (): 47cdf0e10cSrcweir tokens = ['FUNC_FOO', '(', 'left', ',', 'right', ')'] 48cdf0e10cSrcweir defines = {} 49cdf0e10cSrcweir macro = globals.Macro('FUNC_FOO') 50cdf0e10cSrcweir macro.tokens = ['Here', 'comes', 'X', 'and', 'Y'] 51cdf0e10cSrcweir macro.vars['X'] = 0 52cdf0e10cSrcweir macro.vars['Y'] = 1 53cdf0e10cSrcweir defines['FUNC_FOO'] = macro 54cdf0e10cSrcweir TestCase.run(tokens, defines) 55cdf0e10cSrcweir 56cdf0e10cSrcweir @staticmethod 57cdf0e10cSrcweir def multiTokenArgs (): 58cdf0e10cSrcweir tokens = ['FUNC_FOO', '(', 'left1', 'left2', 'left3', ',', 'right', ')'] 59cdf0e10cSrcweir defines = {} 60cdf0e10cSrcweir macro = globals.Macro('FUNC_FOO') 61cdf0e10cSrcweir macro.tokens = ['Here', 'comes', 'X', 'and', 'Y'] 62cdf0e10cSrcweir macro.vars['X'] = 0 63cdf0e10cSrcweir macro.vars['Y'] = 1 64cdf0e10cSrcweir defines['FUNC_FOO'] = macro 65cdf0e10cSrcweir TestCase.run(tokens, defines) 66cdf0e10cSrcweir 67cdf0e10cSrcweir @staticmethod 68cdf0e10cSrcweir def nestedTokenArgs (): 69cdf0e10cSrcweir tokens = ['FUNC_BAA', '(', 'left', ',', 'right', ')'] 70cdf0e10cSrcweir defines = {} 71cdf0e10cSrcweir macro = globals.Macro('FUNC_FOO') 72cdf0e10cSrcweir macro.tokens = ['Here', 'comes', 'X', 'and', 'Y'] 73cdf0e10cSrcweir macro.vars['X'] = 0 74cdf0e10cSrcweir macro.vars['Y'] = 1 75cdf0e10cSrcweir defines['FUNC_FOO'] = macro 76cdf0e10cSrcweir macro = globals.Macro('FUNC_BAA') 77cdf0e10cSrcweir macro.tokens = ['FUNC_FOO'] 78cdf0e10cSrcweir defines['FUNC_BAA'] = macro 79cdf0e10cSrcweir TestCase.run(tokens, defines) 80cdf0e10cSrcweir 81cdf0e10cSrcweirdef main (): 82*7d9fa7c3SPedro Giffuni print("simple expansion with no arguments") 83cdf0e10cSrcweir TestCase.simpleNoArgs() 84*7d9fa7c3SPedro Giffuni print("simple argument expansion") 85cdf0e10cSrcweir TestCase.simpleArgs() 86*7d9fa7c3SPedro Giffuni print("multi-token argument expansion") 87cdf0e10cSrcweir TestCase.multiTokenArgs() 88*7d9fa7c3SPedro Giffuni print("nested argument expansion") 89cdf0e10cSrcweir TestCase.nestedTokenArgs() 90cdf0e10cSrcweir 91cdf0e10cSrcweirif __name__ == '__main__': 92cdf0e10cSrcweir main() 93