Skip to content

Commit

Permalink
lregex: generalize prelude and sequel mechanism to allow adding more …
Browse files Browse the repository at this point in the history
…hooks

Signed-off-by: Masatake YAMATO <[email protected]>
  • Loading branch information
masatake committed May 16, 2021
1 parent d619a4c commit b6124a6
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 41 deletions.
31 changes: 10 additions & 21 deletions main/lregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,6 @@ typedef struct {
unsigned int advanceto_delta;
} scriptWindow;

enum hook {
HOOK_PRELUDE,
HOOK_SEQUEL,
HOOK_MAX,
};

struct lregexControlBlock {
int currentScope;
ptrArray *entries [2];
Expand All @@ -240,8 +234,8 @@ struct lregexControlBlock {

EsObject *local_dict;

ptrArray *hook[HOOK_MAX];
ptrArray *hook_code[HOOK_MAX];
ptrArray *hook[SCRIPT_HOOK_MAX];
ptrArray *hook_code[SCRIPT_HOOK_MAX];

langType owner;

Expand Down Expand Up @@ -272,7 +266,7 @@ static void guestRequestSubmit (struct guestRequest *);
static EsObject *scriptRead (OptVM *vm, const char *src);
static void scriptSetup (OptVM *vm, struct lregexControlBlock *lcb, int corkIndex, scriptWindow *window);
static EsObject* scriptEval (OptVM *vm, EsObject *optscript);
static void scriptEvalHook (OptVM *vm, struct lregexControlBlock *lcb, enum hook hook);
static void scriptEvalHook (OptVM *vm, struct lregexControlBlock *lcb, enum scriptHook hook);
static void scriptTeardown (OptVM *vm, struct lregexControlBlock *lcb);

static char* make_match_string (scriptWindow *window, int group);
Expand Down Expand Up @@ -352,7 +346,7 @@ extern struct lregexControlBlock* allocLregexControlBlock (parserDefinition *par
lcb->guest_req = guestRequestNew ();
lcb->local_dict = es_nil;

for (int i = 0; i< HOOK_MAX; i++)
for (int i = 0; i< SCRIPT_HOOK_MAX; i++)
{
lcb->hook[i] = ptrArrayNew (eFree);
lcb->hook_code[i] = ptrArrayNew ((ptrArrayDeleteFunc)es_object_unref);
Expand Down Expand Up @@ -383,7 +377,7 @@ extern void freeLregexControlBlock (struct lregexControlBlock* lcb)
es_object_unref (lcb->local_dict);
lcb->local_dict = es_nil;

for (int i = 0; i < HOOK_MAX; i++)
for (int i = 0; i < SCRIPT_HOOK_MAX; i++)
{
ptrArrayDelete (lcb->hook[i]);
lcb->hook[i] = NULL;
Expand Down Expand Up @@ -2020,12 +2014,12 @@ extern void notifyRegexInputStart (struct lregexControlBlock *lcb)
lcb->local_dict = opt_dict_new (23);
opt_vm_dstack_push (optvm, lcb->local_dict);
opt_vm_set_app_data (optvm, lcb);
scriptEvalHook (optvm, lcb, HOOK_PRELUDE);
scriptEvalHook (optvm, lcb, SCRIPT_HOOK_PRELUDE);
}

extern void notifyRegexInputEnd (struct lregexControlBlock *lcb)
{
scriptEvalHook (optvm, lcb, HOOK_SEQUEL);
scriptEvalHook (optvm, lcb, SCRIPT_HOOK_SEQUEL);
opt_vm_set_app_data (optvm, NULL);
opt_vm_clear (optvm);
opt_dict_clear (lcb->local_dict);
Expand Down Expand Up @@ -3000,7 +2994,7 @@ extern EsObject* scriptEval (OptVM *vm, EsObject *optscript)
return optscriptEval (vm, optscript);
}

static void scriptEvalHook (OptVM *vm, struct lregexControlBlock *lcb, enum hook hook)
static void scriptEvalHook (OptVM *vm, struct lregexControlBlock *lcb, enum scriptHook hook)
{
if (ptrArrayCount (lcb->hook_code[hook]) == 0)
{
Expand Down Expand Up @@ -3036,14 +3030,9 @@ static void scriptTeardown (OptVM *vm, struct lregexControlBlock *lcb)
lcb->window = NULL;
}

extern void addOptscriptPrelude (struct lregexControlBlock *lcb, const char *code)
{
ptrArrayAdd (lcb->hook[HOOK_PRELUDE], eStrdup (code));
}

extern void addOptscriptSequel (struct lregexControlBlock *lcb, const char *code)
extern void addOptscriptToHook (struct lregexControlBlock *lcb, enum scriptHook hook, const char *code)
{
ptrArrayAdd (lcb->hook[HOOK_SEQUEL], eStrdup (code));
ptrArrayAdd (lcb->hook[hook], eStrdup (code));
}

/* Return true if available. */
Expand Down
5 changes: 3 additions & 2 deletions main/lregex_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "general.h"
#include "kind_p.h"
#include "lregex.h"
#include "parse.h"

/*
* DATA DECLARATIONS
Expand Down Expand Up @@ -74,8 +75,8 @@ extern void extendRegexTable (struct lregexControlBlock *lcb, const char *src, c

extern void initRegexOptscript (void);
extern void listRegexOpscriptOperators (FILE *fp);
extern void addOptscriptPrelude (struct lregexControlBlock *lcb, const char *code);
extern void addOptscriptSequel (struct lregexControlBlock *lcb, const char *code);

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

extern void printMultitableStatistics (struct lregexControlBlock *lcb);

Expand Down
17 changes: 6 additions & 11 deletions main/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -5017,18 +5017,13 @@ extern void addLanguageTagMultiTableRegex(const langType language,
name, kinds, flags, disabled);
}

extern void addLanguageOptscriptPrelude (langType language, const char *const src)
extern void addLanguageOptscriptToHook (langType language, enum scriptHook hook, const char *const src)
{
addOptscriptPrelude (LanguageTable [language].lregexControlBlock, src);
}

extern void addLanguageOptscriptSequel (langType language, const char *const src)
{
addOptscriptSequel (LanguageTable [language].lregexControlBlock, src);
addOptscriptToHook (LanguageTable [language].lregexControlBlock, hook, src);
}

static bool processHookOption (const char *const option, const char *const parameter, const char *prefix,
void (* add) (langType, const char *))
enum scriptHook hook)
{
langType language = getLanguageComponentInOption (option, prefix);
if (language == LANG_IGNORE)
Expand All @@ -5040,19 +5035,19 @@ static bool processHookOption (const char *const option, const char *const param
const char * code = flagsEval (parameter, NULL, 0, NULL);
if (code == NULL)
error (FATAL, "Cannot recognized a code block surrounded by `{{' and `}}' after \"%s\" option", option);
(* add) (language, code);
addLanguageOptscriptToHook (language, hook, code);

return true;
}

extern bool processPreludeOption (const char *const option, const char *const parameter)
{
return processHookOption (option, parameter, "_prelude-", addLanguageOptscriptPrelude);
return processHookOption (option, parameter, "_prelude-", SCRIPT_HOOK_PRELUDE);
}

extern bool processSequelOption (const char *const option, const char *const parameter)
{
return processHookOption (option, parameter, "_sequel-", addLanguageOptscriptSequel);
return processHookOption (option, parameter, "_sequel-", SCRIPT_HOOK_SEQUEL);
}

extern bool processPretendOption (const char *const option, const char *const parameter)
Expand Down
10 changes: 8 additions & 2 deletions main/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ typedef enum {
CORK_SYMTAB = (1 << 1),
} corkUsage;

/* optlib2c requires the declaration here. */
enum scriptHook {
SCRIPT_HOOK_PRELUDE,
SCRIPT_HOOK_SEQUEL,
SCRIPT_HOOK_MAX,
};

struct sParserDefinition {
/* defined by parser */
char* name; /* name of language */
Expand Down Expand Up @@ -170,8 +177,7 @@ extern void addLanguageTagMultiTableRegex(const langType language,
const char* const name, const char* const kinds, const char* const flags,
bool *disabled);

extern void addLanguageOptscriptPrelude (langType language, const char *const src);
extern void addLanguageOptscriptSequel (langType language, const char *const src);
extern void addLanguageOptscriptToHook (langType language, enum scriptHook hook, const char *const src);

extern void anonGenerate (vString *buffer, const char *prefix, int kind);
extern vString *anonGenerateNew (const char *prefix, int kind);
Expand Down
4 changes: 2 additions & 2 deletions misc/optlib2c
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ EOF
: '\n"');
}
print <<EOF;
addLanguageOptscriptPrelude (language,$prelude);
addLanguageOptscriptToHook (language, SCRIPT_HOOK_PRELUDE,$prelude);
EOF
}
for (@{$opts->{'sequel'}}) {
Expand All @@ -625,7 +625,7 @@ EOF
: '\n"');
}
print <<EOF;
addLanguageOptscriptSequel (language,$sequel);
addLanguageOptscriptToHook (language, SCRIPT_HOOK_SEQUEL,$sequel);
EOF
}
if ($opts->{'tablenames'}) {
Expand Down
2 changes: 1 addition & 1 deletion optlib/markdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef enum {

static void initializeMarkdownParser (const langType language)
{
addLanguageOptscriptPrelude (language,
addLanguageOptscriptToHook (language, SCRIPT_HOOK_PRELUDE,
"{{ % group:int SCOPEPOPWITHADJUSTMENT -\n"
" /scopePopWithAdjustment {\n"
" _scopetop {\n"
Expand Down
2 changes: 1 addition & 1 deletion optlib/pod.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

static void initializePodParser (const langType language CTAGS_ATTR_UNUSED)
{
addLanguageOptscriptPrelude (language,
addLanguageOptscriptToHook (language, SCRIPT_HOOK_PRELUDE,
"{{ /kindTable\n"
" [ /chapter /section /subsection /subsubsection ] def\n"
"}}");
Expand Down
2 changes: 1 addition & 1 deletion optlib/puppetManifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef enum {

static void initializePuppetManifestParser (const langType language)
{
addLanguageOptscriptPrelude (language,
addLanguageOptscriptToHook (language, SCRIPT_HOOK_PRELUDE,
"{{ % spec:dict<num,kind> TRYMAKETAG tag:int true\n"
" % spec:dict<num,kind> TRYMAKETAG false\n"
" /trymaketag {\n"
Expand Down

0 comments on commit b6124a6

Please sign in to comment.