Skip to content

Commit 2ece0af

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 6edf46f commit 2ece0af

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
@@ -2027,6 +2027,14 @@ extern void notifyRegexInputEnd (struct lregexControlBlock *lcb)
20272027
fillEndLineFieldOfUpperScopes (lcb, endline);
20282028
}
20292029

2030+
extern void notifyRegexMakeTagEntry (struct lregexControlBlock *lcb,
2031+
int corkIndex)
2032+
{
2033+
optscriptSetup (optvm, lcb->local_dict, corkIndex);
2034+
scriptEvalHook (optvm, lcb, SCRIPT_HOOK_MAKE_TAG_ENTRY_REFLECTION);
2035+
optscriptTeardown (optvm, lcb->local_dict);
2036+
}
2037+
20302038
extern void findRegexTagsMainloop (int (* driver)(void))
20312039
{
20322040
/* 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
@@ -69,6 +69,7 @@ extern bool matchMultitableRegex (struct lregexControlBlock *lcb, const vString*
6969

7070
extern void notifyRegexInputStart (struct lregexControlBlock *lcb);
7171
extern void notifyRegexInputEnd (struct lregexControlBlock *lcb);
72+
extern void notifyRegexMakeTagEntry (struct lregexControlBlock *lcb, int corkIndex);
7273

7374
extern void addRegexTable (struct lregexControlBlock *lcb, const char *name);
7475
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
@@ -377,6 +377,8 @@ static optionDescription LongOptionDescription [] = {
377377
{1,1," Define new extra for <LANG>. --extras-<LANG>=+{name} enables it."},
378378
{1,1," --_fielddef-<LANG>=<name>,<description>"},
379379
{1,1," Define new field for <LANG>."},
380+
{1,1," --_makeTagEnryReflection-<LANG>={{ optscript-code }}"},
381+
{1,1," Specify code run when <LANG> parser makes a tag."},
380382
{1,1," --_mtable-extend-<LANG>=disttable+srctable."},
381383
{1,1," Copy patterns of a regex table to another regex table."},
382384
{1,1," --_mtable-regex-<LANG>=<table>/<line_pattern>/<name_pattern>/[<flags>]"},
@@ -3350,6 +3352,8 @@ static void processLongOption (
33503352
;
33513353
else if (processSequelOption (option, parameter))
33523354
;
3355+
else if (processMakeTagEntryReflectionOption (option, parameter))
3356+
;
33533357
else if (processPretendOption (option, parameter))
33543358
;
33553359
else if (processRolesOption (option, parameter))

main/options_p.h

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

main/parse.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3797,6 +3797,11 @@ extern void notifyLanguageRegexInputEnd (langType language)
37973797
notifyRegexInputEnd((LanguageTable + language)->lregexControlBlock);
37983798
}
37993799

3800+
extern void notifyLanguageRegexMakeTagEntry (langType language, int corkIndex)
3801+
{
3802+
notifyRegexMakeTagEntry((LanguageTable + language)->lregexControlBlock, corkIndex);
3803+
}
3804+
38003805
static unsigned int parserCorkFlags (parserDefinition *parser)
38013806
{
38023807
subparser *tmp;
@@ -5108,6 +5113,11 @@ extern bool processSequelOption (const char *const option, const char *const par
51085113
return processHookOption (option, parameter, "_sequel-", SCRIPT_HOOK_SEQUEL);
51095114
}
51105115

5116+
extern bool processMakeTagEntryReflectionOption (const char *const option, const char *const parameter)
5117+
{
5118+
return processHookOption (option, parameter, "_makeTagEntryReflection-", SCRIPT_HOOK_MAKE_TAG_ENTRY_REFLECTION);
5119+
}
5120+
51115121
extern bool processPretendOption (const char *const option, const char *const parameter)
51125122
{
51135123
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
@@ -140,6 +140,7 @@ extern void freeEncodingResources (void);
140140
extern bool processLanguageRegexOption (langType language, enum regexParserType regptype, const char *const parameter);
141141
extern void notifyLanguageRegexInputStart (langType language);
142142
extern void notifyLanguageRegexInputEnd (langType language);
143+
extern void notifyLanguageRegexMakeTagEntry (langType language, int corkIndex);
143144

144145
extern void matchLanguageRegex (const langType language, const vString* const line);
145146
extern void freeRegexResources (void);

main/subparser_p.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ extern langType getSubparserLanguage (subparser *s);
2929

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

3634
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"
@@ -348,6 +349,14 @@ static void cppInitCommon(langType clientLang,
348349
: clientLang) & CORK_SYMTAB))
349350
? makeMacroTable ()
350351
: NULL;
352+
353+
if (Cpp.lang != Cpp.clientLang
354+
&& Cpp.clientLang != LANG_IGNORE)
355+
{
356+
pushLanguage (Cpp.lang);
357+
notifyInputStart ();
358+
popLanguage ();
359+
}
351360
}
352361

353362
extern void cppInit (const bool state, const bool hasAtLiteralStrings,
@@ -381,6 +390,14 @@ static void cppClearMacroInUse (cppMacroInfo **pM)
381390

382391
extern void cppTerminate (void)
383392
{
393+
if (Cpp.lang != Cpp.clientLang
394+
&& Cpp.clientLang != LANG_IGNORE)
395+
{
396+
pushLanguage (Cpp.lang);
397+
notifyInputEnd ();
398+
popLanguage ();
399+
}
400+
384401
if (Cpp.directive.name != NULL)
385402
{
386403
vStringDelete (Cpp.directive.name);

0 commit comments

Comments
 (0)