Open Fortran Parser XML wrapper examples

These examples are for Python wrapper to the OFP XML. Run ant to build the OFP XML itself before executing them.


In [1]:
import pathlib
import tempfile
import xml.etree.ElementTree as ET

import open_fortran_parser

In [2]:
xml_tree = open_fortran_parser.parse(pathlib.Path('test', 'examples', 'empty.f'))
ET.dump(xml_tree)


<ofp version="0.8.5-1">
  <file col_begin="6" col_end="24" line_begin="2" line_end="8" path="/home/mateusz/Projects/open-fortran-parser-xml/test/examples/empty.f">
    <start-of-file filename="test/examples/empty.f" path="/home/mateusz/Projects/open-fortran-parser-xml/test/examples/empty.f" rule="-2" />
    <comment col_begin="6" col_end="36" line_begin="2" line_end="2" text="! minimalistic Fortran program" />
    <program col_begin="6" col_end="24" line_begin="4" line_end="8" name="empty">
      <main-program__begin addendum="begin" rule="1101" />
      <header />
      <program-stmt col_begin="6" col_end="20" eos="&#10;" id="empty" line_begin="4" line_end="4" programKeyword="program" rule="1102" />
      <body col_begin="6" col_end="20" line_begin="6" line_end="6">
        <specification col_begin="6" col_end="20" declarations="0" implicits="1" imports="0" line_begin="6" line_end="6" uses="0">
          <declaration col_begin="6" col_end="20" line_begin="6" line_end="6" subtype="none" type="implicit">
            <implicit-stmt col_begin="6" col_end="20" eos="&#10;" hasImplicitSpecList="false" implicitKeyword="implicit" line_begin="6" line_end="6" noneKeyword="none" rule="549" />
          </declaration>
          <declaration />
          <specification-part numDeclConstructs="0" numImplicitStmts="1" numImportStmts="0" numUseStmts="0" rule="204" />
        </specification>
        <statement />
      </body>
      <end-program-stmt col_begin="6" col_end="24" endKeyword="end" eos="&#10;" id="empty" line_begin="8" line_end="8" programKeyword="program" rule="1103" />
      <main-program hasExecutionPart="false" hasInternalSubprogramPart="false" hasProgramStmt="true" rule="1101" />
    </program>
    <end-of-file filename="test/examples/empty.f" path="/home/mateusz/Projects/open-fortran-parser-xml/test/examples/empty.f" rule="-2" />
  </file>
</ofp>

In [3]:
code = '''
program HowBadCanItBe

    goto main_sub3

end program
'''

with tempfile.NamedTemporaryFile('w+') as tmp:
    print(code, file=tmp, flush=True)
    xml_tree = open_fortran_parser.parse(pathlib.Path(tmp.name), raise_on_error=True)

for prog in xml_tree.findall('.//program'):
    print(prog.attrib['name'])


HowBadCanItBe