Initial commit
[yaffs-website] / node_modules / node-sass / src / libsass / src / error_handling.cpp
1 #include "sass.hpp"
2 #include "ast.hpp"
3 #include "prelexer.hpp"
4 #include "backtrace.hpp"
5 #include "error_handling.hpp"
6
7 #include <iostream>
8
9 namespace Sass {
10
11   namespace Exception {
12
13     Base::Base(ParserState pstate, std::string msg, std::vector<Sass_Import_Entry>* import_stack)
14     : std::runtime_error(msg), msg(msg),
15       prefix("Error"), pstate(pstate),
16       import_stack(import_stack)
17     { }
18
19     InvalidSass::InvalidSass(ParserState pstate, std::string msg)
20     : Base(pstate, msg)
21     { }
22
23
24     InvalidParent::InvalidParent(Selector_Ptr parent, Selector_Ptr selector)
25     : Base(selector->pstate()), parent(parent), selector(selector)
26     {
27       msg = "Invalid parent selector for \"";
28       msg += selector->to_string(Sass_Inspect_Options());
29       msg += "\": \"";
30       msg += parent->to_string(Sass_Inspect_Options());
31       msg += "\"";
32     }
33
34     InvalidArgumentType::InvalidArgumentType(ParserState pstate, std::string fn, std::string arg, std::string type, const Value_Ptr value)
35     : Base(pstate), fn(fn), arg(arg), type(type), value(value)
36     {
37       msg  = arg + ": \"";
38       if (value) msg += value->to_string(Sass_Inspect_Options());
39       msg += "\" is not a " + type;
40       msg += " for `" + fn + "'";
41     }
42
43     MissingArgument::MissingArgument(ParserState pstate, std::string fn, std::string arg, std::string fntype)
44     : Base(pstate), fn(fn), arg(arg), fntype(fntype)
45     {
46       msg  = fntype + " " + fn;
47       msg += " is missing argument ";
48       msg += arg + ".";
49     }
50
51     InvalidSyntax::InvalidSyntax(ParserState pstate, std::string msg, std::vector<Sass_Import_Entry>* import_stack)
52     : Base(pstate, msg, import_stack)
53     { }
54
55     UndefinedOperation::UndefinedOperation(Expression_Ptr_Const lhs, Expression_Ptr_Const rhs, const std::string& op)
56     : lhs(lhs), rhs(rhs), op(op)
57     {
58       msg  = def_op_msg + ": \"";
59       msg += lhs->to_string({ NESTED, 5 });
60       msg += " " + op + " ";
61       msg += rhs->to_string({ TO_SASS, 5 });
62       msg += "\".";
63     }
64
65     InvalidNullOperation::InvalidNullOperation(Expression_Ptr_Const lhs, Expression_Ptr_Const rhs, const std::string& op)
66     : UndefinedOperation(lhs, rhs, op)
67     {
68       msg  = def_op_null_msg + ": \"";
69       msg += lhs->inspect();
70       msg += " " + op + " ";
71       msg += rhs->inspect();
72       msg += "\".";
73     }
74
75     ZeroDivisionError::ZeroDivisionError(const Expression& lhs, const Expression& rhs)
76     : lhs(lhs), rhs(rhs)
77     {
78       msg  = "divided by 0";
79     }
80
81     DuplicateKeyError::DuplicateKeyError(const Map& dup, const Expression& org)
82     : Base(org.pstate()), dup(dup), org(org)
83     {
84       msg  = "Duplicate key ";
85       msg += dup.get_duplicate_key()->inspect();
86       msg += " in map (";
87       msg += org.inspect();
88       msg += ").";
89     }
90
91     TypeMismatch::TypeMismatch(const Expression& var, const std::string type)
92     : Base(var.pstate()), var(var), type(type)
93     {
94       msg  = var.to_string();
95       msg += " is not an ";
96       msg += type;
97       msg += ".";
98     }
99
100     InvalidValue::InvalidValue(const Expression& val)
101     : Base(val.pstate()), val(val)
102     {
103       msg  = val.to_string();
104       msg += " isn't a valid CSS value.";
105     }
106
107     StackError::StackError(const AST_Node& node)
108     : Base(node.pstate()), node(node)
109     {
110       msg  = "stack level too deep";
111     }
112
113     IncompatibleUnits::IncompatibleUnits(const Number& lhs, const Number& rhs)
114     : lhs(lhs), rhs(rhs)
115     {
116       msg  = "Incompatible units: '";
117       msg += rhs.unit();
118       msg += "' and '";
119       msg += lhs.unit();
120       msg += "'.";
121     }
122
123     AlphaChannelsNotEqual::AlphaChannelsNotEqual(Expression_Ptr_Const lhs, Expression_Ptr_Const rhs, const std::string& op)
124     : lhs(lhs), rhs(rhs), op(op)
125     {
126       msg  = "Alpha channels must be equal: ";
127       msg += lhs->to_string({ NESTED, 5 });
128       msg += " " + op + " ";
129       msg += rhs->to_string({ NESTED, 5 });
130       msg += ".";
131     }
132
133
134     SassValueError::SassValueError(ParserState pstate, OperationError& err)
135     : Base(pstate, err.what())
136     {
137       msg = err.what();
138       prefix = err.errtype();
139     }
140
141   }
142
143
144   void warn(std::string msg, ParserState pstate)
145   {
146     std::cerr << "Warning: " << msg<< std::endl;
147   }
148
149   void warn(std::string msg, ParserState pstate, Backtrace* bt)
150   {
151     Backtrace top(bt, pstate, "");
152     msg += top.to_string();
153     warn(msg, pstate);
154   }
155
156   void deprecated_function(std::string msg, ParserState pstate)
157   {
158     std::string cwd(Sass::File::get_cwd());
159     std::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
160     std::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
161     std::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.path));
162
163     std::cerr << "DEPRECATION WARNING: " << msg << std::endl;
164     std::cerr << "will be an error in future versions of Sass." << std::endl;
165     std::cerr << "        on line " << pstate.line+1 << " of " << output_path << std::endl;
166   }
167
168   void deprecated(std::string msg, std::string msg2, ParserState pstate)
169   {
170     std::string cwd(Sass::File::get_cwd());
171     std::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
172     std::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
173     std::string output_path(Sass::File::path_for_console(rel_path, pstate.path, pstate.path));
174
175     std::cerr << "DEPRECATION WARNING on line " << pstate.line + 1;
176     if (output_path.length()) std::cerr << " of " << output_path;
177     std::cerr << ":" << std::endl;
178     std::cerr << msg << " and will be an error in future versions of Sass." << std::endl;
179     if (msg2.length()) std::cerr << msg2 << std::endl;
180     std::cerr << std::endl;
181   }
182
183   void deprecated_bind(std::string msg, ParserState pstate)
184   {
185     std::string cwd(Sass::File::get_cwd());
186     std::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
187     std::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
188     std::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.path));
189
190     std::cerr << "WARNING: " << msg << std::endl;
191     std::cerr << "        on line " << pstate.line+1 << " of " << output_path << std::endl;
192     std::cerr << "This will be an error in future versions of Sass." << std::endl;
193   }
194
195   void error(std::string msg, ParserState pstate)
196   {
197     throw Exception::InvalidSyntax(pstate, msg);
198   }
199
200   void error(std::string msg, ParserState pstate, Backtrace* bt)
201   {
202     Backtrace top(bt, pstate, "");
203     msg += "\n" + top.to_string();
204     error(msg, pstate);
205   }
206
207 }