trifecta: don't allow reserved in identifier

trifecta should not parse reserved words as identifiers. Before
this commit, `in` would parse as an identifier name and thus
`let a = b; in c` fails to parse.
This commit is contained in:
Benno Fünfstück 2014-08-03 16:16:52 +02:00
parent 2b186aed67
commit 19c77ae2d1
2 changed files with 17 additions and 2 deletions

View file

@ -73,15 +73,28 @@ import Data.List (nub)
import Data.Text hiding (map)
import Text.Parser.Expression as X
import Text.Parser.LookAhead as X
import Text.Parser.Token.Highlight
import Text.Trifecta as X hiding (whiteSpace, symbol, symbolic, parseString)
import Text.Trifecta (parseString)
import Text.Trifecta.Delta
import qualified Data.HashSet as HashSet
identStyle :: IdentifierStyle Parser
identStyle = IdentifierStyle
{ _styleName = "nix"
, _styleStart = letter
, _styleLetter = alphaNum <|> oneOf "_."
, _styleReserved = HashSet.fromList reservedNames
, _styleHighlight = Identifier
, _styleReservedHighlight = ReservedIdentifier
}
identifier :: Parser Text
identifier = pack <$> ((:) <$> letter <*> many (alphaNum <|> oneOf "_."))
identifier = ident identStyle
reserved :: String -> Parser Text
reserved = fmap pack . symbol
reserved n = pack n <$ reserve identStyle n
reservedOp :: String -> Parser Text
reservedOp = reserved

View file

@ -55,6 +55,7 @@ Library
Build-depends:
parsers
, trifecta
, unordered-containers
ghc-options: -Wall
executable hnix
@ -90,6 +91,7 @@ executable hnix
Build-depends:
parsers
, trifecta
, unordered-containers
ghc-options: -Wall
test-suite hnix-tests