digitalmars.D.learn - Using custom attribute for a general parser. Is possible in D ?
- bioinfornatics (20/20) May 23 2014 I bask originally this qustion in this thread :
- bioinfornatics (29/52) May 23 2014 Sorry for typos they are severals.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (33/38) May 26 2014 I was able to make that work if I used function instead of delegate:
- bioinfornatics (3/43) May 26 2014 thanks Ali nice works as usual :-)
I bask originally this qustion in this thread : http://forum.dlang.org/post/rsnswykpjfzenplivkhm forum.dlang.org but another thread is better as that is not exactly same topic. I would like to use custom annotate/attribute on members as: struct A { Parser( start = "( word ) => word[0] == '>'", end = "( word ) => word[0] == '\n'" ) string header; } but it seem we can't use curstom annotation in D and mxin in this case are not easy. This custom attribute is a metadata to explain how to retrieve this field from a file. This will aloow to create a global parser and give to this parrser data structure to get back. Parser could to b an input range were hront give currentfilled struct and popfront will look foward in buffer given by file.byChunk() using this metadata. When all field are filled give the instance to front …
May 23 2014
On Friday, 23 May 2014 at 15:17:52 UTC, bioinfornatics wrote:I bask originally this qustion in this thread : http://forum.dlang.org/post/rsnswykpjfzenplivkhm forum.dlang.org but another thread is better as that is not exactly same topic. I would like to use custom annotate/attribute on members as: struct A { Parser( start = "( word ) => word[0] == '>'", end = "( word ) => word[0] == '\n'" ) string header; } but it seem we can't use curstom annotation in D and mxin in this case are not easy. This custom attribute is a metadata to explain how to retrieve this field from a file. This will aloow to create a global parser and give to this parrser data structure to get back. Parser could to b an input range were hront give currentfilled struct and popfront will look foward in buffer given by file.byChunk() using this metadata. When all field are filled give the instance to front …Sorry for typos they are severals. So to continue te code below is more close to D but do not build: import std.stdio; struct attribute { } attribute struct section { bool delegate( string ) start; bool delegate( string ) end; this( in bool delegate( string ) start, in bool delegate( string ) end) { this.start = start, this.end = end; } } struct A { section( ( word ) => word[0] == ' ', ( word ) => word[0] == '\n') int a; } int main(string[] args) { A a; return 0; } testCustomAttributes.d(43): Error: delegate testCustomAttributes.A.__lambda2!(string).__lambda2 function literals cannot be class members
May 23 2014
On 05/23/2014 09:09 AM, bioinfornatics wrote:struct A { section( ( word ) => word[0] == ' ', ( word ) => word[0] == '\n') int a; }I was able to make that work if I used function instead of delegate: import std.stdio; struct attribute { } alias MatchFunc = bool function( string ); attribute struct section { MatchFunc start; MatchFunc end; this( in MatchFunc start, in MatchFunc end) { this.start = start, this.end = end; } } struct A { section((string word) => word[0] == ' ', (string word) => word[0] == '\n') int a; } import std.traits; void main() { auto aa = A(); auto t = __traits(getAttributes, A.a); foreach (sect; t) { auto start_result = sect.start(" test"); auto end_result = sect.end("\n"); writefln("Results: %s and %s", start_result, end_result); } } Ali
May 26 2014
On Monday, 26 May 2014 at 19:50:29 UTC, Ali Çehreli wrote:On 05/23/2014 09:09 AM, bioinfornatics wrote:thanks Ali nice works as usual :-) it seem delegate will be suppoted into dmdfe 2.066struct A { section( ( word ) => word[0] == ' ', ( word ) => word[0] == '\n') int a; }I was able to make that work if I used function instead of delegate: import std.stdio; struct attribute { } alias MatchFunc = bool function( string ); attribute struct section { MatchFunc start; MatchFunc end; this( in MatchFunc start, in MatchFunc end) { this.start = start, this.end = end; } } struct A { section((string word) => word[0] == ' ', (string word) => word[0] == '\n') int a; } import std.traits; void main() { auto aa = A(); auto t = __traits(getAttributes, A.a); foreach (sect; t) { auto start_result = sect.start(" test"); auto end_result = sect.end("\n"); writefln("Results: %s and %s", start_result, end_result); } } Ali
May 26 2014