The R Project SVN R

Rev

Rev 75050 | Rev 79005 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 75050 Rev 76343
Line 1... Line 1...
1
% File src/library/base/man/parse.Rd
1
% File src/library/base/man/parse.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2018 R Core Team
3
% Copyright 1995-2019 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{parse}
6
\name{parse}
-
 
7
\title{Parse R Expressions}
7
\alias{parse}
8
\alias{parse}
-
 
9
\alias{str2lang}
8
\title{Parse Expressions}
10
\alias{str2expression}
9
\description{
11
\description{
10
  \code{parse} returns the parsed but unevaluated expressions in a
12
  \code{parse()} returns the parsed but unevaluated expressions in an
-
 
13
  \code{\link{expression}}, a \dQuote{list} of \code{\link{call}}s.
11
  list.
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.
12
}
18
}
13
\usage{
19
\usage{
14
parse(file = "", n = NULL, text = NULL, prompt = "?",
20
parse(file = "", n = NULL, text = NULL, prompt = "?",
15
      keep.source = getOption("keep.source"), srcfile,
21
      keep.source = getOption("keep.source"), srcfile,
16
      encoding = "unknown")
22
      encoding = "unknown")
-
 
23
 
-
 
24
str2lang(s)
-
 
25
str2expression(text)
17
}
26
}
18
\arguments{
27
\arguments{
19
  \item{file}{a \link{connection}, or a character string giving the name of a
28
  \item{file}{a \link{connection}, or a character string giving the name of a
20
    file or a URL to read the expressions from.
29
    file or a URL to read the expressions from.
21
    If \code{file} is \code{""} and \code{text} is missing or \code{NULL}
30
    If \code{file} is \code{""} and \code{text} is missing or \code{NULL}
Line 37... Line 46...
37
    character strings as known to be in Latin-1 or UTF-8: it is not used
46
    character strings as known to be in Latin-1 or UTF-8: it is not used
38
    to re-encode the input.  To do the latter, specify the encoding as
47
    to re-encode the input.  To do the latter, specify the encoding as
39
    part of the connection \code{con} or \emph{via}
48
    part of the connection \code{con} or \emph{via}
40
    \code{\link{options}(encoding=)}: see the example under
49
    \code{\link{options}(encoding=)}: see the example under
41
    \code{\link{file}}.}
50
    \code{\link{file}}.}
-
 
51
  \item{s}{a \code{\link{character}} vector of length \code{1}, i.e., a
-
 
52
    \dQuote{string}.}
42
}
53
}
43
\references{
54
\references{
44
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
55
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
45
  \emph{The New S Language}.
56
  \emph{The New S Language}.
46
  Wadsworth & Brooks/Cole.
57
  Wadsworth & Brooks/Cole.
Line 53... Line 64...
53
  \code{\link{scan}}, \code{\link{source}}, \code{\link{eval}},
64
  \code{\link{scan}}, \code{\link{source}}, \code{\link{eval}},
54
  \code{\link{deparse}}.
65
  \code{\link{deparse}}.
55
 
66
 
56
  The source reference information can be used for debugging (see
67
  The source reference information can be used for debugging (see
57
  e.g.\sspace{}\code{\link{setBreakpoint}}) and profiling (see
68
  e.g.\sspace{}\code{\link{setBreakpoint}}) and profiling (see
58
  \code{\link{Rprof}}). It can be examined by \code{\link{getSrcref}}
69
  \code{\link{Rprof}}).  It can be examined by \code{\link{getSrcref}}
59
  and related functions.  More detailed information is available through
70
  and related functions.  More detailed information is available through
60
  \code{\link{getParseData}}.
71
  \code{\link{getParseData}}.
61
}
72
}
62
\details{
73
\details{
-
 
74
  \describe{
-
 
75
    \item{\code{parse(....)}: }{
-
 
76
 
63
  If \code{text} has length greater than zero (after coercion) it is used in
77
  If \code{text} has length greater than zero (after coercion) it is used in
64
  preference to \code{file}.
78
  preference to \code{file}.
65
 
79
 
66
  All versions of \R accept input from a connection with end of line
80
  All versions of \R accept input from a connection with end of line
67
  marked by LF (as used on Unix), CRLF (as used on DOS/Windows)
81
  marked by LF (as used on Unix), CRLF (as used on DOS/Windows)
Line 85... Line 99...
85
 
99
 
86
  When \code{srcfile} is a character string, error messages will
100
  When \code{srcfile} is a character string, error messages will
87
  include the name, but source reference information will not be added
101
  include the name, but source reference information will not be added
88
  to the result.  When \code{srcfile} is a \code{\link{srcfile}}
102
  to the result.  When \code{srcfile} is a \code{\link{srcfile}}
89
  object, source reference information will be retained.
103
  object, source reference information will be retained.
-
 
104
  }
-
 
105
    \item{\code{str2expression(s)}: }{for a \code{\link{character}} vector
-
 
106
      \code{s}, \code{str2expression(s)} corresponds to
-
 
107
      \code{\link{parse}(text = s, keep.source=FALSE)}, which is always of
-
 
108
      type (\code{\link{typeof}}) and \code{\link{class}} \code{expression}.
-
 
109
    }
-
 
110
    \item{\code{str2lang(s)}: }{for a \code{\link{character}} string
-
 
111
      \code{s}, \code{str2lang(s)} corresponds to
-
 
112
      \code{\link{parse}(text = s, keep.source=FALSE)[[1]]} (plus a check
-
 
113
      that both \code{s} and the \code{parse(*)} result are of length one)
-
 
114
      which is typically a \code{call} but may also be a \code{symbol} aka
-
 
115
      \code{\link{name}}, \code{\link{NULL}} or an atomic constant such as
-
 
116
      \code{2}, \code{1L}, or \code{TRUE}.  Put differently, the value of
-
 
117
      \code{str2lang(.)} is a call or one of its parts, in short
-
 
118
      \dQuote{a call or simpler}.
-
 
119
    }
-
 
120
  }% describe
-
 
121
  Currently, encoding is not handled in \code{str2lang()} and
-
 
122
  \code{str2expression()}.
90
}
123
}
91
\section{Partial parsing}{
124
\section{Partial parsing}{
92
  When a syntax error occurs during parsing, \code{parse}
125
  When a syntax error occurs during parsing, \code{parse}
93
  signals an error.  The partial parse data will be stored in the
126
  signals an error.  The partial parse data will be stored in the
94
  \code{srcfile} argument if it is a \code{\link{srcfile}} object
127
  \code{srcfile} argument if it is a \code{\link{srcfile}} object
Line 99... Line 132...
99
  \code{\link{getParseData}} applied to the \code{srcfile} object.
132
  \code{\link{getParseData}} applied to the \code{srcfile} object.
100
  Because parsing was incomplete, it will typically include references
133
  Because parsing was incomplete, it will typically include references
101
  to \code{"parent"} entries that are not present.
134
  to \code{"parent"} entries that are not present.
102
}
135
}
103
\value{
136
\value{
-
 
137
  \code{parse()} and \code{str2expression()} return an object of type
104
  An object of type \code{"\link{expression}"}, with up to \code{n}
138
  \code{"\link{expression}"}, for \code{parse()} with up to \code{n}
105
  elements if specified as a non-negative integer.
139
  elements if specified as a non-negative integer.
106
 
140
 
-
 
141
  \code{str2lang(s)}, \code{s} a string, returns \dQuote{a
-
 
142
  \code{\link{call}} or simpler}, see the \sQuote{Details:} section.
-
 
143
 
107
  When \code{srcfile} is non-\code{NULL}, a \code{"srcref"} attribute
144
  When \code{srcfile} is non-\code{NULL}, a \code{"srcref"} attribute
108
  will be attached to the result containing a list of
145
  will be attached to the result containing a list of
109
  \code{\link{srcref}} records corresponding to each element, a
146
  \code{\link{srcref}} records corresponding to each element, a
110
  \code{"srcfile"} attribute will be attached containing a copy of
147
  \code{"srcfile"} attribute will be attached containing a copy of
111
  \code{srcfile}, and a \code{"wholeSrcref"} attribute will be
148
  \code{srcfile}, and a \code{"wholeSrcref"} attribute will be
Line 119... Line 156...
119
  Character strings in the result will have a declared encoding if
156
  Character strings in the result will have a declared encoding if
120
  \code{encoding} is \code{"latin1"} or \code{"UTF-8"}, or if
157
  \code{encoding} is \code{"latin1"} or \code{"UTF-8"}, or if
121
  \code{text} is supplied with every element of known encoding in a
158
  \code{text} is supplied with every element of known encoding in a
122
  Latin-1 or UTF-8 locale.
159
  Latin-1 or UTF-8 locale.
123
}
160
}
-
 
