Haskell Parsec - error messages are less helpful while using custom tokens

后端 未结 1 1994
野的像风
野的像风 2021-02-01 11:05

I\'m working on seperating lexing and parsing stages of a parser. After some tests, I realized error messages are less helpful when I\'m using some tokens other than Parsec\'s C

相关标签:
1条回答
  • 2021-02-01 11:32

    A beginning of solution can be to define your choice function in the Parser, use a specific unexpected function to override unexpected error and finally use the <?> operator to override the expecting message:

    mychoice [] = mzero
    mychoice (x:[]) = (tok x <|> myUnexpected) <?> show x 
    mychoice (x:xs) = ((tok x <|> mychoice xs) <|> myUnexpected)  <?> show (x:xs)
    
    myUnexpected =  do 
                 input <- getInput 
                 unexpected $ (id $ first input )
               where 
                first [] = "eof"
                first (x:xs) = show $ fst x
    

    and call your parser like that :

    ghci> Parser.parseTest (mychoice [Ide "ok", Ide "nop"]) "asdf  "
    parse error at (line 1, column 1):
    unexpected Ide "asdf"
    expecting [Ide "ok",Ide "nop"]
    
    0 讨论(0)
提交回复
热议问题