Commit 594d4b31 by Aaron Leung

Moving a node comparison function into the refactored version.

parent a82ee26f
...@@ -3,41 +3,64 @@ ...@@ -3,41 +3,64 @@
namespace Sass { namespace Sass {
using namespace std; using namespace std;
// Node::Node(Node_Impl* ip) : ip_(ip) { } // ------------------------------------------------------------------------
// // Node method implementations
// inline Node_Type Node::type() { return ip_->type; } // ------------------------------------------------------------------------
//
// inline bool Node::has_children() { return ip_->has_children; } bool Node::operator==(Node rhs) const
// inline bool Node::has_statements() { return ip_->has_statements; } {
// inline bool Node::has_blocks() { return ip_->has_blocks; } Type t = type();
// inline bool Node::has_expansions() { return ip_->has_expansions; } if (t != rhs.type()) return false;
// inline bool Node::has_backref() { return ip_->has_backref; }
// inline bool Node::from_variable() { return ip_->from_variable; } switch (t)
// inline bool Node::eval_me() { return ip_->eval_me; } {
// inline bool Node::is_unquoted() { return ip_->is_unquoted; } case comma_list:
// inline bool Node::is_numeric() { return ip_->is_numeric(); } case space_list:
// case expression:
// inline string Node::file_name() const { return *(ip_->file_name); } case term:
// inline size_t Node::line_number() const { return ip_->line_number; } case numeric_color: {
// inline size_t Node::size() const { return ip_->size(); } for (size_t i = 0; i < size(); ++i) {
// if (at(i) == rhs[i]) continue;
// inline Node& Node::at(size_t i) const { return ip_->at(i); } else return false;
// inline Node& Node::operator[](size_t i) const { return at(i); } }
// inline Node& Node::pop_back() { return ip_->pop_back(); } return true;
// inline Node& Node::push_back(Node n) } break;
// {
// ip_->push_back(n); case variable:
// return *this; case identifier:
// } case uri:
// inline Node& Node::operator<<(Node n) { return push_back(n); } case textual_percentage:
// inline Node& Node::operator+=(Node n) case textual_dimension:
// { case textual_number:
// for (size_t i = 0, L = n.size(); i < L; ++i) push_back(n[i]); case textual_hex:
// return *this; case string_constant: {
// } return token().unquote() == rhs.token().unquote();
// inline bool Node::boolean_value() { return ip_->boolean_value(); } } break;
// inline double Node::numeric_value() { return ip_->numeric_value(); }
case number:
case numeric_percentage: {
return numeric_value() == rhs.numeric_value();
} break;
case numeric_dimension: {
if (unit() == rhs.unit()) {
return numeric_value() == rhs.numeric_value();
}
else {
return false;
}
} break;
case boolean: {
return boolean_value() == rhs.boolean_value();
} break;
default: {
return true;
} break;
}
}
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Token method implementations // Token method implementations
...@@ -152,27 +175,6 @@ namespace Sass { ...@@ -152,27 +175,6 @@ namespace Sass {
// Node_Impl method implementations // Node_Impl method implementations
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// inline bool Node_Impl::is_numeric()
// { return type >= number && type <= numeric_dimension; }
//
// inline size_t Node_Impl::size()
// { return children.size(); }
//
// inline Node& Node_Impl::at(size_t i)
// { return children.at(i); }
//
// inline Node& Node_Impl::back()
// { return children.back(); }
//
// inline void Node_Impl::push_back(const Node& n)
// { children.push_back(n); }
//
// inline Node& Node_Impl::pop_back()
// { children.pop_back(); }
//
// inline bool Node_Impl::boolean_value()
// { return value.boolean; }
//
double Node_Impl::numeric_value() double Node_Impl::numeric_value()
{ {
switch (type) switch (type)
...@@ -189,24 +191,23 @@ namespace Sass { ...@@ -189,24 +191,23 @@ namespace Sass {
return 0; return 0;
} }
string Node_Impl::unit() extern const char percent_str[] = "%";
extern const char empty_str[] = "";
Token Node_Impl::unit()
{ {
switch (type) switch (type)
{ {
case Node::numeric_percentage: { case Node::numeric_percentage: {
return "\"%\""; return Token::make(percent_str);
} break; } break;
case Node::numeric_dimension: { case Node::numeric_dimension: {
string result("\""); return value.dimension.unit;
result += value.dimension.unit.to_string();
result += "\"";
return result;
} break; } break;
default: break; default: break;
} }
return "\"\""; return Token::make(empty_str);
} }
} }
\ No newline at end of file
...@@ -5,6 +5,57 @@ ...@@ -5,6 +5,57 @@
namespace Sass { namespace Sass {
using namespace std; using namespace std;
struct Token {
const char* begin;
const char* end;
// Need Token::make(...) because tokens are union members, and hence they
// can't have non-trivial constructors.
static Token make()
{
Token t;
t.begin = 0;
t.end = 0;
return t;
}
static Token make(const char* s)
{
Token t;
t.begin = s;
t.end = s + std::strlen(s);
return t;
}
static Token make(const char* b, const char* e)
{
Token t;
t.begin = b;
t.end = e;
return t;
}
size_t length() const
{ return end - begin; }
string to_string() const
{ return string(begin, end - begin); }
string unquote() const;
void unquote_to_stream(std::stringstream& buf) const;
operator bool()
{ return begin && end && begin >= end; }
bool operator<(const Token& rhs) const;
bool operator==(const Token& rhs) const;
};
struct Dimension {
double numeric;
Token unit;
};
struct Node_Impl; struct Node_Impl;
...@@ -14,7 +65,7 @@ namespace Sass { ...@@ -14,7 +65,7 @@ namespace Sass {
Node_Impl* ip_; Node_Impl* ip_;
// private constructors; must use a Node_Factory // private constructors; must use a Node_Factory
Node(); Node();
Node(Node_Impl* ip); // : ip_(ip) { } Node(Node_Impl* ip);
public: public:
enum Type { enum Type {
...@@ -101,87 +152,35 @@ namespace Sass { ...@@ -101,87 +152,35 @@ namespace Sass {
assignment assignment
}; };
Type type(); // { return ip_->type; } Type type() const;
bool has_children(); // { return ip_->has_children; } bool has_children() const;
bool has_statements(); // { return ip_->has_statements; } bool has_statements() const;
bool has_blocks(); // { return ip_->has_blocks; } bool has_blocks() const;
bool has_expansions(); // { return ip_->has_expansions; } bool has_expansions() const;
bool has_backref(); // { return ip_->has_backref; } bool has_backref() const;
bool from_variable(); // { return ip_->from_variable; } bool from_variable() const;
bool should_eval(); // { return ip_->should_eval; } bool should_eval() const;
bool is_unquoted(); // { return ip_->is_unquoted; } bool is_unquoted() const;
bool is_numeric(); // { return ip_->is_numeric(); } bool is_numeric() const;
string file_name() const; // { return *(ip_->file_name); } string file_name() const;
size_t line_number() const; // { return ip_->line_number; } size_t line_number() const;
size_t size() const; // { return ip_->size(); } size_t size() const;
Node& at(size_t i) const; // { return ip_->at(i); } Node& at(size_t i) const;
Node& operator[](size_t i) const; // { return at(i); } Node& operator[](size_t i) const;
void pop_back(); // { return ip_->pop_back(); } void pop_back();
Node& push_back(Node n); Node& push_back(Node n);
// { ip_->push_back(n); return *this; }
Node& operator<<(Node n); Node& operator<<(Node n);
// { return push_back(n); }
Node& operator+=(Node n); Node& operator+=(Node n);
// {
// for (size_t i = 0, L = n.size(); i < L; ++i) push_back(n[i]);
// return *this;
// }
bool boolean_value(); // { return ip_->boolean_value(); }
double numeric_value(); // { return ip_->numeric_value(); }
};
struct Token {
const char* begin;
const char* end;
// Need Token::make(...) because tokens are union members, and hence they
// can't have non-trivial constructors.
static Token make()
{
Token t;
t.begin = 0;
t.end = 0;
return t;
}
static Token make(const char* s)
{
Token t;
t.begin = s;
t.end = s + std::strlen(s);
return t;
}
static Token make(const char* b, const char* e)
{
Token t;
t.begin = b;
t.end = e;
return t;
}
size_t length() const bool boolean_value() const;
{ return end - begin; } double numeric_value() const;
Token token() const;
string to_string() const Token unit() const;
{ return string(begin, end - begin); }
string unquote() const;
void unquote_to_stream(std::stringstream& buf) const;
operator bool() bool operator==(Node rhs) const;
{ return begin && end && begin >= end; }
bool operator<(const Token& rhs) const;
bool operator==(const Token& rhs) const;
};
struct Dimension {
double numeric;
Token unit;
}; };
struct Node_Impl { struct Node_Impl {
...@@ -207,16 +206,6 @@ namespace Sass { ...@@ -207,16 +206,6 @@ namespace Sass {
bool from_variable; bool from_variable;
bool should_eval; bool should_eval;
bool is_unquoted; bool is_unquoted;
// bool is_numeric();
//
// size_t size();
// Node& at(size_t i);
// Node& back();
// Node& pop_back();
// void push_back(const Node& n);
//
// bool boolean_value();
bool is_numeric() bool is_numeric()
{ return type >= Node::number && type <= Node::numeric_dimension; } { return type >= Node::number && type <= Node::numeric_dimension; }
...@@ -240,7 +229,7 @@ namespace Sass { ...@@ -240,7 +229,7 @@ namespace Sass {
{ return value.boolean; } { return value.boolean; }
double numeric_value(); double numeric_value();
string unit(); Token unit();
}; };
...@@ -252,17 +241,17 @@ namespace Sass { ...@@ -252,17 +241,17 @@ namespace Sass {
inline Node::Node(Node_Impl* ip) : ip_(ip) { } inline Node::Node(Node_Impl* ip) : ip_(ip) { }
inline Node::Type Node::type() { return ip_->type; } inline Node::Type Node::type() const { return ip_->type; }
inline bool Node::has_children() { return ip_->has_children; } inline bool Node::has_children() const { return ip_->has_children; }
inline bool Node::has_statements() { return ip_->has_statements; } inline bool Node::has_statements() const { return ip_->has_statements; }
inline bool Node::has_blocks() { return ip_->has_blocks; } inline bool Node::has_blocks() const { return ip_->has_blocks; }
inline bool Node::has_expansions() { return ip_->has_expansions; } inline bool Node::has_expansions() const { return ip_->has_expansions; }
inline bool Node::has_backref() { return ip_->has_backref; } inline bool Node::has_backref() const { return ip_->has_backref; }
inline bool Node::from_variable() { return ip_->from_variable; } inline bool Node::from_variable() const { return ip_->from_variable; }
inline bool Node::should_eval() { return ip_->should_eval; } inline bool Node::should_eval() const { return ip_->should_eval; }
inline bool Node::is_unquoted() { return ip_->is_unquoted; } inline bool Node::is_unquoted() const { return ip_->is_unquoted; }
inline bool Node::is_numeric() { return ip_->is_numeric(); } inline bool Node::is_numeric() const { return ip_->is_numeric(); }
inline string Node::file_name() const { return *(ip_->file_name); } inline string Node::file_name() const { return *(ip_->file_name); }
inline size_t Node::line_number() const { return ip_->line_number; } inline size_t Node::line_number() const { return ip_->line_number; }
...@@ -276,13 +265,15 @@ namespace Sass { ...@@ -276,13 +265,15 @@ namespace Sass {
ip_->push_back(n); ip_->push_back(n);
return *this; return *this;
} }
inline Node& Node::operator<<(Node n) { return push_back(n); } inline Node& Node::operator<<(Node n) { return push_back(n); }
inline Node& Node::operator+=(Node n) inline Node& Node::operator+=(Node n)
{ {
for (size_t i = 0, L = n.size(); i < L; ++i) push_back(n[i]); for (size_t i = 0, L = n.size(); i < L; ++i) push_back(n[i]);
return *this; return *this;
} }
inline bool Node::boolean_value() { return ip_->boolean_value(); } inline bool Node::boolean_value() const { return ip_->boolean_value(); }
inline double Node::numeric_value() { return ip_->numeric_value(); } inline double Node::numeric_value() const { return ip_->numeric_value(); }
inline Token Node::token() const { return ip_->value.token; }
inline Token Node::unit() const { return ip_->unit(); }
} }
\ No newline at end of file
...@@ -23,6 +23,8 @@ int main() ...@@ -23,6 +23,8 @@ int main()
cout << interior.should_eval() << endl << endl; cout << interior.should_eval() << endl << endl;
Node num(make.node(0, 0, 255, 123, 32)); Node num(make.node(0, 0, 255, 123, 32));
Node num2(make.node(0, 0, 255, 123, 32));
Node num3(make.node(0, 0, 255, 122, 20, .75));
cout << num.size() << endl; cout << num.size() << endl;
cout << num.has_children() << endl; cout << num.has_children() << endl;
...@@ -31,5 +33,8 @@ int main() ...@@ -31,5 +33,8 @@ int main()
cout << num[1].is_numeric() << endl; cout << num[1].is_numeric() << endl;
cout << num[1].numeric_value() << endl << endl; cout << num[1].numeric_value() << endl << endl;
cout << (num == num2) << endl;
cout << (num == num3) << endl << endl;
return 0; return 0;
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment