fparser Reference Guide  0.0.14
parsefortran.py
1 #!/usr/bin/env python
2 # Modified work Copyright (c) 2017-2018 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 """Provides FortranParser.
68 """
69 # Author: Pearu Peterson <pearu@cens.ioc.ee>
70 # Created: May 2006
71 
72 import logging
73 
74 from fparser.one.block_statements import BeginSource
75 from fparser.common.utils import AnalyzeError
76 
77 __autodoc__ = ["FortranParser"]
78 __all__ = ["FortranParser"]
79 
80 
82  """
83  Parser of FortranReader structure.
84 
85  Use .parse() method for parsing, parsing result is saved in .block
86  attribute.
87  """
88 
89  cache = {}
90 
91  def __init__(self, reader, ignore_comments=True):
92  self.reader = reader
93  logging.getLogger(__name__).setLevel(logging.DEBUG)
94  if reader.id in self.cache:
95  parser = self.cache[reader.id]
96  self.block = parser.block
97  self.is_analyzed = parser.is_analyzed
98  logging.getLogger(__name__).info("using cached %s", (reader.id))
99  else:
100  self.cache[reader.id] = self
101  self.block = None
102  self.is_analyzed = False
103  self.ignore_comments = ignore_comments
104  return
105 
106  def get_item(self):
107  """
108  Retrieves the next item from the reader.
109  """
110  try:
111  item = self.reader.next(ignore_comments=self.ignore_comments)
112  return item
113  except StopIteration:
114  pass
115  return
116 
117  def put_item(self, item):
118  """
119  Pushes the given item to the reader.
120  """
121  self.reader.fifo_item.insert(0, item)
122  return
123 
124  def parse(self):
125  """Parses the program specified in the reader object."""
126  if self.block is not None:
127  return
128  try:
129  self.block = BeginSource(self)
130  except KeyboardInterrupt:
131  raise
132  except Exception as error:
133  reader = self.reader
134  logger = logging.getLogger(__name__)
135  while reader is not None:
136  message = reader.format_message(
137  "FATAL ERROR",
138  "while processing line",
139  reader.linecount,
140  reader.linecount,
141  )
142  logger.critical(message)
143  reader = reader.reader
144  logger.debug("An error occurred during parsing.", exc_info=error)
145  logger.critical("STOPPED PARSING")
146  raise error
147  return
148 
149  def analyze(self):
150  """
151  Attempts to analyse the parsed Fortran. It is not clear what for.
152  """
153  if self.is_analyzed:
154  return
155  if self.block is None:
156  logging.getLogger(__name__).info("Nothing to analyze.")
157  return
158 
159  try:
160  self.block.analyze()
161  except AnalyzeError:
162  pass
163  self.is_analyzed = True
164  return