static void _parse_parent_child_nonterminal(){ ParserContext context; Ast ast; AstBuilder builder; _build_child_rule_fsm(&fix.parser.fsm); ast_init(&ast, NULL, &fix.parser.table); ast_builder_init(&builder, &ast); ast_builder_append_follow(&builder, &(Token){1, 1, 'a'}); ast_builder_append(&builder, &(Token){2, 1, 'b'}); ast_builder_append(&builder, &(Token){3, 1, 'c'}); ast_builder_done(&builder); ast_builder_dispose(&builder); parser_context_init(&context, &fix.parser); parser_context_set_source_ast(&context, &ast); int error = parser_context_execute(&context); t_assert(!error); parser_context_dispose(&context); ast_dispose(&ast); }
void _parser_basic_parse(){ ParserContext context; Ast ast; AstBuilder builder; _build_simple_ab_fsm(&fix.parser.fsm); ast_init(&ast, NULL, &fix.parser.table); ast_builder_init(&builder, &ast); ast_builder_append_follow(&builder, &(Token){1, 1, 'a'}); ast_builder_append(&builder, &(Token){2, 1, 'b'}); ast_builder_done(&builder); ast_builder_dispose(&builder); parser_context_init(&context, &fix.parser); parser_context_set_source_ast(&context, &ast); int error = parser_context_execute(&context); t_assert(!error); parser_context_dispose(&context); ast_dispose(&ast); }
static void _parse_sibling_with_children_test(){ ParserContext context; Ast ast; AstBuilder builder; _build_siblings_with_children(&fix.parser.fsm); ast_init(&ast, NULL, &fix.parser.table); ast_builder_init(&builder, &ast); ast_builder_append_follow(&builder, &(Token){1, 1, 'a'}); ast_builder_append(&builder, &(Token){2, 1, 'b'}); ast_builder_append(&builder, &(Token){3, 1, 'c'}); ast_builder_parent(&builder); ast_builder_append_follow(&builder, &(Token){4, 1, 'd'}); ast_builder_append(&builder, &(Token){5, 1, 'e'}); ast_builder_append(&builder, &(Token){6, 1, 'f'}); ast_builder_done(&builder); ast_builder_dispose(&builder); parser_context_init(&context, &fix.parser); parser_context_set_source_ast(&context, &ast); int error = parser_context_execute(&context); t_assert(!error); parser_context_dispose(&context); ast_dispose(&ast); }
/* Thread for servicing connections */ static void *hypervisor_thread(void *arg) { hypervisor_conn_t *conn = arg; char buffer[512],**tokens; parser_context_t ctx; int res; tokens = NULL; parser_context_init(&ctx); while(conn->active) { if (!fgets(buffer,sizeof(buffer),conn->in)) break; if (!*buffer) continue; /* Tokenize command line */ res = parser_scan_buffer(&ctx,buffer,strlen(buffer)); if (res != 0) { tokens = NULL; if (ctx.error != 0) { hypervisor_send_reply(conn,HSC_ERR_PARSING,1,"Parse error: %s", parser_strerror(&ctx)); goto free_tokens; } if (ctx.tok_count < 2) { hypervisor_send_reply(conn,HSC_ERR_PARSING,1, "At least a module and a command " "must be specified"); goto free_tokens; } /* Map token list to an array */ tokens = parser_map_array(&ctx); if (!tokens) { hypervisor_send_reply(conn,HSC_ERR_PARSING,1,"No memory"); goto free_tokens; } /* Execute command */ //m_log("hypervisor_exec","%s\n",buffer); hypervisor_exec_cmd(conn,tokens[0],tokens[1],ctx. tok_count-2,&tokens[2]); free_tokens: free(tokens); tokens = NULL; parser_context_free(&ctx); } } free(tokens); parser_context_free(&ctx); return NULL; }
/* Free memory used by a parser context */ void parser_context_free(parser_context_t *ctx) { parser_free_tokens(ctx->tok_head); if (ctx->tmp_tok != NULL) free(ctx->tmp_tok); parser_context_init(ctx); }