Blame src/backtrace.hpp

Packit Service 7770af
#ifndef SASS_BACKTRACE_H
Packit Service 7770af
#define SASS_BACKTRACE_H
Packit Service 7770af
Packit Service 7770af
#include <sstream>
Packit Service 7770af
Packit Service 7770af
#include "file.hpp"
Packit Service 7770af
#include "position.hpp"
Packit Service 7770af
Packit Service 7770af
namespace Sass {
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
  struct Backtrace {
Packit Service 7770af
Packit Service 7770af
    Backtrace*  parent;
Packit Service 7770af
    ParserState pstate;
Packit Service 7770af
    std::string      caller;
Packit Service 7770af
Packit Service 7770af
    Backtrace(Backtrace* prn, ParserState pstate, std::string c)
Packit Service 7770af
    : parent(prn),
Packit Service 7770af
      pstate(pstate),
Packit Service 7770af
      caller(c)
Packit Service 7770af
    { }
Packit Service 7770af
Packit Service 7770af
    const std::string to_string(bool warning = false)
Packit Service 7770af
    {
Packit Service 7770af
      size_t i = -1;
Packit Service 7770af
      std::stringstream ss;
Packit Service 7770af
      std::string cwd(Sass::File::get_cwd());
Packit Service 7770af
      Backtrace* this_point = this;
Packit Service 7770af
Packit Service 7770af
      if (!warning) ss << std::endl << "Backtrace:";
Packit Service 7770af
      // the first tracepoint (which is parent-less) is an empty placeholder
Packit Service 7770af
      while (this_point->parent) {
Packit Service 7770af
Packit Service 7770af
        // make path relative to the current directory
Packit Service 7770af
        std::string rel_path(Sass::File::abs2rel(this_point->pstate.path, cwd, cwd));
Packit Service 7770af
Packit Service 7770af
        if (warning) {
Packit Service 7770af
          ss << std::endl
Packit Service 7770af
             << "\t"
Packit Service 7770af
             << (++i == 0 ? "on" : "from")
Packit Service 7770af
             << " line "
Packit Service 7770af
             << this_point->pstate.line + 1
Packit Service 7770af
             << " of "
Packit Service 7770af
             << rel_path;
Packit Service 7770af
        } else {
Packit Service 7770af
          ss << std::endl
Packit Service 7770af
             << "\t"
Packit Service 7770af
             << rel_path
Packit Service 7770af
             << ":"
Packit Service 7770af
             << this_point->pstate.line + 1
Packit Service 7770af
             << this_point->parent->caller;
Packit Service 7770af
        }
Packit Service 7770af
Packit Service 7770af
        this_point = this_point->parent;
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      return ss.str();
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    size_t depth()
Packit Service 7770af
    {
Packit Service 7770af
      size_t d = std::string::npos;
Packit Service 7770af
      Backtrace* p = parent;
Packit Service 7770af
      while (p) {
Packit Service 7770af
        ++d;
Packit Service 7770af
        p = p->parent;
Packit Service 7770af
      }
Packit Service 7770af
      return d;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
#endif