comparison src/lex.l @ 975:e7165acbf96f

[project @ 1994-12-12 20:30:19 by jwe]
author jwe
date Mon, 12 Dec 1994 20:30:19 +0000
parents 46673c918034
children 4b483cf9f6b0
comparison
equal deleted inserted replaced
974:a0fa18fa9d0c 975:e7165acbf96f
18 along with GNU CC; see the file COPYING. If not, write to the Free 18 along with GNU CC; see the file COPYING. If not, write to the Free
19 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 19 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 20
21 */ 21 */
22 22
23 %x NEW_MATRIX
24 %x HELP_FCN 23 %x HELP_FCN
25 %s TEXT_FCN 24 %s TEXT_FCN
26 %s MATRIX 25 %s MATRIX
27 26
28 %{ 27 %{
106 static void grab_help_text (void); 105 static void grab_help_text (void);
107 static int match_any (char c, char *s); 106 static int match_any (char c, char *s);
108 static int next_token_is_bin_op (int spc_prev, char *yytext); 107 static int next_token_is_bin_op (int spc_prev, char *yytext);
109 static int next_token_is_postfix_unary_op (int spc_prev, char *yytext); 108 static int next_token_is_postfix_unary_op (int spc_prev, char *yytext);
110 static char *strip_trailing_whitespace (char *s); 109 static char *strip_trailing_whitespace (char *s);
110 static void eat_whitespace (void);
111 static void handle_number (char *yytext); 111 static void handle_number (char *yytext);
112 static int handle_string (char delim); 112 static int handle_string (char delim, int text_style = 0);
113 static int handle_close_brace (char *yytext); 113 static int handle_close_brace (char *yytext);
114 static int handle_identifier (char *s, int next_tok_is_eq); 114 static int handle_identifier (char *s, int next_tok_is_eq);
115 115
116 %} 116 %}
117 117
130 POW ((\*\*)|(\^)) 130 POW ((\*\*)|(\^))
131 EPOW (\.{POW}) 131 EPOW (\.{POW})
132 PLUS ((\+)|(\.\+)) 132 PLUS ((\+)|(\.\+))
133 MINUS ((\-)|(\.\-)) 133 MINUS ((\-)|(\.\-))
134 NOT ((\~)|(\!)) 134 NOT ((\~)|(\!))
135 QQ (\'\')
136 ECHAR (\\.)
137 QSTR ([^\n\'\\]*({QQ}|{ECHAR})*)
138 DQSTR ([^\n\"\\]*{ECHAR}*)
139 IDENT ([_a-zA-Z][_a-zA-Z0-9]*) 135 IDENT ([_a-zA-Z][_a-zA-Z0-9]*)
140 EXPON ([DdEe][+-]?{D}+) 136 EXPON ([DdEe][+-]?{D}+)
141 NUMBER (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?)) 137 NUMBER (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?))
142 %% 138 %%
143
144 %{
145 // XXX FIXME XXX -- this probably doesn't need to be an exclusive
146 // start state since it always matches. Also, we can probably
147 // eliminate it by doing the check below using yyinput() in the only
148 // place where we actually set this start state.
149 %}
150
151 <NEW_MATRIX>[^ \t\n#%] {
152 yyless (0);
153 BEGIN MATRIX;
154 }
155
156 <NEW_MATRIX>{SNLCMT}* {
157 fixup_column_count (yytext);
158 BEGIN MATRIX;
159 }
160 139
161 %{ 140 %{
162 // Help and other text-style functions are a pain in the ass. This 141 // Help and other text-style functions are a pain in the ass. This
163 // stuff needs to be simplified. May require some changes in the 142 // stuff needs to be simplified. May require some changes in the
164 // parser too. 143 // parser too.
189 else 168 else
190 TOK_RETURN (';'); 169 TOK_RETURN (';');
191 } 170 }
192 } 171 }
193 172
173 <TEXT_FCN>[\"\'] {
174 current_input_column++;
175 return handle_string (yytext[0], 1);
176 }
177
194 <HELP_FCN>[^ \t\n]*{S}* | 178 <HELP_FCN>[^ \t\n]*{S}* |
195 <TEXT_FCN>[^ \t\n\;\,]*{S}* { 179 <TEXT_FCN>[^ \t\n\;\,]*{S}* {
196 static char *tok = 0; 180 static char *tok = 0;
197 delete [] tok; 181 delete [] tok;
198 tok = strip_trailing_whitespace (yytext); 182 tok = strip_trailing_whitespace (yytext);
199 yylval.tok_val = new token (tok); 183 yylval.tok_val = new token (tok);
200 token_stack.push (yylval.tok_val); 184 token_stack.push (yylval.tok_val);
201 TOK_RETURN (TEXT); 185 TOK_RETURN (TEXT);
202 }
203
204 <TEXT_FCN>\'{QSTR}*[\n\'] {
205 if (yytext[yyleng-1] == '\n')
206 {
207 error ("unterminated string constant");
208 current_input_column = 1;
209 return LEXICAL_ERROR;
210 }
211 else
212 {
213 static char *tok = 0;
214 delete [] tok;
215 int off1 = doing_set ? 0 : 1;
216 int off2 = doing_set ? 0 : 2;
217 tok = strsave (&yytext[off1]);
218 tok[yyleng-off2] = '\0';
219 do_string_escapes (tok);
220 yylval.tok_val = new token (tok);
221 token_stack.push (yylval.tok_val);
222 current_input_column += yyleng;
223 }
224 return TEXT;
225 }
226
227 <TEXT_FCN>\"{DQSTR}*[\n\"] {
228 if (yytext[yyleng-1] == '\n')
229 {
230 error ("unterminated string constant");
231 current_input_column = 1;
232 return LEXICAL_ERROR;
233 }
234 else
235 {
236 static char *tok = 0;
237 delete [] tok;
238 int off1 = doing_set ? 0 : 1;
239 int off2 = doing_set ? 0 : 2;
240 tok = strsave (&yytext[off1]);
241 tok[yyleng-off2] = '\0';
242 do_string_escapes (tok);
243 yylval.tok_val = new token (tok);
244 token_stack.push (yylval.tok_val);
245 current_input_column += yyleng;
246 }
247 return TEXT;
248 } 186 }
249 187
250 %{ 188 %{
251 // For this and the next two rules, we're looking at ']', and we 189 // For this and the next two rules, we're looking at ']', and we
252 // need to know if the next token is `=' or `=='. 190 // need to know if the next token is `=' or `=='.
304 } 242 }
305 243
306 %{ 244 %{
307 // Open and close brace are handled differently if we are in the range 245 // Open and close brace are handled differently if we are in the range
308 // part of a plot command. 246 // part of a plot command.
247 //
309 %} 248 %}
310 249
311 \[{S}* { 250 \[{S}* {
251 fixup_column_count (yytext);
252
312 in_brace_or_paren.push (1); 253 in_brace_or_paren.push (1);
254
255 promptflag--;
256 eat_whitespace ();
257
313 if (plotting && ! past_plot_range) 258 if (plotting && ! past_plot_range)
314 { 259 {
315 in_plot_range = 1; 260 in_plot_range = 1;
316 TOK_RETURN (OPEN_BRACE); 261 TOK_RETURN (OPEN_BRACE);
317 } 262 }
318 else 263 else
319 { 264 {
320 mlnm.push (1); 265 mlnm.push (1);
321 braceflag++; 266 braceflag++;
322 promptflag--; 267 BEGIN MATRIX;
323 BEGIN NEW_MATRIX;
324 TOK_RETURN ('['); 268 TOK_RETURN ('[');
325 } 269 }
326 } 270 }
327 271
328 \] { 272 \] {
273 promptflag++;
274
329 if (! in_brace_or_paren.empty ()) 275 if (! in_brace_or_paren.empty ())
330 in_brace_or_paren.pop (); 276 in_brace_or_paren.pop ();
331 277
332 if (plotting && ! past_plot_range) 278 if (plotting && ! past_plot_range)
333 { 279 {
1325 1271
1326 return retval; 1272 return retval;
1327 } 1273 }
1328 1274
1329 static void 1275 static void
1276 eat_whitespace (void)
1277 {
1278 int in_comment = 0;
1279 int c;
1280 while ((c = yyinput ()) != EOF)
1281 {
1282 current_input_column++;
1283
1284 switch (c)
1285 {
1286 case ' ':
1287 case '\t':
1288 break;
1289
1290 case '\n':
1291 in_comment = 0;
1292 current_input_column = 0;
1293 break;
1294
1295 case '#':
1296 case '%':
1297 in_comment = 1;
1298 break;
1299
1300 default:
1301 if (in_comment)
1302 break;
1303 else
1304 goto done;
1305 }
1306 }
1307
1308 done:
1309 yyunput (c, yytext);
1310 return;
1311 }
1312
1313 static void
1330 handle_number (char *yytext) 1314 handle_number (char *yytext)
1331 { 1315 {
1332 double value; 1316 double value;
1333 int nread = sscanf (yytext, "%lf", &value); 1317 int nread = sscanf (yytext, "%lf", &value);
1334 1318
1377 case '#': 1361 case '#':
1378 in_comment = 1; 1362 in_comment = 1;
1379 break; 1363 break;
1380 1364
1381 case '\n': 1365 case '\n':
1366 current_input_column = 0;
1382 return 1; 1367 return 1;
1383 1368
1384 default: 1369 default:
1385 if (in_comment) 1370 if (in_comment)
1386 break; 1371 break;
1425 1410
1426 return 0; 1411 return 0;
1427 } 1412 }
1428 1413
1429 static int 1414 static int
1430 handle_string (char delim) 1415 handle_string (char delim, int text_style)
1431 { 1416 {
1432 ostrstream buf; 1417 ostrstream buf;
1433 1418
1434 int c; 1419 int c;
1435 int prev = 0; 1420 int prev = 0;
1471 { 1456 {
1472 yyunput (c, yytext); 1457 yyunput (c, yytext);
1473 buf << ends; 1458 buf << ends;
1474 char *tok = buf.str (); 1459 char *tok = buf.str ();
1475 do_string_escapes (tok); 1460 do_string_escapes (tok);
1461
1462 if (text_style && doing_set)
1463 {
1464 if (tok)
1465 {
1466 int len = strlen (tok) + 3;
1467 char *tmp = tok;
1468 tok = new char [len];
1469 tok[0] = delim;
1470 strcpy (tok+1, tmp);
1471 tok[len-2] = delim;
1472 tok[len-1] = '\0';
1473 delete [] tmp;
1474 }
1475 }
1476 else
1477 {
1478 quote_is_transpose = 1;
1479 cant_be_identifier = 1;
1480 convert_spaces_to_comma = 1;
1481 }
1482
1476 yylval.tok_val = new token (tok); 1483 yylval.tok_val = new token (tok);
1477 delete [] tok; 1484 delete [] tok;
1478 token_stack.push (yylval.tok_val); 1485 token_stack.push (yylval.tok_val);
1479 quote_is_transpose = 1;
1480 cant_be_identifier = 1;
1481 convert_spaces_to_comma = 1;
1482 return TEXT; 1486 return TEXT;
1483 } 1487 }
1484 } 1488 }
1485 } 1489 }
1486 else 1490 else