Minor simplifications in the parser

This commit is contained in:
John Wiegley 2018-04-12 21:19:50 -07:00
parent 3788cb4e75
commit 70cebb2aa4
2 changed files with 7 additions and 29 deletions

View file

@ -93,14 +93,7 @@ nixTerm = do
nixToplevelForm :: Parser NExprLoc
nixToplevelForm = keywords <|> nixLambda <|> nixExprLoc
where
keywords = do
word <- try $ lookAhead $ some letterChar <* satisfy reservedEnd
case word of
"let" -> nixLet
"if" -> nixIf
"assert" -> nixAssert
"with" -> nixWith
_ -> empty
keywords = nixLet <|> nixIf <|> nixAssert <|> nixWith
nixSym :: Parser NExprLoc
nixSym = annotateLocation1 $ mkSymF <$> identifier

View file

@ -47,31 +47,16 @@ reservedEnd x = isSpace x ||
x == '"' || x == '\'' || x == ','
reserved :: Text -> Parser ()
reserved n = lexeme $ try $ do
_ <- string n <* lookAhead (void (satisfy reservedEnd) <|> eof)
return ()
opStart :: Parser Char
opStart = satisfy $ \x ->
-- jww (2018-04-09): Could this be faster?
x `elem` (".+-*/=<>&|!?" :: String)
{-
opLetter :: CharParsing m => m Char
opLetter = oneOf ">+/&|="
-}
identStart :: Parser Char
identStart = letterChar <|> char '_'
identLetter :: Parser Char
identLetter = satisfy $ \x ->
isAlpha x || isDigit x || x == '_' || x == '\'' || x == '-'
reserved n = lexeme $ try $
string n *> lookAhead (void (satisfy reservedEnd) <|> eof)
identifier = lexeme $ try $ do
ident <- pack <$> ((:) <$> identStart <*> many identLetter)
ident <- cons <$> satisfy (\x -> isAlpha x || x == '_')
<*> takeWhileP Nothing identLetter
guard (not (ident `HashSet.member` reservedNames))
return ident
where
identLetter x = isAlpha x || isDigit x || x == '_' || x == '\'' || x == '-'
parens = between (symbol "(") (symbol ")")
braces = between (symbol "{") (symbol "}")