Using custom attribute for a general parser. Is possible in D ?

bioinfornatics via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 23 09:09:42 PDT 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


More information about the Digitalmars-d-learn mailing list