我最後寫parseLexeme,在this tutorial
data Lexeme a = Lexeme a String -- String contains the spaces after the lexeme
whites :: Parser String
whites = many spaceChar
parseLexeme :: Parser a -> Parser (Lexeme a)
parseLexeme p = do
value <- p
w <- whites
return $ Lexeme value w
instance PPrint a => PPrint (Lexeme a) where
pprint (Lexeme value w) = (pprint value) ++ w
更換爲語義識別解析器變爲:
data Identifier = Identifier (Lexeme String)
parseIdentifier :: Parser Identifier
parseIdentifier = do
v <- parseLexeme $ (:) <$> letterChar <*> many (alphaNumChar <|> char '_')
return $ Identifier v
instance PPrint Identifier where
pprint (Identifier l) = pprint l
我覺得這已經問過,但不知道一個標準的解決方案(我曾經以一種有點麻煩的方式自己做了這件事)。 – leftaroundabout