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"?
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)
> expr <- parse("foo.R") # Closes the open string
> print(expr)
expression(x <- "123\n")
Parsing from stdin(), that is parse(), gives an error too.
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).
Best
Henrik Bengtsson
parse():ing a unclosed string
3 messages · Brian Ripley, Henrik Bengtsson
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?
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.
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).
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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