Changeset 294647b for src/Common


Ignore:
Timestamp:
Feb 14, 2017, 2:54:51 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
138e29e
Parents:
eafb094
Message:

Filename and linenumber handling for parsing errors

Location:
src/Common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Common/SemanticError.cc

    reafb094 r294647b  
    3434
    3535void SemanticError::append( const std::string & msg ) {
    36   errors.push_back( std::string( "Error: ") + msg );
     36  using std::to_string;
     37  const std::string loc = location.linenumber >= 0 ? "At \"" + to_string(location) + "\" " : "";
     38  errors.push_back( loc + "Error: " + msg );
    3739}
    3840
     
    4547}
    4648
     49void SemanticError::set_location( const CodeLocation& location ) {
     50  this->location = location;
     51  using std::to_string;
     52  const std::string loc = location.linenumber >= 0 ? "At \"" + to_string(location) + "\" " : "";
     53  auto& error = *errors.begin();
     54  error.insert( 0, loc.c_str());
     55}
     56
    4757// Local Variables: //
    4858// tab-width: 4 //
  • src/Common/SemanticError.h

    reafb094 r294647b  
    2323#include <iostream>
    2424
     25#include "utility.h"
     26
    2527class SemanticError : public std::exception {
    2628  public:
     
    3537        void print( std::ostream &os );
    3638
     39        void set_location( const CodeLocation& location );
    3740        // constructs an exception using the given message and the printed
    3841        // representation of the obj (T must have a print method)
    3942  private:
    4043        std::list< std::string > errors;
     44        CodeLocation location;
    4145};
    4246
  • src/Common/utility.h

    reafb094 r294647b  
    2525#include <sstream>
    2626#include <string>
     27
    2728#include <cassert>
    28 
    2929template< typename T >
    3030static inline T * maybeClone( const T *orig ) {
     
    303303        return group_iterate_t<Args...>(args...);
    304304}
     305
     306struct CodeLocation {
     307        int linenumber;
     308        std::string filename;
     309
     310        CodeLocation()
     311                : linenumber( -1 )
     312                , filename("")
     313        {}
     314
     315        CodeLocation( const char* filename, int lineno )
     316                : linenumber( lineno )
     317                , filename(filename ? filename : "")
     318        {}
     319};
     320
     321inline std::string to_string( const CodeLocation& location ) {
     322        return location.filename + ":" + std::to_string(location.linenumber);
     323}
    305324#endif // _UTILITY_H
    306325
Note: See TracChangeset for help on using the changeset viewer.