Skip to content

Commit

Permalink
Merge pull request #3701 from ntrel/d-attr
Browse files Browse the repository at this point in the history
D: parse user-defined attributes
  • Loading branch information
masatake committed Apr 30, 2023
2 parents fbd8778 + 9f54e5d commit 7e5c8dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Units/parser-d.r/simple.d.d/expected.tags
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ TemplateAlias input.d /^ alias TemplateAlias = a!T;$/;" a file:
UT input.d /^union UT(T){}$/;" u file:
Union input.d /^ union Union$/;" u struct:Struct file:
_bar input.d /^ private AliasInt _bar;$/;" m class:Class file:
attr_anon input.d /^@(obj) T attr_anon;$/;" v
attr_decl input.d /^@attr(i) int attr_decl = 1;$/;" v
bar input.d /^ bar,$/;" e enum:Enum file:
bar input.d /^ public AliasInt bar()$/;" f class:Class
conditional input.d /^ T conditional;$/;" v file:
Expand Down
5 changes: 5 additions & 0 deletions Units/parser-d.r/simple.d.d/input.d
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ int i;
int error;
+/

@attr(i) int attr_decl = 1;
@attr(i) attr_decl_infer = 1; // FIXME
@(obj) T attr_anon;
void attr_post() @attr(obj); // FIXME

static if (is(typeof(__traits(getMember, a, name)) == function))
T conditional;

Expand Down
21 changes: 15 additions & 6 deletions parsers/c-based.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ static const keywordDesc KeywordTable [] = {
* FUNCTION PROTOTYPES
*/
static void createTags (const unsigned int nestLevel, statementInfo *const parent);
static void parseAtMarkStyleAnnotation (statementInfo *const st);

/*
* FUNCTION DEFINITIONS
Expand Down Expand Up @@ -2122,6 +2123,10 @@ static bool skipPostArgumentStuff (
break;
}
}
else if (isInputLanguage (Lang_d) && c == '@')
{
parseAtMarkStyleAnnotation (st);
}
}
if (! end)
{
Expand Down Expand Up @@ -2213,7 +2218,7 @@ static void processAngleBracket (void)
}
}

static void parseJavaAnnotation (statementInfo *const st)
static void parseAtMarkStyleAnnotation (statementInfo *const st)
{
/*
* @Override
Expand All @@ -2225,7 +2230,11 @@ static void parseJavaAnnotation (statementInfo *const st)
tokenInfo *const token = activeToken (st);

int c = skipToNonWhite ();
readIdentifier (token, c);
if (cppIsident1 (c))
readIdentifier (token, c);
else
cppUngetc (c); // D allows: @ ( ArgumentList )

if (token->keyword == KEYWORD_INTERFACE)
{
/* Oops. This was actually "@interface" defining a new annotation. */
Expand Down Expand Up @@ -2346,9 +2355,9 @@ static int parseParens (statementInfo *const st, parenInfo *const info)
break;

default:
if (c == '@' && isInputLanguage (Lang_java))
if (c == '@' && (isInputLanguage (Lang_d) || isInputLanguage (Lang_java)))
{
parseJavaAnnotation(st);
parseAtMarkStyleAnnotation (st);
}
else if (cppIsident1 (c))
{
Expand Down Expand Up @@ -2637,9 +2646,9 @@ static void parseGeneralToken (statementInfo *const st, const int c)
if (c2 != '=')
cppUngetc (c2);
}
else if (c == '@' && isInputLanguage (Lang_java))
else if (c == '@' && (isInputLanguage (Lang_d) || isInputLanguage (Lang_java)))
{
parseJavaAnnotation (st);
parseAtMarkStyleAnnotation (st);
}
else if (c == STRING_SYMBOL) {
setToken(st, TOKEN_NONE);
Expand Down

0 comments on commit 7e5c8dc

Please sign in to comment.