# Path: fparser/readfortran.py
# class FortranFileReader(FortranReaderBase):
# 
#     def __init__(self, filename, include_dirs = None, source_only=None):
#         isfree, isstrict = get_source_info(filename)
#         self.id = filename
#         self.file = open(filename,'r')
#         FortranReaderBase.__init__(self, self.file, isfree, isstrict)
#         if include_dirs is None:
#             self.include_dirs.insert(0, os.path.dirname(filename))
#         else:
#             self.include_dirs = include_dirs[:]
#         if source_only is not None:
#             self.source_only = source_only[:]
#         return
# 
#     def close_source(self):
#         self.file.close()
# 
# class FortranStringReader(FortranReaderBase):
# 
#     def __init__(self, string, include_dirs = None, source_only = None):
#         self.id = 'string-'+string
#         source = StringIO(string)
#         isfree, isstrict = get_source_info_str(string)
#         FortranReaderBase.__init__(self, source, isfree, isstrict)
#         if include_dirs is not None:
#             self.include_dirs = include_dirs[:]
#         if source_only is not None:
#             self.source_only = source_only[:]
#         return
# 
# Path: fparser/block_statements.py
# class BeginSource(BeginStatement):
#     """
#     Fortran source content.
#     """
#     match = staticmethod(lambda s: True)
#     end_stmt_cls = EndSource
#     a = AttributeHolder(module = {},
#                         external_subprogram = {},
#                         blockdata = {},
#                         )
#     def tofortran(self,isfix=None):
#         if isfix:
#             tab = 'C'
#         else:
#             tab = self.get_indent_tab(isfix=isfix) + '!'
#         return tab + BeginStatement.tofortran(self, isfix=isfix)
#         
#     def tostr(self):
#         return self.blocktype.upper() + ' '+ self.name
# 
#     def process_item(self):
#         self.name = self.reader.name
#         self.top = self
#         self.fill(end_flag = True)
#         return
# 
#     def analyze(self):
#         for stmt in self.content:
#             if isinstance(stmt, Module):
#                 stmt.analyze()
#                 self.a.module[stmt.name] = stmt
#             elif isinstance(stmt, SubProgramStatement):
#                 stmt.analyze()
#                 self.a.external_subprogram[stmt.name] = stmt
#             elif isinstance(stmt, BlockData):
#                 stmt.analyze()
#                 self.a.blockdata[stmt.name] = stmt
#             else:
#                 stmt.analyze()
#         return
# 
#     def get_classes(self):
#         if self.reader.ispyf:
#             return [PythonModule] + program_unit
#         return program_unit
# 
#     def process_subitem(self, item):
#         # MAIN block does not define start/end line conditions,
#         # so it should never end until all lines are read.
#         # However, sometimes F77 programs lack the PROGRAM statement,
#         # and here we fix that:
#         if self.reader.isf77:
#             line = item.get_line()
#             if line=='end':
#                 message = item.reader.format_message(\
#                         'WARNING',
#                         'assuming the end of undefined PROGRAM statement',
#                         item.span[0],item.span[1])
#                 logger.warning(message)
#                 # print >> sys.stderr, message
#                 p = Program(self)
#                 p.content.extend(self.content)
#                 p.content.append(EndProgram(p,item))
#                 self.content[:] = [p]
#                 return
#         return BeginStatement.process_subitem(self, item)
# 
#     def topyf(self, tab=''): # XXXX
#         s = ''
#         for name, stmt in list(self.a.module.items()):
#             s += stmt.topyf(tab=tab)
#         for name, stmt in list(self.a.external_subprogram.items()):
#             s += stmt.topyf(tab=tab)
#         for name, stmt in list(self.a.blockdata.items()):
#             s += stmt.topyf(tab=tab)
#         return s
# 
# Path: fparser/utils.py
# class AnalyzeError(Exception):
#     pass


import re
import sys
import traceback
import logging
    import sys
    import hotshot, hotshot.stats
from numpy.distutils.misc_util import yellow_text, red_text
from .readfortran import FortranFileReader, FortranStringReader
from .block_statements import BeginSource
from .utils import AnalyzeError
    parser = FortranParser(reader)
    block = parser.parse()
    print(block)

def test_free90():
    string = """
module foo

   subroutine bar
    real r
    if ( pc_get_lun() .ne. 6) &
    write ( pc_get_lun(), '( &
    & /, a, /, " p=", i4, " stopping c_flag=", a, &
    & /, " print unit=", i8)') &
    trim(title), pcpsx_i_pel(), trim(c_flag), pc_get_lun()
    if (.true.) then
      call smth
    end if
    aaa : if (.false.) then
    else if (a) then aaa
    else aaa
    end if aaa
    hey = 1
    end subroutine bar
    abstract interface

    end interface

end module foo
"""

