Changeset 294647b


Ignore:
Timestamp:
Feb 14, 2017, 2:54:51 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
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:
138e29e
Parents:
eafb094
Message:

Filename and linenumber handling for parsing errors

Location:
src
Files:
1 added
9 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
  • src/Parser/DeclarationNode.cc

    reafb094 r294647b  
    921921                                Declaration * decl = extr->build();
    922922                                if ( decl ) {
     923                                        decl->location = cur->location;
    923924                                        * out++ = decl;
    924925                                } // if
     
    928929                        Declaration * decl = cur->build();
    929930                        if ( decl ) {
     931                                decl->location = cur->location;
    930932                                * out++ = decl;
    931933                        } // if
    932934                } catch( SemanticError &e ) {
     935                        e.set_location( cur->location );
    933936                        errors.append( e );
    934937                } // try
     
    950953                        if ( decl ) {
    951954                                if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
     955                                        dwt->location = cur->location;
    952956                                        * out++ = dwt;
    953957                                } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
    954958                                        StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    955                                         * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
     959                                        auto obj = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
     960                                        obj->location = cur->location;
     961                                        * out++ = obj;
    956962                                        delete agg;
    957963                                } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
    958964                                        UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    959                                         * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
     965                                        auto obj = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
     966                                        obj->location = cur->location;
     967                                        * out++ = obj;
    960968                                } // if
    961969                        } // if
    962970                } catch( SemanticError &e ) {
     971                        e.set_location( cur->location );
    963972                        errors.append( e );
    964973                } // try
  • src/Parser/ParseNode.h

    reafb094 r294647b  
    3939//##############################################################################
    4040
     41extern char* yyfilename;
     42extern int yylineno;
     43
    4144class ParseNode {
    4245  public:
     
    6568        ParseNode * next = nullptr;
    6669        std::string * name = nullptr;
     70        CodeLocation location = { yyfilename, yylineno };
    6771}; // ParseNode
    6872
     
    410414        while ( cur ) {
    411415                try {
    412 //                      SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::result_of< decltype(&NodeType::build)(NodeType)>::type >( cur ) );
    413416                        SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) );
    414417                        if ( result ) {
     418                                result->location = cur->location;
    415419                                * out++ = result;
    416420                        } // if
    417421                } catch( SemanticError &e ) {
     422                        e.set_location( cur->location );
    418423                        errors.append( e );
    419424                } // try
  • src/SynTree/Declaration.h

    reafb094 r294647b  
    1717#define DECLARATION_H
    1818
     19#include <string>
     20
     21#include "BaseSyntaxNode.h"
     22#include "Mutator.h"
     23#include "Visitor.h"
    1924#include "SynTree.h"
    20 #include "Visitor.h"
    21 #include "Mutator.h"
    2225#include "Parser/LinkageSpec.h"
    2326#include "Parser/ParseNode.h"
    24 #include <string>
    25 
    26 class Declaration {
     27
     28class Declaration : public BaseSyntaxNode {
    2729  public:
    2830        Declaration( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage );
  • src/SynTree/Expression.h

    reafb094 r294647b  
    1919#include <map>
    2020#include <memory>
     21
     22#include "BaseSyntaxNode.h"
     23#include "Constant.h"
     24#include "Mutator.h"
    2125#include "SynTree.h"
    2226#include "Visitor.h"
    23 #include "Mutator.h"
    24 #include "Constant.h"
    2527#include "Common/UniqueName.h"
    2628
    2729/// Expression is the root type for all expressions
    28 class Expression {
     30class Expression : public BaseSyntaxNode{
    2931  public:
    3032        Expression( Expression * _aname = nullptr );
  • src/SynTree/Initializer.h

    reafb094 r294647b  
    1717#define INITIALIZER_H
    1818
     19#include <cassert>
     20
     21#include "BaseSyntaxNode.h"
     22#include "Mutator.h"
    1923#include "SynTree.h"
     24#include "Type.h"
    2025#include "Visitor.h"
    21 #include "Mutator.h"
    22 #include "Type.h"
    23 
    24 #include <cassert>
    2526
    2627const std::list<Expression*> noDesignators;
    2728
    2829// Initializer: base class for object initializers (provide default values)
    29 class Initializer {
     30class Initializer : public BaseSyntaxNode {
    3031  public:
    3132        //      Initializer( std::string _name = std::string(""), int _pos = 0 );
  • src/SynTree/Statement.h

    reafb094 r294647b  
    1717#define STATEMENT_H
    1818
     19#include "BaseSyntaxNode.h"
     20#include "Label.h"
     21#include "Mutator.h"
    1922#include "SynTree.h"
     23#include "Type.h"
    2024#include "Visitor.h"
    21 #include "Mutator.h"
    2225#include "Common/SemanticError.h"
    23 #include "Type.h"
    24 #include "Label.h"
    25 
    26 class Statement {
     26
     27class Statement : public BaseSyntaxNode {
    2728  public:
    2829        Statement( std::list<Label> labels );
Note: See TracChangeset for help on using the changeset viewer.