fsource icon indicating copy to clipboard operation
fsource copied to clipboard

Parser fails at INCLUDE statements

Open fkraushofer opened this issue 5 years ago • 5 comments

Hi, I tried to run the parser on (part of) this package, and got the following error:

> fsource parse ref-calc.v1.6.f --fixed-form


ref-calc.v1.6.f:42:7: parser error: malformed statement inside program
|
|             INCLUDE "PARAM"
|             ^~~~~~~

The ref-calc.v1.6.f file begins:

      program leed

C  include parameter statements for dimensions etc

      INCLUDE "PARAM"

C  set unchangeable constants

      INCLUDE "GLOBAL"

Am I missing some option to run the parser on the entire project in a way that would make the INCLUDE statements work, or is this simply not implemented yet?

fkraushofer avatar Jun 03 '20 12:06 fkraushofer

No, this is not implemented yet. Thanks for the pointer!

mwallerb avatar Jun 03 '20 13:06 mwallerb

Would this be added to _DECLARATION_HANDLERS and STMT_HANDLERS in parser.py? Typically it's found in the declaration section but INCLUDE can show up almost anywhere - see https://www.ibm.com/docs/en/xffbg/121.141?topic=descriptions-include

apthorpe avatar Jul 13 '21 19:07 apthorpe

I managed to patch around the error by adding

def include_stmt(tokens):
    expect(tokens, 'include')
    ignore_stmt(tokens)
    return tokens.produce('include_stmt')

(based on data_stmt())

then added

'include':     include_stmt,

to both _DECLARATION_HANDLERS and STMT_HANDLERS

This is admittedly a partial effort; it doesn't attempt to implement INCLUDE but does avoid the error and adds include_stmt to the AST.

apthorpe avatar Jul 14 '21 12:07 apthorpe

Hmm, the problem is that the standard specifies that include is not a statement at all -- it is called "include line" and is much closer in spirit to a preprocessor statement. I think it would therefore be better to treat it as preprocessor statement.

mwallerb avatar Jul 14 '21 12:07 mwallerb

That's reasonable; that's the interpretation I remember from looking at the spec. I approached this from a "how do I make this error stop" perspective rather than a "how do I accurately interpret this entity" perspective so there's likely a better (standards-compliant) way of handling this.

apthorpe avatar Jul 14 '21 17:07 apthorpe