Blame src/position.hpp

Packit Service 7770af
#ifndef SASS_POSITION_H
Packit Service 7770af
#define SASS_POSITION_H
Packit Service 7770af
Packit Service 7770af
#include <string>
Packit Service 7770af
#include <cstring>
Packit Service 7770af
// #include <iostream>
Packit Service 7770af
Packit Service 7770af
namespace Sass {
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
  class Offset {
Packit Service 7770af
Packit Service 7770af
    public: // c-tor
Packit Service 7770af
      Offset(const char* string);
Packit Service 7770af
      Offset(const std::string& text);
Packit Service 7770af
      Offset(const size_t line, const size_t column);
Packit Service 7770af
Packit Service 7770af
      // return new position, incremented by the given string
Packit Service 7770af
      Offset add(const char* begin, const char* end);
Packit Service 7770af
      Offset inc(const char* begin, const char* end) const;
Packit Service 7770af
Packit Service 7770af
      // init/create instance from const char substring
Packit Service 7770af
      static Offset init(const char* beg, const char* end);
Packit Service 7770af
Packit Service 7770af
    public: // overload operators for position
Packit Service 7770af
      void operator+= (const Offset &pos;;
Packit Service 7770af
      bool operator== (const Offset &pos) const;
Packit Service 7770af
      bool operator!= (const Offset &pos) const;
Packit Service 7770af
      Offset operator+ (const Offset &off) const;
Packit Service 7770af
      Offset operator- (const Offset &off) const;
Packit Service 7770af
Packit Service 7770af
    public: // overload output stream operator
Packit Service 7770af
      // friend std::ostream& operator<<(std::ostream& strm, const Offset& off);
Packit Service 7770af
Packit Service 7770af
    public:
Packit Service 7770af
      Offset off() { return *this; }
Packit Service 7770af
Packit Service 7770af
    public:
Packit Service 7770af
      size_t line;
Packit Service 7770af
      size_t column;
Packit Service 7770af
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  class Position : public Offset {
Packit Service 7770af
Packit Service 7770af
    public: // c-tor
Packit Service 7770af
      Position(const size_t file); // line(0), column(0)
Packit Service 7770af
      Position(const size_t file, const Offset& offset);
Packit Service 7770af
      Position(const size_t line, const size_t column); // file(-1)
Packit Service 7770af
      Position(const size_t file, const size_t line, const size_t column);
Packit Service 7770af
Packit Service 7770af
    public: // overload operators for position
Packit Service 7770af
      void operator+= (const Offset &off;;
Packit Service 7770af
      bool operator== (const Position &pos) const;
Packit Service 7770af
      bool operator!= (const Position &pos) const;
Packit Service 7770af
      const Position operator+ (const Offset &off) const;
Packit Service 7770af
      const Offset operator- (const Offset &off) const;
Packit Service 7770af
      // return new position, incremented by the given string
Packit Service 7770af
      Position add(const char* begin, const char* end);
Packit Service 7770af
      Position inc(const char* begin, const char* end) const;
Packit Service 7770af
Packit Service 7770af
    public: // overload output stream operator
Packit Service 7770af
      // friend std::ostream& operator<<(std::ostream& strm, const Position& pos);
Packit Service 7770af
Packit Service 7770af
    public:
Packit Service 7770af
      size_t file;
Packit Service 7770af
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  // Token type for representing lexed chunks of text
Packit Service 7770af
  class Token {
Packit Service 7770af
  public:
Packit Service 7770af
    const char* prefix;
Packit Service 7770af
    const char* begin;
Packit Service 7770af
    const char* end;
Packit Service 7770af
Packit Service 7770af
    Token()
Packit Service 7770af
    : prefix(0), begin(0), end(0) { }
Packit Service 7770af
    Token(const char* b, const char* e)
Packit Service 7770af
    : prefix(b), begin(b), end(e) { }
Packit Service 7770af
    Token(const char* str)
Packit Service 7770af
    : prefix(str), begin(str), end(str + strlen(str)) { }
Packit Service 7770af
    Token(const char* p, const char* b, const char* e)
Packit Service 7770af
    : prefix(p), begin(b), end(e) { }
Packit Service 7770af
Packit Service 7770af
    size_t length()    const { return end - begin; }
Packit Service 7770af
    std::string ws_before() const { return std::string(prefix, begin); }
Packit Service 7770af
    const std::string to_string() const { return std::string(begin, end); }
Packit Service 7770af
    std::string time_wspace() const {
Packit Service 7770af
      std::string str(to_string());
Packit Service 7770af
      std::string whitespaces(" \t\f\v\n\r");
Packit Service 7770af
      return str.erase(str.find_last_not_of(whitespaces)+1);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    operator bool()        { return begin && end && begin >= end; }
Packit Service 7770af
    operator std::string() { return to_string(); }
Packit Service 7770af
Packit Service 7770af
    bool operator==(Token t)  { return to_string() == t.to_string(); }
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  class ParserState : public Position {
Packit Service 7770af
Packit Service 7770af
    public: // c-tor
Packit Service 7770af
      ParserState(const char* path, const char* src = 0, const size_t file = std::string::npos);
Packit Service 7770af
      ParserState(const char* path, const char* src, const Position& position, Offset offset = Offset(0, 0));
Packit Service 7770af
      ParserState(const char* path, const char* src, const Token& token, const Position& position, Offset offset = Offset(0, 0));
Packit Service 7770af
Packit Service 7770af
    public: // down casts
Packit Service 7770af
      Offset off() { return *this; }
Packit Service 7770af
      Position pos() { return *this; }
Packit Service 7770af
      ParserState pstate() { return *this; }
Packit Service 7770af
Packit Service 7770af
    public:
Packit Service 7770af
      const char* path;
Packit Service 7770af
      const char* src;
Packit Service 7770af
      Offset offset;
Packit Service 7770af
      Token token;
Packit Service 7770af
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
#endif