Skip to content

Commit 9e59df0

Browse files
committed
lregex: provide the way to intercept a parser making a tag
TODO: - write about the new option in docs/optlib.rst. - add --_makeTagEntryNotification-<LANG>={{...}}. Signed-off-by: Masatake YAMATO <[email protected]>
1 parent be99f60 commit 9e59df0

File tree

14 files changed

+156
-6
lines changed

14 files changed

+156
-6
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# See #3020 and #3027.
2+
3+
--sort=no
4+
--extras=+q
5+
6+
--_extradef-Markdown=withfname,appending input filename
7+
--extras-Markdown=+{withfname}
8+
9+
--_prelude-Markdown={{
10+
% You can customize the string combining the original name
11+
% and the basen name of input file.
12+
/SEP (@) def
13+
14+
% dict<original-index:int, withfname-index:int>
15+
/scope-remapping-table 31 dict def
16+
17+
% (abc/input.d) dropext => (abc/input)
18+
% (abc.d/input) dropext => (abc.d/input)
19+
/dropext {
20+
(/.) _strrpbrk {
21+
% string offset
22+
2 copy get ?/ eq {
23+
pop
24+
} {
25+
0 exch 0 string _copyinterval
26+
} ifelse
27+
} if
28+
} def
29+
30+
% (abc/efg) basename => (efg)
31+
/basename {
32+
?/ _strrchr {
33+
1 add dup 2 index length exch sub
34+
0 string _copyinterval
35+
} if
36+
} def
37+
}}
38+
39+
--_sequel-Markdown={{
40+
% Fill the scope field of withfname extra tags.
41+
scope-remapping-table {
42+
% Make the original tag invisible
43+
exch dup _markplaceholder
44+
:scope dup
45+
% withfname-index:int original-scope:int original-scope:int
46+
0 eq {
47+
pop pop
48+
} {
49+
% withfname-index:int original-scope:int
50+
scope-remapping-table exch get
51+
% withfname-index:int withfname-scope:int
52+
scope:
53+
} ifelse
54+
} forall
55+
}}
56+
57+
--_makeTagEntryReflection-Markdown={{
58+
/Markdown.withfname _extraenabled {
59+
. :extras {
60+
/Markdown.withfname _amember not
61+
} {
62+
true
63+
} ifelse
64+
{
65+
mark
66+
. :name
67+
SEP
68+
. :input dropext basename
69+
_buildstring
70+
71+
. :kind
72+
. _tagloc _tag dup /Markdown.withfname _markextra
73+
_commit
74+
% Record the pair of original-index:int and withfname-index:int.
75+
scope-remapping-table exch . exch put
76+
} if
77+
78+
} if
79+
}}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ABC@input input.md /^# ABC$/;" c
2+
DEF@input input.md /^## DEF$/;" s chapter:ABC@input
3+
GHI@input input.md /^### GHI$/;" S section:ABC@input""DEF@input
4+
HIJ@input input.md /^### HIJ$/;" S section:ABC@input""DEF@input
5+
KLM@input input.md /^## KLM$/;" s chapter:ABC@input
6+
OPQ@input input.md /^# OPQ$/;" c
7+
RST@input input.md /^### RST$/;" S chapter:OPQ@input
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ABC
2+
3+
## DEF
4+
5+
### GHI
6+
7+
### HIJ
8+
9+
## KLM
10+
11+
# OPQ
12+
13+
### RST

main/dependency.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "debug.h"
1616
#include "dependency.h"
17+
#include "entry.h"
1718
#include "options.h"
1819
#include "parse_p.h"
1920
#include "read.h"
@@ -184,14 +185,18 @@ extern void notifyMakeTagEntry (const tagEntryInfo *tag, int corkIndex)
184185
{
185186
subparser *s;
186187

188+
/* running optscript code attaching to --makeTagEntryReflection-<LANG> */
189+
langType lang = tag->langType;
190+
notifyLanguageRegexMakeTagEntry (lang, corkIndex);
191+
187192
foreachSubparser(s, false)
188193
{
194+
enterSubparser(s);
189195
if (s->makeTagEntryNotify)
190-
{
191-
enterSubparser(s);
192196
s->makeTagEntryNotify (s, tag, corkIndex);
193-
leaveSubparser();
194-
}
197+
/* propagate the event recursively */
198+
notifyMakeTagEntry (tag, corkIndex);
199+
leaveSubparser();
195200
}
196201
}
197202

