Skip to content

Commit

Permalink
D: parse user-defined attributes
Browse files Browse the repository at this point in the history
Quoted from the pull request (#3701):

  UDAs can precede a declaration or come after a function parameter list.
  https://dlang.org/spec/attribute.html#uda

@mastake edited the commit log.
  • Loading branch information
ntrel authored and masatake committed Apr 29, 2023
1 parent 5fb6767 commit 9f54e5d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 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
14 changes: 11 additions & 3 deletions parsers/c-based.c
Original file line number Diff line number Diff line change
Expand Up @@ -2123,6 +2123,10 @@ static bool skipPostArgumentStuff (
break;
}
}
else if (isInputLanguage (Lang_d) && c == '@')
{
parseAtMarkStyleAnnotation (st);
}
}
if (! end)
{
Expand Down Expand Up @@ -2226,7 +2230,11 @@ static void parseAtMarkStyleAnnotation (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 @@ -2347,7 +2355,7 @@ static int parseParens (statementInfo *const st, parenInfo *const info)
break;

default:
if (c == '@' && isInputLanguage (Lang_java))
if (c == '@' && (isInputLanguage (Lang_d) || isInputLanguage (Lang_java)))
{
parseAtMarkStyleAnnotation (st);
}
Expand Down Expand Up @@ -2638,7 +2646,7 @@ 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)))
{
parseAtMarkStyleAnnotation (st);
}
Expand Down

0 comments on commit 9f54e5d

Please sign in to comment.