# HG changeset patch # User John W. Eaton # Date 1583763847 14400 # Node ID 648202bebcb0cbb4f7cedbcae2398040bc2f6636 # Parent 9e983eb1749d984ed587ad32d4de18a868f3c991 improve treatment of comments at interactive command line (bug #57924) * lex.ll (HANDLE_EOB_OR_EOF): New macro. (HANDLE_STRING_CONTINUATION, ^{S}*{CCHAR}\{{S}*{NL}, ^{S}*{CCHAR}\}{S}*{NL}, {ANY_EXCEPT_NL}*{NL}, {ANY_INCLUDING_NL}): Use HANDLE_EOB_OR_EOF to signal that we parsed a comment or need more input when using the push lexer interface. * oct-parse.yy (push_parser::run): Handle token value of -2 returned from the lexer to indicate that a comment was recognized. diff -r 9e983eb1749d -r 648202bebcb0 libinterp/parse-tree/lex.ll --- a/libinterp/parse-tree/lex.ll Sun Mar 08 17:49:43 2020 -0400 +++ b/libinterp/parse-tree/lex.ll Mon Mar 09 10:24:07 2020 -0400 @@ -237,25 +237,29 @@ } \ while (0) -// We can't rely on the trick used elsewhere of sticking ASCII 1 in -// the input buffer and recognizing it as a special case because ASCII -// 1 is a valid character for a character string. If we are at the -// end of the buffer, ask for more input. If we are at the end of the -// file, deal with it. Otherwise, just keep going with the text from -// the current buffer. +#define HANDLE_EOB_OR_EOF(STATUS) \ + do \ + { \ + if (curr_lexer->is_push_lexer ()) \ + { \ + if (curr_lexer->at_end_of_buffer ()) \ + return STATUS; \ + \ + if (curr_lexer->at_end_of_file ()) \ + return curr_lexer->handle_end_of_input (); \ + } \ + } \ + while (0) + +// If we are at the end of the buffer, ask for more input. +// If we are at the end of the file, deal with it. +// Otherwise, just keep going with the text from the current buffer. #define HANDLE_STRING_CONTINUATION \ do \ { \ curr_lexer->m_filepos.next_line (); \ \ - if (curr_lexer->is_push_lexer ()) \ - { \ - if (curr_lexer->at_end_of_buffer ()) \ - return -1; \ - \ - if (curr_lexer->at_end_of_file ()) \ - return curr_lexer->handle_end_of_input (); \ - } \ + HANDLE_EOB_OR_EOF (-1); \ } \ while (0) @@ -666,6 +670,8 @@ curr_lexer->m_comment_text = "\n"; curr_lexer->m_block_comment_nesting_level++; + + HANDLE_EOB_OR_EOF (-1); } %{ @@ -690,8 +696,16 @@ curr_lexer->m_block_comment_nesting_level--; + int status = -1; + if (curr_lexer->m_block_comment_nesting_level == 0) - curr_lexer->pop_start_state (); + { + status = -2; + + curr_lexer->pop_start_state (); + } + + HANDLE_EOB_OR_EOF (status); } %{ @@ -703,6 +717,8 @@ curr_lexer->m_filepos.next_line (); curr_lexer->m_comment_text += yytext; + + HANDLE_EOB_OR_EOF (-1); } %{ @@ -802,15 +818,17 @@ {ANY_INCLUDING_NL} { curr_lexer->lexer_debug ("{ANY_INCLUDING_NL}"); + curr_lexer->finish_comment (octave::comment_elt::full_line); + + curr_lexer->pop_start_state (); + + HANDLE_EOB_OR_EOF (-2); + // Restore all characters except the ASCII 1 marker that was // inserted by push_lexer::fill_flex_buffer. if (yytext[0] != '\001') curr_lexer->xunput (yytext[0]); - - curr_lexer->finish_comment (octave::comment_elt::full_line); - - curr_lexer->pop_start_state (); } %{ diff -r 9e983eb1749d -r 648202bebcb0 libinterp/parse-tree/oct-parse.yy --- a/libinterp/parse-tree/oct-parse.yy Sun Mar 08 17:49:43 2020 -0400 +++ b/libinterp/parse-tree/oct-parse.yy Mon Mar 09 10:24:07 2020 -0400 @@ -4661,7 +4661,12 @@ if (token < 0) { - status = -1; + // TOKEN == -2 means that the lexer recognized a comment + // and we should be at the end of the buffer but not the + // end of the file so we should return 0 to indicate + // "complete input" instead of -1 to request more input. + + status = (token == -2 ? 0 : -1); if (! eof && m_lexer.at_end_of_buffer ()) return status;