Issue 126272 - OpenOffice.org Basic compile error when comment is at the end of a single line if then else statement
Summary: OpenOffice.org Basic compile error when comment is at the end of a single lin...
Status: RESOLVED FIXED
Alias: None
Product: General
Classification: Code
Component: code (show other issues)
Version: 4.1.1
Hardware: All All
: P5 (lowest) Normal (vote)
Target Milestone: 4.1.13
Assignee: AOO issues mailing list
QA Contact:
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-04-27 00:09 UTC by E.Zido
Modified: 2023-02-08 22:01 UTC (History)
5 users (show)

See Also:
Issue Type: DEFECT
Latest Confirmation in: 4.2.0-dev
Developer Difficulty: ---


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description E.Zido 2015-04-27 00:09:27 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
Comment 1 E.Zido 2015-04-27 00:55:41 UTC
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
Comment 2 bmarcelly 2015-04-30 07:38:52 UTC
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..."
Comment 3 mroe 2015-04-30 09:18:26 UTC
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.
Comment 4 mroe 2015-04-30 10:35:41 UTC
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.
Comment 5 bmarcelly 2015-05-01 07:44:44 UTC
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.
Comment 6 damjan 2015-12-15 17:34:38 UTC
The Basic parser was only allowing Else to end in EOL, not in a comment. I've fixed that in r1720205, so resolving fixed.
Comment 7 Carl Marcum 2022-06-08 21:18:19 UTC
I've updated the subject to make it clear what the problem was
Comment 8 damjan 2023-02-08 18:05:37 UTC
Cherry-picked for AOO42X with commit 3b5ec47b15ab141460e2a7c60e19e9a0616b9d3d.
Comment 9 Matthias Seidel 2023-02-08 21:37:19 UTC
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?
Comment 10 Keith N. McKenna 2023-02-08 21:49:25 UTC
(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.
Comment 11 Matthias Seidel 2023-02-08 22:01:28 UTC
No problem!

I wasn't sure myself, so I better looked it up.