Anyone interested in a Spirit for D?

BCS BCS at pathlink.com
Thu Oct 19 09:09:25 PDT 2006


Sorry about that garbald post, lets try that again:

Walter Bright wrote:

 > Bill Baxter wrote:
 >
 >> But given Don's experiments with compile-time text parsing in D, 
it's conceivable that in D the above parser could just be created with:
 >>
 >>    r = make_parser("real_number (',' real_number)*");
 >>
 >> I.e. use the EBNF version directly in a string literal that gets 
parsed at compile time.
 >> That would be pretty cool.
 >
 >
 >
 > Yes, it would be. But there's a catastrophic problem with it. Spirit 
enables code snippets to be attached to terminals by overloading the [] 
operator. If the EBNF was all in a string literal, this would be impossible.



How about delegate literals for code snipits??


template parse(char[] rulename, char[] rule, T, T delegate(T[]) act)
{
     // mixin allows this to be used in the scope
     // parse!(rulename)(out T);

     // for rule ==
     // "AssignExpression | AssignExpression ',' Expression",
     // parse!(rulename) expand to something like this

         // mixin a specialization
     bool parse!(rulename)(out T ret)
     {
         T[2] set
         if(T* ret = rule!("AssignExpression")(set[0]))
         {
             ret = act[0](set);
             return true;
         }
         if(rule!("AssignExpression")(set[0]) &&
             rule!("Expression")(set[1]))
         {
             ret = act[1](set);
             return true;
         }
         false;
     }

}

///used something like this

class Parser : RootParser
{
     mixin parse("Expression",
             "
             AssignExpression |
             AssignExpression ',' Expression
             ",
         Expr,
         [
         (Expr[] e){return e[0];},
         (Expr[] e){return new Expr(e[0], e[1]);}
         ]
         );

     mixin parse("AssignExpression",
             "
             ConditionalExpression |
             ConditionalExpression '=' AssignExpression |
             ConditionalExpression '+=' AssignExpression
             "
         Expr,
         [
         (Expr[] e){return e[0];},
         (Expr[] e){return new AssignExper(e[0], e[1]);},
         (Expr[] e){return new AssignExper(e[0], e[1]);}
         ]
         );

}

//// I've never used mixins so I most likely have something wrong in there



More information about the Digitalmars-d mailing list