161
\note{
-
 
162
  Using \code{parse(text = *, ..)} or its simplified and hence more
-
 
163
  efficient versions \code{str2lang()} or \code{str2expression()} is at
-
 
164
  least an order of magnitude less efficient than \code{\link{call}(..)} or
-
 
165
  \code{\link{as.call}()}.
124
 
166
}
125
\examples{
167
\examples{
126
fil <- tempfile(fileext = ".Rdmped")
168
fil <- tempfile(fileext = ".Rdmped")
127
cat("x <- c(1, 4)\n  x ^ 3 -10 ; outer(1:7, 5:9)\n", file = fil)
169
cat("x <- c(1, 4)\n  x ^ 3 -10 ; outer(1:7, 5:9)\n", file = fil)
128
# parse 3 statements from our temp file
170
# parse 3 statements from our temp file
129
parse(file = fil, n = 3)
171
parse(file = fil, n = 3)
130
unlink(fil)
172
unlink(fil)
131
 
173
 
-
 
174
## str2lang(<string>)  || str2expression(<character>) :
-
 
175
stopifnot(exprs = {
-
 
176
  identical( str2lang("x[3] <- 1+4"), quote(x[3] <- 1+4))
-
 
177
  identical( str2lang("log(y)"),      quote(log(y)) )
-
 
178
  identical( str2lang("abc"   ),      quote(abc) -> qa)
-
 
179
  is.symbol(qa) & !is.call(qa)           # a symbol/name, not a call
-
 
180
  identical( str2lang("1.375" ), 1.375)  # just a number, not a call
-
 
181
})
-
 
182
 
132
# A partial parse with a syntax error
183
# A partial parse with a syntax error
133
txt <- "
184
txt <- "
134
x <- 1
185
x <- 1
135
an error
186
an error
136
"
187
"