36 '''Utility module containing classes and functions that are used by
43 from fparser.two.parser
import ParserFactory
44 from fparser.common.readfortran
import FortranFileReader
45 from fparser.two.utils
import FortranSyntaxError
54 '''Provides a PSyclone-specific error class for the situation when
55 the PSyclone code parsing finds an error in the input.
57 :param str value: the message associated with the error.
60 def __init__(self, value):
61 PSycloneError.__init__(self, value)
62 self.
valuevaluevalue =
"Parse Error: " + str(value)
68 '''Check that the supplied API is valid.
70 :param str api: The API to check.
71 :raises ParseError: if the supplied API is not recognised.
74 _config = Config.get()
76 if api
not in _config.supported_apis:
78 f
"utils.py:check_api: Unsupported API '{api}' specified. "
79 f
"Supported types are {_config.supported_apis}.")
82 def check_line_length(filename):
83 '''Check that the code contained within the filename file
84 conforms to the 132 line length limit.
86 :param str filename: The file containing the code.
88 :raises InternalError: if the specified file can not be opened or read.
89 :raises ParseError: if one of more lines are longer than the 132 \
94 with io.open(filename,
"r", encoding=
'utf8')
as myfile:
95 code_str = myfile.read()
96 except IOError
as excinfo:
97 raise InternalError(f
"In utils.py:check_line_length: {excinfo}")
99 if fll.long_lines(code_str):
101 f
"the file does not conform to the specified {fll.length} line "
105 def parse_fp2(filename):
106 '''Parse a Fortran source file contained in the file 'filename' using
109 :param str filename: source file (including path) to read.
110 :returns: fparser2 AST for the source file.
111 :rtype: :py:class:`fparser.two.Fortran2003.Program`
112 :raises ParseError: if the file could not be parsed.
115 parser = ParserFactory().create(std=
"f2008")
118 config = Config.get()
120 reader = FortranFileReader(filename, include_dirs=config.include_paths)
121 except IOError
as error:
123 f
"algorithm.py:parse_fp2: Failed to parse file '{filename}'. "
124 f
"Error returned was ' {error} '.")
126 parse_tree = parser(reader)
127 except FortranSyntaxError
as msg:
129 f
"algorithm.py:parse_fp2: Syntax error in file '{filename}':\n"