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

Error Parsing Custom Type Declaration #26

Closed
Philipp1297 opened this issue Nov 26, 2024 · 9 comments · Fixed by #28
Closed

Error Parsing Custom Type Declaration #26

Philipp1297 opened this issue Nov 26, 2024 · 9 comments · Fixed by #28

Comments

@Philipp1297
Copy link

Description

I encountered an issue when parsing a type declaration in a Structured Text (ST) file. The declaration is as follows:

TYPE T_INTERLOCK : WORD; 
END_TYPE

However, when attempting to parse it using the current configuration, I receive the following error:

Error parsing node T_INTERLOCK in file test_files\baselib\BaseLibProj\BaseLib\POUs\HMI\Datentypen\T_INTERLOCK.TcDUT
(None:1:24: Expected '.' or '\s' or '//' or '(*(.|\n)?*)' or '{' or 'END_TYPE' => 'OCK : WORD; END_TYP')

Steps Taken:

I tried modifying the st_declaration.tx grammar in an attempt to accommodate this type declaration by adding or adjusting the TypeDef rule as follows:

TypeDef:
    comments*=CommentAny?                
    'TYPE'
    name=ID                             
    ('EXTENDS' extends=AnyType)?  
    ':'  
    CommentAny* 
    type=AnyType
    ';'?
    CommentAny* 
    'END_TYPE'
;

AnyType:
    TypeStruct | TypeUnion | TypeEnum | StringType | Fqn | 'WORD'
;

However, making these changes requires modifying the source code, and I’m wondering if there's a better approach.

@Philipp1297
Copy link
Author

If changing the PlcDeclaration class to handle string types (like WORD) is an acceptable solution, I can make the necessary changes. But before proceeding, I wanted to confirm if that would be the right approach, or if there is a more elegant or preferable solution that avoids modifying the source code directly.

@Philipp1297 Philipp1297 changed the title Error Parsing Custom Type Declaration in Structured Text (ST) File Error Parsing Custom Type Declaration Nov 26, 2024
@Philipp1297
Copy link
Author

Philipp1297 commented Nov 26, 2024

I tried this inside the st_declaration.tx, but it seems not a proper approach:

TypeDef:
    comments*=CommentAny?               
    'TYPE'
    name=ID                             
    ('EXTENDS' extends=AnyType)?     
    ':'
    CommentAny*                       
    type=BaseTypeOrAnyType
    ';'?                       
    CommentAny*
    'END_TYPE'                          
;

BaseTypeOrAnyType:
    AnyType | BaseType     
;

AnyType:
    TypeStruct | TypeUnion | TypeEnum | StringType | Fqn
;

BaseType:
    WORD
;

WORD:
    'WORD'
;

@RobertoRoos
Copy link
Member

RobertoRoos commented Nov 26, 2024

Thanks for your report @Philipp1297 !

I'll have to admit I've never seen a TYPE ... declaration in structured text, I didn't know that existed.
EDIT: It's an alias: https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/3928317323.html&id=

Am I understanding correctly this prevents Sphinx from completing, as in your run into an uncaught exception?

There's two things to pick up then:

  • TYPE should be supported - the grammar should be updated to be able to parse it and custom TYPES should be handled with new directives in our plugin.
  • Failed code parsing with our grammar should cause a bright red error in the log, but the plugin should continue, allowing docs generation to complete at least.

I'll look into either. I can use your suggested grammar modifications as a starting point. But I'll have to admit it might be a while before I can make some progress here.

@Philipp1297
Copy link
Author

I have identified additional cases where the grammar is currently not functioning as expected.
I will gather these cases and provide the existing limitations, along with potential grammar or source code changes, here.

@Philipp1297
Copy link
Author

Philipp1297 commented Nov 26, 2024

Limitation in case of default value for TYPE Decleration:

TYPE E_HMI_BUTTON_FEEDBACK :
(
	NONE := 0,
	ACTIVE := 1,
	PENDING := 2,
	WARNING := 3,
	ERROR := 4
) INT := NONE;
END_TYPE

I fixed this by changing the grammer to this:

TypeDef:
    comments*=CommentAny?               
    'TYPE'
    name=ID                             
    ('EXTENDS' extends=AnyType)?     
    ':'
    CommentAny*                        
    type=BaseTypeOrAnyType
    ';'?                       
    CommentAny*
    'END_TYPE'                          
;

BaseTypeOrAnyType:
    AnyType | BaseType     
;

AnyType:
    TypeStruct | TypeUnion | TypeEnum | StringType | Fqn
;

BaseType:
    WORD | INT
;

WORD:
    'WORD'
;

TypeStruct:
    'STRUCT'
    members*=Variable
    CommentAny*
    // Catch trailing comments here, not at the end of `Variable`
    'END_STRUCT'
;

TypeUnion:
    'UNION'
    members*=Variable
    CommentAny*
    // Catch trailing comments here, not at the end of `Variable`
    'END_UNION'
;


TypeEnum:
    '('
    values*=EnumOption
    CommentAny* 
    ')'
    (base_type=AssignmentValue)? 
    ';'?
;

EnumOption:
    CommentAny*
    name=ID
    (':=' number=INT)?
    (',')?
    (comment=CommentLine)?
;

@Philipp1297
Copy link
Author

Limitation in case of the char ';' for string default value like:

S_GET_CURR_MIN_MAX : STRING := 'CURR:LIM:NEG?;:CURR:LIM:POS?$L';

@Philipp1297
Copy link
Author

Philipp1297 commented Nov 27, 2024

Last (atm) found Limitation

_stHMIValve : ST_HMI_ANALOG_VALVE_DATA;;

Double Semicolon

@RobertoRoos
Copy link
Member

Thanks again, I'll look into these. Are these from an open source project by any chance? In order to test the grammar for failures I would ideally just throw it against big existing projects. I'm already doing that for TcUnit for example.

@RobertoRoos
Copy link
Member

I think all issues will be fixed with #28 , except for the string escape, which is more tricky. Let's continue that in #29 .

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 a pull request may close this issue.

2 participants