From ea7649aa143bbd1820df887273f8756b7a569c17 Mon Sep 17 00:00:00 2001 From: xianjimli Date: Tue, 26 Mar 2019 18:07:30 +0800 Subject: [PATCH] add func_call_parse --- src/tkc/func_call_parser.c | 37 ++++++++++++++++++++++++++++++++++ src/tkc/func_call_parser.h | 15 +++++++++++++- tests/func_call_parser_test.cc | 17 ++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/tkc/func_call_parser.c b/src/tkc/func_call_parser.c index 227d083c0..96cd7cc78 100644 --- a/src/tkc/func_call_parser.c +++ b/src/tkc/func_call_parser.c @@ -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; +} diff --git a/src/tkc/func_call_parser.h b/src/tkc/func_call_parser.h index dd603b892..448c831e3 100644 --- a/src/tkc/func_call_parser.h +++ b/src/tkc/func_call_parser.h @@ -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*/ diff --git a/tests/func_call_parser_test.cc b/tests/func_call_parser_test.cc index 9856f61c2..c78d01b78 100644 --- a/tests/func_call_parser_test.cc +++ b/tests/func_call_parser_test.cc @@ -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 )"); +}