1#!/usr/bin/env python 2# Copyright 2014 the V8 project authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""This program either generates the parser files for Torque, generating 7the source and header files directly in V8's src directory.""" 8 9import subprocess 10import sys 11import os 12import ntpath 13import re 14 15cwd = os.getcwd() 16tools = ntpath.dirname(sys.argv[0]); 17grammar = tools + '/../../src/torque/Torque.g4' 18basename = ntpath.basename(grammar) 19dirname = ntpath.dirname(grammar) 20os.chdir(dirname) 21cargs = ['java', '-Xmx500M', 'org.antlr.v4.Tool', '-visitor', basename] 22result = subprocess.call(cargs) 23os.chdir(cwd) 24 25def fix_file(filename): 26 is_header = re.search(r'\.h', filename) <> None; 27 header_macro = filename.upper(); 28 header_macro = re.sub('\.', '_', header_macro); 29 header_macro = "V8_TORQUE_" + header_macro + '_'; 30 31 copyright = '// Copyright 2018 the V8 project authors. All rights reserved.\n' 32 copyright += '// Use of this source code is governed by a BSD-style license that can be\n' 33 copyright += '// found in the LICENSE file.\n' 34 file_path = tools + '/../../src/torque/' + filename; 35 temp_file_path = file_path + '.tmp' 36 output_file = open(temp_file_path, 'w') 37 output_file.write(copyright); 38 if is_header: 39 output_file.write('#ifndef ' + header_macro + '\n'); 40 output_file.write('#define ' + header_macro + '\n'); 41 42 with open(file_path) as f: 43 content = f.readlines() 44 for x in content: 45 x = re.sub(';;', ';', x) 46 x = re.sub('antlr4-runtime\.h', './antlr4-runtime.h', x) 47 x = re.sub(' TorqueParser.antlr4', ' explicit TorqueParser(antlr4', x) 48 x = re.sub(' TorqueLexer.antlr4', ' explicit TorqueLexer(antlr4', x) 49 if not re.search('= 0', x): 50 x = re.sub('virtual', '', x) 51 output_file.write(x) 52 53 if is_header: 54 output_file.write('#endif // ' + header_macro + '\n'); 55 output_file.close(); 56 57 subprocess.call(['rm', file_path]) 58 subprocess.call(['mv', temp_file_path, file_path]) 59 60fix_file('TorqueBaseListener.h'); 61fix_file('TorqueBaseListener.cpp'); 62fix_file('TorqueBaseVisitor.h'); 63fix_file('TorqueBaseVisitor.cpp'); 64fix_file('TorqueLexer.h'); 65fix_file('TorqueLexer.cpp'); 66fix_file('TorqueParser.h'); 67fix_file('TorqueParser.cpp'); 68fix_file('TorqueListener.h'); 69fix_file('TorqueListener.cpp'); 70fix_file('TorqueVisitor.h'); 71fix_file('TorqueVisitor.cpp'); 72