Je suis nouveau à parsec (et parseurs en général), et je vais avoir des problèmes avec cet analyseur je l'ai écrit:Difficulté à obtenir un analyseur parsec pour sauter des espaces correctement
list = char '(' *> many (spaces *> some letter) <* spaces <* char ')'
L'idée est de analyser les listes dans ce format (je travaille à s-expressions):
(firstElement secondElement thirdElement and so on)
j'ai écrit ce code pour le tester:
import Control.Applicative
import Text.ParserCombinators.Parsec hiding (many)
list = char '(' *> many (spaces *> some letter) <* spaces <* char ')'
test s = do
putStrLn $ "Testing " ++ show s ++ ":"
parseTest list s
putStrLn ""
main = do
test "()"
test "(hello)"
test "(hello world)"
test "(hello world)"
test "(hello world)"
test "()"
T son est la sortie I get:
Testing "()":
[]
Testing "(hello)":
["hello"]
Testing "(hello world)":
["hello","world"]
Testing "(hello world)":
["hello","world"]
Testing "(hello world)":
parse error at (line 1, column 14):
unexpected ")"
expecting space or letter
Testing "()":
parse error at (line 1, column 3):
unexpected ")"
expecting space or letter
Comme vous pouvez le voir, il échoue quand il y a un espace blanc entre le dernier élément de la liste, et la fermeture )
. Je ne comprends pas pourquoi l'espace blanc n'est pas consommé par le spaces
que j'ai mis juste avant <* char ')'
. Quelle erreur idiote ai-je faite?