Commit 5f0e2f92 authored by Peter Eastman's avatar Peter Eastman
Browse files

Made parsing exceptions more informative

parent d8c88e74
...@@ -38,25 +38,25 @@ using namespace std; ...@@ -38,25 +38,25 @@ using namespace std;
ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const vector<ExpressionTreeNode>& children) : operation(operation), children(children) { ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const vector<ExpressionTreeNode>& children) : operation(operation), children(children) {
if (operation->getNumArguments() != children.size()) if (operation->getNumArguments() != children.size())
throw Exception("Parse error: wrong number of arguments to function"); throw Exception("Parse error: wrong number of arguments to function: "+operation->getName());
} }
ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child1, const ExpressionTreeNode& child2) : operation(operation) { ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child1, const ExpressionTreeNode& child2) : operation(operation) {
children.push_back(child1); children.push_back(child1);
children.push_back(child2); children.push_back(child2);
if (operation->getNumArguments() != children.size()) if (operation->getNumArguments() != children.size())
throw Exception("Parse error: wrong number of arguments to function"); throw Exception("Parse error: wrong number of arguments to function: "+operation->getName());
} }
ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child) : operation(operation) { ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child) : operation(operation) {
children.push_back(child); children.push_back(child);
if (operation->getNumArguments() != children.size()) if (operation->getNumArguments() != children.size())
throw Exception("Parse error: wrong number of arguments to function"); throw Exception("Parse error: wrong number of arguments to function: "+operation->getName());
} }
ExpressionTreeNode::ExpressionTreeNode(Operation* operation) : operation(operation) { ExpressionTreeNode::ExpressionTreeNode(Operation* operation) : operation(operation) {
if (operation->getNumArguments() != children.size()) if (operation->getNumArguments() != children.size())
throw Exception("Parse error: wrong number of arguments to function"); throw Exception("Parse error: wrong number of arguments to function: "+operation->getName());
} }
ExpressionTreeNode::ExpressionTreeNode(const ExpressionTreeNode& node) : operation(&node.getOperation() == NULL ? NULL : node.getOperation().clone()), children(node.getChildren()) { ExpressionTreeNode::ExpressionTreeNode(const ExpressionTreeNode& node) : operation(&node.getOperation() == NULL ? NULL : node.getOperation().clone()), children(node.getChildren()) {
......
...@@ -177,7 +177,7 @@ ParsedExpression Parser::parse(const string& expression, const map<string, Custo ...@@ -177,7 +177,7 @@ ParsedExpression Parser::parse(const string& expression, const map<string, Custo
int pos = 0; int pos = 0;
subexpDefs[name] = parsePrecedence(tokens, pos, customFunctions, subexpDefs, 0); subexpDefs[name] = parsePrecedence(tokens, pos, customFunctions, subexpDefs, 0);
if (pos != tokens.size()) if (pos != tokens.size())
throw Exception("Parse error: unexpected text at end of subexpression"); throw Exception("Parse error: unexpected text at end of subexpression: "+tokens[pos].getText());
} }
// Now parse the primary expression. // Now parse the primary expression.
...@@ -186,7 +186,7 @@ ParsedExpression Parser::parse(const string& expression, const map<string, Custo ...@@ -186,7 +186,7 @@ ParsedExpression Parser::parse(const string& expression, const map<string, Custo
int pos = 0; int pos = 0;
ExpressionTreeNode result = parsePrecedence(tokens, pos, customFunctions, subexpDefs, 0); ExpressionTreeNode result = parsePrecedence(tokens, pos, customFunctions, subexpDefs, 0);
if (pos != tokens.size()) if (pos != tokens.size())
throw Exception("Parse error: unexpected text at end of expression"); throw Exception("Parse error: unexpected text at end of expression: "+tokens[pos].getText());
return ParsedExpression(result); return ParsedExpression(result);
} }
...@@ -250,7 +250,7 @@ ExpressionTreeNode Parser::parsePrecedence(const vector<ParseToken>& tokens, int ...@@ -250,7 +250,7 @@ ExpressionTreeNode Parser::parsePrecedence(const vector<ParseToken>& tokens, int
result = ExpressionTreeNode(new Operation::Negate(), toNegate); result = ExpressionTreeNode(new Operation::Negate(), toNegate);
} }
else else
throw Exception("Parse error: unexpected token"); throw Exception("Parse error: unexpected token: "+token.getText());
// Now deal with the next binary operator. // Now deal with the next binary operator.
...@@ -332,7 +332,7 @@ Operation* Parser::getFunctionOperation(const std::string& name, const map<strin ...@@ -332,7 +332,7 @@ Operation* Parser::getFunctionOperation(const std::string& name, const map<strin
map<string, Operation::Id>::const_iterator iter = opMap.find(trimmed); map<string, Operation::Id>::const_iterator iter = opMap.find(trimmed);
if (iter == opMap.end()) if (iter == opMap.end())
throw Exception("Parse error: unknown function"); throw Exception("Parse error: unknown function: "+trimmed);
switch (iter->second) { switch (iter->second) {
case Operation::SQRT: case Operation::SQRT:
return new Operation::Sqrt(); return new Operation::Sqrt();
......
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