2017-03-07 3 views
1

j'ai écrit un petit analyseur avec mégaparsec:Comment faire correspondre le résultat megaparsec?

module App (main) where 

import Control.Monad (void) 
import Text.Megaparsec 
import Text.Megaparsec.String 
import qualified Text.Megaparsec.Lexer as L 

sc :: Parser() 
sc = L.space (void spaceChar) lineCmnt blockCmnt 
    where lineCmnt = L.skipLineComment "//" 
     blockCmnt = L.skipBlockComment "/*" "*/" 

symbol :: String -> Parser String 
symbol = L.symbol sc 

semiParser :: Parser String 
semiParser = symbol ";" 

main :: IO() 
main = do 
    input <- getLine 
    case parse semiParser input of 
    Left val -> putStrLn $ "Failed! " 
    Right val -> putStrLn $ "Passed! " 
    print "Done" 

sc et le symbole et semiParser sont des tutoriels. Maintenant, je voudrais travailler avec mon résultat mais je reçois une erreur de type:

App.hs:23:5: error: 
    • Couldn't match expected type ‘String 
            -> Either (ParseError (Token String) Dec) String’ 
        with actual type ‘Either t0 t1’ 
    • In the pattern: Left val 
     In a case alternative: Left val -> putStrLn $ "Failed! " 
     In a stmt of a 'do' block: 
     case parse semiParser input of { 
      Left val -> putStrLn $ "Failed! " 
      Right val -> putStrLn $ "Passed! " } 

App.hs:24:5: error: 
    • Couldn't match expected type ‘String 
            -> Either (ParseError (Token String) Dec) String’ 
        with actual type ‘Either t2 t3’ 
    • In the pattern: Right val 
     In a case alternative: Right val -> putStrLn $ "Passed! " 
     In a stmt of a 'do' block: 
     case parse semiParser input of { 
      Left val -> putStrLn $ "Failed! " 
      Right val -> putStrLn $ "Passed! " } 

Alors, ma question est: comment puis-je match contre String -> Either (ParseError (Token String) Dec) String et le message d'erreur ou le résultat, si analyse syntaxique a échoué?

Je suppose que la forme générale de cette question est la suivante: Comment fonctionne le filtrage? Et je suis confus en général comment je peux lier le résultat d'une monade (comme Either dans une autre monade, comme IO (je suppose que je dois correspondre à la forme et ensuite soulever la valeur dans le contexte de la monade).

Répondre

0
parse :: Stream s Identity t => Parsec s() a -> SourceName -> s -> Either ParseError a 
               -- ^^^^^^^^^^ -- 

Vous avez oublié cet argument Essayez

case parse semiParser "input name here" input of 
+0

'' maintenant :: Head -.> Table'' –