comparison src/variables.cc @ 8746:5dd06f19e9be

handle commands in the lexer
author John W. Eaton <jwe@octave.org>
date Sun, 15 Feb 2009 23:49:15 -0500
parents 095ae5e0a831
children eb63fbe60fab
comparison
equal deleted inserted replaced
8745:6dc61981d18b 8746:5dd06f19e9be
88 { 88 {
89 symbol_table::clear_symbol (nm); 89 symbol_table::clear_symbol (nm);
90 } 90 }
91 91
92 // Attributes of variables and functions. 92 // Attributes of variables and functions.
93
94 // Is this a command-style function?
95
96 static std::set <std::string> command_set;
97
98 void
99 mark_as_command (const std::string& s)
100 {
101 command_set.insert (s);
102 }
103
104 static inline void
105 unmark_command (const std::string& s)
106 {
107 command_set.erase (s);
108 }
109
110 DEFCMD (mark_as_command, args, ,
111 "-*- texinfo -*-\n\
112 @deftypefn {Built-in Function} {} mark_as_command (@var{name})\n\
113 Enter @var{name} into the list of commands.\n\
114 @seealso{unmark_command, iscommand}\n\
115 @end deftypefn")
116 {
117 octave_value_list retval;
118
119 if (symbol_table::at_top_level ())
120 {
121 int nargin = args.length ();
122
123 if (nargin > 0)
124 {
125 int argc = nargin + 1;
126
127 string_vector argv = args.make_argv ("mark_as_command");
128
129 if (! error_state)
130 {
131 for (int i = 1; i < argc; i++)
132 mark_as_command (argv[i]);
133 }
134 }
135 else
136 print_usage ();
137 }
138 else
139 warning ("mark_as_command: invalid use inside function body");
140
141 return retval;
142 }
143
144 DEFCMD (unmark_command, args, ,
145 "-*- texinfo -*-\n\
146 @deftypefn {Built-in Function} {} unmark_command (@var{name})\n\
147 Remove @var{name} from the list of commands.\n\
148 @seealso{mark_as_command, iscommand}\n\
149 @end deftypefn")
150 {
151 octave_value_list retval;
152
153 if (symbol_table::at_top_level ())
154 {
155 int nargin = args.length ();
156
157 if (nargin > 0)
158 {
159 int argc = nargin + 1;
160
161 string_vector argv = args.make_argv ("unmark_command");
162
163 if (! error_state)
164 {
165 for (int i = 1; i < argc; i++)
166 unmark_command (argv[i]);
167 }
168 }
169 else
170 print_usage ();
171 }
172 else
173 warning ("mark_as_command: invalid use inside function body");
174
175 return retval;
176 }
177
178 bool
179 is_command_name (const std::string& s)
180 {
181 return command_set.find (s) != command_set.end ();
182 }
183
184
185 DEFCMD (iscommand, args, ,
186 "-*- texinfo -*-\n\
187 @deftypefn {Built-in Function} {} iscommand (@var{name})\n\
188 Return true if @var{name} is a command style function. If @var{name}\n\
189 is omitted, return a list of identifiers which are marked as commands with\n\
190 @code{mark_as_command}.\n\
191 @seealso{mark_as_command, unmark_command}\n\
192 @end deftypefn")
193 {
194 octave_value retval;
195
196 int nargin = args.length ();
197
198 if (nargin == 0)
199 {
200 string_vector lst (command_set.size ());
201
202 int i = 0;
203 for (std::set<std::string>::const_iterator p = command_set.begin ();
204 p != command_set.end (); p++)
205 lst[i++] = *p;
206
207 retval = Cell (lst.sort ());
208 }
209 else if (nargin == 1)
210 {
211 string_vector argv = args.make_argv ("iscommand");
212
213 if (! error_state)
214 {
215 std::string s = argv[1];
216 retval = is_command_name(s);
217 }
218 }
219 else
220 print_usage ();
221
222 return retval;
223 }
224
225 // Is this a raw input command?
226
227 static std::set <std::string> rawcommand_set;
228
229 void
230 mark_as_rawcommand (const std::string& s)
231 {
232 command_set.insert (s);
233 rawcommand_set.insert (s);
234 }
235
236 void
237 unmark_rawcommand (const std::string& s)
238 {
239 rawcommand_set.erase (s);
240 }
241
242 DEFCMD (mark_as_rawcommand, args, ,
243 "-*- texinfo -*-\n\
244 @deftypefn {Built-in Function} {} mark_as_rawcommand (@var{name})\n\
245 Enter @var{name} into the list of raw input commands and to the list of\n\
246 command style functions.\n\
247 Raw input commands are like normal command style functions, but they\n\
248 receive their input unprocessed (ie. strings still contain the quotes\n\
249 and escapes they had when input). However, comments and continuations\n\
250 are handled as usual, you cannot pass a token starting with a comment\n\
251 character ('#' or '%') to your function, and the last token cannot be\n\
252 a continuation token ('\\' or '...').\n\
253 @seealso{unmark_rawcommand, israwcommand, iscommand, mark_as_command}\n\
254 @end deftypefn")
255 {
256 octave_value_list retval;
257
258 if (symbol_table::at_top_level ())
259 {
260 int nargin = args.length ();
261
262 if (nargin > 0)
263 {
264 int argc = nargin + 1;
265
266 string_vector argv = args.make_argv ("mark_as_rawcommand");
267
268 if (! error_state)
269 {
270 for (int i = 1; i < argc; i++)
271 mark_as_rawcommand (argv[i]);
272 }
273 }
274 else
275 print_usage ();
276 }
277 else
278 warning ("mark_as_rawcommand: invalid use inside function body");
279
280 return retval;
281 }
282
283 DEFCMD (unmark_rawcommand, args, ,
284 "-*- texinfo -*-\n\
285 @deftypefn {Built-in Function} {} unmark_rawcommand (@var{name})\n\
286 Remove @var{name} from the list of raw input commands.\n\
287 Note that this does not remove @var{name} from the list of command style\n\
288 functions.\n\
289 @seealso{mark_as_rawcommand, israwcommand, iscommand, unmark_command}\n\
290 @end deftypefn")
291 {
292 octave_value_list retval;
293
294 if (symbol_table::at_top_level ())
295 {
296 int nargin = args.length ();
297
298 if (nargin > 0)
299 {
300 int argc = nargin + 1;
301
302 string_vector argv = args.make_argv ("unmark_rawcommand");
303
304 if (! error_state)
305 {
306 for (int i = 1; i < argc; i++)
307 unmark_rawcommand (argv[i]);
308 }
309 }
310 else
311 print_usage ();
312 }
313 else
314 warning ("unmark_rawcommand: invalid use inside function body");
315
316 return retval;
317 }
318
319 bool
320 is_rawcommand_name (const std::string& s)
321 {
322 return rawcommand_set.find (s) != rawcommand_set.end ();
323 }
324
325 DEFCMD (israwcommand, args, ,
326 "-*- texinfo -*-\n\
327 @deftypefn {Built-in Function} {} israwcommand (@var{name})\n\
328 Return true if @var{name} is a raw input command function.\n\
329 If @var{name} is omitted, return a list of identifiers which are marked as\n\
330 raw input commands with mark_as_rawcommand.\n\
331 @seealso{mark_as_rawcommand, unmark_rawcommand}\n\
332 @end deftypefn")
333 {
334 octave_value retval;
335
336 int nargin = args.length ();
337
338 if (nargin == 0)
339 {
340 string_vector lst (rawcommand_set.size());
341
342 int i = 0;
343 for (std::set<std::string>::const_iterator p = rawcommand_set.begin ();
344 p != rawcommand_set.end ();
345 p++)
346 lst[i++] = *p;
347
348 retval = Cell (lst.sort ());
349 }
350 else if (nargin == 1)
351 {
352 string_vector argv = args.make_argv ("israwcommand");
353
354 if (! error_state)
355 {
356 std::string s = argv[1];
357 retval = is_rawcommand_name(s);
358 }
359 }
360 else
361 print_usage ();
362
363 return retval;
364 }
365 93
366 // Is this octave_value a valid function? 94 // Is this octave_value a valid function?
367 95
368 octave_function * 96 octave_function *
369 is_valid_function (const std::string& fcn_name, 97 is_valid_function (const std::string& fcn_name,
1821 } 1549 }
1822 1550
1823 return retval; 1551 return retval;
1824 } 1552 }
1825 1553
1826 DEFCMD (who, args, nargout, 1554 DEFUN (who, args, nargout,
1827 "-*- texinfo -*-\n\ 1555 "-*- texinfo -*-\n\
1828 @deffn {Command} who options pattern @dots{}\n\ 1556 @deffn {Command} who options pattern @dots{}\n\
1829 @deffnx {Command} whos options pattern @dots{}\n\ 1557 @deffnx {Command} whos options pattern @dots{}\n\
1830 List currently defined symbols matching the given patterns. The\n\ 1558 List currently defined symbols matching the given patterns. The\n\
1831 following are valid options. They may be shortened to one character but\n\ 1559 following are valid options. They may be shortened to one character but\n\
1867 print_usage (); 1595 print_usage ();
1868 1596
1869 return retval; 1597 return retval;
1870 } 1598 }
1871 1599
1872 DEFCMD (whos, args, nargout, 1600 DEFUN (whos, args, nargout,
1873 "-*- texinfo -*-\n\ 1601 "-*- texinfo -*-\n\
1874 @deffn {Command} whos options pattern @dots{}\n\ 1602 @deffn {Command} whos options pattern @dots{}\n\
1875 See who.\n\ 1603 See who.\n\
1876 @end deffn") 1604 @end deffn")
1877 { 1605 {
1969 } 1697 }
1970 1698
1971 return retval; 1699 return retval;
1972 } 1700 }
1973 1701
1974 DEFCMD (mlock, args, , 1702 DEFUN (mlock, args, ,
1975 "-*- texinfo -*-\n\ 1703 "-*- texinfo -*-\n\
1976 @deftypefn {Built-in Function} {} mlock ()\n\ 1704 @deftypefn {Built-in Function} {} mlock ()\n\
1977 Lock the current function into memory so that it can't be cleared.\n\ 1705 Lock the current function into memory so that it can't be cleared.\n\
1978 @seealso{munlock, mislocked, persistent}\n\ 1706 @seealso{munlock, mislocked, persistent}\n\
1979 @end deftypefn") 1707 @end deftypefn")
1993 print_usage (); 1721 print_usage ();
1994 1722
1995 return retval; 1723 return retval;
1996 } 1724 }
1997 1725
1998 DEFCMD (munlock, args, , 1726 DEFUN (munlock, args, ,
1999 "-*- texinfo -*-\n\ 1727 "-*- texinfo -*-\n\
2000 @deftypefn {Built-in Function} {} munlock (@var{fcn})\n\ 1728 @deftypefn {Built-in Function} {} munlock (@var{fcn})\n\
2001 Unlock the named function. If no function is named\n\ 1729 Unlock the named function. If no function is named\n\
2002 then unlock the current function.\n\ 1730 then unlock the current function.\n\
2003 @seealso{mlock, mislocked, persistent}\n\ 1731 @seealso{mlock, mislocked, persistent}\n\
2028 1756
2029 return retval; 1757 return retval;
2030 } 1758 }
2031 1759
2032 1760
2033 DEFCMD (mislocked, args, , 1761 DEFUN (mislocked, args, ,
2034 "-*- texinfo -*-\n\ 1762 "-*- texinfo -*-\n\
2035 @deftypefn {Built-in Function} {} mislocked (@var{fcn})\n\ 1763 @deftypefn {Built-in Function} {} mislocked (@var{fcn})\n\
2036 Return true if the named function is locked. If no function is named\n\ 1764 Return true if the named function is locked. If no function is named\n\
2037 then return true if the current function is locked.\n\ 1765 then return true if the current function is locked.\n\
2038 @seealso{mlock, munlock, persistent}\n\ 1766 @seealso{mlock, munlock, persistent}\n\
2280 return retval; \ 2008 return retval; \
2281 } \ 2009 } \
2282 } \ 2010 } \
2283 while (0) 2011 while (0)
2284 2012
2285 DEFCMD (clear, args, , 2013 DEFUN (clear, args, ,
2286 "-*- texinfo -*-\n\ 2014 "-*- texinfo -*-\n\
2287 @deffn {Command} clear [options] pattern @dots{}\n\ 2015 @deffn {Command} clear [options] pattern @dots{}\n\
2288 Delete the names matching the given patterns from the symbol table. The\n\ 2016 Delete the names matching the given patterns from the symbol table. The\n\
2289 pattern may contain the following special characters:\n\ 2017 pattern may contain the following special characters:\n\
2290 \n\ 2018 \n\