bool prepare( MemoryManager mm, const Functional::value_vec_t& static_args, Environment environment, NodeReporter reporter ) { ConstByteString expression = static_args[0].as_string(); try { m_expression.assign( expression.const_data(), expression.length(), boost::regex_constants::normal ); } catch (const boost::bad_expression& e) { reporter.error( "Could not compile regexp: " + expression.to_s() + " (" + e.what() + ")" ); return false; } m_replacement = static_args[1].as_string().to_s(); return true; }
virtual void validate(NodeReporter node_reporter) const { for (size_t i = 0; i < N; ++i) { node_reporter.warn("warning"); } Chain::validate(node_reporter); }
void validate_argument( int n, Value v, NodeReporter reporter ) const { if (n < 2 && v.type() != Value::STRING) { reporter.error("Must be of type string: " + v.to_s()); } }
void Operator::pre_eval(Environment environment, NodeReporter reporter) { // Validation guarantees that the first two children are string // literals and thus can be evaluated with default EvalContext. node_list_t::const_iterator child_i = children().begin(); Value op_name_value = literal_value(*child_i); ++child_i; Value params_value = literal_value(*child_i); ConstByteString op_name = op_name_value.value_as_byte_string(); ConstByteString params = params_value.value_as_byte_string(); if (! op_name) { reporter.error("Missing operator name."); return; } if (! params) { reporter.error("Missing parameters."); return; } try { m_data->op = ConstOperator::lookup(environment, op_name.to_s().c_str()); } catch (IronBee::enoent) { reporter.error("No such operator: " + op_name.to_s()); return; } if (! (m_data->op.capabilities() & IB_OP_CAPABILITY_NON_STREAM)) { reporter.error("Only non-stream operator currently supported."); return; } m_data->instance_data = m_data->op.create_instance( environment.main_context(), IB_OP_CAPABILITY_NON_STREAM, params.to_s().c_str() ); }
bool FinishPhase::validate(NodeReporter reporter) const { bool result = true; result = Validate::n_children(reporter, 2) && result; result = Validate::nth_child_is_string(reporter, 0) && result; if (result) { string phase_string = literal_value(children().front()).value_as_byte_string().to_s(); if (phase_lookup(phase_string) == IB_PHASE_INVALID) { reporter.error("Invalid phase argument: " + phase_string); result = false; } } return result; }
void Transformation::pre_eval(Environment environment, NodeReporter reporter) { // Validation guarantees that the first child is a string interval // and thus can be evaluated with default EvalContext. Value name_value = literal_value(children().front()); ConstByteString name = name_value.value_as_byte_string(); if (! name) { reporter.error("Missing transformation name."); return; } m_data->transformation = ConstTransformation::lookup( environment, string(name.const_data(), name.length()).c_str() ); }
bool Template::transform( MergeGraph& merge_graph, const CallFactory& call_factory, Environment environment, NodeReporter reporter ) { node_p me = shared_from_this(); // Construct map of argument name to children. typedef map<string, node_p> arg_map_t; arg_map_t arg_map; { template_arg_list_t::const_iterator arg_i = m_args.begin(); node_list_t::const_iterator children_i = children().begin(); while (arg_i != m_args.end() && children_i != children().end()) { arg_map.insert(make_pair(*arg_i, tree_copy(*children_i, call_factory))); ++arg_i; ++children_i; } if (arg_i != m_args.end() || children_i != children().end()) { reporter.error( "Number of children not equal to number of arguments. " "Should have been caught in validation." ); return false; } } // Construct copy of body to replace me with. node_p replacement = tree_copy(m_body, call_factory); // Special case. Body might be itself a ref node. { string top_ref = template_ref(m_body); if (! top_ref.empty()) { arg_map_t::const_iterator arg_i = arg_map.find(top_ref); if (arg_i == arg_map.end()) { reporter.error( "Reference to \"" + top_ref + "\" but not such " "argument to template " + name() = "." ); return false; } node_p replacement = arg_i->second; merge_graph.replace(me, replacement); merge_graph.add_origin( replacement, m_origin_prefix + m_body->to_s() ); return true; } } // Replace arguments. typedef map<node_p, string> origin_info_t; origin_info_t origin_info; { node_list_t todo; todo.push_back(replacement); while (! todo.empty()) { node_p n = todo.front(); todo.pop_front(); // Enforce that we are working on a tree, not a dag. assert(n->parents().size() <= 1); string ref_param = template_ref(n); if (! ref_param.empty()) { arg_map_t::const_iterator arg_i = arg_map.find(ref_param); if (arg_i == arg_map.end()) { reporter.error( "Reference to \"" + ref_param + "\" but not such " "argument to template " + name() = "." ); continue; } node_p arg = arg_i->second; n->parents().front().lock()->replace_child(n, arg); origin_info[arg] = m_origin_prefix + n->to_s(); } else { copy( n->children().begin(), n->children().end(), back_inserter(todo) ); origin_info[n] = m_origin_prefix + n->to_s(); } } } // Replace with body. merge_graph.replace(me, replacement); BOOST_FOREACH(origin_info_t::const_reference v, origin_info) { merge_graph.add_origin(v.first, v.second); }
void Template::post_transform(NodeReporter reporter) const { reporter.error( "Template node should not exist post-transform." ); }
bool Template::transform( MergeGraph& merge_graph, const CallFactory& call_factory, NodeReporter reporter ) { node_p me = shared_from_this(); // Construct map of argument name to children. typedef map<string, node_p> arg_map_t; arg_map_t arg_map; { template_arg_list_t::const_iterator arg_i = m_args.begin(); node_list_t::const_iterator children_i = children().begin(); while (arg_i != m_args.end() && children_i != children().end()) { arg_map.insert(make_pair(*arg_i, *children_i)); ++arg_i; ++children_i; } if (arg_i != m_args.end() || children_i != children().end()) { reporter.error( "Number of children not equal to number of arguments. " "Should have been caught in validation." ); return false; } } // Construct copy of body to replace me with. node_p replacement = tree_copy(m_body, call_factory); // Special case. Body might be itself a ref node. { string top_ref = template_ref(m_body); if (! top_ref.empty()) { arg_map_t::const_iterator arg_i = arg_map.find(top_ref); if (arg_i == arg_map.end()) { reporter.error( "Reference to \"" + top_ref + "\" but not such " "argument to template " + name() = "." ); return false; } node_p replacement = arg_i->second; merge_graph.replace(me, replacement); return true; } } // Make list of all descendants. We don't want to iterate over the // replacements, so we make the entire list in advance. list<node_p> to_transform; bfs_down(replacement, back_inserter(to_transform)); BOOST_FOREACH(const node_p& node, to_transform) { BOOST_FOREACH(const node_p& child, node->children()) { string ref_param = template_ref(child); if (! ref_param.empty()) { arg_map_t::const_iterator arg_i = arg_map.find(ref_param); if (arg_i == arg_map.end()) { reporter.error( "Reference to \"" + ref_param + "\" but not such " "argument to template " + name() = "." ); continue; } node->replace_child(child, arg_i->second); } } }