Parses R code with parse(keep.source = TRUE), converts it to XML with
xmlparsedata::xml_parse_data() and reads it with xml2::read_xml(). Each
step is wrapped so malformed code yields a recoverable error rather than
aborting the scan.
Arguments
- lines
Character vector of R source, one element per line, as returned by
read_code().
Value
A list with two elements:
- tree
The
xml_documentparse tree, orNULLon failure.- error
NULLon success, or a character error message on failure.
Details
Code is taken as text, not a path, so one parser serves both an R script and
the code read_Rd_code() recovers from a help file. parse() numbers lines
from the start of what it is given, so a caller whose lines stay aligned to
the source file gets source references pointing into it directly. Both
readers keep that alignment.