From: yu.dongliang <18588496441@163.com> Date: Mon, 27 Oct 2025 13:45:02 +0000 (+0800) Subject: js: support RegExpr such as 'var p = /L/gi;' X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=36a2da6051ace5a85f4041b124bf26294dfe9229;p=abc.git js: support RegExpr such as 'var p = /L/gi;' --- diff --git a/examples/js.html b/examples/js.html index 4c21780..852612d 100644 --- a/examples/js.html +++ b/examples/js.html @@ -9,7 +9,7 @@ diff --git a/js/parse/scf_dfa.c b/js/parse/scf_dfa.c index 790cbfb..e10711b 100644 --- a/js/parse/scf_dfa.c +++ b/js/parse/scf_dfa.c @@ -357,36 +357,36 @@ static int _scf_dfa_node_parse_word(scf_dfa_t* dfa, scf_dfa_node_t* node, scf_ve } } } + } - if (SCF_DFA_OK == ret) { - scf_dfa_hook_t** pp = &(dfa->hooks[SCF_DFA_HOOK_END]); + if (SCF_DFA_OK == ret) { + scf_dfa_hook_t** pp = &(dfa->hooks[SCF_DFA_HOOK_END]); - while (*pp) { - scf_dfa_hook_t* hook = *pp; - scf_dfa_node_t* hook_node = hook->node; + while (*pp) { + scf_dfa_hook_t* hook = *pp; + scf_dfa_node_t* hook_node = hook->node; - *pp = hook->next; - free(hook); - hook = NULL; + *pp = hook->next; + free(hook); + hook = NULL; - scf_logi("\033[34m end hook: %s->action()\033[0m\n", hook_node->name); + scf_logi("\033[34m end hook: %s->action()\033[0m\n", hook_node->name); - if (!hook_node->action) - continue; + if (!hook_node->action) + continue; - ret = hook_node->action(dfa, words, data); + ret = hook_node->action(dfa, words, data); - if (SCF_DFA_OK == ret) - continue; + if (SCF_DFA_OK == ret) + continue; - if (SCF_DFA_SWITCH_TO == ret) { - scf_logi("\033[34m end hook: switch to %s->%s\033[0m\n\n", node->name, hook_node->name); + if (SCF_DFA_SWITCH_TO == ret) { + scf_logi("\033[34m end hook: switch to %s->%s\033[0m\n\n", node->name, hook_node->name); - node = hook_node; - ret = SCF_DFA_NEXT_WORD; - } - break; + node = hook_node; + ret = SCF_DFA_NEXT_WORD; } + break; } } diff --git a/js/parse/scf_dfa_expr.c b/js/parse/scf_dfa_expr.c index 205c3c4..60348e7 100644 --- a/js/parse/scf_dfa_expr.c +++ b/js/parse/scf_dfa_expr.c @@ -49,6 +49,9 @@ static int _expr_is_unary_op(scf_dfa_t* dfa, void* word) || SCF_LEX_WORD_KEY_SIZEOF == w->type) return 0; + if (SCF_LEX_WORD_DIV == w->type) + return 1; + scf_operator_t* op = scf_find_base_operator(w->text->data, 1); if (op) return 1; @@ -422,7 +425,58 @@ static int _expr_action_op(scf_dfa_t* dfa, scf_vector_t* words, void* data, int static int _expr_action_unary_op(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_logd("\n"); + scf_lex_word_t* w = words->data[words->size - 1]; + scf_lex_word_t* w1; + scf_lex_word_t* w2; + + if (SCF_LEX_WORD_DIV == w->type) { + while (1) { + w1 = dfa->ops->pop_word(dfa); + assert(w1); + + if (SCF_LEX_WORD_EOF == w1->type) { + scf_loge("\n"); + + scf_lex_word_free(w1); + return SCF_DFA_ERROR; + } + + int tmp = w1->type; + int ret = scf_string_cat(w->text, w1->text); + + scf_lex_word_free(w1); + w1 = NULL; + if (ret < 0) + return ret; + + if (SCF_LEX_WORD_DIV == tmp) { + w2 = dfa->ops->pop_word(dfa); + assert(w2); + + if (SCF_LEX_WORD_ID != w2->type) { + scf_loge("\n"); + + scf_lex_word_free(w2); + return SCF_DFA_ERROR; + } + + ret = scf_string_cat(w->text, w2->text); + + scf_lex_word_free(w2); + w2 = NULL; + if (ret < 0) + return ret; + + w->data.s = scf_string_clone(w->text); + if (!w->data.s) + return SCF_DFA_ERROR; + + w->type = SCF_LEX_WORD_CONST_STRING; + return SCF_DFA_CONTINUE; + } + } + } + return _expr_action_op(dfa, words, data, 1); }