# Path: fparser/readfortran.py
# class FortranFileReader(FortranReaderBase):
# 
#     def __init__(self, filename, include_dirs = None, source_only=None):
#         isfree, isstrict = get_source_info(filename)
#         self.id = filename
#         self.file = open(filename,'r')
#         FortranReaderBase.__init__(self, self.file, isfree, isstrict)
#         if include_dirs is None:
#             self.include_dirs.insert(0, os.path.dirname(filename))
#         else:
#             self.include_dirs = include_dirs[:]
#         if source_only is not None:
#             self.source_only = source_only[:]
#         return
# 
#     def close_source(self):
#         self.file.close()
# 
# class FortranStringReader(FortranReaderBase):
# 
#     def __init__(self, string, include_dirs = None, source_only = None):
#         self.id = 'string-'+string
#         source = StringIO(string)
#         isfree, isstrict = get_source_info_str(string)
#         FortranReaderBase.__init__(self, source, isfree, isstrict)
#         if include_dirs is not None:
#             self.include_dirs = include_dirs[:]
#         if source_only is not None:
#             self.source_only = source_only[:]
#         return
# 
# Path: fparser/block_statements.py
# class BeginSource(BeginStatement):
#     """
#     Fortran source content.
#     """
#     match = staticmethod(lambda s: True)
#     end_stmt_cls = EndSource
#     a = AttributeHolder(module = {},
#                         external_subprogram = {},
#                         blockdata = {},
#                         )
#     def tofortran(self,isfix=None):
#         if isfix:
#             tab = 'C'
#         else:
#             tab = self.get_indent_tab(isfix=isfix) + '!'
#         return tab + BeginStatement.tofortran(self, isfix=isfix)
#         
#     def tostr(self):
#         return self.blocktype.upper() + ' '+ self.name
# 
#     def process_item(self):
#         self.name = self.reader.name
#         self.top = self
#         self.fill(end_flag = True)
#         return
# 
#     def analyze(self):
#         for stmt in self.content:
#             if isinstance(stmt, Module):
#                 stmt.analyze()
#                 self.a.module[stmt.name] = stmt
#             elif isinstance(stmt, SubProgramStatement):
#                 stmt.analyze()
#                 self.a.external_subprogram[stmt.name] = stmt
#             elif isinstance(stmt, BlockData):
#                 stmt.analyze()
#                 self.a.blockdata[stmt.name] = stmt
#             else:
#                 stmt.analyze()
#         return
# 
#     def get_classes(self):
#         if self.reader.ispyf:
#             return [PythonModule] + program_unit
#         return program_unit
# 
#     def process_subitem(self, item):
#         # MAIN block does not define start/end line conditions,
#         # so it should never end until all lines are read.
#         # However, sometimes F77 programs lack the PROGRAM statement,
#         # and here we fix that:
#         if self.reader.isf77:
#             line = item.get_line()
#             if line=='end':
#                 message = item.reader.format_message(\
#                         'WARNING',
#                         'assuming the end of undefined PROGRAM statement',
#                         item.span[0],item.span[1])
#                 logger.warning(message)
#                 # print >> sys.stderr, message
#                 p = Program(self)
#                 p.content.extend(self.content)
#                 p.content.append(EndProgram(p,item))
#                 self.content[:] = [p]
#                 return
#         return BeginStatement.process_subitem(self, item)
# 
#     def topyf(self, tab=''): # XXXX
#         s = ''
#         for name, stmt in list(self.a.module.items()):
#             s += stmt.topyf(tab=tab)
#         for name, stmt in list(self.a.external_subprogram.items()):
#             s += stmt.topyf(tab=tab)
#         for name, stmt in list(self.a.blockdata.items()):
#             s += stmt.topyf(tab=tab)
#         return s
# 
# Path: fparser/utils.py
# class AnalyzeError(Exception):
#     pass


# Path: fparser/parsefortran.py
import re
import sys
import traceback
import logging
    import sys
    import hotshot, hotshot.stats
from numpy.distutils.misc_util import yellow_text, red_text
from .readfortran import FortranFileReader, FortranStringReader
from .block_statements import BeginSource
from .utils import AnalyzeError

    parser = FortranParser(reader)
    block = parser.parse()
    print(block)

def test_free90():
    string = """
module foo

   subroutine bar
    real r
    if ( pc_get_lun() .ne. 6) &
    write ( pc_get_lun(), '( &
    & /, a, /, " p=", i4, " stopping c_flag=", a, &
    & /, " print unit=", i8)') &
    trim(title), pcpsx_i_pel(), trim(c_flag), pc_get_lun()
    if (.true.) then
      call smth
    end if
    aaa : if (.false.) then
    else if (a) then aaa
    else aaa
    end if aaa
    hey = 1
    end subroutine bar
    abstract interface

    end interface

end module foo
"""

    reader = FortranStringReader(string, True, False)