comparison src/lex.l @ 4608:22ca4cc02525

[project @ 2003-11-14 03:55:04 by jwe]
author jwe
date Fri, 14 Nov 2003 03:55:04 +0000
parents b7360f8eb035
children d44675070f1a
comparison
equal deleted inserted replaced
4607:eb84ffc19e44 4608:22ca4cc02525
120 120
121 void paren (void) { context.push (PAREN); } 121 void paren (void) { context.push (PAREN); }
122 bool is_paren (void) 122 bool is_paren (void)
123 { return ! context.empty () && context.top () == PAREN; } 123 { return ! context.empty () && context.top () == PAREN; }
124 124
125 bool is_bracket_or_brace (void)
126 { return (! context.empty ()
127 && (context.top () == BRACKET || context.top () == BRACE)); }
128
125 bool none (void) { return context.empty (); } 129 bool none (void) { return context.empty (); }
126 130
127 void remove (void) { if (! context.empty ()) context.pop (); } 131 void remove (void) { if (! context.empty ()) context.pop (); }
128 132
129 void clear (void) { while (! context.empty ()) context.pop (); } 133 void clear (void) { while (! context.empty ()) context.pop (); }
165 static bool next_token_is_bin_op (bool spc_prev); 169 static bool next_token_is_bin_op (bool spc_prev);
166 static bool next_token_is_postfix_unary_op (bool spc_prev); 170 static bool next_token_is_postfix_unary_op (bool spc_prev);
167 static std::string strip_trailing_whitespace (char *s); 171 static std::string strip_trailing_whitespace (char *s);
168 static void handle_number (void); 172 static void handle_number (void);
169 static int handle_string (char delim, int text_style = 0); 173 static int handle_string (char delim, int text_style = 0);
170 static int handle_close_bracket (int spc_gobbled); 174 static char handle_close_bracket (bool spc_gobbled, char bracket_type);
171 static int handle_identifier (void); 175 static int handle_identifier (void);
172 static bool have_continuation (bool trailing_comments_ok = true); 176 static bool have_continuation (bool trailing_comments_ok = true);
173 static bool have_ellipsis_continuation (bool trailing_comments_ok = true); 177 static bool have_ellipsis_continuation (bool trailing_comments_ok = true);
174 static void scan_for_comments (const char *); 178 static void scan_for_comments (const char *);
175 static yum_yum eat_whitespace (void); 179 static yum_yum eat_whitespace (void);
270 <MATRIX_START>{SNLCMT}*\]{S}* { 274 <MATRIX_START>{SNLCMT}*\]{S}* {
271 scan_for_comments (yytext); 275 scan_for_comments (yytext);
272 fixup_column_count (yytext); 276 fixup_column_count (yytext);
273 int c = yytext[yyleng-1]; 277 int c = yytext[yyleng-1];
274 int cont_is_spc = eat_continuation (); 278 int cont_is_spc = eat_continuation ();
275 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); 279 bool spc_gobbled = (cont_is_spc || c == ' ' || c == '\t');
276 return handle_close_bracket (spc_gobbled); 280 return handle_close_bracket (spc_gobbled, ']');
281 }
282
283 <MATRIX_START>{SNLCMT}*\}{S}* {
284 scan_for_comments (yytext);
285 fixup_column_count (yytext);
286 int c = yytext[yyleng-1];
287 int cont_is_spc = eat_continuation ();
288 bool spc_gobbled = (cont_is_spc || c == ' ' || c == '\t');
289 return handle_close_bracket (spc_gobbled, '}');
277 } 290 }
278 291
279 %{ 292 %{
280 // Commas are element separators in matrix constants. If we don't 293 // Commas are element separators in matrix constants. If we don't
281 // check for continuations here we can end up inserting too many 294 // check for continuations here we can end up inserting too many
314 int tmp = eat_continuation (); 327 int tmp = eat_continuation ();
315 int bin_op = next_token_is_bin_op (true); 328 int bin_op = next_token_is_bin_op (true);
316 int postfix_un_op = next_token_is_postfix_unary_op (true); 329 int postfix_un_op = next_token_is_postfix_unary_op (true);
317 330
318 if (! (postfix_un_op || bin_op) 331 if (! (postfix_un_op || bin_op)
319 && nesting_level.is_bracket () 332 && nesting_level.is_bracket_or_brace ()
320 && lexer_flags.convert_spaces_to_comma) 333 && lexer_flags.convert_spaces_to_comma)
321 { 334 {
322 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) 335 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE)
323 { 336 {
324 maybe_warn_separator_insert (';'); 337 maybe_warn_separator_insert (';');
369 lexer_flags.convert_spaces_to_comma = true; 382 lexer_flags.convert_spaces_to_comma = true;
370 383
371 if (nesting_level.none ()) 384 if (nesting_level.none ())
372 return LEXICAL_ERROR; 385 return LEXICAL_ERROR;
373 386
374 if (nesting_level.is_bracket ()) 387 if (nesting_level.is_bracket_or_brace ())
375 { 388 {
376 maybe_warn_separator_insert (';'); 389 maybe_warn_separator_insert (';');
377 390
378 return ';'; 391 return ';';
379 } 392 }
525 538
526 if (nesting_level.none ()) 539 if (nesting_level.none ())
527 return '\n'; 540 return '\n';
528 else if (nesting_level.is_paren ()) 541 else if (nesting_level.is_paren ())
529 gripe_matlab_incompatible ("bare newline inside parentheses"); 542 gripe_matlab_incompatible ("bare newline inside parentheses");
530 else if (nesting_level.is_bracket ()) 543 else if (nesting_level.is_bracket_or_brace ())
531 return LEXICAL_ERROR; 544 return LEXICAL_ERROR;
532 } 545 }
533 546
534 %{ 547 %{
535 // Single quote can either be the beginning of a string or a transpose 548 // Single quote can either be the beginning of a string or a transpose
625 if (YY_START == COMMAND_START) 638 if (YY_START == COMMAND_START)
626 BEGIN (INITIAL); 639 BEGIN (INITIAL);
627 640
628 if (nesting_level.none ()) 641 if (nesting_level.none ())
629 return '\n'; 642 return '\n';
630 else if (nesting_level.is_bracket ()) 643 else if (nesting_level.is_bracket_or_brace ())
631 return ';'; 644 return ';';
632 } 645 }
633 646
634 %{ 647 %{
635 // Other operators. 648 // Other operators.
702 nesting_level.remove (); 715 nesting_level.remove ();
703 716
704 current_input_column++; 717 current_input_column++;
705 lexer_flags.cant_be_identifier = true; 718 lexer_flags.cant_be_identifier = true;
706 lexer_flags.quote_is_transpose = true; 719 lexer_flags.quote_is_transpose = true;
707 lexer_flags.convert_spaces_to_comma = nesting_level.is_bracket (); 720 lexer_flags.convert_spaces_to_comma = nesting_level.is_bracket_or_brace ();
708 do_comma_insert_check (); 721 do_comma_insert_check ();
709 return ')'; 722 return ')';
710 } 723 }
711 724
712 "." { 725 "." {
728 "&=" { XBIN_OP_RETURN (AND_EQ, false); } 741 "&=" { XBIN_OP_RETURN (AND_EQ, false); }
729 "|=" { XBIN_OP_RETURN (OR_EQ, false); } 742 "|=" { XBIN_OP_RETURN (OR_EQ, false); }
730 "<<=" { XBIN_OP_RETURN (LSHIFT_EQ, false); } 743 "<<=" { XBIN_OP_RETURN (LSHIFT_EQ, false); }
731 ">>=" { XBIN_OP_RETURN (RSHIFT_EQ, false); } 744 ">>=" { XBIN_OP_RETURN (RSHIFT_EQ, false); }
732 745
733 "{" { 746 \{{S}* {
734 nesting_level.brace (); 747 nesting_level.brace ();
748
749 current_input_column += yyleng;
750 lexer_flags.quote_is_transpose = false;
751 lexer_flags.cant_be_identifier = false;
752 lexer_flags.convert_spaces_to_comma = true;
753
735 promptflag--; 754 promptflag--;
736 TOK_RETURN ('{'); 755 eat_whitespace ();
756
757 lexer_flags.bracketflag++;
758 BEGIN (MATRIX_START);
759 return '{';
737 } 760 }
738 761
739 "}" { 762 "}" {
740 nesting_level.remove (); 763 nesting_level.remove ();
741 764
742 current_input_column++; 765 TOK_RETURN ('}');
743 lexer_flags.cant_be_identifier = true;
744 lexer_flags.quote_is_transpose = true;
745 lexer_flags.convert_spaces_to_comma = nesting_level.is_bracket ();
746 do_comma_insert_check (); // Is this really necessary?
747
748 return '}';
749 } 766 }
750 767
751 %{ 768 %{
752 // Unrecognized input is a lexical error. 769 // Unrecognized input is a lexical error.
753 %} 770 %}
2207 yyunput (c0, yytext); 2224 yyunput (c0, yytext);
2208 2225
2209 return retval; 2226 return retval;
2210 } 2227 }
2211 2228
2212 static int 2229 static char
2213 handle_close_bracket (int spc_gobbled) 2230 handle_close_bracket (bool spc_gobbled, char bracket_type)
2214 { 2231 {
2215 int retval = ']'; 2232 char retval = bracket_type;
2216 2233
2217 if (! nesting_level.none ()) 2234 if (! nesting_level.none ())
2218 { 2235 {
2219 nesting_level.remove (); 2236 nesting_level.remove ();
2220 lexer_flags.bracketflag--; 2237 lexer_flags.bracketflag--;
2221 } 2238 }
2222 2239
2223 if (lexer_flags.bracketflag == 0) 2240 if (lexer_flags.bracketflag == 0)
2224 BEGIN (INITIAL); 2241 BEGIN (INITIAL);
2225 2242
2226 if (next_token_is_assign_op () && ! lexer_flags.looking_at_return_list) 2243 if (bracket_type == ']'
2244 && next_token_is_assign_op ()
2245 && ! lexer_flags.looking_at_return_list)
2227 { 2246 {
2228 retval = CLOSE_BRACE; 2247 retval = CLOSE_BRACE;
2229 } 2248 }
2230 else 2249 else
2231 { 2250 {
2239 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); 2258 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled);
2240 2259
2241 int sep_op = next_token_is_sep_op (); 2260 int sep_op = next_token_is_sep_op ();
2242 2261
2243 if (! (postfix_un_op || bin_op || sep_op) 2262 if (! (postfix_un_op || bin_op || sep_op)
2244 && nesting_level.is_bracket () 2263 && nesting_level.is_bracket_or_brace ()
2245 && lexer_flags.convert_spaces_to_comma) 2264 && lexer_flags.convert_spaces_to_comma)
2246 { 2265 {
2247 maybe_warn_separator_insert (','); 2266 maybe_warn_separator_insert (',');
2248 2267
2249 yyunput (',', yytext); 2268 yyunput (',', yytext);
2250 return ']'; 2269 return retval;
2251 } 2270 }
2252 } 2271 }
2253 } 2272 }
2254 2273
2255 lexer_flags.quote_is_transpose = true; 2274 lexer_flags.quote_is_transpose = true;
2260 } 2279 }
2261 2280
2262 static void 2281 static void
2263 maybe_unput_comma (int spc_gobbled) 2282 maybe_unput_comma (int spc_gobbled)
2264 { 2283 {
2265 if (nesting_level.is_bracket ()) 2284 if (nesting_level.is_bracket_or_brace ())
2266 { 2285 {
2267 int bin_op = next_token_is_bin_op (spc_gobbled); 2286 int bin_op = next_token_is_bin_op (spc_gobbled);
2268 2287
2269 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); 2288 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled);
2270 2289