Gauche:SRE

Gauche:SRE

leque: Gauche:Regexpの中間表現 の仕組みをつかって GaucheSRE を使ってみようとする試み。

http://www.katch.ne.jp/~leque/software/repos/gauche-sre/

API

マクロ: (rx sre ...) -> regexp

,<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->regexp sre) -> regexp

SRE をパースし、正規表現に変換する。

gosh> (sre->regexp '(? whitespace))
#<regexp 0x79bbe0>

手続き: (sre-parse sre) -> AST

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)

コメント

Tags: 正規表現, SRE


Last modified : 2013/04/21 04:35:57 UTC