Move 2.0 changes collected in branches/pending back to trunk for further
[scons.git] / test / YACC / live.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Test YACC and YACCFLAGS with a live yacc compiler.
29 """
30
31 import TestSCons
32
33 _exe = TestSCons._exe
34 _python_ = TestSCons._python_
35
36 test = TestSCons.TestSCons()
37
38 yacc = test.where_is('yacc') or test.where_is('bison')
39
40 if not yacc:
41     test.skip_test('No yacc or bison found; skipping test.\n')
42
43 test.write("wrapper.py",
44 """import os
45 import sys
46 open('%s', 'wb').write("wrapper.py\\n")
47 os.system(" ".join(sys.argv[1:]))
48 """ % test.workpath('wrapper.out').replace('\\', '\\\\'))
49
50 test.write('SConstruct', """
51 foo = Environment(YACCFLAGS='-d')
52 yacc = foo.Dictionary('YACC')
53 bar = Environment(YACC = r'%(_python_)s wrapper.py ' + yacc)
54 foo.Program(target = 'foo', source = 'foo.y')
55 bar.Program(target = 'bar', source = 'bar.y')
56 foo.Program(target = 'hello', source = ['hello.cpp'])
57 foo.CXXFile(target = 'file.cpp', source = ['file.yy'], YACCFLAGS='-d')
58 foo.CFile(target = 'not_foo', source = 'foo.y')
59 """ % locals())
60
61 yacc = r"""
62 %%{
63 #include <stdio.h>
64
65 main()
66 {
67     yyparse();
68 }
69
70 yyerror(s)
71 char *s;
72 {
73     fprintf(stderr, "%%s\n", s);
74     return 0;
75 }
76
77 yylex()
78 {
79     int c;
80
81     c = fgetc(stdin);
82     return (c == EOF) ? 0 : c;
83 }
84 %%}
85 %%%%
86 input:  letter newline { printf("%s\n"); };
87 letter:  'a' | 'b';
88 newline: '\n';
89 """
90
91 test.write("file.yy", """\
92 %token   GRAPH_T NODE_T EDGE_T DIGRAPH_T EDGEOP_T SUBGRAPH_T
93
94 %pure_parser
95
96 %%
97 graph:        GRAPH_T
98               ;
99
100 %%
101 """)
102
103 # Apparently, OS X now creates file.hpp like everybody else
104 # I have no idea when it changed; it was fixed in 10.4
105 #import sys
106 #if sys.platform[:6] == 'darwin':
107 #   file_hpp = 'file.cpp.h'
108 #else:
109 #   file_hpp = 'file.hpp'
110 file_hpp = 'file.hpp'
111
112 test.write("hello.cpp", """\
113 #include "%(file_hpp)s"
114
115 int main()
116 {
117 }
118 """ % locals())
119
120 test.write('foo.y', yacc % 'foo.y')
121
122 test.write('bar.y', yacc % 'bar.y')
123
124
125
126 test.run(arguments = 'foo' + _exe, stderr = None)
127
128 test.up_to_date(arguments = 'foo' + _exe)
129
130 test.must_not_exist(test.workpath('wrapper.out'))
131
132 test.run(program = test.workpath('foo'), stdin = "a\n", stdout = "foo.y\n")
133
134 test.must_exist(test.workpath('foo.h'))
135
136 test.run(arguments = '-c .')
137
138 test.must_not_exist(test.workpath('foo.h'))
139
140
141
142 test.run(arguments = 'not_foo.c')
143
144 test.up_to_date(arguments = 'not_foo.c')
145
146 test.must_not_exist(test.workpath('foo.h'))
147 test.must_exist(test.workpath('not_foo.h'))
148
149 test.run(arguments = '-c .')
150
151 test.must_not_exist(test.workpath('not_foo.h'))
152
153
154
155 test.run(arguments = 'bar' + _exe)
156
157 test.up_to_date(arguments = 'bar' + _exe)
158
159 test.must_match(test.workpath('wrapper.out'), "wrapper.py\n")
160
161 test.run(program = test.workpath('bar'), stdin = "b\n", stdout = "bar.y\n")
162
163
164
165 test.run(arguments = '.')
166
167 test.up_to_date(arguments = '.')
168
169
170
171 test.pass_test()
172
173 # Local Variables:
174 # tab-width:4
175 # indent-tabs-mode:nil
176 # End:
177 # vim: set expandtab tabstop=4 shiftwidth=4: