1cdf0e10cSrcweir#!/usr/bin/env python
2*9f22d7c2SAndrew Rist# *************************************************************
3*9f22d7c2SAndrew Rist#
4*9f22d7c2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
5*9f22d7c2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
6*9f22d7c2SAndrew Rist#  distributed with this work for additional information
7*9f22d7c2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
8*9f22d7c2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
9*9f22d7c2SAndrew Rist#  "License"); you may not use this file except in compliance
10*9f22d7c2SAndrew Rist#  with the License.  You may obtain a copy of the License at
11*9f22d7c2SAndrew Rist#
12*9f22d7c2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
13*9f22d7c2SAndrew Rist#
14*9f22d7c2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
15*9f22d7c2SAndrew Rist#  software distributed under the License is distributed on an
16*9f22d7c2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17*9f22d7c2SAndrew Rist#  KIND, either express or implied.  See the License for the
18*9f22d7c2SAndrew Rist#  specific language governing permissions and limitations
19*9f22d7c2SAndrew Rist#  under the License.
20*9f22d7c2SAndrew Rist#
21*9f22d7c2SAndrew Rist# *************************************************************
22*9f22d7c2SAndrew 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()
34cdf0e10cSrcweir        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 ():
82cdf0e10cSrcweir    print "simple expansion with no arguments"
83cdf0e10cSrcweir    TestCase.simpleNoArgs()
84cdf0e10cSrcweir    print "simple argument expansion"
85cdf0e10cSrcweir    TestCase.simpleArgs()
86cdf0e10cSrcweir    print "multi-token argument expansion"
87cdf0e10cSrcweir    TestCase.multiTokenArgs()
88cdf0e10cSrcweir    print "nested argument expansion"
89cdf0e10cSrcweir    TestCase.nestedTokenArgs()
90cdf0e10cSrcweir
91cdf0e10cSrcweirif __name__ ==  '__main__':
92cdf0e10cSrcweir    main()
93