The R Project SVN R

Rev

Rev 29864 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 29864 Rev 29865
Line 38... Line 38...
38
 .Internal( parse(file, n, text, prompt) )
38
 .Internal( parse(file, n, text, prompt) )
39
 If there is text then that is read and the other arguments are ignored.
39
 If there is text then that is read and the other arguments are ignored.
40
*/
40
*/
41
SEXP do_parse(SEXP call, SEXP op, SEXP args, SEXP env)
41
SEXP do_parse(SEXP call, SEXP op, SEXP args, SEXP env)
42
{
42
{
43
    SEXP text, prompt, s;
43
    SEXP text, prompt, s, source;
44
    Rconnection con;
44
    Rconnection con;
45
    Rboolean wasopen;
45
    Rboolean wasopen;
46
    int ifile, num;
46
    int ifile, num;
47
    ParseStatus status;
47
    ParseStatus status;
48
 
48
 
Line 55... Line 55...
55
    con = getConnection(ifile);
55
    con = getConnection(ifile);
56
    wasopen = con->isopen;
56
    wasopen = con->isopen;
57
    num = asInteger(CAR(args));				args = CDR(args);
57
    num = asInteger(CAR(args));				args = CDR(args);
58
    PROTECT(text = coerceVector(CAR(args), STRSXP));	args = CDR(args);
58
    PROTECT(text = coerceVector(CAR(args), STRSXP));	args = CDR(args);
59
    prompt = CAR(args);					args = CDR(args);
59
    prompt = CAR(args);					args = CDR(args);
-
 
60
    source = CAR(args);					args = CDR(args);
-
 
61
 
60
    if (prompt == R_NilValue)
62
    if (prompt == R_NilValue)
61
	PROTECT(prompt);
63
	PROTECT(prompt);
62
    else
64
    else
63
	PROTECT(prompt = coerceVector(prompt, STRSXP));
65
	PROTECT(prompt = coerceVector(prompt, STRSXP));
64
 
66
 
-
 
67
    if (isLogical(source) && length(source) && LOGICAL(source)[0])
-
 
68
    	KeepAllSource = TRUE;
-
 
69
 
65
    if (length(text) > 0) {
70
    if (length(text) > 0) {
66
	if (num == NA_INTEGER)
71
	if (num == NA_INTEGER)
67
	    num = -1;
72
	    num = -1;
68
	s = R_ParseVector(text, num, &status);
73
	s = R_ParseVector(text, num, &status);
69
	if (status != PARSE_OK)
74
	if (status != PARSE_OK)
Line 84... Line 89...
84
	    num = 1;
89
	    num = 1;
85
	s = R_ParseBuffer(&R_ConsoleIob, num, &status, prompt);
90
	s = R_ParseBuffer(&R_ConsoleIob, num, &status, prompt);
86
	if (status != PARSE_OK)
91
	if (status != PARSE_OK)
87
	    errorcall(call, "parse error");
92
	    errorcall(call, "parse error");
88
    }
93
    }
-
 
94
 
89
    UNPROTECT(2);
95
    UNPROTECT(2);
-
 
96
    KeepAllSource = FALSE;
90
    return s;
97
    return s;
91
}
98
}
92
 
99
 
-
 
100
/* return a protected STRSXP containing source lines from the buffer,
-
 
101
   assumes that end points to the character following the desired source,
-
 
102
   possibly null, but temporarily writeable */
93
 
103
 
-
 
104
SEXP collectLines(char *buffer, char *end)
-
 
105
{
-
 
106
    int lines = 0;
-
 
107
    char *p, *p0, save;
-
 
108
    SEXP source;
-
 
109
 
-
 
110
    for (p = buffer; p < end ; p++)
-
 
111
	if (*p == '\n') lines++;
-
 
112
    if ( *(end - 1) != '\n' ) lines++;
-
 
113
    PROTECT(source = allocVector(STRSXP, lines));
-
 
114
    p0 = buffer;
-
 
115
    lines = 0;
-
 
116
    for (p = buffer ; p < end ; p++)
-
 
117
	if ( *p == '\n' ) {
-
 
118
	    save = *p;
-
 
119
	    *p = '\0';
-
 
120
	    SET_STRING_ELT(source, lines++, mkChar(p0));
-
 
121
	    *p = save;
-
 
122
	    p0 = p + 1;
-
 
123
	}
-
 
124
    if ( *(end - 1) != '\n' ) {
-
 
125
	save = *end;
-
 
126
	*end = '\0';
-
 
127
	SET_STRING_ELT(source, lines++, mkChar(p0));
-
 
128
	*end = save;
-
 
129
    }
-
 
130
    return(source);
-
 
131
}
-
 
132
 
-
 
133
SEXP convertSource(SEXP e)
-
 
134
{
-
 
135
    SEXP source = getAttrib(e, R_SourceSymbol);
-
 
136
 
-
 
137
    if (!isNull(source) && isInteger(source) && length(source) ==  2) {
-
 
138
	source = collectLines(CHAR(SavedSource)+INTEGER(source)[0], CHAR(SavedSource)+INTEGER(source)[1]);
-
 
139
	setAttrib(e, R_SourceSymbol, source);
-
 
140
	UNPROTECT(1);
-
 
141
    }
-
 
142
    return(e);
-
 
143
}