Home | History | Annotate | Download | only in torque
      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
      7 the source and header files directly in V8's src directory."""
      8 
      9 import subprocess
     10 import sys
     11 import os
     12 import ntpath
     13 import re
     14 
     15 cwd = os.getcwd()
     16 tools = ntpath.dirname(sys.argv[0]);
     17 grammar = tools + '/../../src/torque/Torque.g4'
     18 basename = ntpath.basename(grammar)
     19 dirname = ntpath.dirname(grammar)
     20 os.chdir(dirname)
     21 cargs = ['java', '-Xmx500M', 'org.antlr.v4.Tool', '-visitor', basename]
     22 result = subprocess.call(cargs)
     23 os.chdir(cwd)
     24 
     25 def 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 
     60 fix_file('TorqueBaseListener.h');
     61 fix_file('TorqueBaseListener.cpp');
     62 fix_file('TorqueBaseVisitor.h');
     63 fix_file('TorqueBaseVisitor.cpp');
     64 fix_file('TorqueLexer.h');
     65 fix_file('TorqueLexer.cpp');
     66 fix_file('TorqueParser.h');
     67 fix_file('TorqueParser.cpp');
     68 fix_file('TorqueListener.h');
     69 fix_file('TorqueListener.cpp');
     70 fix_file('TorqueVisitor.h');
     71 fix_file('TorqueVisitor.cpp');
     72