fparser Reference Guide  0.0.14
fparser2.py
1 #!/usr/bin/env python
2 # Modified work Copyright (c) 2017-2019 Science and Technology
3 # Facilities Council
4 # Original work Copyright (c) 1999-2008 Pearu Peterson
5 #
6 # All rights reserved.
7 #
8 # Modifications made as part of the fparser project are distributed
9 # under the following license:
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions are
13 # met:
14 #
15 # 1. Redistributions of source code must retain the above copyright
16 # notice, this list of conditions and the following disclaimer.
17 #
18 # 2. Redistributions in binary form must reproduce the above copyright
19 # notice, this list of conditions and the following disclaimer in the
20 # documentation and/or other materials provided with the distribution.
21 #
22 # 3. Neither the name of the copyright holder nor the names of its
23 # contributors may be used to endorse or promote products derived from
24 # this software without specific prior written permission.
25 #
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 
38 # --------------------------------------------------------------------
39 
40 # The original software (in the f2py project) was distributed under
41 # the following license:
42 
43 # Redistribution and use in source and binary forms, with or without
44 # modification, are permitted provided that the following conditions are met:
45 
46 # a. Redistributions of source code must retain the above copyright notice,
47 # this list of conditions and the following disclaimer.
48 # b. Redistributions in binary form must reproduce the above copyright
49 # notice, this list of conditions and the following disclaimer in the
50 # documentation and/or other materials provided with the distribution.
51 # c. Neither the name of the F2PY project nor the names of its
52 # contributors may be used to endorse or promote products derived from
53 # this software without specific prior written permission.
54 
55 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
56 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
59 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
61 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
62 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
65 # DAMAGE.
66 
67 """ Example script to parse a Fortran program using fparser """
68 
69 import logging
70 import sys
71 from fparser.scripts.script_options import set_fparser_options
72 
73 logging.basicConfig()
74 
75 try:
76  from iocbio.optparse_gui import OptionParser
77 except ImportError:
78  from optparse import OptionParser
79 
80 
81 def runner(_, options, args):
82  """
83  Function to read, parse and output Fortran source code.
84 
85  :param options: object constructed by OptionParser with cmd-line flags.
86  :param args: list of Fortran files to parse.
87  :type args: list of str
88 
89  """
90  from fparser.two.parser import ParserFactory
91  from fparser.two.Fortran2003 import FortranSyntaxError, InternalError
92  from fparser.common.readfortran import FortranFileReader
93 
94  if not args:
95  print("Error: No fortran files specified", file=sys.stderr)
96  raise SystemExit(1)
97  for filename in args:
98  print("File: '{0}'".format(filename), file=sys.stderr)
99  try:
100  reader = FortranFileReader(filename, ignore_comments=False)
101  except IOError as error:
102  print(error, file=sys.stderr)
103  continue
104  try:
105  fparser = ParserFactory().create(std=options.std)
106  program = fparser(reader)
107  if options.task == "show":
108  print(str(program))
109  if options.task == "repr":
110  print(repr(program))
111  except FortranSyntaxError as msg:
112  print(f"Syntax error: {msg}", file=sys.stderr)
113  except InternalError as msg:
114  print(f"Internal error in fparser: {msg}", file=sys.stderr)
115 
116 
117 def main():
118  """Check arguments before parsing code"""
119  parser = OptionParser()
120  set_fparser_options(parser)
121  options, args = parser.parse_args()
122  runner(parser, options, args)
123 
124 
125 if __name__ == "__main__":
126  main() # pragma: no cover