|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Open Fortran Parser XML wrapper examples" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "These examples are for Python wrapper to the OFP XML. Run `ant` to build the OFP XML itself before executing them. " |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 1, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "import pathlib\n", |
| 24 | + "import tempfile\n", |
| 25 | + "import xml.etree.ElementTree as ET\n", |
| 26 | + "\n", |
| 27 | + "import open_fortran_parser" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 2, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [ |
| 35 | + { |
| 36 | + "name": "stdout", |
| 37 | + "output_type": "stream", |
| 38 | + "text": [ |
| 39 | + "<ofp version=\"0.8.4\">\n", |
| 40 | + " <file col_begin=\"6\" col_end=\"24\" line_begin=\"2\" line_end=\"8\" path=\"/Users/mateusz/Projects/open-fortran-parser-xml/test/examples/empty.f\">\n", |
| 41 | + " <start-of-file filename=\"test/examples/empty.f\" path=\"/Users/mateusz/Projects/open-fortran-parser-xml/test/examples/empty.f\" rule=\"-2\" />\n", |
| 42 | + " <comment col_begin=\"6\" col_end=\"36\" line_begin=\"2\" line_end=\"2\" text=\"! minimalistic Fortran program\" />\n", |
| 43 | + " <program col_begin=\"6\" col_end=\"24\" line_begin=\"4\" line_end=\"8\" name=\"empty\">\n", |
| 44 | + " <main-program__begin addendum=\"begin\" rule=\"1101\" />\n", |
| 45 | + " <header />\n", |
| 46 | + " <program-stmt col_begin=\"6\" col_end=\"20\" eos=\" \" id=\"empty\" line_begin=\"4\" line_end=\"4\" programKeyword=\"program\" rule=\"1102\" />\n", |
| 47 | + " <body col_begin=\"6\" col_end=\"20\" line_begin=\"6\" line_end=\"6\">\n", |
| 48 | + " <specification col_begin=\"6\" col_end=\"20\" declarations=\"0\" implicits=\"1\" imports=\"0\" line_begin=\"6\" line_end=\"6\" uses=\"0\">\n", |
| 49 | + " <declaration col_begin=\"6\" col_end=\"20\" line_begin=\"6\" line_end=\"6\" subtype=\"none\" type=\"implicit\">\n", |
| 50 | + " <implicit-stmt col_begin=\"6\" col_end=\"20\" eos=\" \" hasImplicitSpecList=\"false\" implicitKeyword=\"implicit\" line_begin=\"6\" line_end=\"6\" noneKeyword=\"none\" rule=\"549\" />\n", |
| 51 | + " </declaration>\n", |
| 52 | + " <declaration />\n", |
| 53 | + " <specification-part numDeclConstructs=\"0\" numImplicitStmts=\"1\" numImportStmts=\"0\" numUseStmts=\"0\" rule=\"204\" />\n", |
| 54 | + " </specification>\n", |
| 55 | + " <statement />\n", |
| 56 | + " </body>\n", |
| 57 | + " <end-program-stmt col_begin=\"6\" col_end=\"24\" endKeyword=\"end\" eos=\" \" id=\"empty\" line_begin=\"8\" line_end=\"8\" programKeyword=\"program\" rule=\"1103\" />\n", |
| 58 | + " <main-program hasExecutionPart=\"false\" hasInternalSubprogramPart=\"false\" hasProgramStmt=\"true\" rule=\"1101\" />\n", |
| 59 | + " </program>\n", |
| 60 | + " <end-of-file filename=\"test/examples/empty.f\" path=\"/Users/mateusz/Projects/open-fortran-parser-xml/test/examples/empty.f\" rule=\"-2\" />\n", |
| 61 | + " </file>\n", |
| 62 | + "</ofp>\n" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + "source": [ |
| 67 | + "xml_tree = open_fortran_parser.parse(pathlib.Path('test', 'examples', 'empty.f'))\n", |
| 68 | + "ET.dump(xml_tree)" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": 3, |
| 74 | + "metadata": { |
| 75 | + "scrolled": true |
| 76 | + }, |
| 77 | + "outputs": [ |
| 78 | + { |
| 79 | + "name": "stdout", |
| 80 | + "output_type": "stream", |
| 81 | + "text": [ |
| 82 | + "HowBadCanItBe\n" |
| 83 | + ] |
| 84 | + } |
| 85 | + ], |
| 86 | + "source": [ |
| 87 | + "code = '''\n", |
| 88 | + "program HowBadCanItBe\n", |
| 89 | + "\n", |
| 90 | + " goto main_sub3\n", |
| 91 | + "\n", |
| 92 | + "end program\n", |
| 93 | + "'''\n", |
| 94 | + "\n", |
| 95 | + "with tempfile.NamedTemporaryFile('w+') as tmp:\n", |
| 96 | + " print(code, file=tmp, flush=True)\n", |
| 97 | + " xml_tree = open_fortran_parser.parse(pathlib.Path(tmp.name), raise_on_error=True)\n", |
| 98 | + "\n", |
| 99 | + "for prog in xml_tree.findall('.//program'):\n", |
| 100 | + " print(prog.attrib['name'])" |
| 101 | + ] |
| 102 | + } |
| 103 | + ], |
| 104 | + "metadata": { |
| 105 | + "kernelspec": { |
| 106 | + "display_name": "Python 3", |
| 107 | + "language": "python", |
| 108 | + "name": "python3" |
| 109 | + }, |
| 110 | + "language_info": { |
| 111 | + "codemirror_mode": { |
| 112 | + "name": "ipython", |
| 113 | + "version": 3 |
| 114 | + }, |
| 115 | + "file_extension": ".py", |
| 116 | + "mimetype": "text/x-python", |
| 117 | + "name": "python", |
| 118 | + "nbconvert_exporter": "python", |
| 119 | + "pygments_lexer": "ipython3", |
| 120 | + "version": "3.6.6" |
| 121 | + } |
| 122 | + }, |
| 123 | + "nbformat": 4, |
| 124 | + "nbformat_minor": 2 |
| 125 | +} |
0 commit comments