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