Parsec は <?> 演算子でユーザ定義のエラーメッセージを設定できる。次のようにすれば word パーサにユーザ定義のエラーメッセージを設定できる。
word :: Parser String word = many1 letter <?> "word" 簡単な例を Hugs のコンソールからテストしてみた。 Hugs> :l Text.ParserCombinators.Parsec Text.ParserCombinators.Parsec> parseTest word "123" where word = many1 letter <?> "word" parse error at (line 1, column 1): unexpected "1" expecting word expecting の後のエラーメッセージがデフォールトの代わりに word が追加されている。 エラーメッセージはパーサ関数のどのレベルにも設定できる。sentence.hs パーサの word パーサに設定した例が、Parsec, a fast combinator parser に解説してある。 import Text.ParserCombinators.Parsec sentence :: Parser [String] sentence = do{ words <- sepBy1 word separator ; oneOf ".?!" ; return words } separator :: Parser () separator = skipMany1 (space <|> char ',') word :: Parser String word = many1 (letter <?> "") <?> "word" 実行例: Text.ParserCombinators.Parsec> :e sentence.hs Text.ParserCombinators.Parsec> :l sentence.hs Main> parseTest sentence "hi,123" parse error at (line 1, column 4): unexpected "1" expecting space, "," or word
by tnomura9
| 2009-09-30 16:37
| Haskell
|
Comments(0)
|
カテゴリ
新型コロナウイルス 主インデックス Haskell 記事リスト 圏論記事リスト 考えるということのリスト 考えるということ ラッセルのパラドックス Haskell Prelude Ocaml ボーカロイド 圏論 jQuery デモ HTML Python ツールボックス XAMPP Ruby ubuntu WordPress 脳の話 話のネタ リンク 幸福論 キリスト教 心の話 メモ 電子カルテ Dojo JavaScript C# NetWalker ed と sed HTML Raspberry Pi C 言語 命題論理 以前の記事
最新のトラックバック
最新のコメント
ファン
記事ランキング
ブログジャンル
画像一覧
|
ファン申請 |
||