Skip to content

Commit

Permalink
dsl: allow to specify a default value in $ and & operators
Browse files Browse the repository at this point in the history
$ and & return #f if a specified field is not found in a tag.
With this change, you can make the operators return a value
other than #f in the situation.

You can write

	($ field default-value)

as a shorthand of

	(or ($ field) default-value)
.

Signed-off-by: Masatake YAMATO <[email protected]>
  • Loading branch information
masatake committed Apr 18, 2024
1 parent 8ce1677 commit 19bc9ad
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 6 deletions.
1 change: 1 addition & 0 deletions Tmain/readtags-default-field-val.d/exit-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
15 changes: 15 additions & 0 deletions Tmain/readtags-default-field-val.d/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* ctags -o output.tags --fields-C=+'{properties}' input.c */
static int a(void)
{
return 0;
}

inline int b(void)
{
return 0;
}

int c(void)
{
return 0;
}
40 changes: 40 additions & 0 deletions Tmain/readtags-default-field-val.d/output.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
!_TAG_EXTRA_DESCRIPTION anonymous /Include tags for non-named objects like lambda/
!_TAG_EXTRA_DESCRIPTION fileScope /Include tags of file scope/
!_TAG_EXTRA_DESCRIPTION pseudo /Include pseudo tags/
!_TAG_EXTRA_DESCRIPTION subparser /Include tags generated by subparsers/
!_TAG_FIELD_DESCRIPTION epoch /the last modified time of the input file (only for F\/file kind tag)/
!_TAG_FIELD_DESCRIPTION file /File-restricted scoping/
!_TAG_FIELD_DESCRIPTION input /input file/
!_TAG_FIELD_DESCRIPTION name /tag name/
!_TAG_FIELD_DESCRIPTION pattern /pattern/
!_TAG_FIELD_DESCRIPTION typeref /Type and name of a variable or typedef/
!_TAG_FIELD_DESCRIPTION!C properties /properties (static, inline, mutable,...)/
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_KIND_DESCRIPTION!C d,macro /macro definitions/
!_TAG_KIND_DESCRIPTION!C e,enumerator /enumerators (values inside an enumeration)/
!_TAG_KIND_DESCRIPTION!C f,function /function definitions/
!_TAG_KIND_DESCRIPTION!C g,enum /enumeration names/
!_TAG_KIND_DESCRIPTION!C h,header /included header files/
!_TAG_KIND_DESCRIPTION!C m,member /struct, and union members/
!_TAG_KIND_DESCRIPTION!C s,struct /structure names/
!_TAG_KIND_DESCRIPTION!C t,typedef /typedefs/
!_TAG_KIND_DESCRIPTION!C u,union /union names/
!_TAG_KIND_DESCRIPTION!C v,variable /variable definitions/
!_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/
!_TAG_OUTPUT_FILESEP slash /slash or backslash/
!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
!_TAG_OUTPUT_VERSION 0.0 /current.age/
!_TAG_PARSER_VERSION!C 0.0 /current.age/
!_TAG_PATTERN_LENGTH_LIMIT 96 /0 for no limit/
!_TAG_PROC_CWD /home/yamato/var/ctags-github/Tmain/readtags-default-field-val.d/ //
!_TAG_PROGRAM_AUTHOR Universal Ctags Team //
!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
!_TAG_PROGRAM_URL https://ctags.io/ /official site/
!_TAG_PROGRAM_VERSION 6.0.0 //
!_TAG_ROLE_DESCRIPTION!C!header local /local header/
!_TAG_ROLE_DESCRIPTION!C!header system /system header/
!_TAG_ROLE_DESCRIPTION!C!macro undef /undefined/
a input.c /^static int a(void)$/;" f typeref:typename:int file: properties:static
b input.c /^inline int b(void)$/;" f typeref:typename:int properties:inline
c input.c /^int c(void)$/;" f typeref:typename:int
25 changes: 25 additions & 0 deletions Tmain/readtags-default-field-val.d/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

# Copyright: 2024 Masatake YAMATO
# License: GPL-2

READTAGS=$3

. ../utils.sh