main/dependency.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ struct sSlaveParser {
4343
slaveParser *next;
4444
};
4545

46+
/* These are for CPreProcessor.
47+
* Don't use in the other parsers. */
48+
extern void notifyInputStart (void);
49+
extern void notifyInputEnd (void);
50+
4651
#endif /* CTAGS_MAIN_DEPENDENCY_H */

main/lregex.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,14 @@ extern void notifyRegexInputEnd (struct lregexControlBlock *lcb)
21532153
fillEndLineFieldOfUpperScopes (lcb, endline);
21542154
}
21552155

2156+
extern void notifyRegexMakeTagEntry (struct lregexControlBlock *lcb,
2157+
int corkIndex)
2158+
{
2159+
optscriptSetup (optvm, lcb->local_dict, corkIndex);
2160+
scriptEvalHook (optvm, lcb, SCRIPT_HOOK_MAKE_TAG_ENTRY_REFLECTION);
2161+
optscriptTeardown (optvm, lcb->local_dict);
2162+
}
2163+
21562164
extern void findRegexTagsMainloop (int (* driver)(void))
21572165
{
21582166
/* merely read all lines of the file */

main/lregex_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ extern bool matchMultitableRegex (struct lregexControlBlock *lcb, const vString*
103103

104104
extern void notifyRegexInputStart (struct lregexControlBlock *lcb);
105105
extern void notifyRegexInputEnd (struct lregexControlBlock *lcb);
106+
extern void notifyRegexMakeTagEntry (struct lregexControlBlock *lcb, int corkIndex);
106107

107108
extern void addRegexTable (struct lregexControlBlock *lcb, const char *name);
108109
extern void extendRegexTable (struct lregexControlBlock *lcb, const char *src, const char *dist);

main/options.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ static optionDescription LongOptionDescription [] = {
378378
{1,1," Define new extra for <LANG>. --extras-<LANG>=+{name} enables it."},
379379
{1,1," --_fielddef-<LANG>=<name>,<description>"},
380380
{1,1," Define new field for <LANG>."},
381+
{1,1," --_makeTagEnryReflection-<LANG>={{ optscript-code }}"},
382+
{1,1," Specify code run when <LANG> parser makes a tag."},
381383
{1,1," --_mtable-extend-<LANG>=disttable+srctable."},
382384
{1,1," Copy patterns of a regex table to another regex table."},
383385
{1,1," --_mtable-regex-<LANG>=<table>/<line_pattern>/<name_pattern>/[<flags>]"},
@@ -3348,6 +3350,8 @@ static void processLongOption (
33483350
;
33493351
else if (processSequelOption (option, parameter))
33503352
;
3353+
else if (processMakeTagEntryReflectionOption (option, parameter))
3354+
;
33513355
else if (processPretendOption (option, parameter))
33523356
;
33533357
else if (processRolesOption (option, parameter))

main/options_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ extern bool processRoledefOption (const char *const option, const char *const pa
177177
extern bool processScopesepOption (const char *const option, const char *const parameter);
178178
extern bool processPreludeOption (const char *const option, const char *const parameter);
179179
extern bool processSequelOption (const char *const option, const char *const parameter);
180+
extern bool processMakeTagEntryReflectionOption (const char *const option, const char *const parameter);
180181
extern bool processPretendOption (const char *const option, const char *const parameter);
181182
extern bool processRolesOption (const char *const option, const char *const parameter);
182183

main/parse.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4070,6 +4070,11 @@ extern void notifyLanguageRegexInputEnd (langType language)
40704070
notifyRegexInputEnd(pobj->lregexControlBlock);
40714071
}
40724072

4073+
extern void notifyLanguageRegexMakeTagEntry (langType language, int corkIndex)
4074+
{
4075+
notifyRegexMakeTagEntry((LanguageTable + language)->lregexControlBlock, corkIndex);
4076+
}
4077+
40734078
static unsigned int parserCorkFlags (parserDefinition *parser)
40744079
{
40754080
subparser *tmp;
@@ -5364,6 +5369,11 @@ extern bool processSequelOption (const char *const option, const char *const par
53645369
return processHookOption (option, parameter, "_sequel-", SCRIPT_HOOK_SEQUEL);
53655370
}
53665371

5372+
extern bool processMakeTagEntryReflectionOption (const char *const option, const char *const parameter)
5373+
{
5374+
return processHookOption (option, parameter, "_makeTagEntryReflection-", SCRIPT_HOOK_MAKE_TAG_ENTRY_REFLECTION);
5375+
}
5376+
53675377
extern bool processPretendOption (const char *const option, const char *const parameter)
53685378
{
53695379
langType new_language, old_language;

main/parse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ typedef enum {
7474
enum scriptHook {
7575
SCRIPT_HOOK_PRELUDE,
7676
SCRIPT_HOOK_SEQUEL,
77+
SCRIPT_HOOK_MAKE_TAG_ENTRY_REFLECTION,
7778
SCRIPT_HOOK_MAX,
7879
};
7980

main/parse_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ extern bool doesLanguageHaveForeignDependency (const langType language, const la
144144
extern bool processLanguageRegexOption (langType language, enum regexParserType regptype, const char *const parameter);
145145
extern void notifyLanguageRegexInputStart (langType language);
146146
extern void notifyLanguageRegexInputEnd (langType language);
147+
extern void notifyLanguageRegexMakeTagEntry (langType language, int corkIndex);
147148

148149
extern void matchLanguageRegex (const langType language, const vString* const line);
149150
extern void freeRegexResources (void);

main/subparser_p.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ extern subparser *getFirstSubparser(struct slaveControlBlock *controlBlock);
2828

2929
/* A base parser doesn't have to call the following three functions.
3030
The main part calls them internally. */
31-
extern void notifyInputStart (void);
32-
extern void notifyInputEnd (void);
3331
extern void notifyMakeTagEntry (const tagEntryInfo *info, int corkIndex);
3432

3533
extern void setupSubparsersInUse (struct slaveControlBlock *controlBlock);

parsers/cpreprocessor.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <string.h>
1717

1818
#include "debug.h"
19+
#include "dependency.h" /* notifyInputStart, notifyInputEnd */
1920
#include "entry.h"
2021
#include "htable.h"
2122
#include "cpreprocessor.h"
@@ -377,6 +378,14 @@ static void cppInitCommon(langType clientLang,
377378
: clientLang) & CORK_SYMTAB))
378379
? makeMacroTable ()
379380
: NULL;
381+
382+
if (Cpp.lang != Cpp.clientLang
383+
&& Cpp.clientLang != LANG_IGNORE)
384+
{
385+
pushLanguage (Cpp.lang);
386+
notifyInputStart ();
387+
popLanguage ();
388+
}
380389
}
381390

382391
extern void cppInit (const bool state, const bool hasAtLiteralStrings,
@@ -412,6 +421,14 @@ static void cppClearMacroInUse (cppMacroInfo **pM)
412421

413422
extern void cppTerminate (void)
414423
{
424+
if (Cpp.lang != Cpp.clientLang
425+
&& Cpp.clientLang != LANG_IGNORE)
426+
{
427+
pushLanguage (Cpp.lang);
428+
notifyInputEnd ();
429+
popLanguage ();
430+
}
431+
415432
if (Cpp.directive.name != NULL)
416433
{
417434
vStringDelete (Cpp.directive.name);

0 commit comments

Comments
 (0)