Skip to content

Commit ff6417f

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 cd7e273 commit ff6417f

File tree

14 files changed

+159
-6
lines changed

14 files changed

+159
-6
lines changed
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 . 3 -1 roll put
76+
} if
77+
78+
} if
79+
}}
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
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

+9-4
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

+5
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

+11
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,17 @@ extern void notifyRegexInputEnd (struct lregexControlBlock *lcb)
22492249
fillEndLineFieldOfUpperScopes (lcb, endline);
22502250
}
22512251

2252+
extern void notifyRegexMakeTagEntry (struct lregexControlBlock *lcb,
2253+
int corkIndex)
2254+
{
2255+
if (ptrArrayCount (lcb->hook[SCRIPT_HOOK_MAKE_TAG_ENTRY_REFLECTION]) > 0)
2256+
{
2257+
optscriptSetup (optvm, lcb->local_dict, corkIndex);
2258+
scriptEvalHook (optvm, lcb, SCRIPT_HOOK_MAKE_TAG_ENTRY_REFLECTION);
2259+
optscriptTeardown (optvm, lcb->local_dict);
2260+
}
2261+
}
2262+
22522263
extern void findRegexTagsMainloop (int (* driver)(void))
22532264
{
22542265
/* merely read all lines of the file */

main/lregex_p.h

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ extern bool matchMultitableRegex (struct lregexControlBlock *lcb, const vString*
107107

108108
extern void notifyRegexInputStart (struct lregexControlBlock *lcb);
109109
extern void notifyRegexInputEnd (struct lregexControlBlock *lcb);
110+
extern void notifyRegexMakeTagEntry (struct lregexControlBlock *lcb, int corkIndex);
110111

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

main/options.c

+4
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," --_makeTagEntryReflection-<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>]"},
@@ -3363,6 +3365,8 @@ static void processLongOption (
33633365
;
33643366
else if (processSequelOption (option, parameter))
33653367
;
3368+
else if (processMakeTagEntryReflectionOption (option, parameter))
3369+
;
33663370
else if (processPretendOption (option, parameter))
33673371
;
33683372
else if (processRolesOption (option, parameter))

main/options_p.h

+1
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

+10
Original file line numberDiff line numberDiff line change
@@ -4110,6 +4110,11 @@ extern void notifyLanguageRegexInputEnd (langType language)
41104110
notifyRegexInputEnd(pobj->lregexControlBlock);
41114111
}
41124112

4113+
extern void notifyLanguageRegexMakeTagEntry (langType language, int corkIndex)
4114+
{
4115+
notifyRegexMakeTagEntry((LanguageTable + language)->lregexControlBlock, corkIndex);
4116+
}
4117+
41134118
static unsigned int parserCorkFlags (parserDefinition *parser)
41144119
{
41154120
subparser *tmp;
@@ -5418,6 +5423,11 @@ extern bool processSequelOption (const char *const option, const char *const par
54185423
return processHookOption (option, parameter, "_sequel-", SCRIPT_HOOK_SEQUEL);
54195424
}
54205425

5426+
extern bool processMakeTagEntryReflectionOption (const char *const option, const char *const parameter)
5427+
{
5428+
return processHookOption (option, parameter, "_makeTagEntryReflection-", SCRIPT_HOOK_MAKE_TAG_ENTRY_REFLECTION);
5429+
}
5430+
54215431
extern bool processPretendOption (const char *const option, const char *const parameter)
54225432
{
54235433
langType new_language, old_language;

main/parse.h

+1
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

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ extern bool doesLanguageHaveForeignDependency (const langType language, const la
152152
extern bool processLanguageRegexOption (langType language, enum regexParserType regptype, const char *const parameter);
153153
extern void notifyLanguageRegexInputStart (langType language);
154154
extern void notifyLanguageRegexInputEnd (langType language);
155+
extern void notifyLanguageRegexMakeTagEntry (langType language, int corkIndex);
155156

156157
extern bool hasLanguagePostRunRegexPatterns (const langType language);
157158
extern void matchLanguageRegex (const langType language, const vString* const line, bool postrun);

main/subparser_p.h

-2
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

+17
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 "x-cpreprocessor.h"
@@ -392,6 +393,14 @@ static void cppInitCommon(langType clientLang,
392393
: clientLang) & CORK_SYMTAB))
393394
? makeMacroTable ()
394395
: NULL;
396+
397+
if (Cpp.lang != Cpp.clientLang
398+
&& Cpp.clientLang != LANG_IGNORE)
399+
{
400+
pushLanguage (Cpp.lang);
401+
notifyInputStart ();
402+
popLanguage ();
403+
}
395404
}
396405

397406
extern void cppInit (const bool state, const bool hasAtLiteralStrings,
@@ -427,6 +436,14 @@ static void cppClearMacroInUse (cppMacroInfo **pM)
427436

428437
extern void cppTerminate (void)
429438
{
439+
if (Cpp.lang != Cpp.clientLang
440+
&& Cpp.clientLang != LANG_IGNORE)
441+
{
442+
pushLanguage (Cpp.lang);
443+
notifyInputEnd ();
444+
popLanguage ();
445+
}
446+
430447
if (Cpp.directive.name != NULL)
431448
{
432449
vStringDelete (Cpp.directive.name);

0 commit comments

Comments
 (0)