パーサジェネレータもどき
とりあえず的に出来た物を張っておきます。
構文解析部分は全く出来てないどころか恐らく作らないというか、多分作れないというか…。
いらないところ削ってこれじゃ、日記に貼るには無闇に長すぎる気もするけれどまあ折角なので貼っておきます。あんまり面白くないです。
import std.stdio; class Parser(T){ static Sequence!(T, U) opShl(U)(U rhs){ return new Sequence!(T,U); } static char[] str(){ return typeid(typeof(T)).toString(); } } class Sequence(T, U) : Parser!(Sequence!(T, U)){ alias T lhstype; alias U rhstype; static char[] str(){ return T.str ~ U.str; } } class CharParser(char c) : Parser!(CharParser!(c)){ static char[] str(){ return [c]; } } template ch_p(char c){ CharParser!(c) ch_p; } class StringParser(char[] s) : Parser!(StringParser!(s)){ static char[] str(){ return s; } } template str_p(char[] s){ StringParser!(s) str_p; } class RepeatParser(P, int n1 : 0, T : more) : Parser!(RepeatParser!(P, n1, T)){ alias P parser; static int min = 0; static char[] str(){ return "(" ~ parser.str ~ ")" ~ "*"; } } class RepeatParser(P, int n1 : 1, T : more) : Parser!(RepeatParser!(P, n1, T)){ alias P parser; static int min = 1; static char[] str(){ return "(" ~ parser.str ~ ")" ~ "+"; } } class more{} class Postfix{ static RepeatParser!(P, 0, more) opMul_r(P)(P rhs){ return new RepeatParser!(P, 0, more); } static RepeatParser!(P, 1, more) opAdd_r(P)(P rhs){ return new RepeatParser!(P, 1, more); } } const Postfix _; void main(){ auto p = ( ch_p!('h') << ch_p!('o')*_ << str_p!("ge") )+_; writefln(p.str()); }