leque: Gauche:Regexpの中間表現 の仕組みをつかって Gauche で SRE を使ってみようとする試み。
http://www.katch.ne.jp/~leque/software/repos/gauche-sre/
,<expr> と ,@<expr> を展開したうえで SRE をパースし、正規表現に変換する。
gosh> (rx (+ whitespace)) #<regexp 0x1024c08> gosh> (regexp-decompile (rx "a" "b" "c")) (0 #f #\a #\b #\c) gosh> (define p (rx (submatch "b" (submatch "a")))) gosh> p #<regexp 0x10249d8> gosh> (rx (submatch "c" ,p)) #<regexp 0x1024730> gosh> (regexp-decompile (rx (submatch "c" ,p))) (0 #f (1 #f #\c #\b #\a)) gosh> (regexp-decompile (rx (submatch "c" ,@p))) (0 #f (1 #f #\c (2 #f #\b (3 #f #\a))))
SRE をパースし、正規表現に変換する。
gosh> (sre->regexp '(? whitespace)) #<regexp 0x79bbe0>
SRE をパースし、Gauche の正規表現の抽象構文木(Gauche:Regexpの中間表現参照)に変換する。
gosh> (sre-parse '(* whitespace)) (0 #f (rep 0 #f #[\x09-\x0d ]))
Gauche の正規表現に合わせて、一部 SRE の構文を拡張しています。
Gauche | 構文 |
(?:re...)*? | (*? re ...) |
(?:re...)+? | (+? re ...) |
(?:re...)?? | (?? re ...) |
(?:re...){n,}? | (>=? n re ...) |
(?:re...){n,m}? | (**? n m re ...) |
\B | nowb |
(?=re...) | (?= re ...) |
(?!re...) | (?! re ...) |
(?:re...)*+ | (*+ re ...) |
(?:re...)++ | (++ re ...) |
(?:re...)?+ | (?+ re ...) |
(?<=re...) | (?<= re ...) |
(?<!re...) | (?<! re ...) |
(?<name>re...) | (named-match name re ...) |
\n | (backref n) |
\k<name> | (backref name) |
a regexp whose body exp is a Scheme expression producing a string, character, char-set, or regexp as its value.とあるので、Gaucheネイティブの#<regexp>の埋め込みもアリにしたいところですが、 そうなると regexp->中間表現 が必要ですね。