2016-04-12 2 views
2

J'ai l'analyseur suivant pour l'analyse des lignes de type BASH contenant la redirection.Parsec: Comment faire pour réduire les erreurs d'analyseur à propos des espaces blancs

lineRedirect :: P.Parsec String() String 
lineRedirect = do 
    lcmd <- P.spaces *> (P.many1 command P.<?> "command") 
    P.char '>' 
    rcmd <- P.spaces *> (P.many1 command P.<?> "redirection target") 
    return $ (intercalate ";" lcmd) ++ " ++ " ++ (intercalate ";" rcmd) 
    where 
    command :: P.Parsec String() String 
    command = P.many1 P.alphaNum <* P.spaces 

Il semble fonctionner très bien, mais j'aimerais faire taire tout « espace attendre » ou « attendre des espaces » qui apparaissent.

Par exemple:

> P.parseTest lineRedirect " > target" 
parse error at (line 1, column 4): 
unexpected ">" 
expecting space or command 

Je voudrais avoir juste "attendre commande" ici.

> P.parseTest lineRedirect "invalid! > target" 
parse error at (line 1, column 8): 
unexpected "!" 
expecting letter or digit, white space or ">" 

Même ici, sans "l'espace blanc".

Répondre

2

Après avoir réalisé que spaces est déjà un analyseur composite avec son propre message d'erreur, tout était facile ...

Voici ce que je suis venu avec:

lineRedirect :: P.Parsec String() (String, String) 
lineRedirect = do 
    cmd <- spaces' *> command 
    P.char '>' 
    tgt <- spaces' *> target 
    return (cmd, tgt) 

    where 
    command :: P.Parsec String() String 
    command = (P.many1 (P.noneOf ">") P.<?> "command") <* spaces' 

    target :: P.Parsec String() String 
    target = (P.many1 P.anyChar P.<?> "redirection target") <* spaces' <* P.eof 

    spaces' = P.skipMany (P.space P.<?> "") 
0

Cela semble fonctionner

lineRedirect = do 
    lcmd <- P.try (P.spaces *> P.many1 command) P.<?> "command" 
    ... 

Je n'aime pas le try, cependant.

+0

Malheureusement, cela ne fonctionne pas . Même en faisant quelque chose de similaire à la fonction 'command', l'analyseur se plaint encore des espaces. –

+0

@ JanSynáček Édité et testé. Cela fonctionne, mais il pourrait y avoir un moyen plus efficace. – chi