Skip to content
Back to formatted view

Raw Message

Message-ID: <4065c171-a6a9-49a4-9bad-e78adf1d9db6@gmail.com>
Date: 2024-12-02T16:18:44Z
From: Mikael Jagan
Subject: 'else' on new line not parsed at top level as part of preceding 'if'
In-Reply-To: <e6fc0e66-08d1-4581-8de6-930ef24d4ffb@gmail.com>

Thanks - that makes sense.  It's also documented in help("if"), so it's
doubly clear now that I did not do my homework here.

Mikael

On 2024-12-02 11:09 am, Duncan Murdoch wrote:
> BTW, this is discussed in section 3.2.1 of "The R Language Definition".
> 
> Duncan Murdoch
> 
> On 2024-12-02 10:56 a.m., Mikael Jagan wrote:
>> I wonder if there is room for improvement here:
>>
>>        > str2lang("if (a) b1    else b0")
>>        if (a) b1 else b0
>>        > str2lang("if (a) b1 \n else b0")
>>        Error in str2lang("if (a) b1 \n else b0") : <text>:2:2: unexpected 'else'
>>        1: if (a) b1
>>        2:  else
>>            ^
>>
>> It only occurs at top level.  When the parser knows that it needs to
>> "keep reading", e.g., because the call to 'if' is enclosed in braces,
>> it detects the 'else':
>>
>>        > str2lang("{ if (a) b1    else b0 }")
>>        {
>>            if (a)
>>                b1
>>            else b0
>>        }
>>        > str2lang("{ if (a) b1 \n else b0 }")
>>        {
>>            if (a)
>>                b1
>>            else b0
>>        }
>>
>> If people agree that the above is a deficiency/bug then I could create a
>> formal bug report.  Then others or I could start thinking about a patch
>> for the parser.
>>
>> Apologies if this issue has been seen and discussed before (most likely
>> by people like me in the habit of dropping braces in calls to 'if' where
>> not strictly necessary).
>>
>> Mikael
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>