OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 # usage: rule_bison.py INPUT_FILE OUTPUT_DIR |
| 8 # INPUT_FILE is a path to either CSSGrammar.y or XPathGrammar.y. |
| 9 # OUTPUT_DIR is where the bison-generated .cpp and .h files should be placed. |
| 10 |
| 11 import errno |
| 12 import os |
| 13 import os.path |
| 14 import subprocess |
| 15 import sys |
| 16 |
| 17 assert len(sys.argv) == 3 |
| 18 |
| 19 input_file = sys.argv[1] |
| 20 output_dir = sys.argv[2] |
| 21 |
| 22 input_name = os.path.basename(input_file) |
| 23 assert input_name == 'CSSGrammar.y' or input_name == 'XPathGrammar.y' |
| 24 prefix = {'CSSGrammar.y': 'cssyy', 'XPathGrammar.y': 'xpathyy'}[input_name] |
| 25 |
| 26 (input_root, input_ext) = os.path.splitext(input_name) |
| 27 |
| 28 # The generated .h will be in a different location depending on the bison |
| 29 # version. |
| 30 output_h_tries = [ |
| 31 os.path.join(output_dir, input_root + '.cpp.h'), |
| 32 os.path.join(output_dir, input_root + '.hpp'), |
| 33 ] |
| 34 |
| 35 for output_h_try in output_h_tries: |
| 36 try: |
| 37 os.unlink(output_h_try) |
| 38 except OSError, e: |
| 39 if e.errno != errno.ENOENT: |
| 40 raise |
| 41 |
| 42 output_cpp = os.path.join(output_dir, input_root + '.cpp') |
| 43 |
| 44 return_code = subprocess.call(['bison', '-d', '-p', prefix, input_file, |
| 45 '-o', output_cpp]) |
| 46 assert return_code == 0 |
| 47 |
| 48 # Find the name that bison used for the generated header file. |
| 49 output_h_tmp = None |
| 50 for output_h_try in output_h_tries: |
| 51 try: |
| 52 os.stat(output_h_try) |
| 53 output_h_tmp = output_h_try |
| 54 break |
| 55 except OSError, e: |
| 56 if e.errno != errno.ENOENT: |
| 57 raise |
| 58 |
| 59 assert output_h_tmp != None |
| 60 |
| 61 # Read the header file in under the generated name and remove it. |
| 62 output_h_file = open(output_h_tmp) |
| 63 output_h_contents = output_h_file.read() |
| 64 output_h_file.close() |
| 65 os.unlink(output_h_tmp) |
| 66 |
| 67 # Rewrite the generated header with #include guards. |
| 68 output_h = os.path.join(output_dir, input_root + '.h') |
| 69 |
| 70 output_h_file = open(output_h, 'w') |
| 71 print >>output_h_file, '#ifndef %s_h' % input_root |
| 72 print >>output_h_file, '#define %s_h' % input_root |
| 73 print >>output_h_file, output_h_contents |
| 74 print >>output_h_file, '#endif' |
| 75 output_h_file.close() |
OLD | NEW |