Ejemplo n.º 1
0
void test_foreach() {

    using phoenix::placeholders::_1;
    using phoenix::ref;
    using phoenix::lambda;

  int a[10][20];
  int sum = 0;

  //for_each(arg1, for_each_tester())(array).value_;

  // Was:
  // std::for_each(a, a + 10,
  //               bind(ll::for_each(), _1, _1 + 20,
  //                    protect((_1 = var(sum), ++var(sum)))));
  // var replaced with ref, protect(..) replaced with lambda[..], no need for bind
  // phoenix algorithms are range based
  std::for_each(a, a + 10,
          phoenix::for_each(_1, lambda[_1 = ref(sum), ++ref(sum)]));
                /*phoenix::bind(phoenix::for_each, _1,
                            lambda[_1 = ref(sum), ++ref(sum)]));*/

  sum = 0;
  // Was:
  // std::for_each(a, a + 10,
  //               bind(ll::for_each(), _1, _1 + 20,
  //                 protect((sum += _1))));
  //
  std::for_each(a, a + 10,
                phoenix::for_each( _1,
                     lambda[(ref(sum) += _1)]));

  BOOST_CHECK(sum == (199 + 1)/ 2 * 199);
}
Ejemplo n.º 2
0
bool Compiler::execute() {
  using phoenix::ref;
  using qi::lit;
  using qi::no_skip;

  // iterate over stream input
  typedef std::istreambuf_iterator<char> base_iterator_type;
  base_iterator_type in_begin(m_input);

  // convert input iterator to forward iterator, usable by spirit parser
  typedef spirit::multi_pass<base_iterator_type> forward_iterator_type;
  forward_iterator_type fwd_begin = spirit::make_default_multi_pass(in_begin);
  forward_iterator_type fwd_end;

  // Initialize global scope
  Scope globalScope;
  StdLib::Init(globalScope);

  // Parsers
  ExpParser<forward_iterator_type> exp_;
  CmdParser<forward_iterator_type> cmd_(exp_);
  CodeParser<forward_iterator_type> code_(exp_);

  typedef qi::rule<forward_iterator_type, std::string(), ascii::space_type> StringRule;
  typedef qi::rule<forward_iterator_type, ascii::space_type> VoidRule;
  StringRule cmdline_ =
    lit('[')
    > (cmd_(ref(globalScope)) | code_(ref(globalScope)))
    > lit(']')
  ;
  VoidRule program_ = +(
    cmdline_[ref(m_output) << qi::_1]
    > -no_skip[ lit("\n")[ref(m_output) << endl] ]
  );

  //BOOST_SPIRIT_DEBUG_NODE(cmdline_);
  //BOOST_SPIRIT_DEBUG_NODE(program_);

  bool r = qi::phrase_parse(
    fwd_begin, fwd_end,
    program_,
    ascii::space
  );

  if (!r || fwd_begin != fwd_end) {
    return false;
  }

  // Cleanup the global scope :)
  m_output << globalScope.bytecode() << endl;

  return r;
}
Ejemplo n.º 3
0
bool parse_dasharray(Iterator first, Iterator last, std::vector<double>& dasharray)
{
    using qi::double_;
    using qi::phrase_parse;
    using qi::_1;
    using qi::lit;
    using qi::char_;
#if BOOST_VERSION > 104200
    using qi::no_skip;
#else
    using qi::lexeme;
#endif
    using phoenix::push_back;
    // SVG
    // dasharray ::= (length | percentage) (comma-wsp dasharray)?
    // no support for 'percentage' as viewport is unknown at load_map
    //
    bool r = phrase_parse(first, last,
                          (double_[push_back(phoenix::ref(dasharray), _1)] %
#if BOOST_VERSION > 104200
                          no_skip[char_(", ")]
#else
                          lexeme[char_(", ")]
#endif
                          | lit("none")),
                          qi::ascii::space);
    if (first != last)
    {
        return false;
    }
    return r;
}
Ejemplo n.º 4
0
        effects_group_rules() {
            const parse::lexer& tok = parse::lexer::instance();

            qi::_1_type _1;
            qi::_a_type _a;
            qi::_b_type _b;
            qi::_c_type _c;
            qi::_d_type _d;
            qi::_e_type _e;
            qi::_f_type _f;
            qi::_g_type _g;
            qi::_val_type _val;
            qi::lit_type lit;
            qi::eps_type eps;
            using phoenix::construct;
            using phoenix::new_;
            using phoenix::push_back;

            effects_group
                =   tok.EffectsGroup_
                > -(parse::label(Description_token)      > tok.string [ _g = _1 ])
                >   parse::label(Scope_token)            > parse::detail::condition_parser [ _a = _1 ]
                > -(parse::label(Activation_token)       > parse::detail::condition_parser [ _b = _1 ])
                > -(parse::label(StackingGroup_token)    > tok.string [ _c = _1 ])
                > -(parse::label(AccountingLabel_token)  > tok.string [ _e = _1 ])
                > ((parse::label(Priority_token)         > tok.int_ [ _f = _1 ]) | eps [ _f = 100 ])
                >   parse::label(Effects_token)
                >   (
                            ('[' > +parse::effect_parser() [ push_back(_d, _1) ] > ']')
                        |    parse::effect_parser() [ push_back(_d, _1) ]
                    )
                    [ _val = new_<Effect::EffectsGroup>(_a, _b, _d, _e, _c, _f, _g) ]
                ;

            start
                =    ('[' > +effects_group [ push_back(_val, construct<std::shared_ptr<Effect::EffectsGroup>>(_1)) ] > ']')
                |     effects_group [ push_back(_val, construct<std::shared_ptr<Effect::EffectsGroup>>(_1)) ]
                ;

            effects_group.name("EffectsGroup");
            start.name("EffectsGroups");

#if DEBUG_PARSERS
            debug(effects_group);
            debug(start);
#endif
        }
