|
|
@ -12,7 +12,8 @@ import Text.Megaparsec.Error (ParseErrorBundle, errorBundlePretty)
|
|
|
|
import qualified Data.Set as Set
|
|
|
|
import qualified Data.Set as Set
|
|
|
|
|
|
|
|
|
|
|
|
data PState = PState
|
|
|
|
data PState = PState
|
|
|
|
{ depth :: Int
|
|
|
|
{ parenDepth :: Int
|
|
|
|
|
|
|
|
, bracketDepth :: Int
|
|
|
|
} deriving (Show)
|
|
|
|
} deriving (Show)
|
|
|
|
|
|
|
|
|
|
|
|
type ParserM = StateT PState (Parsec Void [LToken])
|
|
|
|
type ParserM = StateT PState (Parsec Void [LToken])
|
|
|
@ -24,24 +25,33 @@ satisfyM f = do
|
|
|
|
return token
|
|
|
|
return token
|
|
|
|
|
|
|
|
|
|
|
|
updateDepth :: LToken -> PState -> PState
|
|
|
|
updateDepth :: LToken -> PState -> PState
|
|
|
|
updateDepth LOpenParen st = st { depth = depth st + 1 }
|
|
|
|
updateDepth LOpenParen st = st { parenDepth = parenDepth st + 1 }
|
|
|
|
updateDepth LCloseParen st = st { depth = max 0 (depth st - 1) }
|
|
|
|
updateDepth LOpenBracket st = st { bracketDepth = bracketDepth st + 1 }
|
|
|
|
|
|
|
|
updateDepth LCloseParen st = st { parenDepth = parenDepth st - 1 }
|
|
|
|
|
|
|
|
updateDepth LCloseBracket st = st { bracketDepth = bracketDepth st - 1 }
|
|
|
|
updateDepth _ st = st
|
|
|
|
updateDepth _ st = st
|
|
|
|
|
|
|
|
|
|
|
|
topLevelNewline :: ParserM ()
|
|
|
|
topLevelNewline :: ParserM ()
|
|
|
|
topLevelNewline = do
|
|
|
|
topLevelNewline = do
|
|
|
|
st <- get
|
|
|
|
st <- get
|
|
|
|
if depth st == 0
|
|
|
|
if parenDepth st == 0 && bracketDepth st == 0
|
|
|
|
then void (satisfyM (== LNewline))
|
|
|
|
then void (satisfyM (== LNewline))
|
|
|
|
else fail "Top-level exit in paren context"
|
|
|
|
else fail "Top-level exit in nested context (paren or bracket)"
|
|
|
|
|
|
|
|
|
|
|
|
parseProgram :: [LToken] -> Either (ParseErrorBundle [LToken] Void) [TricuAST]
|
|
|
|
parseProgram :: [LToken] -> Either (ParseErrorBundle [LToken] Void) [TricuAST]
|
|
|
|
parseProgram tokens =
|
|
|
|
parseProgram tokens =
|
|
|
|
runParser (evalStateT parseProgramM (PState 0)) "" tokens
|
|
|
|
runParser (evalStateT (parseProgramM <* finalizeDepth <* eof) (PState 0 0)) "" tokens
|
|
|
|
|
|
|
|
|
|
|
|
parseSingleExpr :: [LToken] -> Either (ParseErrorBundle [LToken] Void) TricuAST
|
|
|
|
parseSingleExpr :: [LToken] -> Either (ParseErrorBundle [LToken] Void) TricuAST
|
|
|
|
parseSingleExpr tokens =
|
|
|
|
parseSingleExpr tokens =
|
|
|
|
runParser (evalStateT (scnParserM *> parseExpressionM <* eofM) (PState 0)) "" tokens
|
|
|
|
runParser (evalStateT (scnParserM *> parseExpressionM <* finalizeDepth <* eof) (PState 0 0)) "" tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
finalizeDepth :: ParserM ()
|
|
|
|
|
|
|
|
finalizeDepth = do
|
|
|
|
|
|
|
|
st <- get
|
|
|
|
|
|
|
|
case (parenDepth st, bracketDepth st) of
|
|
|
|
|
|
|
|
(0, 0) -> pure ()
|
|
|
|
|
|
|
|
(p, b) -> fail $ "Unmatched tokens: " ++ show (p, b)
|
|
|
|
|
|
|
|
|
|
|
|
parseTricu :: String -> [TricuAST]
|
|
|
|
parseTricu :: String -> [TricuAST]
|
|
|
|
parseTricu input =
|
|
|
|
parseTricu input =
|
|
|
@ -49,7 +59,7 @@ parseTricu input =
|
|
|
|
[] -> []
|
|
|
|
[] -> []
|
|
|
|
toks ->
|
|
|
|
toks ->
|
|
|
|
case parseProgram toks of
|
|
|
|
case parseProgram toks of
|
|
|
|
Left err -> error (handleParseError err)
|
|
|
|
Left err -> errorWithoutStackTrace (handleParseError err)
|
|
|
|
Right asts -> asts
|
|
|
|
Right asts -> asts
|
|
|
|
|
|
|
|
|
|
|
|
parseSingle :: String -> TricuAST
|
|
|
|
parseSingle :: String -> TricuAST
|
|
|
@ -58,7 +68,7 @@ parseSingle input =
|
|
|
|
[] -> SEmpty
|
|
|
|
[] -> SEmpty
|
|
|
|
toks ->
|
|
|
|
toks ->
|
|
|
|
case parseSingleExpr toks of
|
|
|
|
case parseSingleExpr toks of
|
|
|
|
Left err -> error (handleParseError err)
|
|
|
|
Left err -> errorWithoutStackTrace (handleParseError err)
|
|
|
|
Right ast -> ast
|
|
|
|
Right ast -> ast
|
|
|
|
|
|
|
|
|
|
|
|
parseProgramM :: ParserM [TricuAST]
|
|
|
|
parseProgramM :: ParserM [TricuAST]
|
|
|
@ -75,10 +85,13 @@ scnParserM :: ParserM ()
|
|
|
|
scnParserM = skipMany $ do
|
|
|
|
scnParserM = skipMany $ do
|
|
|
|
t <- lookAhead anySingle
|
|
|
|
t <- lookAhead anySingle
|
|
|
|
st <- get
|
|
|
|
st <- get
|
|
|
|
if depth st > 0 && isNewline t
|
|
|
|
if | (parenDepth st > 0 || bracketDepth st > 0) && case t of
|
|
|
|
then void (satisfyM isNewline)
|
|
|
|
LNewline -> True
|
|
|
|
else
|
|
|
|
_ -> False -> void $ satisfyM $ \case
|
|
|
|
fail "In paren context or no space token" <|> empty
|
|
|
|
LNewline -> True
|
|
|
|
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
| otherwise -> fail "In nested context or no space token" <|> empty
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eofM :: ParserM ()
|
|
|
|
eofM :: ParserM ()
|
|
|
|
eofM = lift eof
|
|
|
|
eofM = lift eof
|
|
|
@ -96,18 +109,27 @@ parseExpressionM = choice
|
|
|
|
|
|
|
|
|
|
|
|
parseFunctionM :: ParserM TricuAST
|
|
|
|
parseFunctionM :: ParserM TricuAST
|
|
|
|
parseFunctionM = do
|
|
|
|
parseFunctionM = do
|
|
|
|
LIdentifier name <- satisfyM isIdentifier
|
|
|
|
LIdentifier name <- satisfyM $ \case
|
|
|
|
args <- many (satisfyM isIdentifier)
|
|
|
|
LIdentifier _ -> True
|
|
|
|
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
args <- many $ satisfyM $ \case
|
|
|
|
|
|
|
|
LIdentifier _ -> True
|
|
|
|
|
|
|
|
_ -> False
|
|
|
|
_ <- satisfyM (== LAssign)
|
|
|
|
_ <- satisfyM (== LAssign)
|
|
|
|
scnParserM
|
|
|
|
scnParserM
|
|
|
|
body <- parseExpressionM
|
|
|
|
body <- parseExpressionM
|
|
|
|
pure (SFunc name (map getIdentifier args) body)
|
|
|
|
pure (SFunc name (map getIdentifier args) body)
|
|
|
|
|
|
|
|
|
|
|
|
parseLambdaM :: ParserM TricuAST
|
|
|
|
parseLambdaM :: ParserM TricuAST
|
|
|
|
parseLambdaM = between (satisfyM (== LOpenParen)) (satisfyM (== LCloseParen)) $ do
|
|
|
|
parseLambdaM =
|
|
|
|
|
|
|
|
between (satisfyM (== LOpenParen)) (satisfyM (== LCloseParen)) $ do
|
|
|
|
_ <- satisfyM (== LBackslash)
|
|
|
|
_ <- satisfyM (== LBackslash)
|
|
|
|
param <- satisfyM isIdentifier
|
|
|
|
param <- satisfyM $ \case
|
|
|
|
rest <- many (satisfyM isIdentifier)
|
|
|
|
LIdentifier _ -> True
|
|
|
|
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
rest <- many $ satisfyM $ \case
|
|
|
|
|
|
|
|
LIdentifier _ -> True
|
|
|
|
|
|
|
|
_ -> False
|
|
|
|
_ <- satisfyM (== LColon)
|
|
|
|
_ <- satisfyM (== LColon)
|
|
|
|
scnParserM
|
|
|
|
scnParserM
|
|
|
|
body <- parseLambdaExpressionM
|
|
|
|
body <- parseLambdaExpressionM
|
|
|
@ -158,8 +180,10 @@ parseAtomicBaseM = choice
|
|
|
|
|
|
|
|
|
|
|
|
parseTreeLeafM :: ParserM TricuAST
|
|
|
|
parseTreeLeafM :: ParserM TricuAST
|
|
|
|
parseTreeLeafM = do
|
|
|
|
parseTreeLeafM = do
|
|
|
|
_ <- satisfyM isKeywordT
|
|
|
|
_ <- satisfyM $ \case
|
|
|
|
notFollowedBy (lift (satisfy (== LAssign)))
|
|
|
|
LKeywordT -> True
|
|
|
|
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
notFollowedBy $ lift $ satisfy (== LAssign)
|
|
|
|
pure TLeaf
|
|
|
|
pure TLeaf
|
|
|
|
|
|
|
|
|
|
|
|
parseTreeTermM :: ParserM TricuAST
|
|
|
|
parseTreeTermM :: ParserM TricuAST
|
|
|
@ -201,7 +225,10 @@ parseLiteralM = choice
|
|
|
|
parseListLiteralM :: ParserM TricuAST
|
|
|
|
parseListLiteralM :: ParserM TricuAST
|
|
|
|
parseListLiteralM = do
|
|
|
|
parseListLiteralM = do
|
|
|
|
_ <- satisfyM (== LOpenBracket)
|
|
|
|
_ <- satisfyM (== LOpenBracket)
|
|
|
|
elements <- many parseListItemM
|
|
|
|
elements <- many $ do
|
|
|
|
|
|
|
|
scnParserM
|
|
|
|
|
|
|
|
parseListItemM
|
|
|
|
|
|
|
|
scnParserM
|
|
|
|
_ <- satisfyM (== LCloseBracket)
|
|
|
|
_ <- satisfyM (== LCloseBracket)
|
|
|
|
pure (SList elements)
|
|
|
|
pure (SList elements)
|
|
|
|
|
|
|
|
|
|
|
@ -221,7 +248,10 @@ parseGroupedItemM = do
|
|
|
|
|
|
|
|
|
|
|
|
parseSingleItemM :: ParserM TricuAST
|
|
|
|
parseSingleItemM :: ParserM TricuAST
|
|
|
|
parseSingleItemM = do
|
|
|
|
parseSingleItemM = do
|
|
|
|
token <- satisfyM isListItem
|
|
|
|
token <- satisfyM $ \case
|
|
|
|
|
|
|
|
LIdentifier _ -> True
|
|
|
|
|
|
|
|
LKeywordT -> True
|
|
|
|
|
|
|
|
_ -> False
|
|
|
|
case token of
|
|
|
|
case token of
|
|
|
|
LIdentifier name -> pure (SVar name)
|
|
|
|
LIdentifier name -> pure (SVar name)
|
|
|
|
LKeywordT -> pure TLeaf
|
|
|
|
LKeywordT -> pure TLeaf
|
|
|
@ -229,71 +259,46 @@ parseSingleItemM = do
|
|
|
|
|
|
|
|
|
|
|
|
parseVarM :: ParserM TricuAST
|
|
|
|
parseVarM :: ParserM TricuAST
|
|
|
|
parseVarM = do
|
|
|
|
parseVarM = do
|
|
|
|
LIdentifier name <- satisfyM isIdentifier
|
|
|
|
LIdentifier name <- satisfyM $ \case
|
|
|
|
|
|
|
|
LIdentifier _ -> True
|
|
|
|
|
|
|
|
_ -> False
|
|
|
|
if name == "t" || name == "__result"
|
|
|
|
if name == "t" || name == "__result"
|
|
|
|
then fail ("Reserved keyword: " ++ name ++ " cannot be assigned.")
|
|
|
|
then fail ("Reserved keyword: " ++ name ++ " cannot be assigned.")
|
|
|
|
else pure (SVar name)
|
|
|
|
else pure (SVar name)
|
|
|
|
|
|
|
|
|
|
|
|
parseIntLiteralM :: ParserM TricuAST
|
|
|
|
parseIntLiteralM :: ParserM TricuAST
|
|
|
|
parseIntLiteralM = do
|
|
|
|
parseIntLiteralM = do
|
|
|
|
LIntegerLiteral value <- satisfyM isIntegerLiteral
|
|
|
|
LIntegerLiteral value <- satisfyM $ \case
|
|
|
|
|
|
|
|
LIntegerLiteral _ -> True
|
|
|
|
|
|
|
|
_ -> False
|
|
|
|
pure (SInt value)
|
|
|
|
pure (SInt value)
|
|
|
|
|
|
|
|
|
|
|
|
parseStrLiteralM :: ParserM TricuAST
|
|
|
|
parseStrLiteralM :: ParserM TricuAST
|
|
|
|
parseStrLiteralM = do
|
|
|
|
parseStrLiteralM = do
|
|
|
|
LStringLiteral value <- satisfyM isStringLiteral
|
|
|
|
LStringLiteral value <- satisfyM $ \case
|
|
|
|
|
|
|
|
LStringLiteral _ -> True
|
|
|
|
|
|
|
|
_ -> False
|
|
|
|
pure (SStr value)
|
|
|
|
pure (SStr value)
|
|
|
|
|
|
|
|
|
|
|
|
isKeywordT :: LToken -> Bool
|
|
|
|
|
|
|
|
isKeywordT LKeywordT = True
|
|
|
|
|
|
|
|
isKeywordT _ = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isIdentifier :: LToken -> Bool
|
|
|
|
|
|
|
|
isIdentifier (LIdentifier _) = True
|
|
|
|
|
|
|
|
isIdentifier _ = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isIntegerLiteral :: LToken -> Bool
|
|
|
|
|
|
|
|
isIntegerLiteral (LIntegerLiteral _) = True
|
|
|
|
|
|
|
|
isIntegerLiteral _ = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isStringLiteral :: LToken -> Bool
|
|
|
|
|
|
|
|
isStringLiteral (LStringLiteral _) = True
|
|
|
|
|
|
|
|
isStringLiteral _ = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isLiteral :: LToken -> Bool
|
|
|
|
|
|
|
|
isLiteral (LIntegerLiteral _) = True
|
|
|
|
|
|
|
|
isLiteral (LStringLiteral _) = True
|
|
|
|
|
|
|
|
isLiteral _ = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isListItem :: LToken -> Bool
|
|
|
|
|
|
|
|
isListItem (LIdentifier _) = True
|
|
|
|
|
|
|
|
isListItem LKeywordT = True
|
|
|
|
|
|
|
|
isListItem _ = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isNewline :: LToken -> Bool
|
|
|
|
|
|
|
|
isNewline LNewline = True
|
|
|
|
|
|
|
|
isNewline _ = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getIdentifier :: LToken -> String
|
|
|
|
getIdentifier :: LToken -> String
|
|
|
|
getIdentifier (LIdentifier name) = name
|
|
|
|
getIdentifier (LIdentifier name) = name
|
|
|
|
getIdentifier _ = error "Expected identifier"
|
|
|
|
getIdentifier _ = errorWithoutStackTrace "Expected identifier"
|
|
|
|
|
|
|
|
|
|
|
|
handleParseError :: ParseErrorBundle [LToken] Void -> String
|
|
|
|
handleParseError :: ParseErrorBundle [LToken] Void -> String
|
|
|
|
handleParseError bundle =
|
|
|
|
handleParseError bundle =
|
|
|
|
let errors = bundleErrors bundle
|
|
|
|
let errors = bundleErrors bundle
|
|
|
|
errorList = Data.List.NonEmpty.toList errors
|
|
|
|
formattedErrors = map formatError (Data.List.NonEmpty.toList errors)
|
|
|
|
formattedErrs = map showError errorList
|
|
|
|
in unlines ("Parse error(s) encountered:" : formattedErrors)
|
|
|
|
in unlines ("Parse error(s) encountered:" : formattedErrs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
showError :: ParseError [LToken] Void -> String
|
|
|
|
|
|
|
|
showError (TrivialError offset (Just (Tokens tokenStream)) expected) =
|
|
|
|
|
|
|
|
"Parse error at offset " ++ show offset
|
|
|
|
|
|
|
|
++ ": unexpected token " ++ show tokenStream
|
|
|
|
|
|
|
|
++ ", expected one of " ++ show (Set.toList expected)
|
|
|
|
|
|
|
|
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 " ++ show (Set.toList expected)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
formatError :: ParseError [LToken] Void -> String
|
|
|
|
|
|
|
|
formatError (TrivialError offset unexpected expected) =
|
|
|
|
|
|
|
|
let unexpectedMsg = case unexpected of
|
|
|
|
|
|
|
|
Just x -> "unexpected token " ++ show x
|
|
|
|
|
|
|
|
Nothing -> "unexpected end of input"
|
|
|
|
|
|
|
|
expectedMsg = if null expected
|
|
|
|
|
|
|
|
then ""
|
|
|
|
|
|
|
|
else "expected " ++ show (Set.toList expected)
|
|
|
|
|
|
|
|
in "Parse error at offset " ++ show offset ++ ": " ++ unexpectedMsg ++
|
|
|
|
|
|
|
|
if null expectedMsg then "" else " " ++ expectedMsg
|
|
|
|
|
|
|
|
formatError (FancyError offset _) =
|
|
|
|
|
|
|
|
"Parse error at offset " ++ show offset ++ ": unexpected FancyError"
|
|
|
|