Skip to content

Commit a424cf5

Browse files
committed
main: consider foreign languages when solving the field type specifying _field regex flag
The original code assumed the parser specified in a regex pattern was the owner of the field specified with the _field regex flag in the pattern. For an example, in the option --regex-Foo=/.../{_field=afield:\1}... , ctags assumed "afield" was a specific field of "Foo" parser. The original code didn't consider an option like --regex-Foo=/.../{_language=Bar}{_field=afield:\1}... In this case, ctags should consider "afield" as a specific field of "Bar" parser. NOTE: the language specified with _language flag must be declared with _foreignLanguage= in --langdef-Foo. Signed-off-by: Masatake YAMATO <yamato@redhat.com>
1 parent a7b07d7 commit a424cf5

File tree

11 files changed

+60
-6
lines changed

11 files changed

+60
-6
lines changed

Tmain/list-mline-regex-flags.d/stdout-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ p pcre2 use pcre2 regex engine
1010
- _advanceTo=N[start|end] a group in pattern from where the next scan starts [0end]
1111
- _anonymous=PREFIX make an anonymous tag with PREFIX
1212
- _extra=EXTRA record the tag only when the extra is enabled
13-
- _field=FIELD:VALUE record the matched string(VALUE) to parser own FIELD of the tag
13+
- _field=FIELD:VALUE record the matched string(VALUE) to the (foreign) language specific FIELD of the tag
1414
- _guest=PARSERSPEC,N0[start|end],N1[start|end] run guest parser on the area
1515
- _language=LANG make a foreign tag for LANG
1616
- _role=ROLE set the given ROLE to the roles field

Tmain/list-mtable-regex-flags.d/stdout-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ p pcre2 use pcre2 regex engine
1616
- _advanceTo=N[start|end] a group in pattern from where the next scan starts [0end]
1717
- _anonymous=PREFIX make an anonymous tag with PREFIX
1818
- _extra=EXTRA record the tag only when the extra is enabled
19-
- _field=FIELD:VALUE record the matched string(VALUE) to parser own FIELD of the tag
19+
- _field=FIELD:VALUE record the matched string(VALUE) to the (foreign) language specific FIELD of the tag
2020
- _guest=PARSERSPEC,N0[start|end],N1[start|end] run guest parser on the area
2121
- _language=LANG make a foreign tag for LANG
2222
- _role=ROLE set the given ROLE to the roles field

