parse.test(string, n.expr=1)
parse()
(or supplied as keyboard input to S-PLUS)
and S-PLUS will be able to parse it.
"incomplete" means that by adding more text to the end of the string
S-PLUS might be able to parse it
(that is, if typed at the command line,
S-PLUS would issue a continuation prompt to ask you to add some more text,
as for "1+", which requires a second addend and a new line).
"error" means that there is an error in the string,
such as too many right parentheses,
that cannot be corrected by adding more text.
In the last case,
parse.test
will not tell you
the nature of the error;
you must call
parse
for that.
If you specified n.expr>1,
then this error code refers to the last expression dealt with
(all expressions before that one must be complete,
because
parse.test
stops at the first
bad expression).
In addition the output has two attributes,
chars.parsed
and
n.expr
to help you run down a string picking off one (or more) expressions
at a time.
chars.parsed
tells how many characters
were read to come to the conclusion that the string was complete,
incomplete, or erroneous.
You may call
substring(first=chars.parsed+1,...)
to get the remainder of the string to pass to
parse.test
again.
n.expr
tells how many
expressions were found
(it will not be greater than the input
n.expr
,
except when the input was -1,
which means to look for expressions until end of string or error).
Strings passed to
parse
get new lines
appended to them
but strings passed to
parse.test
do not,
hence a string considered "incomplete"
by
parse.test
may be considered fine
by
parse
.
parse.test("lm(y~x, data = mydata)\\n") parse.test("lm(y~x, data = mydata)") # no new line or semicolon at end a <- "p++;1+10\n" # 2 expression, first has error (++ is not legal) parse.test(a) parse.test(substring(a,5)) # start looking where last try left off