The R Project SVN R

Rev

Rev 88591 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/parse.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
82218 kalibera 3
% Copyright 1995-2022 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{parse}
76343 maechler 7
\title{Parse R Expressions}
56186 murdoch 8
\alias{parse}
76343 maechler 9
\alias{str2lang}
10
\alias{str2expression}
24641 hornik 11
\description{
76343 maechler 12
  \code{parse()} returns the parsed but unevaluated expressions in an
13
  \code{\link{expression}}, a \dQuote{list} of \code{\link{call}}s.
14
 
15
  \code{str2expression(s)} and \code{str2lang(s)} return special versions
16
  of \code{parse(text=s, keep.source=FALSE)} and can therefore be regarded as
17
  transforming character strings \code{s} to expressions, calls, etc.
24641 hornik 18
}
2 r 19
\usage{
61650 murdoch 20
parse(file = "", n = NULL, text = NULL, prompt = "?",
21
      keep.source = getOption("keep.source"), srcfile,
40672 ripley 22
      encoding = "unknown")
76343 maechler 23
 
24
str2lang(s)
25
str2expression(text)
2 r 26
}
27
\arguments{
54236 ripley 28
  \item{file}{a \link{connection}, or a character string giving the name of a
13983 ripley 29
    file or a URL to read the expressions from.
2381 maechler 30
    If \code{file} is \code{""} and \code{text} is missing or \code{NULL}
11656 ripley 31
    then input is taken from the console.}
40019 ripley 32
  \item{n}{integer (or coerced to integer).  The maximum number of
40017 ripley 33
    expressions to parse.  If \code{n} is \code{NULL} or negative or
34
    \code{NA} the input is parsed in its entirety.}
38728 ripley 35
  \item{text}{character vector.  The text to parse.  Elements are treated
40017 ripley 36
    as if they were lines of a file.  Other \R objects will be coerced
51330 ripley 37
    to character if possible.}
15561 ripley 38
  \item{prompt}{the prompt to print when parsing from the keyboard.
24641 hornik 39
    \code{NULL} means to use \R's prompt, \code{getOption("prompt")}.}
75050 maechler 40
  \item{keep.source}{a logical value; if \code{TRUE}, keep
61650 murdoch 41
    source reference information.}
61433 ripley 42
  \item{srcfile}{\code{NULL}, a character vector, or a
61399 murdoch 43
    \code{\link{srcfile}} object.  See the \sQuote{Details} section.}
46855 ripley 44
  \item{encoding}{encoding to be assumed for input strings.  If the
45
    value is \code{"latin1"} or \code{"UTF-8"} it is used to mark
46
    character strings as known to be in Latin-1 or UTF-8: it is not used
47
    to re-encode the input.  To do the latter, specify the encoding as
48
    part of the connection \code{con} or \emph{via}
49
    \code{\link{options}(encoding=)}: see the example under
88479 hornik 50
    \code{\link{file}}.
51
    Arguments \code{encoding = "UTF-8"} and \code{encoding = "latin1"} 
52
    are ignored with a warning when running, respectively, in a
53
    non-UTF-8 \abbr{MBCS} locale or in an \abbr{MBCS} locale.}
76343 maechler 54
  \item{s}{a \code{\link{character}} vector of length \code{1}, i.e., a
55
    \dQuote{string}.}
2 r 56
}
24300 ripley 57
\references{
88591 hornik 58
  \bibshow{R:Becker+Chambers+Wilks:1988, R:Murdoch:2010}
24300 ripley 59
}
2 r 60
\seealso{
48 hornik 61
  \code{\link{scan}}, \code{\link{source}}, \code{\link{eval}},
62
  \code{\link{deparse}}.
61664 murdoch 63
 
69446 ripley 64
  The source reference information can be used for debugging (see
65
  e.g.\sspace{}\code{\link{setBreakpoint}}) and profiling (see
76343 maechler 66
  \code{\link{Rprof}}).  It can be examined by \code{\link{getSrcref}}
61664 murdoch 67
  and related functions.  More detailed information is available through
69446 ripley 68
  \code{\link{getParseData}}.
2 r 69
}
12256 pd 70
\details{
76343 maechler 71
  \describe{
72
    \item{\code{parse(....)}: }{
73
 
40019 ripley 74
  If \code{text} has length greater than zero (after coercion) it is used in
40017 ripley 75
  preference to \code{file}.
61433 ripley 76
 
13983 ripley 77
  All versions of \R accept input from a connection with end of line
85925 hornik 78
  marked by \abbr{LF} (as used on Unix), \abbr{CRLF} (as used on DOS/Windows)
79
  or \abbr{CR} (as used on classic Mac OS).  The final line can be incomplete,
85550 hornik 80
  that is missing the final \abbr{EOL} marker.
21139 ripley 81
 
38728 ripley 82
  When input is taken from the console, \code{n = NULL} is equivalent to
85550 hornik 83
  \code{n = 1}, and \code{n < 0} will read until an \abbr{EOF} character is
85908 hornik 84
  read.  (The \abbr{EOF} character is \abbr{Ctrl}-Z for the Windows front-ends.)  The
50623 ripley 85
  line-length limit is 4095 bytes when reading from the console (which
88891 smeyer 86
  may impose a lower limit).% see \manual{R-intro}{}
40261 murdoch 87
 
88
  The default for \code{srcfile} is set as follows.  If
61650 murdoch 89
  \code{keep.source} is not \code{TRUE}, \code{srcfile}
61399 murdoch 90
  defaults to a character string, either \code{"<text>"} or one
61650 murdoch 91
  derived from \code{file}.  When \code{keep.source} is
61399 murdoch 92
  \code{TRUE}, if \code{text} is used, \code{srcfile} will be set to a
93
  \code{\link{srcfilecopy}} containing the text.  If a character
94
  string is used for \code{file}, a \code{\link{srcfile}} object
95
  referring to that file will be used.
61433 ripley 96
 
61399 murdoch 97
  When \code{srcfile} is a character string, error messages will
61664 murdoch 98
  include the name, but source reference information will not be added
99
  to the result.  When \code{srcfile} is a \code{\link{srcfile}}
75050 maechler 100
  object, source reference information will be retained.
76343 maechler 101
  }
102
    \item{\code{str2expression(s)}: }{for a \code{\link{character}} vector
103
      \code{s}, \code{str2expression(s)} corresponds to
87653 smeyer 104
      \code{parse(text = s, keep.source=FALSE)}, which is always of
76343 maechler 105
      type (\code{\link{typeof}}) and \code{\link{class}} \code{expression}.
106
    }
107
    \item{\code{str2lang(s)}: }{for a \code{\link{character}} string
108
      \code{s}, \code{str2lang(s)} corresponds to
87653 smeyer 109
      \code{parse(text = s, keep.source=FALSE)[[1]]} (plus a check
76343 maechler 110
      that both \code{s} and the \code{parse(*)} result are of length one)
111
      which is typically a \code{call} but may also be a \code{symbol} aka
112
      \code{\link{name}}, \code{\link{NULL}} or an atomic constant such as
113
      \code{2}, \code{1L}, or \code{TRUE}.  Put differently, the value of
114
      \code{str2lang(.)} is a call or one of its parts, in short
115
      \dQuote{a call or simpler}.
116
    }
117
  }% describe
118
  Currently, encoding is not handled in \code{str2lang()} and
119
  \code{str2expression()}.
12256 pd 120
}
63371 murdoch 121
\section{Partial parsing}{
75050 maechler 122
  When a syntax error occurs during parsing, \code{parse}
63371 murdoch 123
  signals an error.  The partial parse data will be stored in the
124
  \code{srcfile} argument if it is a \code{\link{srcfile}} object
125
  and the \code{text} argument was used to supply the text.  In other
126
  cases it will be lost when the error is triggered.
75050 maechler 127
 
63371 murdoch 128
  The partial parse data can be retrieved using
129
  \code{\link{getParseData}} applied to the \code{srcfile} object.
130
  Because parsing was incomplete, it will typically include references
75050 maechler 131
  to \code{"parent"} entries that are not present.
63371 murdoch 132
}
38728 ripley 133
\value{
76343 maechler 134
  \code{parse()} and \code{str2expression()} return an object of type
135
  \code{"\link{expression}"}, for \code{parse()} with up to \code{n}
38738 ripley 136
  elements if specified as a non-negative integer.
137
 
76343 maechler 138
  \code{str2lang(s)}, \code{s} a string, returns \dQuote{a
139
  \code{\link{call}} or simpler}, see the \sQuote{Details:} section.
140
 
40261 murdoch 141
  When \code{srcfile} is non-\code{NULL}, a \code{"srcref"} attribute
61433 ripley 142
  will be attached to the result containing a list of
143
  \code{\link{srcref}} records corresponding to each element, a
51758 murdoch 144
  \code{"srcfile"} attribute will be attached containing a copy of
145
  \code{srcfile}, and a \code{"wholeSrcref"} attribute will be
146
  attached containing a \code{\link{srcref}} record corresponding to
59883 murdoch 147
  all of the parsed text. Detailed parse information will be stored in
61433 ripley 148
  the \code{"srcfile"} attribute, to be retrieved by
59883 murdoch 149
  \code{\link{getParseData}}.
61433 ripley 150
 
38738 ripley 151
  A syntax error (including an incomplete expression) will throw an error.
40696 ripley 152
 
153
  Character strings in the result will have a declared encoding if
46864 ripley 154
  \code{encoding} is \code{"latin1"} or \code{"UTF-8"}, or if
155
  \code{text} is supplied with every element of known encoding in a
156
  Latin-1 or UTF-8 locale.
38728 ripley 157
}
76343 maechler 158
\note{
159
  Using \code{parse(text = *, ..)} or its simplified and hence more
160
  efficient versions \code{str2lang()} or \code{str2expression()} is at
161
  least an order of magnitude less efficient than \code{\link{call}(..)} or
162
  \code{\link{as.call}()}.
163
}
2 r 164
\examples{
74546 maechler 165
fil <- tempfile(fileext = ".Rdmped")
74519 maechler 166
cat("x <- c(1, 4)\n  x ^ 3 -10 ; outer(1:7, 5:9)\n", file = fil)
167
# parse 3 statements from our temp file
168
parse(file = fil, n = 3)
169
unlink(fil)
63371 murdoch 170
 
76343 maechler 171
## str2lang(<string>)  || str2expression(<character>) :
172
stopifnot(exprs = {
173
  identical( str2lang("x[3] <- 1+4"), quote(x[3] <- 1+4))
174
  identical( str2lang("log(y)"),      quote(log(y)) )
175
  identical( str2lang("abc"   ),      quote(abc) -> qa)
176
  is.symbol(qa) & !is.call(qa)           # a symbol/name, not a call
177
  identical( str2lang("1.375" ), 1.375)  # just a number, not a call
79005 kalibera 178
  identical( str2expression(c("# a comment", "", "42")), expression(42) )
76343 maechler 179
})
180
 
63371 murdoch 181
# A partial parse with a syntax error
182
txt <- "
183
x <- 1
184
an error
185
"
186
sf <- srcfile("txt")
82218 kalibera 187
tryCatch(parse(text = txt, srcfile = sf), error = function(e) "Syntax error.")
63371 murdoch 188
getParseData(sf)
2 r 189
}
286 maechler 190
\keyword{file}
191
\keyword{programming}
11656 ripley 192
\keyword{connection}