From b24dbc50a670c3b48c028edab77efe8203b01b16 Mon Sep 17 00:00:00 2001 From: lyon Date: Sat, 27 Nov 2021 17:26:06 +0800 Subject: [PATCH] string is ok --- port/linux/test/parse-test.cpp | 23 ++++++++++++-- src/PikaParser.c | 58 +++++++++++++++++++++++++++++----- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/port/linux/test/parse-test.cpp b/port/linux/test/parse-test.cpp index fb3bfed5d..8205d7c79 100644 --- a/port/linux/test/parse-test.cpp +++ b/port/linux/test/parse-test.cpp @@ -989,8 +989,27 @@ TEST(lexser, symbol_and) { printf((char*)"%s\n", printTokens); /* assert */ - EXPECT_STREQ(printTokens, - "{sym}res{opt}={sym}add{dvd}({lit}1{opt}and{sym}lkj{dvd},{lit}2{dvd})"); + EXPECT_STREQ( + printTokens, + "{sym}res{opt}={sym}add{dvd}({lit}1{opt}and{sym}lkj{dvd},{lit}2{dvd})"); + + /* deinit */ + args_deinit(buffs); + EXPECT_EQ(pikaMemNow(), 0); +} + +TEST(lexser, sting) { + /* init */ + pikaMemInfo.heapUsedMax = 0; + Args* buffs = New_strBuff(); + + /* run */ + char* tokens = Lexer_getTokens(buffs, (char*)" a= 'elk 2'"); + char* printTokens = Lexer_printTokens(buffs, tokens); + printf((char*)"%s\n", printTokens); + + /* assert */ + EXPECT_STREQ(printTokens, "{sym}a{opt}={lit}'elk 2'"); /* deinit */ args_deinit(buffs); diff --git a/src/PikaParser.c b/src/PikaParser.c index c744dab2d..8903f5d3c 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -176,9 +176,9 @@ char* Lexer_printTokens(Args* outBuffs, char* tokens) { char* printOut = strsCopy(buffs, ""); /* process */ - uint16_t tokenSize = strCountSign(tokens, ' ') + 1; + uint16_t tokenSize = strCountSign(tokens, 0x1F) + 1; for (int i = 0; i < tokenSize; i++) { - char* token = strsPopToken(buffs, tokens, ' '); + char* token = strsPopToken(buffs, tokens, 0x1F); if (token[0] == TOKEN_operator) { printOut = strsAppend(buffs, printOut, "{opt}"); printOut = strsAppend(buffs, printOut, token + 1); @@ -208,7 +208,7 @@ Arg* Lexer_setToken(Arg* tokens_arg, operator) { Args* buffs = New_strBuff(); char token_type_buff[3] = {0}; - token_type_buff[0] = ' '; + token_type_buff[0] = 0x1F; token_type_buff[1] = token_type; char* tokens = arg_getStr(tokens_arg); tokens = strsAppend(buffs, tokens, token_type_buff); @@ -231,11 +231,13 @@ Arg* Lexer_setSymbel(Arg* tokens_arg, } char* symbol_buff = args_getBuff(buffs, i - *symbol_start_index); memcpy(symbol_buff, stmt + *symbol_start_index, i - *symbol_start_index); - if ((symbol_buff[0] == '-') || - /* literal */ + /* literal */ + if ((symbol_buff[0] == '-') || (symbol_buff[0] == '\'') || + (symbol_buff[0] == '"') || ((symbol_buff[0] >= '0') && (symbol_buff[0] <= '9'))) { tokens_arg = Lexer_setToken(tokens_arg, TOKEN_literal, symbol_buff); - } else { + } + else { /* symbol */ tokens_arg = Lexer_setToken(tokens_arg, TOKEN_symbol, symbol_buff); } @@ -257,6 +259,7 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) { uint8_t c2 = 0; uint8_t c3 = 0; int symbol_start_index = -1; + int is_in_string = 0; /* process */ for (int i = 0; i < size; i++) { @@ -274,6 +277,38 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) { if (-1 == symbol_start_index) { symbol_start_index = i; } + + /* solve string */ + if (0 == is_in_string) { + if ('\'' == c0) { + /* in ' */ + is_in_string = 1; + continue; + } + if ('"' == c0) { + /* in "" */ + is_in_string = 2; + continue; + } + } + + if (1 == is_in_string) { + if ('\'' == c0) { + is_in_string = 0; + tokens_arg = Lexer_setSymbel(tokens_arg, stmt, i + 1, + &symbol_start_index); + } + continue; + } + if (2 == is_in_string) { + if ('"' == c0) { + is_in_string = 0; + tokens_arg = Lexer_setSymbel(tokens_arg, stmt, i + 1, + &symbol_start_index); + } + continue; + } + /* match devider*/ if (('(' == c0) || (')' == c0) || (',' == c0)) { tokens_arg = @@ -400,8 +435,15 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) { } } /* skip spaces */ - if ((' ' == c0) && (i == symbol_start_index)) { - symbol_start_index++; + if (' ' == c0) { + /* not get symbal */ + if (i == symbol_start_index) { + symbol_start_index++; + } else { + /* already get symbal */ + tokens_arg = + Lexer_setSymbel(tokens_arg, stmt, i, &symbol_start_index); + } } if (i == size - 1) { /* last check symbel */