Skip to content

Commit

Permalink
operator: add _makepromise
Browse files Browse the repository at this point in the history
Signed-off-by: Masatake YAMATO <[email protected]>
  • Loading branch information
masatake committed Dec 12, 2022
1 parent 6c8ce10 commit c62933e
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion main/lregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -3940,6 +3940,59 @@ 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);
EsObject *promise_obj = es_integer_new (promise);
opt_vm_ostack_push (vm, promise_obj);
es_object_unref(promise_obj);

return es_false;
}

static struct optscriptOperatorRegistration lropOperators [] = {
{
.name = "_matchstr",
Expand Down Expand Up @@ -4097,7 +4150,13 @@ 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|false"
},
};

extern void initRegexOptscript (void)
Expand Down

0 comments on commit c62933e

Please sign in to comment.