Mae向きなブログ

Mae向きな情報発信を続けていきたいと思います。

lhs2hs

ダウンロードしてきた2つのファイルは「リテレイト形式」で書かれていました。通常形式のほうがやりやすいので,Rubyを使って変換します。

lhs2hs.rb

#!/usr/bin/env ruby

comment_region = false
while line = gets
  if /\A>\s(.*)/ =~ line
    if comment_region
      line = "-}\n" + $1
      comment_region = false
    else
      line = $1
    end
  else
    unless comment_region
      line = "{-\n" + line 
      comment_region = true
    end
  end
  puts line
end
if comment_region
  puts "-}"
end

実行

$ ./lhs2hs.rb parser.lhs > parser.hs
$ ./lhs2hs.rb Parsing.lhs > Parsing.hs