Skip to content

Commit

Permalink
main: define operators accessing parser specific fields
Browse files Browse the repository at this point in the history
The operators (setter and/or getter) for each field.
The name of operatos has following form:

	setter => :<LANG>.<FIELD>
	getter => <LANG>.<FIELD>:

NOTE: The operators for a field are defined only if the
field has C level getters and setters, getValueObject and setValueObject
in fieldDefinition. This commit doesn't provide a getter and a setter.
The test cases and the real accessor will be implemented later.

Signed-off-by: Masatake YAMATO <[email protected]>
  • Loading branch information
masatake committed Aug 30, 2024
1 parent ce6c7e9 commit 7b991c9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 deletions.
2 changes: 2 additions & 0 deletions main/field.c
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,8 @@ extern int defineField (fieldDefinition *def, langType language)
fobj->sibling = FIELD_UNKNOWN;

updateSiblingField (def->ftype, def->name);
installOptscriptFieldAccessor (def->ftype);

return def->ftype;
}

Expand Down
13 changes: 13 additions & 0 deletions main/lregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -4564,6 +4564,19 @@ static struct optscriptOperatorRegistration lropOperators [] = {
},
};

extern bool installOptscriptFieldAccessor (fieldType ftype)
{
/* This function is called via defineField().
defineField() is called via --fielddef option of built-in parsers
defining their specific fields.
built-in parsers are initialized lazily in the current implementation.
optvm is initialized earlier than parsers. */
Assert (!es_null(lregex_dict));

optscriptInstallFieldAccessor (lregex_dict, ftype);
return true;
}

extern void initRegexOptscript (void)
{
if (!regexAvailable)
Expand Down
3 changes: 3 additions & 0 deletions main/lregex_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* INCLUDE FILES
*/
#include "general.h"
#include "field.h"
#include "flags_p.h"
#include "kind_p.h"
#include "lregex.h"
Expand Down Expand Up @@ -114,6 +115,8 @@ extern void extendRegexTable (struct lregexControlBlock *lcb, const char *src, c
extern void initRegexOptscript (void);
extern void listRegexOpscriptOperators (FILE *fp);

extern bool installOptscriptFieldAccessor (fieldType ftype);

extern void addOptscriptToHook (struct lregexControlBlock *lcb, enum scriptHook hook, const char *code);
extern void propagateParamToOptscript (struct lregexControlBlock *lcb, const char *param, const char *value);

Expand Down
1 change: 1 addition & 0 deletions main/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,7 @@ static void processListSubparsersOptions (const char *const option CTAGS_ATTR_UN
static void processListOperators (const char *const option CTAGS_ATTR_UNUSED,
const char *const parameter)
{
initializeParser (LANG_AUTO);
listRegexOpscriptOperators (stdout);
exit (0);
}
Expand Down
56 changes: 49 additions & 7 deletions main/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,22 @@ static EsObject* lrop_set_field_value (OptVM *vm, EsObject *name)
return es_false;
}

static void optscriptInstallFieldGetter (EsObject *dict, fieldType ftype,
vString *op_name, vString *op_desc)
static void optscriptInstallFieldGetterFast (EsObject *dict, fieldType ftype,
vString *op_name, vString *op_desc)
{
const char *fname = getFieldName (ftype);
vStringPut (op_name, ':');

langType lang = getFieldLanguage (ftype);
if (lang != LANG_IGNORE)
{
const char *lname = getLanguageName (lang);
vStringCatS (op_name, lname);
vStringPut (op_name, '.');
}

const char *fname = getFieldName (ftype);
vStringCatS (op_name, fname);

EsObject *op_sym = es_symbol_intern (vStringValue (op_name));
es_symbol_set_data (op_sym, HT_INT_TO_PTR (ftype));

Expand Down Expand Up @@ -213,9 +223,17 @@ static void optscriptInstallFieldGetter (EsObject *dict, fieldType ftype,
es_object_unref (op);
}

static void optscriptInstallFieldSetter (EsObject *dict, fieldType ftype,
vString *op_name, vString *op_desc)
static void optscriptInstallFieldSetterFast (EsObject *dict, fieldType ftype,
vString *op_name, vString *op_desc)
{
langType lang = getFieldLanguage (ftype);
if (lang != LANG_IGNORE)
{
const char *lname = getLanguageName (lang);
vStringCatS (op_name, lname);
vStringPut (op_name, '.');
}

const char *fname = getFieldName (ftype);
vStringCatS (op_name, fname);
vStringPut (op_name, ':');
Expand Down Expand Up @@ -252,22 +270,46 @@ static void optscriptInstallFieldSetter (EsObject *dict, fieldType ftype,
es_object_unref (op);
}

extern void optscriptInstallFieldAccessor (EsObject *dict, fieldType ftype)
{
vString *op_name = vStringNew ();
vString *op_desc = vStringNew ();

if (hasFieldGetter (ftype))
{
optscriptInstallFieldGetterFast (dict, ftype, op_name, op_desc);
vStringClear (op_name);
vStringClear (op_desc);
}

if (hasFieldSetter (ftype))
optscriptInstallFieldSetterFast (dict, ftype, op_name, op_desc);

vStringDelete (op_name);
vStringDelete (op_desc);
}

static void optscriptInstallFieldAccessors (EsObject *dict)
{
vString *op_name = vStringNew ();
vString *op_desc = vStringNew ();

/* In the current implementation, the upper boundary of the loop
can be "<= FIELD_BUILTIN_LAST" because the parser specific fields
are registered via defineField. However, when we change the order
and the way to initialize parsers, we may have to use "< countFields()"
instead. */
for (fieldType ftype = 0; ftype <= FIELD_BUILTIN_LAST; ftype++)
{
if (hasFieldGetter (ftype))
{
optscriptInstallFieldGetter (dict, ftype, op_name, op_desc);
optscriptInstallFieldGetterFast (dict, ftype, op_name, op_desc);
vStringClear (op_name);
vStringClear (op_desc);
}
if (hasFieldSetter (ftype))
{
optscriptInstallFieldSetter (dict, ftype, op_name, op_desc);
optscriptInstallFieldSetterFast (dict, ftype, op_name, op_desc);
vStringClear (op_name);
vStringClear (op_desc);
}
Expand Down
2 changes: 2 additions & 0 deletions main/script_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "general.h" /* must always come first */

#include "field.h"
#include "optscript.h"
#include "mio.h"

Expand All @@ -33,6 +34,7 @@ extern EsObject *OPTSCRIPT_ERR_LANGMISMATCH;
extern OptVM *optscriptInit (void);

extern void optscriptInstallProcs (EsObject *dict, OptOperatorFn matchResultAccessor);
extern void optscriptInstallFieldAccessor (EsObject *dict, fieldType ftype);

extern void optscriptSetup (OptVM *vm, EsObject *dict, int corkIndex);
extern void optscriptTeardown (OptVM *vm, EsObject *dict);
Expand Down

0 comments on commit 7b991c9

Please sign in to comment.