Mercurial > hg > octave-nkf
comparison libinterp/parse-tree/lex.ll @ 16104:c8974e28da59
move nesting_level to lexical_feedback class
* lex.h, lex.ll (brace_bracket_paren_nesting_level): Rename to
bbp_nesting_level and nest class definition inside lexical_feedback
class.
(nesting_level): Move global variable to lexical_feedback_class.
Change all uses.
(reset_parser): Don't clear nesting_level.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 25 Feb 2013 21:54:15 -0500 |
parents | 6434f70f0ee0 |
children | b7de58feb2d3 |
comparison
equal
deleted
inserted
replaced
16103:6434f70f0ee0 | 16104:c8974e28da59 |
---|---|
228 typedef int yum_yum; | 228 typedef int yum_yum; |
229 | 229 |
230 const yum_yum ATE_NOTHING = 0; | 230 const yum_yum ATE_NOTHING = 0; |
231 const yum_yum ATE_SPACE_OR_TAB = 1; | 231 const yum_yum ATE_SPACE_OR_TAB = 1; |
232 const yum_yum ATE_NEWLINE = 2; | 232 const yum_yum ATE_NEWLINE = 2; |
233 | |
234 // Is the closest nesting level a square bracket, squiggly brace or a paren? | |
235 | |
236 class bracket_brace_paren_nesting_level | |
237 { | |
238 public: | |
239 | |
240 bracket_brace_paren_nesting_level (void) : context () { } | |
241 | |
242 ~bracket_brace_paren_nesting_level (void) { } | |
243 | |
244 void bracket (void) { context.push (BRACKET); } | |
245 bool is_bracket (void) | |
246 { return ! context.empty () && context.top () == BRACKET; } | |
247 | |
248 void brace (void) { context.push (BRACE); } | |
249 bool is_brace (void) | |
250 { return ! context.empty () && context.top () == BRACE; } | |
251 | |
252 void paren (void) { context.push (PAREN); } | |
253 bool is_paren (void) | |
254 { return ! context.empty () && context.top () == PAREN; } | |
255 | |
256 bool is_bracket_or_brace (void) | |
257 { return (! context.empty () | |
258 && (context.top () == BRACKET || context.top () == BRACE)); } | |
259 | |
260 bool none (void) { return context.empty (); } | |
261 | |
262 void remove (void) { if (! context.empty ()) context.pop (); } | |
263 | |
264 void clear (void) { while (! context.empty ()) context.pop (); } | |
265 | |
266 private: | |
267 | |
268 std::stack<int> context; | |
269 | |
270 static const int BRACKET; | |
271 static const int BRACE; | |
272 static const int PAREN; | |
273 | |
274 bracket_brace_paren_nesting_level (const bracket_brace_paren_nesting_level&); | |
275 | |
276 bracket_brace_paren_nesting_level& | |
277 operator = (const bracket_brace_paren_nesting_level&); | |
278 }; | |
279 | |
280 const int bracket_brace_paren_nesting_level::BRACKET = 1; | |
281 const int bracket_brace_paren_nesting_level::BRACE = 2; | |
282 const int bracket_brace_paren_nesting_level::PAREN = 3; | |
283 | |
284 static bracket_brace_paren_nesting_level nesting_level; | |
285 | 233 |
286 static bool Vdisplay_tokens = false; | 234 static bool Vdisplay_tokens = false; |
287 | 235 |
288 static unsigned int Vtoken_count = 0; | 236 static unsigned int Vtoken_count = 0; |
289 | 237 |
540 bool bin_op = next_token_is_bin_op (true); | 488 bool bin_op = next_token_is_bin_op (true); |
541 bool postfix_un_op = next_token_is_postfix_unary_op (true); | 489 bool postfix_un_op = next_token_is_postfix_unary_op (true); |
542 bool sep_op = next_token_is_sep_op (); | 490 bool sep_op = next_token_is_sep_op (); |
543 | 491 |
544 if (! (postfix_un_op || bin_op || sep_op) | 492 if (! (postfix_un_op || bin_op || sep_op) |
545 && nesting_level.is_bracket_or_brace () | 493 && lexer_flags.nesting_level.is_bracket_or_brace () |
546 && lexer_flags.convert_spaces_to_comma) | 494 && lexer_flags.convert_spaces_to_comma) |
547 { | 495 { |
548 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) | 496 if ((tmp & ATE_NEWLINE) == ATE_NEWLINE) |
549 { | 497 { |
550 maybe_warn_separator_insert (';'); | 498 maybe_warn_separator_insert (';'); |
604 | 552 |
605 lexer_flags.quote_is_transpose = false; | 553 lexer_flags.quote_is_transpose = false; |
606 lexer_flags.convert_spaces_to_comma = true; | 554 lexer_flags.convert_spaces_to_comma = true; |
607 lexer_flags.at_beginning_of_statement = false; | 555 lexer_flags.at_beginning_of_statement = false; |
608 | 556 |
609 if (nesting_level.none ()) | 557 if (lexer_flags.nesting_level.none ()) |
610 return LEXICAL_ERROR; | 558 return LEXICAL_ERROR; |
611 | 559 |
612 if (! lexer_flags.looking_at_object_index.front () | 560 if (! lexer_flags.looking_at_object_index.front () |
613 && nesting_level.is_bracket_or_brace ()) | 561 && lexer_flags.nesting_level.is_bracket_or_brace ()) |
614 { | 562 { |
615 maybe_warn_separator_insert (';'); | 563 maybe_warn_separator_insert (';'); |
616 | 564 |
617 COUNT_TOK_AND_RETURN (';'); | 565 COUNT_TOK_AND_RETURN (';'); |
618 } | 566 } |
619 } | 567 } |
620 | 568 |
621 \[{S}* { | 569 \[{S}* { |
622 LEXER_DEBUG ("\\[{S}*"); | 570 LEXER_DEBUG ("\\[{S}*"); |
623 | 571 |
624 nesting_level.bracket (); | 572 lexer_flags.nesting_level.bracket (); |
625 | 573 |
626 lexer_flags.looking_at_object_index.push_front (false); | 574 lexer_flags.looking_at_object_index.push_front (false); |
627 | 575 |
628 lexer_flags.current_input_column += yyleng; | 576 lexer_flags.current_input_column += yyleng; |
629 lexer_flags.quote_is_transpose = false; | 577 lexer_flags.quote_is_transpose = false; |
646 } | 594 } |
647 | 595 |
648 \] { | 596 \] { |
649 LEXER_DEBUG ("\\]"); | 597 LEXER_DEBUG ("\\]"); |
650 | 598 |
651 nesting_level.remove (); | 599 lexer_flags.nesting_level.remove (); |
652 | 600 |
653 lexer_flags.looking_at_object_index.pop_front (); | 601 lexer_flags.looking_at_object_index.pop_front (); |
654 | 602 |
655 lexer_flags.looking_for_object_index = true; | 603 lexer_flags.looking_for_object_index = true; |
656 lexer_flags.at_beginning_of_statement = false; | 604 lexer_flags.at_beginning_of_statement = false; |
808 lexer_flags.current_input_column = 1; | 756 lexer_flags.current_input_column = 1; |
809 | 757 |
810 lexer_flags.quote_is_transpose = false; | 758 lexer_flags.quote_is_transpose = false; |
811 lexer_flags.convert_spaces_to_comma = true; | 759 lexer_flags.convert_spaces_to_comma = true; |
812 | 760 |
813 if (nesting_level.none ()) | 761 if (lexer_flags.nesting_level.none ()) |
814 { | 762 { |
815 lexer_flags.at_beginning_of_statement = true; | 763 lexer_flags.at_beginning_of_statement = true; |
816 COUNT_TOK_AND_RETURN ('\n'); | 764 COUNT_TOK_AND_RETURN ('\n'); |
817 } | 765 } |
818 else if (nesting_level.is_paren ()) | 766 else if (lexer_flags.nesting_level.is_paren ()) |
819 { | 767 { |
820 lexer_flags.at_beginning_of_statement = false; | 768 lexer_flags.at_beginning_of_statement = false; |
821 gripe_matlab_incompatible ("bare newline inside parentheses"); | 769 gripe_matlab_incompatible ("bare newline inside parentheses"); |
822 } | 770 } |
823 else if (nesting_level.is_bracket_or_brace ()) | 771 else if (lexer_flags.nesting_level.is_bracket_or_brace ()) |
824 return LEXICAL_ERROR; | 772 return LEXICAL_ERROR; |
825 } | 773 } |
826 | 774 |
827 %{ | 775 %{ |
828 // Single quote can either be the beginning of a string or a transpose | 776 // Single quote can either be the beginning of a string or a transpose |
960 | 908 |
961 lexer_flags.looking_at_indirect_ref = false; | 909 lexer_flags.looking_at_indirect_ref = false; |
962 lexer_flags.looking_for_object_index = false; | 910 lexer_flags.looking_for_object_index = false; |
963 lexer_flags.at_beginning_of_statement = false; | 911 lexer_flags.at_beginning_of_statement = false; |
964 | 912 |
965 nesting_level.paren (); | 913 lexer_flags.nesting_level.paren (); |
966 promptflag--; | 914 promptflag--; |
967 | 915 |
968 TOK_RETURN ('('); | 916 TOK_RETURN ('('); |
969 } | 917 } |
970 | 918 |
971 ")" { | 919 ")" { |
972 LEXER_DEBUG (")"); | 920 LEXER_DEBUG (")"); |
973 | 921 |
974 nesting_level.remove (); | 922 lexer_flags.nesting_level.remove (); |
975 lexer_flags.current_input_column++; | 923 lexer_flags.current_input_column++; |
976 | 924 |
977 lexer_flags.looking_at_object_index.pop_front (); | 925 lexer_flags.looking_at_object_index.pop_front (); |
978 | 926 |
979 lexer_flags.quote_is_transpose = true; | 927 lexer_flags.quote_is_transpose = true; |
980 lexer_flags.convert_spaces_to_comma | 928 lexer_flags.convert_spaces_to_comma |
981 = (nesting_level.is_bracket_or_brace () | 929 = (lexer_flags.nesting_level.is_bracket_or_brace () |
982 && ! lexer_flags.looking_at_anon_fcn_args); | 930 && ! lexer_flags.looking_at_anon_fcn_args); |
983 lexer_flags.looking_for_object_index = true; | 931 lexer_flags.looking_for_object_index = true; |
984 lexer_flags.at_beginning_of_statement = false; | 932 lexer_flags.at_beginning_of_statement = false; |
985 | 933 |
986 if (lexer_flags.looking_at_anon_fcn_args) | 934 if (lexer_flags.looking_at_anon_fcn_args) |
1018 ">>=" { LEXER_DEBUG (">>="); XBIN_OP_RETURN (RSHIFT_EQ, false, false); } | 966 ">>=" { LEXER_DEBUG (">>="); XBIN_OP_RETURN (RSHIFT_EQ, false, false); } |
1019 | 967 |
1020 \{{S}* { | 968 \{{S}* { |
1021 LEXER_DEBUG ("\\{{S}*"); | 969 LEXER_DEBUG ("\\{{S}*"); |
1022 | 970 |
1023 nesting_level.brace (); | 971 lexer_flags.nesting_level.brace (); |
1024 | 972 |
1025 lexer_flags.looking_at_object_index.push_front | 973 lexer_flags.looking_at_object_index.push_front |
1026 (lexer_flags.looking_for_object_index); | 974 (lexer_flags.looking_for_object_index); |
1027 | 975 |
1028 lexer_flags.current_input_column += yyleng; | 976 lexer_flags.current_input_column += yyleng; |
1045 lexer_flags.looking_at_object_index.pop_front (); | 993 lexer_flags.looking_at_object_index.pop_front (); |
1046 | 994 |
1047 lexer_flags.looking_for_object_index = true; | 995 lexer_flags.looking_for_object_index = true; |
1048 lexer_flags.at_beginning_of_statement = false; | 996 lexer_flags.at_beginning_of_statement = false; |
1049 | 997 |
1050 nesting_level.remove (); | 998 lexer_flags.nesting_level.remove (); |
1051 | 999 |
1052 TOK_RETURN ('}'); | 1000 TOK_RETURN ('}'); |
1053 } | 1001 } |
1054 | 1002 |
1055 %{ | 1003 %{ |
1115 // We do want a prompt by default. | 1063 // We do want a prompt by default. |
1116 promptflag = 1; | 1064 promptflag = 1; |
1117 | 1065 |
1118 // We are not in a block comment. | 1066 // We are not in a block comment. |
1119 block_comment_nesting_level = 0; | 1067 block_comment_nesting_level = 0; |
1120 | |
1121 // Error may have occurred inside some brackets, braces, or parentheses. | |
1122 nesting_level.clear (); | |
1123 | 1068 |
1124 // Clear out the stack of token info used to track line and column | 1069 // Clear out the stack of token info used to track line and column |
1125 // numbers. | 1070 // numbers. |
1126 while (! token_stack.empty ()) | 1071 while (! token_stack.empty ()) |
1127 { | 1072 { |
1973 : grab_comment_block (flex_reader, false, eof); | 1918 : grab_comment_block (flex_reader, false, eof); |
1974 | 1919 |
1975 if (lexer_debug_flag) | 1920 if (lexer_debug_flag) |
1976 std::cerr << "C: " << txt << std::endl; | 1921 std::cerr << "C: " << txt << std::endl; |
1977 | 1922 |
1978 if (help_txt.empty () && nesting_level.none ()) | 1923 if (help_txt.empty () && lexer_flags.nesting_level.none ()) |
1979 { | 1924 { |
1980 if (! help_buf.empty ()) | 1925 if (! help_buf.empty ()) |
1981 help_buf.pop (); | 1926 help_buf.pop (); |
1982 | 1927 |
1983 help_buf.push (txt); | 1928 help_buf.push (txt); |
1991 lexer_flags.at_beginning_of_statement = true; | 1936 lexer_flags.at_beginning_of_statement = true; |
1992 | 1937 |
1993 if (YY_START == COMMAND_START) | 1938 if (YY_START == COMMAND_START) |
1994 BEGIN (INITIAL); | 1939 BEGIN (INITIAL); |
1995 | 1940 |
1996 if (nesting_level.none ()) | 1941 if (lexer_flags.nesting_level.none ()) |
1997 return '\n'; | 1942 return '\n'; |
1998 else if (nesting_level.is_bracket_or_brace ()) | 1943 else if (lexer_flags.nesting_level.is_bracket_or_brace ()) |
1999 return ';'; | 1944 return ';'; |
2000 else | 1945 else |
2001 return 0; | 1946 return 0; |
2002 } | 1947 } |
2003 | 1948 |
2772 static int | 2717 static int |
2773 handle_close_bracket (bool spc_gobbled, int bracket_type) | 2718 handle_close_bracket (bool spc_gobbled, int bracket_type) |
2774 { | 2719 { |
2775 int retval = bracket_type; | 2720 int retval = bracket_type; |
2776 | 2721 |
2777 if (! nesting_level.none ()) | 2722 if (! lexer_flags.nesting_level.none ()) |
2778 { | 2723 { |
2779 nesting_level.remove (); | 2724 lexer_flags.nesting_level.remove (); |
2780 | 2725 |
2781 if (bracket_type == ']') | 2726 if (bracket_type == ']') |
2782 lexer_flags.bracketflag--; | 2727 lexer_flags.bracketflag--; |
2783 else if (bracket_type == '}') | 2728 else if (bracket_type == '}') |
2784 lexer_flags.braceflag--; | 2729 lexer_flags.braceflag--; |
2795 { | 2740 { |
2796 retval = CLOSE_BRACE; | 2741 retval = CLOSE_BRACE; |
2797 } | 2742 } |
2798 else if ((lexer_flags.bracketflag || lexer_flags.braceflag) | 2743 else if ((lexer_flags.bracketflag || lexer_flags.braceflag) |
2799 && lexer_flags.convert_spaces_to_comma | 2744 && lexer_flags.convert_spaces_to_comma |
2800 && (nesting_level.is_bracket () | 2745 && (lexer_flags.nesting_level.is_bracket () |
2801 || (nesting_level.is_brace () | 2746 || (lexer_flags.nesting_level.is_brace () |
2802 && ! lexer_flags.looking_at_object_index.front ()))) | 2747 && ! lexer_flags.looking_at_object_index.front ()))) |
2803 { | 2748 { |
2804 bool index_op = next_token_is_index_op (); | 2749 bool index_op = next_token_is_index_op (); |
2805 | 2750 |
2806 // Don't insert comma if we are looking at something like | 2751 // Don't insert comma if we are looking at something like |
2836 } | 2781 } |
2837 | 2782 |
2838 static void | 2783 static void |
2839 maybe_unput_comma (int spc_gobbled) | 2784 maybe_unput_comma (int spc_gobbled) |
2840 { | 2785 { |
2841 if (nesting_level.is_bracket () | 2786 if (lexer_flags.nesting_level.is_bracket () |
2842 || (nesting_level.is_brace () | 2787 || (lexer_flags.nesting_level.is_brace () |
2843 && ! lexer_flags.looking_at_object_index.front ())) | 2788 && ! lexer_flags.looking_at_object_index.front ())) |
2844 { | 2789 { |
2845 int bin_op = next_token_is_bin_op (spc_gobbled); | 2790 int bin_op = next_token_is_bin_op (spc_gobbled); |
2846 | 2791 |
2847 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); | 2792 int postfix_un_op = next_token_is_postfix_unary_op (spc_gobbled); |