Skip to content

Commit

Permalink
operator: add _makepromise
Browse files Browse the repository at this point in the history
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Dec 22, 2022
1 parent d3244d9 commit ffaeee7
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion main/lregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -3940,6 +3940,66 @@ static EsObject *lrop_markplaceholder (OptVM *vm, EsObject *name)
return es_false;
}

static EsObject *lrop_makepromise (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
if (lcb->window->patbuf->regptype == REG_PARSER_SINGLE_LINE)
{
error (WARNING, "don't use `%s' operator in --regex-<LANG> option",
es_symbol_get (name));
return OPTSCRIPT_ERR_NOTMTABLEPTRN; /* TODO */
}

EsObject *endobj = opt_vm_ostack_top (vm);
if (es_object_get_type (endobj) != OPT_TYPE_MATCHLOC)
return OPT_ERR_TYPECHECK;
matchLoc *end = es_pointer_get (endobj);
off_t end_off = (off_t)(end->base + end->delta);

EsObject *startobj = opt_vm_ostack_peek (vm, 1);
if (es_object_get_type (startobj) != OPT_TYPE_MATCHLOC)
return OPT_ERR_TYPECHECK;
matchLoc *start = es_pointer_get (startobj);
off_t start_off = (off_t)(start->base + start->delta);

if (! (start_off < end_off))
return OPT_ERR_RANGECHECK;

EsObject *lang = opt_vm_ostack_peek (vm, 2);
const char *langc = opt_string_get_cstr (lang);
langType t = getNamedLanguageOrAlias (langc, 0);
if (t == LANG_IGNORE)
return OPTSCRIPT_ERR_UNKNOWNLANGUAGE;

if (start_off == end_off)
{
opt_vm_ostack_pop (vm);
opt_vm_ostack_pop (vm);
opt_vm_ostack_pop (vm);
opt_vm_ostack_push (vm, es_false);
return es_false;
}

int promise = makePromiseForAreaSpecifiedWithOffsets (langc,
start_off,
end_off);
opt_vm_ostack_pop (vm);
opt_vm_ostack_pop (vm);
opt_vm_ostack_pop (vm);

if (promise >= 0)
{
EsObject *promise_obj = es_integer_new (promise);
opt_vm_ostack_push (vm, promise_obj);
opt_vm_ostack_push (vm, es_true);
es_object_unref(promise_obj);
}
else
opt_vm_ostack_push (vm, es_false);

return es_false;
}

static struct optscriptOperatorRegistration lropOperators [] = {
{
.name = "_matchstr",
Expand Down Expand Up @@ -4097,7 +4157,14 @@ static struct optscriptOperatorRegistration lropOperators [] = {
.fn = lrop_markplaceholder,
.arity = 1,
.help_str = "tag:int _MARKPLACEHOLDER -",
}
},
{
.name = "_makepromise",
.fn = lrop_makepromise,
.arity = 3,
.help_str = "lang:string start:matchloc end:matchloc _MAKEPROMISE promise:int true%"
"lang:string start:matchloc end:matchloc _MAKEPROMISE false",
},
};

extern void initRegexOptscript (void)
Expand Down

0 comments on commit ffaeee7

Please sign in to comment.