Skip to content

Commit

Permalink
reorganize
Browse files Browse the repository at this point in the history
  • Loading branch information
byorgey committed May 15, 2024
1 parent 1b3116e commit 6a74a96
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/swarm-lang/Swarm/Language/Parser/Lex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ reservedCS = reservedGen string
reserved :: Text -> Parser ()
reserved = reservedGen string'

-- | Parse an identifier together with its source location info. The
-- Bool indicates whether we are parsing a type variable (which are
-- not allowed to start with an uppercase letter).
locIdentifier :: Bool -> Parser LocVar
locIdentifier isTV = uncurry LV <$> parseLocG ((lexeme . try) (p >>= check) <?> "variable name")
where
p = (:) <$> (letterChar <|> char '_') <*> many (alphaNumChar <|> char '_' <|> char '\'')
check (into @Text -> t)
| t `elem` reservedWords || T.toLower t `elem` reservedWords =
failT ["Reserved word", squote t, "cannot be used as a variable name"]
| isTV && T.toTitle t `elem` reservedWords =
failT ["Reserved type name", squote t, "cannot be used as a type variable name; perhaps you meant", squote (T.toTitle t) <> "?"]
| isTV && isUpper (T.head t) =
failT ["Type variable names must start with a lowercase letter"]
| otherwise = return t

-- | Parse an identifier, i.e. any non-reserved string containing
-- alphanumeric characters and underscores, not starting with a
-- digit. The Bool indicates whether we are parsing a type variable.
Expand All @@ -202,22 +218,6 @@ tyVar = identifier True
tmVar :: Parser Var
tmVar = identifier False

-- | Parse an identifier together with its source location info. The
-- Bool indicates whether we are parsing a type variable (which are
-- not allowed to start with an uppercase letter).
locIdentifier :: Bool -> Parser LocVar
locIdentifier isTV = uncurry LV <$> parseLocG ((lexeme . try) (p >>= check) <?> "variable name")
where
p = (:) <$> (letterChar <|> char '_') <*> many (alphaNumChar <|> char '_' <|> char '\'')
check (into @Text -> t)
| t `elem` reservedWords || T.toLower t `elem` reservedWords =
failT ["Reserved word", squote t, "cannot be used as a variable name"]
| isTV && T.toTitle t `elem` reservedWords =
failT ["Reserved type name", squote t, "cannot be used as a type variable name; perhaps you meant", squote (T.toTitle t) <> "?"]
| isTV && isUpper (T.head t) =
failT ["Type variable names must start with a lowercase letter"]
| otherwise = return t

-- | Parse a text literal (including escape sequences) in double quotes.
textLiteral :: Parser Text
textLiteral = into <$> lexeme (char '"' >> manyTill L.charLiteral (char '"'))
Expand Down

0 comments on commit 6a74a96

Please sign in to comment.