Apache OpenOffice (AOO) Bugzilla – Issue 126272
OpenOffice.org Basic compile error when comment is at the end of a single line if then else statement
Last modified: 2023-02-08 22:01:28 UTC
' This will not cause an Syntax-Err If A > 3 Then B = 2 Else B = 0 ' This will not cause an Syntax-Err If A > 3 Then B = 2 Else B = 0 ' This one cause Syntax-ERR
Function Foo(A as Integer) As Integer Dim B as Integer If A > 0 Then ' REM 1 B = 2 ' REM 2 If A > 2 Then B = 3 Else B = 0 ' REM 3 => syntax err End If Foo = B End Function Function Foo2(A as Integer) As Integer Dim B as Integer If A > 0 Then ' REM 1 B = 2 ' REM 2 If A > 2 Then B = 3 Else B = 0 ' REM 3 => NO syntax err End If Foo2 = B End Function
Confirmed. The syntax analyzer is disturbed by any comment at right of a line containing if ... then ... else ... The comment may be a single apostrophe or a single REM Further examples: >> this throws Unexpected symbol : End Sub Sub Main for a = -5 to 3 if a > 1 then b = 0 else b = 2 REM next End Sub >> this throws Unexpected symbol : Until Sub Main1 a= 0 Do a = a+1 if a > 1 then b = 0 else b = 2 rem Loop Until a > 3 End Sub Workaround : do not add a comment on a line "if...then...else..."
The comment isn't the problem. As I know only oneliners with only one statement is allowed. legal: If <condition> Then <statement> illegal: If <condition> Then <statement> Else <statement> Modify Foo() to Function Foo(A as Integer) As Integer Dim B as Integer If A > 0 Then 'comment B = 2 'comment If A > 2 Then B = 3 Else B = 0 'comment MsgBox( "A" ) 'comment MsgBox( "B" ) 'comment MsgBox( "C" ) 'comment MsgBox( "D" ) End If Foo = B End Function Then you will see that the MsgBoxes will not be reached for A>2. A comment after MsgBox ( "D" ) will also cause the error because the nested If statement is unclosed. IMHO this isn't an issue, it's an illegal oneliner.
It seems <comment><newline> is treated like a colon. Sub Foo1( A as Integer ) If A > 2 Then MsgBox( 0 ) Else MsgBox( 1 ) 'comment MsgBox( 2 ) 'comment MsgBox( 3 ) End Sub Sub Foo2( A as Integer ) If A > 2 Then MsgBox( 0 ) Else MsgBox( 1 ) : MsgBox( 2 ) : MsgBox( 3 ) End Sub Foo1 and Foo2 gives the same result. But nevertheless the parser should give an error if /If/ and /Else/ in one line or it should simply stop at the comment.
mroe said : illegal: If <condition> Then <statement> Else <statement> the parser should give an error if /If/ and /Else/ in one line or it should simply stop at the comment. Look at Microsoft VBA doc on IF statement: https://msdn.microsoft.com/en-us/library/gg251599%28v=office.14%29.aspx The single-line form is accepted for if-then-else : "You can use the single-line form (first syntax) for short, simple tests. " This single form is widely used in Basic macros, it must be kept for compatibility. It is not described in the F1 help, but appears in the Wiki Basic Programming Guide. The statement works correctly if there is no comment in the same line. This restriction is minor, if you know it.
The Basic parser was only allowing Else to end in EOL, not in a comment. I've fixed that in r1720205, so resolving fixed.
I've updated the subject to make it clear what the problem was
Cherry-picked for AOO42X with commit 3b5ec47b15ab141460e2a7c60e19e9a0616b9d3d.
This was fixed and released with AOO 4.1.13: https://github.com/apache/openoffice/commit/4dcbef3e4f42e9515ba2313db12f4aecd595a57f I think Damjan cherry-picked the missing test for AOO42X?
(In reply to Matthias Seidel from comment #9) > This was fixed and released with AOO 4.1.13: > > https://github.com/apache/openoffice/commit/ > 4dcbef3e4f42e9515ba2313db12f4aecd595a57f > > I think Damjan cherry-picked the missing test for AOO42X? Yes he did, i saw the conversation later and was coming back to reverse this. Thank you for doing it. Sorry for the noise.
No problem! I wasn't sure myself, so I better looked it up.