From hb at maths.lth.se  Mon Aug 15 13:44:02 2005
From: hb at maths.lth.se (Henrik Bengtsson)
Date: Mon, 15 Aug 2005 13:44:02 +0200
Subject: [Rd] parse():ing a unclosed string
In-Reply-To: <Pine.LNX.4.61.0508141303280.32176@gannet.stats>
References: <42FF11F4.4070802@maths.lth.se>
	<Pine.LNX.4.61.0508141303280.32176@gannet.stats>
Message-ID: <43008002.7060508@maths.lth.se>

Prof Brian Ripley wrote:
> On Sun, 14 Aug 2005, Henrik Bengtsson wrote:
> 
>> When parse():ing R code from *file* with a unclosed string, that is, a
>> string that has an open quoation mark, but not a ending one, the string
>> seems to be closed automagically.  Is this a "bug"?
> 
> 
> It's clearly intentional from the comments in the sources: EOF during 
> parsing is treated specially.
> 
> Generally the parser has not been written to find all syntactic errors: 
> in any case one would need a formal language definition to do that.  
> Prior to R-devel quite a few mal-formed constants were accepted, for 
> example.
> 
>> Example:
>>
>> > code <- "x <- '123";
>> > parse(text=code)           # Gives an error as expected
>> Error in parse(file, n, text, prompt) : parse error
>>
>> > cat(file="foo.R", code)
> 
> 
> What exactly is in this file?

I'm write the contents of character variable 'code' to file 'foo.R', 
that is, I'm generating a file containing the nine characters "x <- 
'123", then parsing it below.

> 
>> > expr <- parse("foo.R")     # Closes the open string
>> > print(expr)
>> expression(x <- "123\n")
>>
>> Parsing from stdin(), that is parse(), gives an error too.
> 
> 
> Those are _not_ the same thing.  I am not clear what you actually tried, 
> as you cannot generate an EOF this way.

 > parse()
?x <- '123
Error in parse(file, n, text, prompt) : parse error
 >

where press ENTER directly after "123". I'm not exactly sure what is 
happen when I press enter and when or if a EOF is generated, but this 
was the only test I did for stdin().

> 
>> Platform: Windows XP Pro SP2, R Version 2.1.1 Patched (2005-07-20) as
>> well as R Version 2.2.0 Under development (unstable) (2005-07-19 r34991).
> 
> 
> These are rather old for non-released versions: there have been many 
> changes since then (especially in R-devel).

I know, but behind a 56k-modem (again) ;( and though was better to 
report anything than nothing.

/Henrik



[Rd] parse():ing a unclosed string
Henrik Bengtsson