Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let the user parse a file from the Python API. #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dareg
Copy link

@dareg dareg commented Oct 3, 2023

For now, parsing file containing preprocessors directives through the python API seems broken.
For instance with this file:

subroutine f()
#ifdef FOO
if(.true.)then
#else
if(.false.)then
#endif
endif
end subroutine f

It works fine when using fxtran, but when using the python wrapper the parsing fail:

Traceback (most recent call last):
  File "/tmp/fxtran/main.py", line 8, in <module>
    xml=fxtran.run('-canonic','-DFOO','-cpp',code)
fxtran.error: 
E.</literal-E></condition-E>) THEN</if-then-stmt>
<cpp>#endif</cpp>
<end-if-stmt>ENDIF</end-if-stmt>
At FXTRAN_STMT.c:3200
Unexpected end-subroutine-stmt statement
      | 0         1         2         3         4         5         6         7         

    5 | if(.false.)then
    6 | #endif
    7 | endif
    8 | end subroutine f
            ^

The problem seems to arise because the code is never preprocessed. Indeed, when FXTRAN_RUN is called with the 'Text' variable being not NULL, the preprocessor is never called. The preprocessor is only called when a file is given.
In consequence this problem can be fixed in the Python API by letting the user provide a file instead of a string containing the code. It will be then fxtran's job to open the file and fxtran will preprocess it.

It also ease the use of the Python API since the user don't have to open a file and read it on the python side just to give it to fxtran. The user can now just give a filename and get the resulting xml.

@pmarguinaud
Copy link
Owner

Hello Judicaël,

I would like the python interface not to enable cpp; the reason is that I cannot reset cpp (it allocates memory and has global variables). In fact the python interface is intended to parse small bits of Fortran (it would be expensive to call the fxtran executable for each small bit).
But for full files (with or without cpp pre-processing), the most desirable fashion is to to invoke the fxtran executable.

And yes, if you do not enable the cpp pre-processor, the result should be valid Fortran, with consistent if/then/else blocks, etc.

So either your invoke the fxtran executable, or you pre-process your code yourself before passing to to the python interface.

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants