Search code examples
formattingcomparisondslxtext

unknown comparison failure occurs with xtext formatter


I'm customizing the xtext formatter of my dsl and whist testing I get a weird comparison failure I don't understand.

Here's the relevant part of my grammar :

EisModel:
    'project' '=' project_name=STRING ';' 
    'plcname' '=' plc_name=STRING ';'
    'author' '=' author_name=STRING ';'
    testcases+=Testcase*;
Testcase:
    "testcase" testcase_name=ID '{'
    testblock=Testblock?
    '}';
Testblock:
    'testActive' '=' testActive=BoolConstant ';'
    'blockType' '=' blockType=BlockConstant ';'
    'description' '=' description=STRING ';'
    define=DefineBlock?;
BoolConstant:
    value=('true' | 'false');
BlockConstant:
    value=('FC' | 'FB');

And the comparison failure I get, I assume, has something to do with a problem regarding a terminal rule, since I am not doing anything extraorinary in the formatter.

This is the expected code of the JUnit failure trace:

74  4    S "true"               BoolConstant:value='true'
78  0    H
78  1    S ";"                  Testblock:(';'  )
79  2    H "\n\t"               Whitespace:TerminalRule'WS'
81  9    S "blockType"          Testblock:'blockType'
90  1    H " "                  Whitespace:TerminalRule'WS'
91  1    S "="                  Testblock:( '=' )
92  1    H " "                  Whitespace:TerminalRule'WS'
93  2    S "FC"                 BlockConstant:value='FC'

And this the actual code:

          B BoolConstant         Testblock:testActive=BoolConstant path:Testblock/testActive=Testcase/testblock=EisModel/testcases[0]
74  4     S "true"               BoolConstant:value='true'
          E BoolConstant         Testblock:testActive=BoolConstant path:Testblock/testActive=Testcase/testblock=EisModel/testcases[0]
78  0    H
78  1    S ";"                  Testblock:(';'  )
79  2    H "\n\t"               Whitespace:TerminalRule'WS'
81  9    S "blockType"          Testblock:'blockType'
90  1    H " "                  Whitespace:TerminalRule'WS'
91  1    S "="                  Testblock:( '=' )
92  1    H " "                  Whitespace:TerminalRule'WS'
          B BlockConstant        Testblock:blockType=BlockConstant path:Testblock/blockType=Testcase/testblock=EisModel/testcases[0]
93  2     S "FC"                 BlockConstant:value='FC'
          E BlockConstant        Testblock:blockType=BlockConstant path:Testblock/blockType=Testcase/testblock=EisModel/testcases[0]

The difference revolves around the lines 74 and 93.

And I don't know what is going wrong or even where I could tweak anything.
Could anyone please help?

Here's the test:

@Test def void testTestblock() {
    assertFormatted[
        toBeFormatted = '''
            project="proj";plcname="name";author="Bob"; 
            testcase One {testActive = true ; blockType = FC ;    
            description = "string" ;    }
        '''

        expectation = '''
            project = "proj";
            plcname = "name";
            author = "Bob";
            testcase One {
                testActive = true;
                blockType = FC;
                description = "string";
            }
        '''
    ]
}

The bug even occurs if I comment out my code in the formatter class which extends AbstractFormatter2, so I'll omit that here.


Solution

  • this sounds like a bug to me. please report it at https://github.com/eclipse/xtext-core

    workaround:

    BoolConstant:
        value=BooleanValue;
    BlockConstant:
        value=BlockValue;
    BlockValue:"FC"|"FB";
    BooleanValue: "true"|"false";