Tmain/list-regex-flags.d/stdout-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ x exclusive skip testing the other pat
1111
- warning="MESSAGE" print the given MESSAGE at WARNING level
1212
- _anonymous=PREFIX make an anonymous tag with PREFIX
1313
- _extra=EXTRA record the tag only when the extra is enabled
14-
- _field=FIELD:VALUE record the matched string(VALUE) to parser own FIELD of the tag
14+
- _field=FIELD:VALUE record the matched string(VALUE) to the (foreign) language specific FIELD of the tag
1515
- _guest=PARSERSPEC,N0[start|end],N1[start|end] run guest parser on the area
1616
- _language=LANG make a foreign tag for LANG
1717
- _role=ROLE set the given ROLE to the roles field
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public func foo(n, m);
2+
protected func bar(n);
3+
private func baz(n,...);
4+
X:tagme@iamowner
5+
Y:iamowner2=tagme2
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--langdef=knownz
2+
--kinddef-knownz=m,mark,makers
3+
4+
--_fielddef-knownz=owner,the owner of the markers
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright: 2024 Masatake YAMATO
2+
# License: GPL-2
3+
4+
. ../utils.sh
5+
6+
CTAGS=$1
7+
8+
V=
9+
# V=valgrind
10+
11+
${V} ${CTAGS} --options=NONE --options=./knownz.ctags --options=./unknownx.ctags \
12+
--fields=+l \
13+
--fields-unknownx=+'{protection}{signature}' \
14+
--fields-knownz=+'{owner}' \
15+
-o - input.unknownx
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ctags: Notice: No options will be read from files or environment
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bar input.unknownx /^protected func bar(n);$/;" f language:unknownx protection:protected signature:(n)
2+
baz input.unknownx /^private func baz(n,...);$/;" f language:unknownx protection:private signature:(n,...)
3+
foo input.unknownx /^public func foo(n, m);$/;" f language:unknownx protection:public signature:(n, m)
4+
tagme input.unknownx /^X:tagme@iamowner$/;" m language:knownz owner:iamowner
5+
tagme2 input.unknownx /^Y:iamowner2=tagme2$/;" m language:knownz owner:iamowner2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--langdef=unknownx{_foreignLanguage=knownz}
2+
--kinddef-unknownx=f,func,functions
3+
--map-unknownx=+.unknownx
4+
5+
--_fielddef-unknownx=protection,protections
6+
--_fielddef-unknownx=signature,signatures
7+
8+
--regex-unknownx=/^((public|protected|private) +)?func ([^\(]+)\((.*)\)/\3/f/{_field=protection:\1}{_field=signature:(\4)}
9+
--regex-unknownx=/^X:([a-z]+)@([a-z]+)/\1/m/{_language=knownz}{_field=owner:\2}
10+
--regex-unknownx=/^Y:([a-z0-9]+)=([a-z0-9]+)/\2/m/{_field=owner:\1}{_language=knownz}

docs/optlib.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ about ``--fields-<LANG>`` option.
463463

464464
`passwd` parser is a simple example that uses ``--fields-<LANG>`` option.
465465

466+
By default, ctags assumes the field is a part of the language specified
467+
with `<LANG>` in ``--regex-<LANG>``. Together with ``{_language=<LANG>}``
468+
flag, you can switch the language of the field. See ":ref:`foreigntag`".
469+
The combination of these flags is new in version 6.2.0.
466470

467471
.. _roles:
468472

@@ -1961,6 +1965,12 @@ the output for input.docc:
19611965
compress input.docc /^- function: compress(const char *plain_text, enum algorithm alg) => char *$/;" function language:C roles:documented
19621966
decompress input.docc /^- function: decompress(const char *compressed_byteseq) => char *$/;" function language:C roles:documented
19631967
1968+
1969+
.. TESTCASE: Tmain/parser-own-fields-for-foreign-lang.d
1970+
1971+
``{_language=<LANG>}`` flag affects ``{_field=FIELDNAME:GROUP}`` flag; ctags looks up
1972+
the field defintion in `<LANG>`.
1973+
19641974
.. END: NOT REVIEWED YET
19651975
19661976
.. _optlib2c:

main/lregex.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,14 @@ static void common_flag_field_long (const char* const s, const char* const v, vo
11341134
}
11351135

11361136
fname = eStrndup (v, tmp - v);
1137-
ftype = getFieldTypeForNameAndLanguage (fname, cdata->owner);
1137+
1138+
langType lang = (ptrn->foreign_lang == LANG_IGNORE)
1139+
? cdata->owner
1140+
: ptrn->foreign_lang;
1141+
ftype = getFieldTypeForNameAndLanguage (fname, lang);
11381142
if (ftype == FIELD_UNKNOWN)
11391143
{
1140-
error (WARNING, "no such field \"%s\" in %s", fname, getLanguageName(cdata->owner));
1144+
error (WARNING, "no such field \"%s\" in %s", fname, getLanguageName(lang));
11411145
eFree (fname);
11421146
return;
11431147
}
@@ -1269,7 +1273,7 @@ static flagDefinition commonSpecFlagDef[] = {
12691273
{ '\0', EXPERIMENTAL "extra", NULL, common_flag_extra_long ,
12701274
"EXTRA", "record the tag only when the extra is enabled"},
12711275
{ '\0', EXPERIMENTAL "field", NULL, common_flag_field_long ,
1272-
"FIELD:VALUE", "record the matched string(VALUE) to parser own FIELD of the tag"},
1276+
"FIELD:VALUE", "record the matched string(VALUE) to the (foreign) language specific FIELD of the tag"},
12731277
{ '\0', EXPERIMENTAL "role", NULL, common_flag_role_long,
12741278
"ROLE", "set the given ROLE to the roles field"},
12751279
{ '\0', EXPERIMENTAL "anonymous", NULL, common_flag_anonymous_long,

0 commit comments

Comments
 (0)