mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-29 18:48:09 +08:00
add func_call_parse
This commit is contained in:
parent
28771a22a1
commit
ea7649aa14
@ -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;
|
||||
}
|
||||
|
@ -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*/
|
||||
|
@ -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 )");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user