Improving std.regex(p)

BCS none at anon.com
Sat Jun 19 20:55:22 PDT 2010


Hello Nick,

> "BCS" <none at anon.com> wrote in message
> news:a6268ff154ca8ccddf1ef51e1d8 at news.digitalmars.com...
> 
>> Hello Ellery,
>> 
>>> Generally I think D's CT capabilities have a way to go yet before
>>> this would be worth tackling. E.g. how do you build a parse tree if
>>> pointers and classes aren't allowed in CT code?
>>> 
>> You use the type system. I can say from experience that (memory
>> issues aside) it works.
>> 
> Trivial example?
> 

Building an arbitrary tree:

int find(char[] data)
{
  foreach(int j, char c; data)
    switch(c)
    {
    default: break;
    case '[', ']': return j;
    }
    return data.length;
}
int match(char[] data)
{
  int i = 0;
  foreach(int j, char c; data)
    switch(c)
    {
    default: break;
    case '[': i++; break;
    case ']': if(--i == 0) return j;
    }
    return data.length;
}
template Tp(T...){ alias T Tp; }
struct Bar(T...) { }
template Foo(char[] data)
{
  static if(data.length == 0)
    alias Tp!() Foo;
  else static if(data[0] == '[')
    alias Tp!(Bar!(Foo!(data[1..match(data)])), Foo!(data[match(data)+1..$])) 
Foo;
  else
    alias Tp!(data[0..find(data)], Foo!(data[find(data)..$])) Foo;
}

import std.stdio;

void main()
{
   writef("%s\n", Foo!("So [she was [considering ][in her] own[ mind [(as 
well] as] she could, ]for the").stringof);
}


Output:
tuple("So ",(Bar!("she was ",Bar!("considering ") ,Bar!("in her") ," own",Bar!(" 
mind ",Bar!("(as well") ," as") ," she could, ") ),"for the")

By placing things under different aliases and whatnot, named members can 
be done.

Also, if the lisp like constructs puts you off, the construction of the tree 
can be done via more or less the same parsing code and some fairly trivial 
ctfe+mixin

-- 
... <IXOYE><





More information about the Digitalmars-d mailing list