2024-12-18 18:55:51 -06:00
|
|
|
module Parser where
|
|
|
|
|
2024-12-27 08:17:06 -06:00
|
|
|
import Debug.Trace
|
2024-12-18 18:55:51 -06:00
|
|
|
import Lexer
|
2024-12-20 11:38:09 -06:00
|
|
|
import Research hiding (toList)
|
|
|
|
import Control.Exception (throw)
|
|
|
|
import Data.List.NonEmpty (toList)
|
|
|
|
import qualified Data.Set as Set
|
|
|
|
import Data.Void
|
2024-12-18 18:55:51 -06:00
|
|
|
import Text.Megaparsec
|
|
|
|
import Text.Megaparsec.Char
|
2024-12-20 11:38:09 -06:00
|
|
|
import Text.Megaparsec.Error (errorBundlePretty, ParseErrorBundle)
|
2024-12-18 18:55:51 -06:00
|
|
|
|
|
|
|
type Parser = Parsec Void [LToken]
|
2024-12-27 12:27:00 -06:00
|
|
|
|
2024-12-18 18:55:51 -06:00
|
|
|
data SaplingAST
|
2024-12-27 12:27:00 -06:00
|
|
|
= SVar String
|
|
|
|
| SInt Int
|
|
|
|
| SStr String
|
|
|
|
| SList [SaplingAST]
|
|
|
|
| SFunc String [String] SaplingAST
|
|
|
|
| SApp SaplingAST SaplingAST
|
2024-12-18 18:55:51 -06:00
|
|
|
| TLeaf
|
2024-12-27 12:27:00 -06:00
|
|
|
| TStem SaplingAST
|
|
|
|
| TFork SaplingAST SaplingAST
|
|
|
|
| SLambda [String] SaplingAST
|
2024-12-18 18:55:51 -06:00
|
|
|
deriving (Show, Eq, Ord)
|
|
|
|
|
2024-12-20 11:38:09 -06:00
|
|
|
parseSapling :: String -> [SaplingAST]
|
|
|
|
parseSapling input =
|
|
|
|
let nonEmptyLines = filter (not . null) (lines input)
|
|
|
|
in map parseSingle nonEmptyLines
|
|
|
|
|
|
|
|
parseSingle :: String -> SaplingAST
|
|
|
|
parseSingle input = case runParser parseExpression "" (lexSapling input) of
|
2024-12-27 12:27:00 -06:00
|
|
|
Left err -> error $ handleParseError err
|
2024-12-19 18:57:57 -06:00
|
|
|
Right ast -> ast
|
2024-12-18 18:55:51 -06:00
|
|
|
|
|
|
|
scnParser :: Parser ()
|
|
|
|
scnParser = skipMany (satisfy isNewline)
|
|
|
|
|
|
|
|
parseExpression :: Parser SaplingAST
|
|
|
|
parseExpression = choice
|
|
|
|
[ try parseFunction
|
2024-12-27 08:17:06 -06:00
|
|
|
, try parseLambda
|
2024-12-27 12:27:00 -06:00
|
|
|
, try parseLambdaExpression
|
2024-12-27 08:17:06 -06:00
|
|
|
, try parseListLiteral
|
2024-12-18 18:55:51 -06:00
|
|
|
, try parseApplication
|
2024-12-27 08:17:06 -06:00
|
|
|
, try parseTreeTerm
|
2024-12-18 18:55:51 -06:00
|
|
|
, parseLiteral
|
|
|
|
]
|
|
|
|
|
|
|
|
parseFunction :: Parser SaplingAST
|
|
|
|
parseFunction = do
|
|
|
|
LIdentifier name <- satisfy isIdentifier
|
|
|
|
args <- many (satisfy isIdentifier)
|
|
|
|
satisfy (== LAssign)
|
|
|
|
body <- parseExpression
|
|
|
|
return (SFunc name (map getIdentifier args) body)
|
|
|
|
|
2024-12-27 12:27:00 -06:00
|
|
|
parseAtomicBase :: Parser SaplingAST
|
|
|
|
parseAtomicBase = choice
|
|
|
|
[ try parseVarWithoutAssignment
|
|
|
|
, parseTreeLeaf
|
|
|
|
, parseGrouped
|
|
|
|
]
|
|
|
|
parseVarWithoutAssignment :: Parser SaplingAST
|
|
|
|
parseVarWithoutAssignment = do
|
|
|
|
LIdentifier name <- satisfy isIdentifier
|
|
|
|
if (name == "t" || name == "__result")
|
|
|
|
then fail $ "Reserved keyword: " ++ name ++ " cannot be assigned."
|
|
|
|
else notFollowedBy (satisfy (== LAssign)) *> return (SVar name)
|
|
|
|
|
2024-12-27 08:17:06 -06:00
|
|
|
parseLambda :: Parser SaplingAST
|
|
|
|
parseLambda = between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) $ do
|
|
|
|
satisfy (== LBackslash)
|
|
|
|
param <- satisfy isIdentifier
|
|
|
|
rest <- many (satisfy isIdentifier)
|
|
|
|
satisfy (== LColon)
|
|
|
|
body <- parseLambdaExpression
|
|
|
|
let nestedLambda = foldr (\v acc -> SLambda [v] acc) body (map getIdentifier rest)
|
|
|
|
return (SLambda [getIdentifier param] nestedLambda)
|
|
|
|
|
|
|
|
parseLambdaExpression :: Parser SaplingAST
|
|
|
|
parseLambdaExpression = choice
|
|
|
|
[ try parseLambdaApplication
|
|
|
|
, parseAtomicLambda
|
|
|
|
]
|
|
|
|
|
|
|
|
parseAtomicLambda :: Parser SaplingAST
|
|
|
|
parseAtomicLambda = choice
|
|
|
|
[ parseVar
|
|
|
|
, parseTreeLeaf
|
|
|
|
, parseLiteral
|
|
|
|
, parseListLiteral
|
2024-12-27 12:27:00 -06:00
|
|
|
, try parseLambda
|
2024-12-27 08:17:06 -06:00
|
|
|
, between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) parseLambdaExpression
|
|
|
|
]
|
|
|
|
|
2024-12-18 18:55:51 -06:00
|
|
|
parseApplication :: Parser SaplingAST
|
|
|
|
parseApplication = do
|
2024-12-19 18:57:57 -06:00
|
|
|
func <- parseAtomicBase
|
2024-12-27 08:17:06 -06:00
|
|
|
args <- many parseAtomic
|
|
|
|
return $ foldl (\acc arg -> SApp acc arg) func args
|
|
|
|
|
|
|
|
parseLambdaApplication :: Parser SaplingAST
|
|
|
|
parseLambdaApplication = do
|
2024-12-27 12:27:00 -06:00
|
|
|
func <- parseAtomicLambda
|
|
|
|
args <- many parseAtomicLambda
|
|
|
|
return $ foldl (\acc arg -> SApp acc arg) func args
|
2024-12-19 18:57:57 -06:00
|
|
|
|
|
|
|
isTreeTerm :: SaplingAST -> Bool
|
2024-12-27 12:27:00 -06:00
|
|
|
isTreeTerm TLeaf = True
|
|
|
|
isTreeTerm (TStem _) = True
|
2024-12-19 18:57:57 -06:00
|
|
|
isTreeTerm (TFork _ _) = True
|
2024-12-27 12:27:00 -06:00
|
|
|
isTreeTerm _ = False
|
2024-12-19 18:57:57 -06:00
|
|
|
|
|
|
|
parseTreeLeaf :: Parser SaplingAST
|
2024-12-20 11:38:09 -06:00
|
|
|
parseTreeLeaf = satisfy isKeywordT *> notFollowedBy (satisfy (== LAssign)) *> pure TLeaf
|
2024-12-18 18:55:51 -06:00
|
|
|
|
|
|
|
getIdentifier :: LToken -> String
|
|
|
|
getIdentifier (LIdentifier name) = name
|
|
|
|
getIdentifier _ = error "Expected identifier"
|
|
|
|
|
|
|
|
parseTreeTerm :: Parser SaplingAST
|
|
|
|
parseTreeTerm = do
|
|
|
|
base <- parseTreeLeafOrParenthesized
|
|
|
|
rest <- many parseTreeLeafOrParenthesized
|
|
|
|
pure $ foldl combine base rest
|
|
|
|
where
|
|
|
|
combine acc next = case acc of
|
2024-12-27 12:27:00 -06:00
|
|
|
TLeaf -> TStem next
|
|
|
|
TStem t -> TFork t next
|
2024-12-18 18:55:51 -06:00
|
|
|
TFork _ _ -> TFork acc next
|
|
|
|
|
|
|
|
parseTreeLeafOrParenthesized :: Parser SaplingAST
|
|
|
|
parseTreeLeafOrParenthesized = choice
|
|
|
|
[ between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) parseTreeTerm
|
2024-12-20 11:38:09 -06:00
|
|
|
, parseTreeLeaf
|
2024-12-18 18:55:51 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
foldTree :: [SaplingAST] -> SaplingAST
|
|
|
|
foldTree [] = TLeaf
|
|
|
|
foldTree [x] = x
|
|
|
|
foldTree (x:y:rest) = TFork x (foldTree (y:rest))
|
|
|
|
|
|
|
|
parseAtomic :: Parser SaplingAST
|
|
|
|
parseAtomic = choice
|
|
|
|
[ parseVar
|
2024-12-27 08:17:06 -06:00
|
|
|
, parseTreeLeaf
|
2024-12-18 18:55:51 -06:00
|
|
|
, parseListLiteral
|
2024-12-27 08:17:06 -06:00
|
|
|
, parseGrouped
|
|
|
|
, parseLiteral
|
2024-12-18 18:55:51 -06:00
|
|
|
]
|
|
|
|
|
2024-12-27 08:17:06 -06:00
|
|
|
parseGrouped :: Parser SaplingAST
|
|
|
|
parseGrouped = between (satisfy (== LOpenParen)) (satisfy (== LCloseParen)) parseExpression
|
2024-12-20 11:38:09 -06:00
|
|
|
|
2024-12-18 18:55:51 -06:00
|
|
|
parseLiteral :: Parser SaplingAST
|
|
|
|
parseLiteral = choice
|
|
|
|
[ parseIntLiteral
|
|
|
|
, parseStrLiteral
|
|
|
|
]
|
|
|
|
|
2024-12-19 18:57:57 -06:00
|
|
|
parens :: Parser SaplingAST -> Parser SaplingAST
|
|
|
|
parens p = do
|
|
|
|
satisfy (== LOpenParen)
|
|
|
|
result <- p
|
|
|
|
satisfy (== LCloseParen)
|
|
|
|
return result
|
|
|
|
|
2024-12-18 18:55:51 -06:00
|
|
|
parseListLiteral :: Parser SaplingAST
|
|
|
|
parseListLiteral = do
|
2024-12-19 18:57:57 -06:00
|
|
|
satisfy (== LOpenBracket)
|
|
|
|
elements <- many parseListItem
|
2024-12-18 18:55:51 -06:00
|
|
|
satisfy (== LCloseBracket)
|
|
|
|
return (SList elements)
|
|
|
|
|
2024-12-19 18:57:57 -06:00
|
|
|
parseListItem :: Parser SaplingAST
|
2024-12-19 19:53:32 -06:00
|
|
|
parseListItem = choice
|
2024-12-20 11:38:09 -06:00
|
|
|
[ parseGroupedItem
|
|
|
|
, parseListLiteral
|
|
|
|
, parseSingleItem
|
2024-12-19 19:53:32 -06:00
|
|
|
]
|
2024-12-19 18:57:57 -06:00
|
|
|
|
|
|
|
parseGroupedItem :: Parser SaplingAST
|
|
|
|
parseGroupedItem = do
|
2024-12-20 11:38:09 -06:00
|
|
|
satisfy (== LOpenParen)
|
2024-12-19 18:57:57 -06:00
|
|
|
inner <- parseExpression
|
|
|
|
satisfy (== LCloseParen)
|
|
|
|
return inner
|
|
|
|
|
|
|
|
parseSingleItem :: Parser SaplingAST
|
|
|
|
parseSingleItem = do
|
2024-12-20 11:38:09 -06:00
|
|
|
token <- satisfy isListItem
|
2024-12-19 18:57:57 -06:00
|
|
|
case token of
|
|
|
|
LIdentifier name -> return (SVar name)
|
2024-12-27 12:27:00 -06:00
|
|
|
LKeywordT -> return TLeaf
|
|
|
|
_ -> fail "Unexpected token in list item"
|
2024-12-19 18:57:57 -06:00
|
|
|
|
|
|
|
isListItem :: LToken -> Bool
|
|
|
|
isListItem (LIdentifier _) = True
|
|
|
|
isListItem LKeywordT = True
|
|
|
|
isListItem _ = False
|
|
|
|
|
2024-12-18 18:55:51 -06:00
|
|
|
parseVar :: Parser SaplingAST
|
2024-12-20 11:38:09 -06:00
|
|
|
parseVar = do
|
2024-12-18 18:55:51 -06:00
|
|
|
LIdentifier name <- satisfy isIdentifier
|
2024-12-20 11:38:09 -06:00
|
|
|
if (name == "t" || name == "__result")
|
|
|
|
then fail $ "Reserved keyword: " ++ name ++ " cannot be assigned."
|
|
|
|
else return (SVar name)
|
2024-12-18 18:55:51 -06:00
|
|
|
|
|
|
|
parseIntLiteral :: Parser SaplingAST
|
|
|
|
parseIntLiteral = do
|
|
|
|
LIntegerLiteral value <- satisfy isIntegerLiteral
|
|
|
|
return (SInt value)
|
|
|
|
|
|
|
|
parseStrLiteral :: Parser SaplingAST
|
|
|
|
parseStrLiteral = do
|
|
|
|
LStringLiteral value <- satisfy isStringLiteral
|
|
|
|
return (SStr value)
|
|
|
|
|
2024-12-19 18:57:57 -06:00
|
|
|
-- Boolean Helpers
|
2024-12-27 12:27:00 -06:00
|
|
|
isKeywordT (LKeywordT) = True
|
|
|
|
isKeywordT _ = False
|
|
|
|
isIdentifier (LIdentifier _) = True
|
|
|
|
isIdentifier _ = False
|
2024-12-18 18:55:51 -06:00
|
|
|
isIntegerLiteral (LIntegerLiteral _) = True
|
2024-12-27 12:27:00 -06:00
|
|
|
isIntegerLiteral _ = False
|
|
|
|
isStringLiteral (LStringLiteral _) = True
|
|
|
|
isStringLiteral _ = False
|
|
|
|
isLiteral (LIntegerLiteral _) = True
|
|
|
|
isLiteral (LStringLiteral _) = True
|
|
|
|
isLiteral _ = False
|
|
|
|
isNewline (LNewline) = True
|
|
|
|
isNewline _ = False
|
2024-12-20 11:38:09 -06:00
|
|
|
|
|
|
|
-- Error Handling
|
|
|
|
handleParseError :: ParseErrorBundle [LToken] Void -> String
|
|
|
|
handleParseError bundle =
|
|
|
|
let errors = bundleErrors bundle
|
|
|
|
errorList = toList errors
|
|
|
|
formattedErrors = map showError errorList
|
|
|
|
in unlines ("Parse error(s) encountered:" : formattedErrors)
|
|
|
|
|
|
|
|
showError :: ParseError [LToken] Void -> String
|
|
|
|
showError (TrivialError offset (Just (Tokens tokenStream)) expected) =
|
|
|
|
"Parse error at offset " ++ show offset ++ ": unexpected token "
|
2024-12-27 12:27:00 -06:00
|
|
|
++ show tokenStream ++ ", expected one of " ++ show (Set.toList expected)
|
2024-12-20 11:38:09 -06:00
|
|
|
showError (FancyError offset fancy) =
|
|
|
|
"Parse error at offset " ++ show offset ++ ":\n " ++ unlines (map show (Set.toList fancy))
|
|
|
|
showError (TrivialError offset Nothing expected) =
|
|
|
|
"Parse error at offset " ++ show offset ++ ": expected one of "
|
2024-12-27 12:27:00 -06:00
|
|
|
++ show (Set.toList expected)
|
|
|
|
|