Fixed a problem in the use of boost::variant<>.

This commit is contained in:
John Wiegley 2007-05-14 23:20:34 +00:00
parent ba86b7f410
commit b36d24481d
4 changed files with 71 additions and 65 deletions

View file

@ -144,13 +144,13 @@ optional<const char *> document_t::lookup_name(nameid_t id) const
if (id < 1000) { if (id < 1000) {
switch (id) { switch (id) {
case CURRENT: case CURRENT:
return "CURRENT"; return ".";
case PARENT: case PARENT:
return "PARENT"; return "..";
case ROOT: case ROOT:
return "ROOT"; return "";
case ALL: case ALL:
return "ALL"; return "*";
default: default:
assert(id >= 10); assert(id >= 10);

View file

@ -143,18 +143,18 @@ static int read_and_report(ledger::report_t * report, int argc, char * argv[],
; ;
else if (verb == "parse") { else if (verb == "parse") {
xml::xpath_t expr(*arg); xml::xpath_t expr(*arg);
xml::document_t temp(xml::LEDGER_NODE);
IF_INFO() { IF_INFO() {
std::cout << "Value expression tree:" << std::endl; std::cout << "Value expression tree:" << std::endl;
expr.dump(std::cout); expr.dump(std::cout);
std::cout << std::endl; std::cout << std::endl;
std::cout << "Value expression parsed was:" << std::endl; std::cout << "Value expression parsed was:" << std::endl;
expr.print(std::cout); expr.print(std::cout, temp);
std::cout << std::endl << std::endl; std::cout << std::endl << std::endl;
std::cout << "Result of calculation: "; std::cout << "Result of calculation: ";
} }
xml::document_t temp(xml::LEDGER_NODE);
std::cout << expr.calc(temp, report).strip_annotations() << std::endl; std::cout << expr.calc(temp, report).strip_annotations() << std::endl;
return 0; return 0;
@ -254,7 +254,7 @@ static int read_and_report(ledger::report_t * report, int argc, char * argv[],
expr.dump(*out); expr.dump(*out);
*out << std::endl; *out << std::endl;
*out << "Value expression parsed was:" << std::endl; *out << "Value expression parsed was:" << std::endl;
expr.print(*out); expr.print(*out, xml_document);
*out << std::endl << std::endl; *out << std::endl << std::endl;
*out << "Result of calculation: "; *out << "Result of calculation: ";
} }
@ -266,7 +266,7 @@ static int read_and_report(ledger::report_t * report, int argc, char * argv[],
else if (verb == "xpath") { else if (verb == "xpath") {
std::cout << "XPath parsed:" << std::endl; std::cout << "XPath parsed:" << std::endl;
xml::xpath_t xpath(*arg); xml::xpath_t xpath(*arg);
xpath.print(*out); xpath.print(*out, xml_document);
*out << std::endl; *out << std::endl;
#if 0 #if 0

View file

@ -575,7 +575,7 @@ xpath_t::parse_value_term(std::istream& in, flags_t tflags) const
// An identifier followed by ( represents a function call // An identifier followed by ( represents a function call
tok = next_token(in, tflags); tok = next_token(in, tflags);
if (tok.kind == token_t::LPAREN) { if (tok.kind == token_t::LPAREN) {
node->kind = op_t::FUNC_NAME; node = new op_t(op_t::FUNC_NAME);
node->set_string(ident); node->set_string(ident);
ptr_op_t call_node(new op_t(op_t::O_EVAL)); ptr_op_t call_node(new op_t(op_t::O_EVAL));
@ -1779,6 +1779,7 @@ void xpath_t::context::describe(std::ostream& out) const throw()
#endif #endif
bool xpath_t::op_t::print(std::ostream& out, bool xpath_t::op_t::print(std::ostream& out,
document_t& document,
const bool relaxed, const bool relaxed,
const ptr_op_t& op_to_find, const ptr_op_t& op_to_find,
unsigned long * start_pos, unsigned long * start_pos,
@ -1836,9 +1837,17 @@ bool xpath_t::op_t::print(std::ostream& out,
break; break;
} }
case NODE_ID: case ATTR_ID:
out << '%' << as_name(); out << '@';
// fall through...
case NODE_ID: {
optional<const char *> name = document.lookup_name(as_name());
if (name)
out << *name;
else
out << '#' << as_name();
break; break;
}
case NODE_NAME: case NODE_NAME:
case FUNC_NAME: case FUNC_NAME:
@ -1863,194 +1872,194 @@ bool xpath_t::op_t::print(std::ostream& out,
case O_NOT: case O_NOT:
out << "!"; out << "!";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
break; break;
case O_NEG: case O_NEG:
out << "-"; out << "-";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
break; break;
case O_UNION: case O_UNION:
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " | "; out << " | ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
break; break;
case O_ADD: case O_ADD:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " + "; out << " + ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_SUB: case O_SUB:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " - "; out << " - ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_MUL: case O_MUL:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " * "; out << " * ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_DIV: case O_DIV:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " / "; out << " / ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_NEQ: case O_NEQ:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " != "; out << " != ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_EQ: case O_EQ:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " == "; out << " == ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_LT: case O_LT:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " < "; out << " < ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_LTE: case O_LTE:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " <= "; out << " <= ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_GT: case O_GT:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " > "; out << " > ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_GTE: case O_GTE:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " >= "; out << " >= ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_AND: case O_AND:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " & "; out << " & ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_OR: case O_OR:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " | "; out << " | ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_QUES: case O_QUES:
out << "("; out << "(";
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " ? "; out << " ? ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_COLON: case O_COLON:
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << " : "; out << " : ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
break; break;
case O_COMMA: case O_COMMA:
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ", "; out << ", ";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
break; break;
case O_DEFINE: case O_DEFINE:
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << '='; out << '=';
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
break; break;
case O_EVAL: case O_EVAL:
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << "("; out << "(";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << ")"; out << ")";
break; break;
case O_FIND: case O_FIND:
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << "/"; out << "/";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
break; break;
case O_RFIND: case O_RFIND:
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << "//"; out << "//";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
break; break;
case O_PRED: case O_PRED:
if (left() && left()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (left() && left()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << "["; out << "[";
if (right() && right()->print(out, relaxed, op_to_find, start_pos, end_pos)) if (right() && right()->print(out, document, relaxed, op_to_find, start_pos, end_pos))
found = true; found = true;
out << "]"; out << "]";
break; break;

View file

@ -311,7 +311,7 @@ public:
value_t& as_value() { value_t& as_value() {
assert(kind == VALUE); assert(kind == VALUE);
value_t * val = boost::get<scoped_ptr<value_t> >(data).get(); value_t * val = boost::get<shared_ptr<value_t> >(data).get();
assert(val); assert(val);
return *val; return *val;
} }
@ -418,6 +418,7 @@ public:
static ptr_op_t defer_sequence(value_t::sequence_t& result_seq); static ptr_op_t defer_sequence(value_t::sequence_t& result_seq);
bool print(std::ostream& out, bool print(std::ostream& out,
document_t& document,
const bool relaxed = true, const bool relaxed = true,
const ptr_op_t& op_to_find = NULL, const ptr_op_t& op_to_find = NULL,
unsigned long * start_pos = NULL, unsigned long * start_pos = NULL,
@ -510,12 +511,13 @@ public:
} }
bool print(std::ostream& out, bool print(std::ostream& out,
document_t& document,
const bool relaxed, const bool relaxed,
const ptr_op_t op_to_find, const ptr_op_t op_to_find,
unsigned long * start_pos, unsigned long * start_pos,
unsigned long * end_pos) const { unsigned long * end_pos) const {
if (ptr) if (ptr)
ptr->print(out, relaxed, op_to_find, start_pos, end_pos); ptr->print(out, document, relaxed, op_to_find, start_pos, end_pos);
return true; return true;
} }
@ -613,8 +615,8 @@ public:
return temp.calc(top, scope); return temp.calc(top, scope);
} }
void print(std::ostream& out) const { void print(std::ostream& out, xml::document_t& document) const {
print(out, true, NULL, NULL, NULL); print(out, document, true, NULL, NULL, NULL);
} }
void dump(std::ostream& out) const { void dump(std::ostream& out) const {
if (ptr) if (ptr)
@ -624,11 +626,6 @@ public:
friend class scope_t; friend class scope_t;
}; };
inline std::ostream& operator<<(std::ostream& out, const xpath_t::op_t& op) {
op.print(out);
return out;
}
inline void intrusive_ptr_add_ref(xpath_t::op_t * op) { inline void intrusive_ptr_add_ref(xpath_t::op_t * op) {
op->acquire(); op->acquire();
} }