#V="valgrind --leak-check=full -v"
V=

if ! [ -x "${READTAGS}" ]; then
skip "no readtags"
fi

if ! ( "${READTAGS}" -h | grep -q -e -Q ); then
skip "no qualifier function in readtags"
fi

# ?a
# 97
# (format "%c" 96)
# => `
"${READTAGS}" -t output.tags -S '(<> ($ "properties" "`") (& "properties" "`"))' -F '(list $name " " ($ "properties" "noprop") #t)' -l
Empty file.
3 changes: 3 additions & 0 deletions Tmain/readtags-default-field-val.d/stdout-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
c noprop
b inline
a static
17 changes: 14 additions & 3 deletions dsl/dsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ static DSLProcBind pbinds [] = {
.helpstr = "-> #f" },
{ "nil", value_nil, NULL, 0, 0UL,
.helpstr = "-> ()" },
{ "$", builtin_entry_ref, NULL, DSL_PATTR_CHECK_ARITY, 1,
.helpstr = "($ <string:field>) -> #f|<string>" },
{ "$", builtin_entry_ref, NULL, DSL_PATTR_CHECK_ARITY_OPT, 1,
.helpstr = "($ <string:field>) -> <string>|#f\n"
"($ <string:field> <any:default>) -> <string>|<any:default>"},
{ "$name", value_name, NULL, DSL_PATTR_MEMORABLE, 0UL,
.helpstr = "-> <string>"},
{ "$input", value_input, NULL, DSL_PATTR_MEMORABLE, 0UL,
Expand Down Expand Up @@ -1037,7 +1038,17 @@ static EsObject* builtin_entry_ref (EsObject *args, DSLEnv *env)
else if (! es_string_p (key))
dsl_throw (WRONG_TYPE_ARGUMENT, es_symbol_intern ("$"));
else
return dsl_entry_xget_string (env->entry, es_string_get (key));
{
EsObject *r = dsl_entry_xget_string (env->entry, es_string_get (key));
if (es_object_equal (r, es_false))
{
EsObject *defaultv = es_car(es_cdr(args));
if (es_null (defaultv))
return r;
return defaultv;
}
return r;
}
}

EsObject* dsl_entry_name (const tagEntry *entry)
Expand Down
17 changes: 14 additions & 3 deletions dsl/sorter.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ static DSLProcBind pbinds [] = {
{ "<or>", sorter_sform_cmp_or, NULL, DSL_PATTR_CHECK_ARITY_OPT, 1,
.helpstr = "(<or> <any> ...) -> -1|0|1; evaluate arguments left to right till one of thme returns -1 or 1." },

{ "&", sorter_alt_entry_ref, NULL, DSL_PATTR_CHECK_ARITY, 1,
.helpstr = "(& <string:field>) -> #f|<string>" },
{ "&", sorter_alt_entry_ref, NULL, DSL_PATTR_CHECK_ARITY_OPT, 1,
.helpstr = "(& <string:field>) -> <string>|#f\n"
"(& <string:field> <any:default>) -> <string>|<any:default>"},
{ "&name", alt_value_name, NULL, DSL_PATTR_MEMORABLE, 0UL,
.helpstr = "-> <string>"},
{ "&input", alt_value_input, NULL, DSL_PATTR_MEMORABLE, 0UL,
Expand Down Expand Up @@ -211,7 +212,17 @@ static EsObject* sorter_alt_entry_ref (EsObject *args, DSLEnv *env)
dsl_throw (WRONG_TYPE_ARGUMENT,
es_symbol_intern ("&"));
else
return dsl_entry_xget_string (env->alt_entry, es_string_get (key));
{
EsObject *r = dsl_entry_xget_string (env->alt_entry, es_string_get (key));
if (es_object_equal (r, es_false))
{
EsObject *defaultv = es_car(es_cdr(args));
if (es_null (defaultv))
return r;
return defaultv;
}
return r;
}
}

static EsObject* sorter_proc_cmp (EsObject* args, DSLEnv *env)
Expand Down

0 comments on commit 19bc9ad

Please sign in to comment.