Check if String is a Valid S-PLUS Expression

DESCRIPTION:

Tells if a character string is a valid S-PLUS expression, if it could be one if extended, or if it contains a syntax error. Also tells how many characters were read to come to that conclusion and how many valid expressions were found.

USAGE:

parse.test(string, n.expr=1)

REQUIRED ARGUMENTS:

string
a single character string containing what might be a S-PLUS expression. Note that it must end with a new line or semicolon to be considered a complete expression.

OPTIONAL ARGUMENTS:

n.expr
the maximum number of expressions to look for. If -1, keep looking until the end of the string or the first error is found.

VALUE:

The main return value is a character string, one of "complete", "incomplete", or "error". "complete" means that the string may be passed to 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).

DETAILS:

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.

SEE ALSO:

, , .

EXAMPLES:

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