view libinterp/interpfcn/input.h @ 16203:127cccb037bf

move more global parser and lexer variables to classes * pt-check.h, pt-check.cc (tree_checker::file_name): New data member. (tree_checker::gripe): Use it instead of curr_fcn_file_name. * input.h, input.cc, octave.cc (input_from_command_line_file): Delete global variable and all uses. * parse.h, oct-parse.in.yy (input_from_startup_file): Delete global variable and all uses. * input.h, input.cc, lex.h, lex.ll (curr_fcn_file_name, curr_fcn_file_full_name): Declare as members of lexical_feedback class. Rename to fcn_file_name and fcn_file_full_name. Change all uses. * oct-parse.in.yy (parse_fcn_file): New arg, file. Set curr_lexer->fcn_file_name and curr_lexer->fcn_file_full_name here. (load_fcn_from_file): Pass short file name to parse_fcn_file. * octave.cc (execute_command_line_file): Not here. * lex.h, lex.ll (lexical_feedback::force_script): New data member. * oct-parse.in.yy (parse_fcn_file): Set it here. * lex.h, lex.ll (lexical_feedback::input_from_terminal, lexical_feedback::input_from_file): New functions. * lex.ll (octave_lexer::handle_keyword): Set reading_fcn_file, reading_classdef_file, and reading_script_file. * lex.h, lex.ll (lexical_feedback::token_count): New variable. (COUNT_TOK_AND_RETURN): Increment it here. Don't count '\n' as a token. * lex.h, lex.ll (lexical_feedback::help_text): New variable. * parse.h, parse.in.yy (help_buf): Delete global variable and all uses. (octave_parser::frob_function, octave_parser::make_script): Use help_text. * lex.ll (octave_lexer::process_comment): Cache doc string directly in help_text variable. (looks_like_copyright): Move here from parse.in.yy. * lex.h, lex.ll (octave_lexer::prep_for_file): New function. (octave_lexer::prep_for_function_file, octave_lexer::prep_for_script_file): Delete. * parse.in.yy (INPUT_FILE_BEGIN): New start state. Delete SCRIPT_FILE_BEGIN and FCN_FILE_BEGIN. Tentatively set curr_lexer->reading_script_file to true. (parse_fcn_file): Call curr_lexer->prep_for_file. Don't call gobble_leading_whitespace. Don't attempt to determine function script, or classdef file status here. * parse.in.yy (INPUT_FILE): New token. (SCRIPT_FILE, FUNCTION_FILE): Delete. * lex.ll (octave_lexer::display_token): Update. * parse.in.yy (nl, opt_nl): New non-terminals. (function_file): Delete rule. (file): Rename from script_file. Allow opt_nl before opt_list. Don't make script if reading fcn file. * parse.in.yy (text_getc, class stdio_stream_reader, skip_white_space, looking_at_classdef_keyword, gobble_leading_white_space, looking_at_function_keyword): Delete. (get_help_from_file): Parse file to get help instead of calling gobble_leading_white_space
author John W. Eaton <jwe@octave.org>
date Wed, 06 Mar 2013 14:36:19 -0500
parents 743b895259e0
children 0467d68ca891
line wrap: on
line source

/*

Copyright (C) 1993-2012 John W. Eaton

This file is part of Octave.

Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, see
<http://www.gnu.org/licenses/>.

*/

// Use the GNU readline library for command line editing and hisory.

#if !defined (octave_input_h)
#define octave_input_h 1

#include <cstdio>

#include <string>

#include "oct-time.h"
#include "oct-obj.h"
#include "pager.h"

class octave_value;

extern OCTINTERP_API FILE *get_input_from_stdin (void);

// TRUE means that stdin is a terminal, not a pipe or redirected file.
extern bool stdin_is_tty;

// TRUE means this is an interactive shell.
extern bool interactive;

// TRUE means the user forced this shell to be interactive (-i).
extern bool forced_interactive;

// Should we issue a prompt?
extern int promptflag;

// A line of input.
extern std::string current_input_line;

// TRUE after a call to completion_matches.
extern bool octave_completion_matches_called;

// TRUE if the plotting system has requested a call to drawnow at
// the next user prompt.
extern OCTINTERP_API bool Vdrawnow_requested;

// TRUE if we are in debugging mode.
extern OCTINTERP_API bool Vdebugging;

extern void initialize_command_input (void);

extern bool octave_yes_or_no (const std::string& prompt);

extern octave_value do_keyboard (const octave_value_list& args = octave_value_list ());

extern std::string VPS4;

extern char Vfilemarker;

enum echo_state
{
  ECHO_OFF = 0,
  ECHO_SCRIPTS = 1,
  ECHO_FUNCTIONS = 2,
  ECHO_CMD_LINE = 4
};

extern int Vecho_executing_commands;

extern octave_time Vlast_prompt_time;

class
octave_base_reader
{
public:

  friend class octave_input_reader;

  octave_base_reader (void) : count (1) { }

  octave_base_reader (const octave_base_reader&) : count (1) { }

  virtual ~octave_base_reader (void) { }

  virtual std::string get_input (bool& eof) = 0;

  virtual std::string input_source (void) const { return in_src; }

  std::string octave_gets (bool& eof);

private:

  int count;

  static const std::string in_src;
};

class
octave_terminal_reader : public octave_base_reader
{
public:

  octave_terminal_reader (void) : octave_base_reader () { }

  std::string get_input (bool& eof);

  std::string input_source (void) const { return in_src; }

private:

  static const std::string in_src;
};

class
octave_file_reader : public octave_base_reader
{
public:

  octave_file_reader (FILE *f_arg)
    : octave_base_reader (), file (f_arg) { }

  std::string get_input (bool& eof);

  std::string input_source (void) const { return in_src; }

private:

  FILE *file;

  static const std::string in_src;
};

class
octave_eval_string_reader : public octave_base_reader
{
public:

  octave_eval_string_reader (const std::string& str)
    : octave_base_reader (), eval_string (str)
  { }

  std::string get_input (bool& eof);

  std::string input_source (void) const { return in_src; }

private:

  std::string eval_string;

  static const std::string in_src;
};

class
octave_input_reader
{
public:
  octave_input_reader (void)
    : rep (new octave_terminal_reader ())
  { }

  octave_input_reader (FILE *file)
    : rep (new octave_file_reader (file))
  { }

  octave_input_reader (const std::string& str)
    : rep (new octave_eval_string_reader (str))
  { }

  octave_input_reader (const octave_input_reader& ir)
  {
    rep = ir.rep;
    rep->count++;
  }

  octave_input_reader& operator = (const octave_input_reader& ir)
  {
    if (&ir != this)
      {
        rep = ir.rep;
        rep->count++;
      }

    return *this;
  }

  ~octave_input_reader (void)
  {
    if (--rep->count == 0)
      delete rep;
  }

  std::string get_input (bool& eof)
  {
    return rep->get_input (eof);
  }

  std::string input_source (void) const
  {
    return rep->input_source ();
  }

private:

  octave_base_reader *rep;
};

#endif