Repository: stfc/PSyclone
Pull request #2014 (2023-01-17 16:15:15)

#2009 : Fix hardcoding value forcing parsing fortran as free-form
Add optional `free_form` option to parser to handle fixed form fortran.

Feel free to say if you want it another way.

codecov[bot]: # [Codecov](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc) Report
Base: **99.84**% // Head: **99.83**% // Decreases project coverage by **`-0.01%`** :warning:
> Coverage data is based on head [(`6794e95`)](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc) compared to base [(`e0d5f5a`)](https://codecov.io/gh/stfc/PSyclone/commit/e0d5f5aacea18ef7e70d5df1a2476441f4c39bef?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc).
> Patch coverage: 100.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #2014      +/-   ##
==========================================
- Coverage   99.84%   99.83%   -0.01%     
==========================================
  Files         306      306              
  Lines       42045    42043       -2     
==========================================
- Hits        41979    41975       -4     
- Misses         66       68       +2     
```


| [Impacted Files](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc) | Coverage Δ | |
|---|---|---|
| [src/psyclone/psyir/frontend/fortran.py](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc#diff-c3JjL3BzeWNsb25lL3BzeWlyL2Zyb250ZW5kL2ZvcnRyYW4ucHk=) | `100.00% <100.00%> (ø)` | |
| [src/psyclone/tests/psyir/frontend/fortran\_test.py](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc#diff-c3JjL3BzeWNsb25lL3Rlc3RzL3BzeWlyL2Zyb250ZW5kL2ZvcnRyYW5fdGVzdC5weQ==) | `100.00% <100.00%> (ø)` | |
| [src/psyclone/psyir/symbols/symbol\_table.py](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc#diff-c3JjL3BzeWNsb25lL3BzeWlyL3N5bWJvbHMvc3ltYm9sX3RhYmxlLnB5) | `99.77% <0.00%> (-0.23%)` | :arrow_down: |
| [src/psyclone/dynamo0p3.py](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc#diff-c3JjL3BzeWNsb25lL2R5bmFtbzBwMy5weQ==) | `99.82% <0.00%> (-0.03%)` | :arrow_down: |
| [src/psyclone/psyGen.py](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc#diff-c3JjL3BzeWNsb25lL3BzeUdlbi5weQ==) | `97.85% <0.00%> (-0.01%)` | :arrow_down: |
| [src/psyclone/f2pygen.py](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc#diff-c3JjL3BzeWNsb25lL2YycHlnZW4ucHk=) | `99.04% <0.00%> (-0.01%)` | :arrow_down: |
| [src/psyclone/transformations.py](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc#diff-c3JjL3BzeWNsb25lL3RyYW5zZm9ybWF0aW9ucy5weQ==) | `99.53% <0.00%> (-0.01%)` | :arrow_down: |
| [src/psyclone/gocean1p0.py](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc#diff-c3JjL3BzeWNsb25lL2dvY2VhbjFwMC5weQ==) | `99.86% <0.00%> (-0.01%)` | :arrow_down: |
| [src/psyclone/psyir/nodes/operation.py](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc#diff-c3JjL3BzeWNsb25lL3BzeWlyL25vZGVzL29wZXJhdGlvbi5weQ==) | `100.00% <0.00%> (ø)` | |
| [src/psyclone/psyir/frontend/fparser2.py](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc#diff-c3JjL3BzeWNsb25lL3BzeWlyL2Zyb250ZW5kL2ZwYXJzZXIyLnB5) | `100.00% <0.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/stfc/PSyclone/pull/2014?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stfc).


Patch:
diff --git a/src/psyclone/psyir/frontend/fortran.py b/src/psyclone/psyir/frontend/fortran.py
index 338f80210..88e8c484b 100644
--- a/src/psyclone/psyir/frontend/fortran.py
+++ b/src/psyclone/psyir/frontend/fortran.py
@@ -61,10 +61,11 @@ class FortranReader():
         self._processor = Fparser2Reader()
         SYMBOL_TABLES.clear()
 
-    def psyir_from_source(self, source_code):
+    def psyir_from_source(self, source_code, free_form=True):
         ''' Generate the PSyIR tree representing the given Fortran source code.
 
         :param str source_code: text representation of the code to be parsed.
+        :param bool free_form: If parsing free-form code or not (default True).
 
         :returns: PSyIR representing the provided Fortran source code.
         :rtype: :py:class:`psyclone.psyir.nodes.Node`
@@ -73,7 +74,7 @@ class FortranReader():
         SYMBOL_TABLES.clear()
         string_reader = FortranStringReader(source_code)
         # Set reader to free format.
-        string_reader.set_format(FortranFormat(True, False))
+        string_reader.set_format(FortranFormat(free_form, False))
         parse_tree = self._parser(string_reader)
         psyir = self._processor.generate_psyir(parse_tree)
         return psyir
@@ -176,12 +177,15 @@ class FortranReader():
                 f"imports which might be bringing them into scope.") from err
         return fake_parent[0].detach()
 
-    def psyir_from_file(self, file_path):
+    def psyir_from_file(self, file_path, free_form=True):
         ''' Generate the PSyIR tree representing the given Fortran file.
 
         :param file_path: path of the file to be read and parsed.
         :type file_path: str or any Python Path format.
 
+        :param free_form: If parsing free-form code or not (default True).
+        :type free_form: bool
+
         :returns: PSyIR representing the provided Fortran file.
         :rtype: :py:class:`psyclone.psyir.nodes.Node`
 
@@ -194,7 +198,7 @@ class FortranReader():
         # that have already been done before.
 
         with open(file_path, "r") as source:
-            return self.psyir_from_source(source.read())
+            return self.psyir_from_source(source.read(), free_form=free_form)
 
 
 # For Sphinx AutoAPI documentation generation


Test patch:
diff --git a/src/psyclone/tests/psyir/frontend/fortran_test.py b/src/psyclone/tests/psyir/frontend/fortran_test.py
index b617814f3..883d74fcf 100644
--- a/src/psyclone/tests/psyir/frontend/fortran_test.py
+++ b/src/psyclone/tests/psyir/frontend/fortran_test.py
@@ -62,6 +62,13 @@ subroutine sub(a)
 end subroutine sub
 '''
 
+FIXED_FORM_CODE = '''
+      subroutine insert_node (lstr, node, nnodes, ierr)
+      integer*4 lstr, ierr, i,j,k, lsffx, digits, power, ndots, idot(3)
+     &                                        , node,  nnodes
+      end
+'''
+
 
 def test_fortran_reader_constructor():
     ''' Test that the constructor initialises the _parser and _processor
@@ -85,6 +92,19 @@ def test_fortran_psyir_from_source():
     assert isinstance(subroutine, Routine)
 
 
+def test_fortran_psyir_from_source_fixed_form():
+    '''
+    Test we parse also fixed-form fortran code when enabling the right
+    option.
+    '''
+    fortran_reader = FortranReader()
+    file_container = fortran_reader.psyir_from_source(FIXED_FORM_CODE,
+                                                      free_form=False)
+    assert isinstance(file_container, FileContainer)
+    subroutine = file_container.children[0]
+    assert isinstance(subroutine, Routine)
+
+
 def test_fortran_psyir_from_expression(fortran_reader):
     ''' Test that the psyir_from_expression method generates the
     expected PSyIR. '''