FParsec のチュートリアル的な何か

さて、今日は FParsec 日本語チュートリアルを読んでやってみるよ。

nuget はインストールしてるかな?
私は nuget3 をインストールしてみたよ:

% yaourt -S nuget3

勉強用のディレクトリを作成しよう:

% mkdir ./hello-fparsec
% cd ./hello-fparsec

FParsec をインストールしてみよう:

% nuget install FParsec -outputDirectory ./packages

-outputDirectory ./packages を付けない場合、カレントディレクトリに FParsec がインストールされてしまうので注意だ。
なので、Visual Studio の慣習に習って ./packages/ にインストールしてみた。

さっそく使ってみる

run 関数に何らかのパーサー関数と文字列を渡すと結果が返ってくる模様。

ʕ•͡ω•ʔ pfloat は FParsec 浮動小数点数をパースして浮動小数点数を返す組み込みのパーサー関数だぞ

// demo000.fsx
open FParsec


let test p str =
  match run p str with
    | Success(result      , _, _)   -> printfn "Success: %A" result
    | Failure(errorMessage, _, _)   -> printfn "Failure: %s" errorMessage

test pfloat "1.25"
test pfloat "1.25E"
test pfloat "1.25E 3"

それで、こんな風に打ってみると:

% fsharpi -I ./packages/FParsec.1.0.2/lib/net40-client/ -r "FParsec.dll" ./demo000.fsx
Success: 1.25
Failure: Error in Ln: 1 Col: 6
1.25E
     ^
Note: The error occurred at the end of the input stream.
Expecting: decimal digit

Failure: Error in Ln: 1 Col: 6
1.25E 3
     ^
Expecting: decimal digit

最初の test pfloat "1.25" だけパースされた。

ヽ(=´▽`=)ノ