Changeset 9ed4f94


Ignore:
Timestamp:
Aug 30, 2017, 8:08:01 AM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
e5fccf4
Parents:
fc1ef62
Message:

update error message printing and add column to parsing errors

Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Common/SemanticError.cc

    rfc1ef62 r9ed4f94  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 19 07:21:25 2015
    13 // Update Count     : 1
     12// Last Modified On : Tue Aug 29 18:17:35 2017
     13// Update Count     : 3
    1414//
    1515
    16 #include <cstdio>            // for fileno, stderr
    17 #include <unistd.h>          // for isatty
    18 #include <iostream>          // for basic_ostream, operator<<, ostream
    19 #include <list>              // for list, _List_iterator
    20 #include <string>            // for string, operator<<, operator+, to_string
     16#include <cstdio>                                                                               // for fileno, stderr
     17#include <unistd.h>                                                                             // for isatty
     18#include <iostream>                                                                             // for basic_ostream, operator<<, ostream
     19#include <list>                                                                                 // for list, _List_iterator
     20#include <string>                                                                               // for string, operator<<, operator+, to_string
    2121
    22 #include "Common/utility.h"  // for to_string, CodeLocation (ptr only)
     22#include "Common/utility.h"                                                             // for to_string, CodeLocation (ptr only)
    2323#include "SemanticError.h"
    24 
    25 inline const std::string& error_str() {
    26         static std::string str = isatty( fileno(stderr) ) ? "\e[31merror:\e[39m " : "error: ";
    27         return str;
    28 }
    2924
    3025SemanticError::SemanticError() {
     
    4944void SemanticError::print( std::ostream &os ) {
    5045        using std::to_string;
    51         for(auto err : errors) {
    52                 os << to_string( err.location ) << err.description << '\n';
     46        for( auto err : errors ) {
     47                os << to_string( err.location ) << err.description << std::endl;
    5348        }
    5449}
  • src/Common/SemanticError.h

    rfc1ef62 r9ed4f94  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Thr Aug 17 14:01:00 2017
    13 // Update Count     : 7
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue Aug 29 22:03:36 2017
     13// Update Count     : 17
    1414//
    1515
    1616#pragma once
    1717
    18 #include <exception>  // for exception
    19 #include <iostream>   // for ostream
    20 #include <list>       // for list
    21 #include <string>     // for string
     18#include <exception>                                                                    // for exception
     19#include <iostream>                                                                             // for ostream
     20#include <list>                                                                                 // for list
     21#include <string>                                                                               // for string
     22#include <unistd.h>                                                                             // for isatty
    2223
    23 #include "CodeLocation.h"  // for CodeLocation, toString
     24#include "CodeLocation.h"                                                               // for CodeLocation, toString
    2425
    2526struct error {
     
    2829
    2930        error() = default;
    30         error( const std::string& str ) : description( str ) {}
     31        error( const std::string & str ) : description( str ) {}
    3132
    32         void maybeSet( const CodeLocation& location ) {
     33        void maybeSet( const CodeLocation & location ) {
    3334                if( this->location.linenumber < 0 ) {
    3435                        this->location = location;
     
    4142        SemanticError();
    4243        SemanticError( std::string error );
    43         template< typename T > SemanticError( const std::string &error, const T *obj );
     44        template< typename T > SemanticError( const std::string & error, const T * obj );
    4445        ~SemanticError() throw() {}
    4546
    46         void append( SemanticError &other );
     47        static inline const std::string & error_str() {
     48                static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
     49                return str;
     50        }
     51
     52        void append( SemanticError & other );
    4753        void append( const std::string & );
    4854        bool isEmpty() const;
    49         void print( std::ostream &os );
     55        void print( std::ostream & os );
    5056
    51         void set_location( const CodeLocation& location );
    52         // constructs an exception using the given message and the printed
    53         // representation of the obj (T must have a print method)
     57        void set_location( const CodeLocation & location );
     58        // constructs an exception using the given message and the printed representation of the obj (T must have a print
     59        // method)
    5460  private:
    5561        std::list< error > errors;
     
    5763
    5864template< typename T >
    59 SemanticError::SemanticError( const std::string &error, const T *obj ) {
     65SemanticError::SemanticError( const std::string & error, const T * obj ) {
    6066        append( toString( error, obj ) );
    6167}
  • src/Parser/lex.ll

    rfc1ef62 r9ed4f94  
    1010 * Created On       : Sat Sep 22 08:58:10 2001
    1111 * Last Modified By : Peter A. Buhr
    12  * Last Modified On : Tue Aug 22 22:43:39 2017
    13  * Update Count     : 558
     12 * Last Modified On : Wed Aug 30 07:53:24 2017
     13 * Update Count     : 583
    1414 */
    1515
     
    1919
    2020%{
    21 // This lexer assumes the program has been preprocessed by cpp. Hence, all user level preprocessor directive have been
     21// The lexer assumes the program has been preprocessed by cpp. Hence, all user level preprocessor directive have been
    2222// performed and removed from the source. The only exceptions are preprocessor directives passed to the compiler (e.g.,
    2323// line-number directives) and C/C++ style comments, which are ignored.
     
    2525//**************************** Includes and Defines ****************************
    2626
     27unsigned int column = 0;                                                                // position of the end of the last token parsed
     28#define YY_USER_ACTION column += yyleng;                                // trigger before each matching rule's action
     29
    2730#include <string>
    2831#include <cstdio>                                                                               // FILENAME_MAX
     32using namespace std;
    2933
    3034#include "ParseNode.h"
     
    3236
    3337char *yyfilename;
    34 std::string *strtext;                                                                   // accumulate parts of character and string constant value
     38string *strtext;                                                                                // accumulate parts of character and string constant value
    3539
    3640#define RETURN_LOCN(x)          yylval.tok.loc.file = yyfilename; yylval.tok.loc.line = yylineno; return( x )
    37 #define RETURN_VAL(x)           yylval.tok.str = new std::string( yytext ); RETURN_LOCN( x )
     41#define RETURN_VAL(x)           yylval.tok.str = new string( yytext ); RETURN_LOCN( x )
    3842#define RETURN_CHAR(x)          yylval.tok.str = nullptr; RETURN_LOCN( x )
    3943#define RETURN_STR(x)           yylval.tok.str = strtext; RETURN_LOCN( x )
    4044
    4145#define WHITE_RETURN(x)         // do nothing
    42 #define NEWLINE_RETURN()        WHITE_RETURN( '\n' )
     46#define NEWLINE_RETURN()        column = 0; WHITE_RETURN( '\n' )
    4347#define ASCIIOP_RETURN()        RETURN_CHAR( (int)yytext[0] ) // single character operator
    4448#define NAMEDOP_RETURN(x)       RETURN_CHAR( x )                        // multichar operator, with a name
     
    154158                memcpy( &filename, begin_string + 1, length );  // copy file name from yytext
    155159                filename[ length ] = '\0';                                              // terminate string with sentinel
    156                 //std::cout << "file " << filename << " line " << lineno << std::endl;
     160                //cout << "file " << filename << " line " << lineno << endl;
    157161                yylineno = lineno;
    158162                yyfilename = filename;
     
    302306
    303307                                /* character constant, allows empty value */
    304 ({cwide_prefix}[_]?)?['] { BEGIN QUOTE; rm_underscore(); strtext = new std::string( yytext, yyleng ); }
     308({cwide_prefix}[_]?)?['] { BEGIN QUOTE; rm_underscore(); strtext = new string( yytext, yyleng ); }
    305309<QUOTE>[^'\\\n]* { strtext->append( yytext, yyleng ); }
    306310<QUOTE>['\n]    { BEGIN 0; strtext->append( yytext, yyleng ); RETURN_STR(CHARACTERconstant); }
     
    308312
    309313                                /* string constant */
    310 ({swide_prefix}[_]?)?["] { BEGIN STRING; rm_underscore(); strtext = new std::string( yytext, yyleng ); }
     314({swide_prefix}[_]?)?["] { BEGIN STRING; rm_underscore(); strtext = new string( yytext, yyleng ); }
    311315<STRING>[^"\\\n]* { strtext->append( yytext, yyleng ); }
    312316<STRING>["\n]   { BEGIN 0; strtext->append( yytext, yyleng ); RETURN_STR(STRINGliteral); }
     
    422426}
    423427
    424                                 /* unknown characters */
    425 .                               { printf("unknown character(s):\"%s\" on line %d\n", yytext, yylineno); }
     428                                /* unknown character */
     429.                               { yyerror( "unknown character" ); }
    426430
    427431%%
     432// ----end of lexer----
     433
     434void yyerror( const char * errmsg ) {
     435        cout << (yyfilename ? yyfilename : "*unknown file*") << ':' << yylineno << ':' << column - yyleng + 1
     436                 << ':' << SemanticError::error_str() << errmsg << " at token \"" << (yytext[0] == '\0' ? "EOF" : yytext) << '"' << endl;
     437}
    428438
    429439// Local Variables: //
  • src/Parser/parser.yy

    rfc1ef62 r9ed4f94  
    1010// Created On       : Sat Sep  1 20:22:55 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug 28 13:24:10 2017
    13 // Update Count     : 2720
     12// Last Modified On : Wed Aug 30 07:04:19 2017
     13// Update Count     : 2740
    1414//
    1515
     
    4848#include <cstdio>
    4949#include <stack>
     50using namespace std;
     51
    5052#include "ParseNode.h"
    5153#include "TypedefTable.h"
    5254#include "TypeData.h"
    5355#include "LinkageSpec.h"
    54 using namespace std;
     56#include "Common/SemanticError.h"                                               // error_str
    5557
    5658extern DeclarationNode * parseTree;
     
    31333135// ----end of grammar----
    31343136
    3135 extern char *yytext;
    3136 
    3137 void yyerror( const char * ) {
    3138         if ( yyfilename ) {
    3139                 cout << yyfilename << ":";
    3140         } // if
    3141         cout << yylineno << ":1 syntax error at token \"" << (yytext[0] == '\0' ? "EOF" : yytext) << "\"" << endl;
    3142 }
    3143 
    31443137// Local Variables: //
    31453138// mode: c++ //
Note: See TracChangeset for help on using the changeset viewer.