|
Packit Service |
7770af |
#include "sass.hpp"
|
|
Packit Service |
7770af |
#include "position.hpp"
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
namespace Sass {
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Offset::Offset(const char* string)
|
|
Packit Service |
7770af |
: line(0), column(0)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
*this = inc(string, string + strlen(string));
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Offset::Offset(const std::string& text)
|
|
Packit Service |
7770af |
: line(0), column(0)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
*this = inc(text.c_str(), text.c_str() + text.size());
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Offset::Offset(const size_t line, const size_t column)
|
|
Packit Service |
7770af |
: line(line), column(column) { }
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// init/create instance from const char substring
|
|
Packit Service |
7770af |
Offset Offset::init(const char* beg, const char* end)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
Offset offset(0, 0);
|
|
Packit Service |
7770af |
if (end == 0) {
|
|
Packit Service |
7770af |
end += strlen(beg);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
offset.add(beg, end);
|
|
Packit Service |
7770af |
return offset;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// increase offset by given string (mostly called by lexer)
|
|
Packit Service |
7770af |
// increase line counter and count columns on the last line
|
|
Packit Service |
7770af |
// ToDo: make the col count utf8 aware
|
|
Packit Service |
7770af |
Offset Offset::add(const char* begin, const char* end)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
if (end == 0) return *this;
|
|
Packit Service |
7770af |
while (begin < end && *begin) {
|
|
Packit Service |
7770af |
if (*begin == '\n') {
|
|
Packit Service |
7770af |
++ line;
|
|
Packit Service |
7770af |
// start new line
|
|
Packit Service |
7770af |
column = 0;
|
|
Packit Service |
7770af |
} else {
|
|
Packit Service |
7770af |
++ column;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
++begin;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
return *this;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// increase offset by given string (mostly called by lexer)
|
|
Packit Service |
7770af |
// increase line counter and count columns on the last line
|
|
Packit Service |
7770af |
Offset Offset::inc(const char* begin, const char* end) const
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
Offset offset(line, column);
|
|
Packit Service |
7770af |
offset.add(begin, end);
|
|
Packit Service |
7770af |
return offset;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
bool Offset::operator== (const Offset &pos) const
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
return line == pos.line && column == pos.column;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
bool Offset::operator!= (const Offset &pos) const
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
return line != pos.line || column != pos.column;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
void Offset::operator+= (const Offset &off)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
*this = Offset(line + off.line, off.line > 0 ? off.column : column + off.column);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Offset Offset::operator+ (const Offset &off) const
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
return Offset(line + off.line, off.line > 0 ? off.column : column + off.column);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Offset Offset::operator- (const Offset &off) const
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
return Offset(line - off.line, off.line == line ? column - off.column : column);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Position::Position(const size_t file)
|
|
Packit Service |
7770af |
: Offset(0, 0), file(file) { }
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Position::Position(const size_t file, const Offset& offset)
|
|
Packit Service |
7770af |
: Offset(offset), file(file) { }
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Position::Position(const size_t line, const size_t column)
|
|
Packit Service |
7770af |
: Offset(line, column), file(-1) { }
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Position::Position(const size_t file, const size_t line, const size_t column)
|
|
Packit Service |
7770af |
: Offset(line, column), file(file) { }
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
ParserState::ParserState(const char* path, const char* src, const size_t file)
|
|
Packit Service |
7770af |
: Position(file, 0, 0), path(path), src(src), offset(0, 0), token() { }
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
ParserState::ParserState(const char* path, const char* src, const Position& position, Offset offset)
|
|
Packit Service |
7770af |
: Position(position), path(path), src(src), offset(offset), token() { }
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
ParserState::ParserState(const char* path, const char* src, const Token& token, const Position& position, Offset offset)
|
|
Packit Service |
7770af |
: Position(position), path(path), src(src), offset(offset), token(token) { }
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Position Position::add(const char* begin, const char* end)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
Offset::add(begin, end);
|
|
Packit Service |
7770af |
return *this;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
Position Position::inc(const char* begin, const char* end) const
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
Offset offset(line, column);
|
|
Packit Service |
7770af |
offset = offset.inc(begin, end);
|
|
Packit Service |
7770af |
return Position(file, offset);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
bool Position::operator== (const Position &pos) const
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
return file == pos.file && line == pos.line && column == pos.column;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
bool Position::operator!= (const Position &pos) const
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
return file == pos.file || line != pos.line || column != pos.column;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
void Position::operator+= (const Offset &off)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
*this = Position(file, line + off.line, off.line > 0 ? off.column : column + off.column);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
const Position Position::operator+ (const Offset &off) const
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
return Position(file, line + off.line, off.line > 0 ? off.column : column + off.column);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
const Offset Position::operator- (const Offset &off) const
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
return Offset(line - off.line, off.line == line ? column - off.column : column);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
/* not used anymore - remove?
|
|
Packit Service |
7770af |
std::ostream& operator<<(std::ostream& strm, const Offset& off)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
if (off.line == string::npos) strm << "-1:"; else strm << off.line << ":";
|
|
Packit Service |
7770af |
if (off.column == string::npos) strm << "-1"; else strm << off.column;
|
|
Packit Service |
7770af |
return strm;
|
|
Packit Service |
7770af |
} */
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
/* not used anymore - remove?
|
|
Packit Service |
7770af |
std::ostream& operator<<(std::ostream& strm, const Position& pos)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
if (pos.file != string::npos) strm << pos.file << ":";
|
|
Packit Service |
7770af |
if (pos.line == string::npos) strm << "-1:"; else strm << pos.line << ":";
|
|
Packit Service |
7770af |
if (pos.column == string::npos) strm << "-1"; else strm << pos.column;
|
|
Packit Service |
7770af |
return strm;
|
|
Packit Service |
7770af |
} */
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
}
|