def test_parse_examples(self, input_path): parser = FortranParser() with _TIME.measure('parse.{}'.format(input_path.name.replace( '.', '_'))) as timer: fortran_ast = parser.parse('', input_path) basic_check_fortran_ast(self, input_path, fortran_ast) _LOG.info('parsed "%s" in %fs', input_path, timer.elapsed)
def test_generalize(self): parser = FortranParser() generalizer = FortranAstGeneralizer() for input_path in EXAMPLES_F77_FILES + EXAMPLES_F95_FILES: with self.subTest(input_path=input_path): tree = generalizer.generalize(parser.parse('', input_path)) basic_check_python_ast(self, input_path, tree)
def test_try_parse_invalid(self): input_path = EXAMPLES_ROOT.joinpath('invalid', 'fortran_parser_error.f90') parser = FortranParser() with self.assertLogs(level=logging.ERROR): with self.assertRaises(RuntimeError) as err: parser.parse('', input_path) _LOG.debug('%s', err.exception)
def test_translate_fortran_to_python(self, input_path): reader = CodeReader() code = reader.read_file(input_path) parser = FortranParser() fortran_ast = parser.parse(code, input_path) generalizer = FortranAstGeneralizer() syntax = generalizer.generalize(fortran_ast) unparser = TypedPythonUnparserWithComments() python_code = unparser.unparse(syntax) basic_check_python_code(self, input_path, python_code)
def test_generalize_examples(self, input_path): parser = FortranParser() fortran_ast = parser.parse('', input_path) basic_check_fortran_ast(self, input_path, fortran_ast) generalizer = FortranAstGeneralizer() with _TIME.measure('generalize.{}'.format( input_path.name.replace('.', '_'))) as timer: syntax = generalizer.generalize(fortran_ast) basic_check_python_ast(self, input_path, syntax) _LOG.info('generalized "%s" in %fs', input_path, timer.elapsed)
def test_unparse(self): parser = FortranParser() generalizer = FortranAstGeneralizer() unparser = Fortran77Unparser() for input_path in EXAMPLES_F77_FILES + EXAMPLES_F95_FILES: with self.subTest(input_path=input_path): tree = generalizer.generalize(parser.parse('', input_path)) # _LOG.debug('generalized Fortran tree %s', typed_astunparse.dump(tree)) code = unparser.unparse(tree) basic_check_fortran_code(self, input_path, code)
def test_unparse_fundamentals(self, input_path): parser = FortranParser() fortran_ast = parser.parse('', input_path) basic_check_fortran_ast(self, input_path, fortran_ast) generalizer = FortranAstGeneralizer() syntax = generalizer.generalize(fortran_ast) basic_check_python_ast(self, input_path, syntax) unparser = Fortran77Unparser() with _TIME.measure('unparse.{}'.format( input_path.name.replace('.', '_'))) as timer: code = unparser.unparse(syntax) basic_check_fortran_code(self, input_path, code) _LOG.info('unparsed "%s" in %fs', input_path, timer.elapsed)
def test_translate_fortran_to_python_to_fortran(self, input_path): parser = FortranParser() fortran_ast = parser.parse('', input_path) generalizer = FortranAstGeneralizer() syntax = generalizer.generalize(fortran_ast) python_unparser = TypedPythonUnparserWithComments() python_code = python_unparser.unparse(syntax) python_parser = TypedPythonParserWithComments(default_mode='exec') reparsed_syntax = python_parser.parse(python_code) fortran_unparser = Fortran77Unparser() if input_path.suffix == '.f' \ else Fortran2008Unparser() fortran_code = fortran_unparser.unparse(reparsed_syntax) basic_check_fortran_code(self, input_path, fortran_code, suffix='.py' + input_path.suffix)
def test_fortran_to_python(self): for input_path in EXAMPLES_F77_FILES: reader = CodeReader() parser = FortranParser() generalizer = FortranAstGeneralizer() unparser = TypedPythonUnparserWithComments() writer = CodeWriter('.py') with self.subTest(input_path=input_path): code = reader.read_file(input_path) fortran_ast = parser.parse(code, input_path) tree = generalizer.generalize(fortran_ast) python_code = unparser.unparse(tree) writer.write_file( python_code, pathlib.Path('/tmp', input_path.name + '.py'))
def test_fortran_to_python_to_fortran(self): for input_path in EXAMPLES_F77_FILES: parser = FortranParser() generalizer = FortranAstGeneralizer() unparser = TypedPythonUnparserWithComments() python_parser = TypedPythonParserWithComments(default_mode='exec') writer = CodeWriter('.f') with self.subTest(input_path=input_path): fortran_ast = parser.parse('', input_path) tree = generalizer.generalize(fortran_ast) python_code = unparser.unparse(tree) tree = python_parser.parse(python_code) fortran_code = unparser.unparse(tree) writer.write_file( fortran_code, pathlib.Path('/tmp', input_path.name + '.py.f'))
def test_parse(self): parser = FortranParser() for input_path in EXAMPLES_F77_FILES + EXAMPLES_F95_FILES: with self.subTest(input_path=input_path): fortran_ast = parser.parse('', input_path) basic_check_fortran_ast(self, input_path, fortran_ast)