Mercurial > octave
changeset 27527:73be3c628eac
refactor input_reader class and its use in lexer
* input.h, input.cc (base_reader::get_input, file_reader::get_input,
terminal_reader::get_input, eval_string_reader::get_input):
Accept prompt as argument. Change all uses.
(base_reader::octave_gets): Accept prompt as argument instead of
getting it from input_system. Change all uses.
* interpreter.cc (interpreter::main_loop): Get promptflag from parser,
prompt string from input_system, decode it here, and pass to the input
reader.
* lex.h, lex.ll (base_lexer::m_promptflag): New data member.
(base_lexer::reset): Also reset m_promptflag.
(base_lexer::increment_promptflag, base_lexer::decrement_promptflag,
base_lexer::promptflag): No longer virtual.
(lexer::increment_promptflag, lexer::decrement_promptflag,
lexer::promptflag): Delete.
(push_lexer::m_pflag): Delete.
(push_lexer::increment_promptflag, push_lexer::decrement_promptflag,
push_lexer::promptflag): Delete.
(push_lexer::reset): Delete.
(lexer::fill_flex_buffer): Get prompt string from input_system, decode
it here, and pass to the input reader.
* parse.h (base_parser::increment_promptflag,
base_parser::decrement_promptflag, base_parser::promptflag):
New convenience functions.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 18 Oct 2019 09:08:06 -0400 |
parents | cb964b74d8a0 |
children | e51284fc0a51 |
files | libinterp/corefcn/input.cc libinterp/corefcn/input.h libinterp/corefcn/interpreter.cc libinterp/parse-tree/lex.h libinterp/parse-tree/lex.ll libinterp/parse-tree/parse.h |
diffstat | 6 files changed, 52 insertions(+), 59 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/input.cc Fri Oct 18 07:41:02 2019 -0400 +++ b/libinterp/corefcn/input.cc Fri Oct 18 09:08:06 2019 -0400 @@ -673,7 +673,7 @@ return retval; } - std::string base_reader::octave_gets (bool& eof) + std::string base_reader::octave_gets (const std::string& prompt, bool& eof) { octave_quit (); @@ -702,10 +702,6 @@ input_system& input_sys = m_interpreter.get_input_system (); - std::string ps = (m_pflag > 0) ? input_sys.PS1 () : input_sys.PS2 (); - - std::string prompt = command_editor::decode_prompt_string (ps); - pipe_handler_error_count = 0; output_system& output_sys = m_interpreter.get_output_system (); @@ -774,7 +770,7 @@ : base_reader (interp) { } - std::string get_input (bool& eof); + std::string get_input (const std::string& prompt, bool& eof); std::string input_source (void) const { return s_in_src; } @@ -793,7 +789,7 @@ file_reader (interpreter& interp, FILE *f_arg) : base_reader (interp), m_file (f_arg) { } - std::string get_input (bool& eof); + std::string get_input (const std::string& prompt, bool& eof); std::string input_source (void) const { return s_in_src; } @@ -815,7 +811,7 @@ : base_reader (interp), m_eval_string (str) { } - std::string get_input (bool& eof); + std::string get_input (const std::string& prompt, bool& eof); std::string input_source (void) const { return s_in_src; } @@ -845,19 +841,19 @@ const std::string terminal_reader::s_in_src ("terminal"); std::string - terminal_reader::get_input (bool& eof) + terminal_reader::get_input (const std::string& prompt, bool& eof) { octave_quit (); eof = false; - return octave_gets (eof); + return octave_gets (prompt, eof); } const std::string file_reader::s_in_src ("file"); std::string - file_reader::get_input (bool& eof) + file_reader::get_input (const std::string& /*prompt*/, bool& eof) { octave_quit (); @@ -907,7 +903,7 @@ const std::string eval_string_reader::s_in_src ("eval_string"); std::string - eval_string_reader::get_input (bool& eof) + eval_string_reader::get_input (const std::string& /*prompt*/, bool& eof) { octave_quit ();
--- a/libinterp/corefcn/input.h Fri Oct 18 07:41:02 2019 -0400 +++ b/libinterp/corefcn/input.h Fri Oct 18 09:08:06 2019 -0400 @@ -207,7 +207,7 @@ virtual ~base_reader (void) = default; - virtual std::string get_input (bool& eof) = 0; + virtual std::string get_input (const std::string& prompt, bool& eof) = 0; virtual std::string input_source (void) const { return s_in_src; } @@ -226,7 +226,7 @@ return retval; } - std::string octave_gets (bool& eof); + std::string octave_gets (const std::string& prompt, bool& eof); virtual bool input_from_terminal (void) const { return false; } @@ -271,9 +271,9 @@ int promptflag (int n) { return m_rep->promptflag (n); } - std::string get_input (bool& eof) + std::string get_input (const std::string& prompt, bool& eof) { - return m_rep->get_input (eof); + return m_rep->get_input (prompt, eof); } std::string input_source (void) const
--- a/libinterp/corefcn/interpreter.cc Fri Oct 18 07:41:02 2019 -0400 +++ b/libinterp/corefcn/interpreter.cc Fri Oct 18 09:08:06 2019 -0400 @@ -1029,9 +1029,16 @@ do { - bool eof; + int promptflag = repl_parser.promptflag (); - std::string input_line = reader.get_input (eof); + std::string ps + = (promptflag > 0 + ? m_input_system.PS1 () : m_input_system.PS2 ()); + + std::string prompt = command_editor::decode_prompt_string (ps); + + bool eof = false; + std::string input_line = reader.get_input (prompt, eof); if (eof) {
--- a/libinterp/parse-tree/lex.h Fri Oct 18 07:41:02 2019 -0400 +++ b/libinterp/parse-tree/lex.h Fri Oct 18 09:08:06 2019 -0400 @@ -709,13 +709,20 @@ // Object that collects comment text. comment_buffer m_comment_buf; - virtual void increment_promptflag (void) = 0; + int m_promptflag; - virtual void decrement_promptflag (void) = 0; + void increment_promptflag (void) { m_promptflag++; } + + void decrement_promptflag (void) { m_promptflag--; } - virtual int promptflag (void) const = 0; + int promptflag (void) const { return m_promptflag; } - virtual int promptflag (int) = 0; + int promptflag (int n) + { + int retval = m_promptflag; + m_promptflag = n; + return retval; + } virtual std::string input_source (void) const { return "unknown"; } @@ -799,14 +806,6 @@ base_lexer::reset (); } - void increment_promptflag (void) { m_reader.increment_promptflag (); } - - void decrement_promptflag (void) { m_reader.decrement_promptflag (); } - - int promptflag (void) const { return m_reader.promptflag (); } - - int promptflag (int n) { return m_reader.promptflag (n); } - std::string input_source (void) const { return m_reader.input_source (); @@ -838,25 +837,25 @@ public: push_lexer (interpreter& interp) - : base_lexer (interp), m_pflag (1) + : base_lexer (interp) { append_input ("", false); } push_lexer (const std::string& input, interpreter& interp) - : base_lexer (interp), m_pflag (1) + : base_lexer (interp) { append_input (input, false); } push_lexer (bool eof, interpreter& interp) - : base_lexer (interp), m_pflag (1) + : base_lexer (interp) { append_input ("", eof); } push_lexer (const std::string& input, bool eof, interpreter& interp) - : base_lexer (interp), m_pflag (1) + : base_lexer (interp) { append_input (input, eof); } @@ -869,35 +868,11 @@ bool is_push_lexer (void) const { return true; } - void reset (void) - { - promptflag (1); - - base_lexer::reset (); - } - void append_input (const std::string& input, bool eof); - void increment_promptflag (void) { m_pflag++; } - - void decrement_promptflag (void) { m_pflag--; } - - int promptflag (void) const { return m_pflag; } - - int promptflag (int n) - { - int retval = m_pflag; - m_pflag = n; - return retval; - } - std::string input_source (void) const { return "push buffer"; } int fill_flex_buffer (char *buf, unsigned int max_size); - - protected: - - int m_pflag; }; }
--- a/libinterp/parse-tree/lex.ll Fri Oct 18 07:41:02 2019 -0400 +++ b/libinterp/parse-tree/lex.ll Fri Oct 18 09:08:06 2019 -0400 @@ -3711,8 +3711,15 @@ if (m_input_buf.empty ()) { + input_system& input_sys = m_interpreter.get_input_system (); + + std::string ps + = promptflag () > 0 ? input_sys.PS1 () : input_sys.PS2 (); + + std::string prompt = command_editor::decode_prompt_string (ps); + bool eof = false; - m_current_input_line = m_reader.get_input (eof); + m_current_input_line = m_reader.get_input (prompt, eof); m_input_buf.fill (m_current_input_line, eof);
--- a/libinterp/parse-tree/parse.h Fri Oct 18 07:41:02 2019 -0400 +++ b/libinterp/parse-tree/parse.h Fri Oct 18 09:08:06 2019 -0400 @@ -457,6 +457,14 @@ const std::string& package_name, bool require_file, bool force_script, bool autoload, bool relative_lookup); + void increment_promptflag (void) { m_lexer.increment_promptflag (); } + + void decrement_promptflag (void) { m_lexer.decrement_promptflag (); } + + int promptflag (void) const { return m_lexer.promptflag (); } + + int promptflag (int n) { return m_lexer.promptflag (n); } + protected: // Contains error message if Bison-generated parser returns non-zero