octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #45967] More consistent error message style fo


From: Rik
Subject: [Octave-bug-tracker] [bug #45967] More consistent error message style for parse errors
Date: Fri, 12 Jan 2024 19:57:06 -0500 (EST)

Follow-up Comment #3, bug#45967 (group octave):

This doesn't seem so hard to fix.  The code is in libinterp/parse-tree.  The
code in question is yyerror.  Using grep, I only see 4 instances.


oct-parse.cc:1568:        yyerror (parser, YY_("syntax error: cannot back
up")); \
oct-parse.cc:3055:      yyerror (parser, YY_("memory exhausted"));
oct-parse.cc:6316:      yyerror (parser, YY_("syntax error"));
oct-parse.cc:6427:  yyerror (parser, YY_("memory exhausted"));


This eventually calls this function in oct-parse.cc


void
base_parser::bison_error (const std::string& str, int err_line, int err_col)
{
  std::ostringstream output_buf;

  if (m_lexer.m_reading_fcn_file || m_lexer.m_reading_script_file
      || m_lexer.m_reading_classdef_file)
    output_buf << "parse error near line " << err_line
               << " of file " << m_lexer.m_fcn_file_full_name;
  else
    output_buf << "parse error:";

  if (str != "parse error")
    output_buf << "\n\n  " << str;

  output_buf << "\n\n";

  std::string curr_line;

  if (m_lexer.m_reading_fcn_file || m_lexer.m_reading_script_file
      || m_lexer.m_reading_classdef_file)
    curr_line = get_file_line (m_lexer.m_fcn_file_full_name, err_line);
  else
    curr_line = m_lexer.m_current_input_line;

  // Adjust the error column for display because it is 1-based in the
  // lexer for easier reporting.
  err_col--;

  if (! curr_line.empty ())
    {
      // FIXME: we could do better if we just cached lines from the
      // input file in a list.  See also functions for managing input
      // buffers in lex.ll.

      std::size_t len = curr_line.length ();

      if (curr_line[len-1] == '\n')
        curr_line.resize (len-1);

      // Print the line, maybe with a pointer near the error token.

      output_buf << ">>> " << curr_line << "\n";

      if (err_col == 0)
        err_col = len;

      for (int i = 0; i < err_col + 3; i++)
        output_buf << " ";

      output_buf << "^";
    }

  output_buf << "\n";

  m_parse_error_msg = output_buf.str ();
}


Octave apparently has the column information so it seems like it would be
helpful to print it.

I worked up a small changeset which is attached.  Using it, the output is 


octave:1> myfoo
error: syntax error near line 3, column 4
error: called from
    myfoo at line 3 column 4
octave:2> mybar
error: 'i_am_a_bug' undefined near line 3, column 1
error: called from
    mybar at line 3 column 1
octave:3> i am a bug
error: syntax error

>>> i am a bug
       ^
octave:3> 




(file #55558)

    _______________________________________________________

Additional Item Attachment:

File name: bug45967.cset                  Size:4 KB
    <https://file.savannah.gnu.org/file/bug45967.cset?file_id=55558>


    AGPL NOTICE

These attachments are served by Savane. You can download the corresponding
source code of Savane at
https://git.savannah.nongnu.org/cgit/administration/savane.git/snapshot/savane-627f867e2787b957955b95c61df60771504f1674.tar.gz


    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?45967>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]