mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
reduce #if
format parser
This commit is contained in:
parent
7e15e5e202
commit
1380c190bb
154
src/PikaParser.c
154
src/PikaParser.c
@ -905,7 +905,6 @@ void Cursor_beforeIter(struct Cursor* ps) {
|
||||
ps->last_token = arg_newStr(Parser_popToken(ps->buffs_p, ps->tokens));
|
||||
}
|
||||
|
||||
#if PIKA_SYNTAX_SLICE_ENABLE
|
||||
static void Slice_getPars(Args* outBuffs,
|
||||
char* inner,
|
||||
char** pStart,
|
||||
@ -960,10 +959,11 @@ static void Slice_getPars(Args* outBuffs,
|
||||
/* clean */
|
||||
strsDeinit(&buffs);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PIKA_SYNTAX_SLICE_ENABLE
|
||||
char* Suger_solveLeftBranckets(Args* outBuffs, char* right, char** left_p) {
|
||||
char* Suger_solveLeftSlice(Args* outBuffs, char* right, char** left_p) {
|
||||
#if !PIKA_SYNTAX_SLICE_ENABLE
|
||||
return right;
|
||||
#endif
|
||||
/* init objects */
|
||||
Args buffs = {0};
|
||||
Arg* right_arg = arg_newStr("");
|
||||
@ -1065,10 +1065,11 @@ exit:
|
||||
strsDeinit(&buffs);
|
||||
return right_res;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PIKA_SYNTAX_FORMAT_ENABLE
|
||||
char* Suger_solveFormat(Args* outBuffs, char* right) {
|
||||
#if !PIKA_SYNTAX_FORMAT_ENABLE
|
||||
return right;
|
||||
#endif
|
||||
/* quick skip */
|
||||
if (!strIsContain(right, '%')) {
|
||||
return right;
|
||||
@ -1164,7 +1165,6 @@ char* Suger_solveFormat(Args* outBuffs, char* right) {
|
||||
strsDeinit(&buffs);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
||||
uint8_t Parser_solveSelfOperator(Args* outbuffs,
|
||||
char* stmt,
|
||||
@ -1341,6 +1341,70 @@ char* Parser_popLastSubStmt(Args* outbuffs, char** stmt_p, char* delimiter) {
|
||||
return strsCacheArg(outbuffs, lastStmt);
|
||||
}
|
||||
|
||||
static void _AST_parse_list(AST* ast, Args* buffs, char* stmt) {
|
||||
#if !PIKA_BUILTIN_STRUCT_ENABLE
|
||||
return;
|
||||
#endif
|
||||
AST_setThisNode(ast, (char*)"list", "list");
|
||||
char* subStmts = strsCut(buffs, stmt, '[', ']');
|
||||
subStmts = strsAppend(buffs, subStmts, ",");
|
||||
while (1) {
|
||||
char* subStmt = Parser_popSubStmt(buffs, &subStmts, ",");
|
||||
AST_parseSubStmt(ast, subStmt);
|
||||
if (strEqu(subStmts, "")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void _AST_parse_dict(AST* ast, Args* buffs, char* stmt) {
|
||||
#if !PIKA_BUILTIN_STRUCT_ENABLE
|
||||
return;
|
||||
#endif
|
||||
AST_setThisNode(ast, (char*)"dict", "dict");
|
||||
char* subStmts = strsCut(buffs, stmt, '{', '}');
|
||||
subStmts = strsAppend(buffs, subStmts, ",");
|
||||
while (1) {
|
||||
char* subStmt = Parser_popSubStmt(buffs, &subStmts, ",");
|
||||
char* key = Parser_popSubStmt(buffs, &subStmt, ":");
|
||||
char* value = subStmt;
|
||||
AST_parseSubStmt(ast, key);
|
||||
AST_parseSubStmt(ast, value);
|
||||
if (strEqu(subStmts, "")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void _AST_parse_slice(AST* ast, Args* buffs, char* stmt) {
|
||||
#if !PIKA_SYNTAX_SLICE_ENABLE
|
||||
return;
|
||||
#endif
|
||||
AST_setThisNode(ast, (char*)"slice", "slice");
|
||||
stmt = strsCopy(buffs, stmt);
|
||||
char* laststmt = Parser_popLastSubStmt(buffs, &stmt, "[");
|
||||
AST_parseSubStmt(ast, stmt);
|
||||
char* slice_list = strsCut(buffs, laststmt, '[', ']');
|
||||
pika_assert(slice_list != NULL);
|
||||
slice_list = strsAppend(buffs, slice_list, ":");
|
||||
int index = 0;
|
||||
while (1) {
|
||||
char* slice_str = Parser_popSubStmt(buffs, &slice_list, ":");
|
||||
if (index == 0 && strEqu(slice_str, "")) {
|
||||
AST_parseSubStmt(ast, "0");
|
||||
} else if (index == 1 && strEqu(slice_str, "")) {
|
||||
AST_parseSubStmt(ast, "-1");
|
||||
} else {
|
||||
AST_parseSubStmt(ast, slice_str);
|
||||
}
|
||||
index++;
|
||||
if (strEqu("", slice_list)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AST* AST_parseStmt(AST* ast, char* stmt) {
|
||||
Args buffs = {0};
|
||||
char* assignment = strsGetFirstToken(&buffs, stmt, '(');
|
||||
@ -1384,14 +1448,9 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
|
||||
isLeftExist = Parser_solveSelfOperator(&buffs, stmt, &right, &left);
|
||||
}
|
||||
|
||||
#if PIKA_SYNTAX_SLICE_ENABLE
|
||||
/* solve the [] stmt */
|
||||
right = Suger_solveLeftBranckets(&buffs, right, &left);
|
||||
#endif
|
||||
|
||||
#if PIKA_SYNTAX_FORMAT_ENABLE
|
||||
right = Suger_solveLeftSlice(&buffs, right, &left);
|
||||
right = Suger_solveFormat(&buffs, right);
|
||||
#endif
|
||||
|
||||
/* set left */
|
||||
if (isLeftExist) {
|
||||
@ -1417,42 +1476,17 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
#if PIKA_BUILTIN_STRUCT_ENABLE
|
||||
/* solve list stmt */
|
||||
if (STMT_list == stmtType) {
|
||||
AST_setThisNode(ast, (char*)"list", "list");
|
||||
char* subStmts = strsCut(&buffs, right, '[', ']');
|
||||
subStmts = strsAppend(&buffs, subStmts, ",");
|
||||
while (1) {
|
||||
char* subStmt = Parser_popSubStmt(&buffs, &subStmts, ",");
|
||||
AST_parseSubStmt(ast, subStmt);
|
||||
if (strEqu(subStmts, "")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_AST_parse_list(ast, &buffs, right);
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PIKA_BUILTIN_STRUCT_ENABLE
|
||||
/* solve dict stmt */
|
||||
if (STMT_dict == stmtType) {
|
||||
AST_setThisNode(ast, (char*)"dict", "dict");
|
||||
char* subStmts = strsCut(&buffs, right, '{', '}');
|
||||
subStmts = strsAppend(&buffs, subStmts, ",");
|
||||
while (1) {
|
||||
char* subStmt = Parser_popSubStmt(&buffs, &subStmts, ",");
|
||||
char* key = Parser_popSubStmt(&buffs, &subStmt, ":");
|
||||
char* value = subStmt;
|
||||
AST_parseSubStmt(ast, key);
|
||||
AST_parseSubStmt(ast, value);
|
||||
if (strEqu(subStmts, "")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_AST_parse_dict(ast, &buffs, right);
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* solve method chain */
|
||||
if (STMT_chain == stmtType) {
|
||||
@ -1463,34 +1497,11 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
#if PIKA_SYNTAX_SLICE_ENABLE
|
||||
if (STMT_slice == stmtType) {
|
||||
/* solve slice stmt */
|
||||
AST_setThisNode(ast, (char*)"slice", "slice");
|
||||
char* stmt = strsCopy(&buffs, right);
|
||||
char* laststmt = Parser_popLastSubStmt(&buffs, &stmt, "[");
|
||||
AST_parseSubStmt(ast, stmt);
|
||||
char* slice_list = strsCut(&buffs, laststmt, '[', ']');
|
||||
pika_assert(slice_list != NULL);
|
||||
slice_list = strsAppend(&buffs, slice_list, ":");
|
||||
int index = 0;
|
||||
while (1) {
|
||||
char* slice_str = Parser_popSubStmt(&buffs, &slice_list, ":");
|
||||
if (index == 0 && strEqu(slice_str, "")) {
|
||||
AST_parseSubStmt(ast, "0");
|
||||
} else if (index == 1 && strEqu(slice_str, "")) {
|
||||
AST_parseSubStmt(ast, "-1");
|
||||
} else {
|
||||
AST_parseSubStmt(ast, slice_str);
|
||||
}
|
||||
index++;
|
||||
if (strEqu("", slice_list)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_AST_parse_slice(ast, &buffs, right);
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* solve method stmt */
|
||||
if (STMT_method == stmtType) {
|
||||
@ -1864,8 +1875,10 @@ exit:
|
||||
return ast;
|
||||
}
|
||||
|
||||
#if PIKA_SYNTAX_IMPORT_EX_ENABLE
|
||||
static char* Suger_import(Args* buffs_p, char* line) {
|
||||
#if !PIKA_SYNTAX_IMPORT_EX_ENABLE
|
||||
return line;
|
||||
#endif
|
||||
Args buffs = {0};
|
||||
char* line_out = line;
|
||||
char* alias = NULL;
|
||||
@ -1907,10 +1920,11 @@ exit:
|
||||
strsDeinit(&buffs);
|
||||
return line_out;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PIKA_SYNTAX_IMPORT_EX_ENABLE
|
||||
static char* Suger_from(Args* buffs_p, char* line) {
|
||||
#if !PIKA_SYNTAX_IMPORT_EX_ENABLE
|
||||
return line;
|
||||
#endif
|
||||
Args buffs = {0};
|
||||
char* line_out = line;
|
||||
char* class = NULL;
|
||||
@ -1964,7 +1978,6 @@ exit:
|
||||
strsDeinit(&buffs);
|
||||
return line_out;
|
||||
}
|
||||
#endif
|
||||
|
||||
static char* Parser_linePreProcess(Args* buffs_p, char* line) {
|
||||
line = Parser_removeAnnotation(line);
|
||||
@ -1975,10 +1988,8 @@ static char* Parser_linePreProcess(Args* buffs_p, char* line) {
|
||||
}
|
||||
/* process EOL */
|
||||
line = strsDeleteChar(buffs_p, line, '\r');
|
||||
#if PIKA_SYNTAX_IMPORT_EX_ENABLE
|
||||
line = Suger_import(buffs_p, line);
|
||||
line = Suger_from(buffs_p, line);
|
||||
#endif
|
||||
exit:
|
||||
return line;
|
||||
}
|
||||
@ -2148,9 +2159,6 @@ static char* _Parser_linesToBytesOrAsm(Args* outBuffs,
|
||||
parse_line:
|
||||
/* parse single Line to Asm */
|
||||
single_ASM = Parser_LineToAsm(&buffs, line, &block_stack);
|
||||
#if PIKA_DEBUG
|
||||
pika_assert(NULL != single_ASM);
|
||||
#endif
|
||||
parse_after:
|
||||
if (NULL == single_ASM) {
|
||||
out_ASM = NULL;
|
||||
|
Loading…
x
Reference in New Issue
Block a user