Ejemplo n.º 5
0
    bool parse_numbers( Iterator first, Iterator last, vector<double> &v )
    {
      using qi::double_;
      using qi::phrase_parse;
      using qi::_1;
      using ascii::space;
      using phoenix::push_back;

      bool r( phrase_parse( first, last,
              (
               double_[ push_back( phoenix::ref( v ), _1 ) ] % ','
              ),
                            space ) );
      return ( first == last ? r : false );
    }
Ejemplo n.º 6
0
    bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
    {
        using qi::double_;
        using qi::phrase_parse;
        using qi::_1;
        using ascii::space;
        using phoenix::push_back;
        using phoenix::ref;

        bool r = phrase_parse(first, last,

            //  Begin grammar
            (
                double_[push_back(ref(v), _1)] % ','
            )
            ,
            //  End grammar

            space);

        if (first != last) // fail if we did not get a full match
            return false;
        return r;
    }
Ejemplo n.º 7
0
int
main()
{
    using phoenix::ref;
    using phoenix::arg_names::arg1;
    using phoenix::arg_names::arg2;
    using std::map;
    using std::string;
    using std::vector;
    
    {
        int x = 123;
        BOOST_TEST((&arg1)(x) == &x);
        //BOOST_TEST((*&arg1)(x) == 123);

        int y = 968;
        (ref(x) = arg1)(y);
        BOOST_TEST(x == y);

        (arg1 = 456)(x);
        BOOST_TEST(x == 456);
        int& r = (arg1 = 456)(x); // must be an lvalue
        BOOST_TEST(&r == &x);

        int c[] = { 1, 2, 3, 4, 5 };
        BOOST_TEST((arg1[3])(c) == 4);

        int& r2 = (arg1[3])(c); // must be an lvalue
        BOOST_TEST(&r2 == &c[3]);

        vector<string> v;
        v.push_back("a");
        v.push_back("b");
        v.push_back("c");
        v.push_back("d");

        BOOST_TEST((arg1[3])(v) == "d");

        map<string, int> m;
        (arg1["Kimpo"] = arg2)(m, x);
        BOOST_TEST(m["Kimpo"] == x);
    }

    return boost::report_errors();
}
Ejemplo n.º 8
0
bool Translator::analysisLine(QString& line,int numLine)
{
    using qi::double_;
    using boost::spirit::standard_wide::char_;
    using boost::spirit::standard_wide::space;
    using boost::spirit::standard_wide::string;

    using qi::int_;
    using qi::phrase_parse;
    using qi::lit;
    using qi::double_;
    using qi::_1;
    using qi::lexeme;


    using phoenix::push_back;
    using phoenix::ref;

    //! обнуление всех параметров
    clear();

    //! очистка внутренних структур
    std::wstring tempString=line.toStdWString();

    //quoted_string %= lexeme['"' >> +(char_ - '"') >> '"'];


//    bool r = phrase_parse(tempString.begin(),tempString.end(),
//    //  Begin grammar
//    (
//        lit("delay")>>'('>>int_[ref(sec_)=_1]>>')'>>';' |
//        lit("iMsg")>>'('>>*(char_[push_back(ref(vShowMessage),_1)]-';'-')'-'(')>>')'>>';'|
//        lit("cMsg")>>'('>>*(char_[push_back(ref(vConsoleMessage),_1)]-';'-')'-'(')>>')'>>';'|
//        lit("qMsg")>>'('>>*(char_[push_back(ref(vQuestionMessage),_1)]-';'-')'-'(')>>')'>>';'|
//        lit("specialCommand")>>'('>>*(char_[push_back(ref(vSpecialCommand),_1)]-';'-')'-'('-',')>>','>>*(char_[push_back(ref(vSpecialParam),_1)]-'='-')'-'('-';'-',')>>')'>>';'|
//        lit("set")>>'('>>*(char_[push_back(ref(vSpecialCommand),_1)]-';'-')'-'(')>>')'>>';'|
//        lit("get")>>'('>>*(char_[push_back(ref(vSpecialParam),_1)]-';'-')'-'(')>>')'>>';'|
//        lit("loadVariant")>>'('>>*(char_[push_back(ref(vLoadVariant),_1)]-';'-')'-'(')>>')'>>';'|
//        lit("setNumSubVariant")>>'('>>int_[ref(numSubVar_)=_1]>>')'>>';' |
//        *(char_[push_back(ref(vValue1),_1)]-'='-';')>>'='>>*(char_[push_back(ref(vValue2),_1)]-'='-';')>>';' |
//        lit("/*")>>*(char_-'*'-'/')>>lit("*/")
//    ),
//    space
//    );

    bool r = phrase_parse(tempString.begin(),tempString.end(),
    //  Begin grammar
    (
        lit("var")>>*(char_[push_back(ref(vVariable),_1)]-'='-';')>>';' |
        lit("delay")>>'('>>int_[ref(sec_)=_1]>>')'>>';' |
        lit("iMsg")>>'('>>lexeme['"' >> +(char_[push_back(ref(vShowMessage),_1)] - '"') >> '"']>>')'>>';'|
        lit("cMsg")>>'('>>lexeme['"' >> +(char_[push_back(ref(vConsoleMessage),_1)] - '"') >> '"']>>')'>>';'|
        lit("qMsg")>>'('>>lexeme['"' >> +(char_[push_back(ref(vQuestionMessage),_1)] - '"') >> '"']>>')'>>';'|
        lit("specialCommand")>>'('>>*(char_[push_back(ref(vSpecialCommand),_1)]-';'-')'-'('-',')>>','>>*(char_[push_back(ref(vSpecialParam),_1)]-'='-')'-'('-';'-',')>>')'>>';'|
        lit("set")>>'('>>*(char_[push_back(ref(vSpecialCommand),_1)]-';'-')'-'(')>>')'>>';'|
        lit("get")>>'('>>*(char_[push_back(ref(vSpecialParam),_1)]-';'-')'-'(')>>')'>>';'|
        lit("loadVariant")>>'('>>*(char_[push_back(ref(vLoadVariant),_1)]-';'-')'-'(')>>')'>>';'|
        lit("setNumSubVariant")>>'('>>int_[ref(numSubVar_)=_1]>>')'>>';' |
        *(char_[push_back(ref(vValue1),_1)]-'='-';')>>'='>>*(char_[push_back(ref(vValue2),_1)]-'='-';')>>';' |
        lit("/*")>>*(char_-'*'-'/')>>lit("*/")
    ),
    space
    );

    createNodes(numLine);
    //! проверка какие узлы нужно создать
    return r;
}
Ejemplo n.º 9
0
geometry_grammar<Iterator, ErrorHandler>::geometry_grammar()
    : geometry_grammar::base_type(start,"geometry"),
      coordinates(error_handler)
{
    qi::lit_type lit;
    qi::int_type int_;
    qi::double_type double_;
    qi::_val_type _val;
    qi::_1_type _1;
    qi::_2_type _2;
    qi::_3_type _3;
    qi::_4_type _4;
    qi::_a_type _a;
    qi::_b_type _b;
    qi::eps_type eps;
    qi::omit_type omit;
    using qi::fail;
    using qi::on_error;
    using phoenix::push_back;

    start = geometry.alias() | lit("null");

    // generic json types
    json_.value =  json_.object | json_.array | json_.string_ | json_.number
        ;

    json_.pairs = json_.key_value % lit(',')
        ;

    json_.key_value = (json_.string_ > lit(':') > json_.value)
        ;

    json_.object = lit('{')
        > *json_.pairs
        > lit('}')
        ;
    json_.array = lit('[')
        > json_.value > *(lit(',') > json_.value)
        > lit(']')
        ;
    json_.number = json_.strict_double
        | json_.int__
        | lit("true")
        | lit ("false")
        | lit("null")
        ;
    geometry = lit('{')[_a = 0]
        > (((lit("\"type\"") > lit(':') > geometry_type_dispatch[_a = _1])
            |
            (lit("\"coordinates\"") > lit(':') > coordinates[_b = _1])
            |
            (lit("\"geometries\"") > lit(':') > lit('[') > geometry_collection[_val = _1] > lit(']'))
            |
            omit[json_.key_value]) % lit(',')) [create_geometry(_val,_a,_b)]
        > lit('}')
        ;

    geometry_collection = geometry[push_back(_val, _1)] % lit(',')
        ;
    geometry_type_dispatch.add
        ("\"Point\"",1)
        ("\"LineString\"",2)
        ("\"Polygon\"",3)
        ("\"MultiPoint\"",4)
        ("\"MultiLineString\"",5)
        ("\"MultiPolygon\"",6)
        ("\"GeometryCollection\"",7)
        ;

    // give some rules names
    geometry.name("Geometry");
    geometry_collection.name("GeometryCollection");
    geometry_type_dispatch.name("type: (Point|LineString|Polygon|MultiPoint|MultiLineString|MultiPolygon|GeometryCollection)");
    coordinates.name("coordinates");
    // error handler
    auto error_handler_function = boost::phoenix::function<ErrorHandler>(error_handler);
    on_error<fail>(start, error_handler_function(_1, _2, _3, _4));
}
Ejemplo n.º 10
0
int main()
{
    using boost::scoped_ptr;
    using boost::shared_ptr;
    using phoenix::val;
    using phoenix::ref;
    using phoenix::arg_names::arg1;
    using phoenix::arg_names::arg2;

    Test test = {1};
    const Test* cptr = &test;
    Test* ptr = &test;

    BOOST_TEST((val(ptr)->*&Test::value)() == 1);
    BOOST_TEST((val(cptr)->*&Test::value)() == 1);
    BOOST_TEST((arg1->*&Test::value)(cptr) == 1);

    ((val(ptr)->*&Test::value) = 2)();
    BOOST_TEST(test.value == 2);


    BOOST_TEST((val(ptr)->*&Test::func)(val(3))() == 3);
    int i = 33;
    BOOST_TEST((arg1->*&Test::func)(arg2)(cptr, i) == i);
    //BOOST_TEST((val(cptr)->*&Test::func)(4)() == 4);
    BOOST_TEST((val(ptr)->*&Test::dunc)()() == 10);

    BOOST_TEST((arg1->*&Test::func)(5)(ptr) == 5);
    BOOST_TEST((arg1->*&Test::kunc)()(ptr));

    shared_ptr<Test> sptr(new Test(test));

    BOOST_TEST((arg1->*&Test::value)(sptr) == 2);
    BOOST_TEST((arg1->*&Test::func)(6)(sptr) == 6);

    scoped_ptr<Test> scptr(new Test(test));

    BOOST_TEST((arg1->*&Test::value)(scptr) == 2);
    BOOST_TEST((arg1->*&Test::func)(7)(scptr) == 7);

    shared_ptr<const Test> csptr(new Test(test));

    BOOST_TEST((arg1->*&Test::value)(csptr) == 2);
    BOOST_TEST((arg1->*&Test::func)(8)(csptr) == 8);

    scoped_ptr<const Test> cscptr(new Test(test));

    BOOST_TEST((arg1->*&Test::value)(cscptr) == 2);
    BOOST_TEST((arg1->*&Test::func)(9)(cscptr) == 9);

    std::auto_ptr<Test> aptr(new Test(test));

    BOOST_TEST((arg1->*&Test::value)(aptr) == 2);
    BOOST_TEST((arg1->*&Test::func)(10)(aptr) == 10);

    std::auto_ptr<const Test> captr(new Test(test));

    BOOST_TEST((arg1->*&Test::value)(captr) == 2);
    BOOST_TEST((arg1->*&Test::func)(11)(captr) == 11);

    return boost::report_errors();
}
Ejemplo n.º 11
0
 static
 functor_parser<trace_<actor<value<char const*> > > >
 trace_p(char const* str) {
     return trace_<actor<value<char const*> > >(val(str));
 }
Ejemplo n.º 12
0
 static
 functor_parser<trace_<actor<value<std::string> > > >
 trace_p(std::string const& str) {
     return trace_<actor<value<std::string> > >(val(str));
 }