Esempio n. 1
0
int main() {
    try {
        std::istringstream is("1+1");
        char c;
        bool done=false;
        std::exception_ptr except;

        // create handle to main execution context
        auto main_ctx( boost::context::execution_context::current() );

        // execute parser in new execution context
        boost::context::execution_context parser_ctx(
                boost::context::fixedsize_stack(),
                [&main_ctx,&is,&c,&done,&except](){
                // create parser with callback function
                Parser p( is,
                          [&main_ctx,&c](char ch){
                                c=ch;
                                // resume main execution context
                                main_ctx();
                        });
                    try {
                        // start recursive parsing
                        p.run();
                    } catch ( ... ) {
                        // store other exceptions in exception-pointer
                        except = std::current_exception();
                    }
                    // set termination flag
                    done=true;
                    // resume main execution context
                    main_ctx();
                });

        // user-code pulls parsed data from parser
        // invert control flow
        parser_ctx();
        if ( except) {
            std::rethrow_exception( except);
        }
        while( ! done) {
            printf("Parsed: %c\n",c);
            parser_ctx();
            if ( except) {
                std::rethrow_exception( except);
            }
        }

        std::cout << "main: done" << std::endl;

        return EXIT_SUCCESS;
    } catch ( std::exception const& e) {
        std::cerr << "exception: " << e.what() << std::endl;
    }
    return EXIT_FAILURE;
}
Esempio n. 2
0
ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool translate) {
    hash_table_t *table = (translate) ? fold->imm_string_untranslate : fold->imm_string_dotranslate;
    ast_value    *out   = NULL;
    size_t        hash  = util_hthash(table, str);

    if ((out = (ast_value*)util_htgeth(table, str, hash)))
        return (ast_expression*)out;

    if (translate) {
        char name[32];
        util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++));
        out                    = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING);
        out->expression.flags |= AST_FLAG_INCLUDE_DEF; /* def needs to be included for translatables */
    } else
        out                    = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING);

    out->cvq              = CV_CONST;
    out->hasvalue         = true;
    out->isimm            = true;
    out->constval.vstring = parser_strdup(str);

    vec_push(fold->imm_string, out);
    util_htseth(table, str, hash, out);

    return (ast_expression*)out;
}
Esempio n. 3
0
static lex_ctx_t fold_ctx(fold_t *fold) {
    lex_ctx_t ctx;
    if (fold->parser->lex)
        return parser_ctx(fold->parser);

    memset(&ctx, 0, sizeof(ctx));
    return ctx;
}
Esempio n. 4
0
int main() {
    std::istringstream is("1+1");
    bool done=false;
    char c;

    // create handle to main execution context
    boost::context::execution_context main_ctx(
        boost::context::execution_context::current() );

    // executes parser in new execution context
    boost::context::execution_context parser_ctx(
        boost::context::fixedsize_stack(),
        [&main_ctx,&is,&c,&done](){
            // create parser with callback function
            Parser p( is,
                      [&main_ctx,&c](char ch){
                          c=ch;
                          // resume main execution context
                          main_ctx.resume();
                      });
            // start recursive parsing
            p.run();
            done=true;
            main_ctx.resume();
        });

    // user-code pulls parsed data from parser
    // invert control flow
    parser_ctx.resume();
    do {
        printf("Parsed: %c\n",c);
        parser_ctx.resume();
    } while( ! done);

    std::cout << "main: done" << std::endl;

    return EXIT_SUCCESS;
}