add func_call_parse

This commit is contained in:
xianjimli 2019-03-26 18:07:30 +08:00
parent 28771a22a1
commit ea7649aa14
3 changed files with 68 additions and 1 deletions

View File

@ -20,6 +20,7 @@
*/
#include "tkc/utils.h"
#include "tkc/object_default.h"
#include "tkc/func_call_parser.h"
func_call_parser_t* func_call_parser_init(func_call_parser_t* parser, const char* str,
@ -72,3 +73,39 @@ ret_t func_call_parser_deinit(func_call_parser_t* parser) {
return RET_OK;
}
typedef struct _object_parser_t {
func_call_parser_t base;
object_t* obj;
} object_parser_t;
static ret_t parser_on_param(func_call_parser_t* parser, const char* name, const char* value) {
object_parser_t* p = (object_parser_t*)parser;
return object_set_prop_str(p->obj, name, value);
}
static ret_t parser_on_name(func_call_parser_t* parser, const char* func_name) {
object_parser_t* p = (object_parser_t*)parser;
return object_set_name(p->obj, func_name);
}
object_t* func_call_parse(const char* str, uint32_t size) {
object_parser_t parser;
return_value_if_fail(str != NULL, NULL);
memset(&parser, 0x00, sizeof(parser));
func_call_parser_init(&(parser.base), str, size);
parser.obj = object_default_create();
parser.base.on_name = parser_on_name;
parser.base.on_param = parser_on_param;
return_value_if_fail(parser.obj != NULL, NULL);
func_call_parser_parse(&(parser.base));
func_call_parser_deinit(&(parser.base));
return parser.obj;
}

View File

@ -22,6 +22,7 @@
#ifndef TK_FUNC_CALL_PARSER_H
#define TK_FUNC_CALL_PARSER_H
#include "tkc/object.h"
#include "tkc/tokenizer.h"
BEGIN_C_DECLS
@ -60,7 +61,7 @@ struct _func_call_parser_t {
* parser对象
* @annotation ["constructor"]
* @param {func_call_parser_t*} parser parser对象
* @param {char*} str
* @param {const char*} str
* @param {uint32_t} size
*
* @return {func_call_parser_t*} parser对象本身
@ -86,6 +87,18 @@ ret_t func_call_parser_parse(func_call_parser_t* parser);
*/
ret_t func_call_parser_deinit(func_call_parser_t* parser);
/**
* @method func_call_parser_parse
* object对象
*
*> object对象
* @param {const char*} str
* @param {uint32_t} size
*
* @return {object_t*} object对象
*/
object_t* func_call_parse(const char* str, uint32_t size);
END_C_DECLS
#endif /*TK_FUNC_CALL_PARSER_H*/

View File

@ -58,3 +58,20 @@ TEST(FuncCallParser, basic) {
parser_test("move(x = 10, y = 20 )");
parser_test(" move(x = 10, y = 20 )");
}
static void parser_test_object(const char* str) {
object_t* obj = func_call_parse(str, strlen(str));
ASSERT_STREQ(obj->name, "move");
ASSERT_EQ(object_get_prop_int(obj, "x", 0), 10);
ASSERT_EQ(object_get_prop_int(obj, "y", 0), 20);
object_unref(obj);
}
TEST(FuncCallParser, object) {
parser_test_object("move(x=10, y=20)");
parser_test_object("move (x=10, y=20)");
parser_test_object("move(x = 10, y = 20 )");
parser_test_object(" move(x = 10, y = 20 )");
}