elna/lib/Language/Elna/Parser.hs

171 lines
4.8 KiB
Haskell
Raw Normal View History

2024-07-23 22:44:42 +02:00
module Language.Elna.Parser
( Parser
, programP
) where
import Control.Monad (void)
2024-07-24 01:22:20 +02:00
import Control.Monad.Combinators.Expr (Operator(..), makeExprParser)
2024-07-23 22:44:42 +02:00
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Void (Void)
import Language.Elna.AST
( Declaration(..)
2024-07-24 01:22:20 +02:00
, Expression(..)
2024-07-23 22:44:42 +02:00
, Identifier(..)
2024-07-24 01:22:20 +02:00
, Literal(..)
2024-07-23 22:44:42 +02:00
, Parameter(..)
, Program(..)
2024-07-24 01:22:20 +02:00
, Statement(..)
2024-07-23 22:44:42 +02:00
, TypeExpression(..)
, VariableDeclaration(..)
)
2024-07-24 01:22:20 +02:00
import Text.Megaparsec (Parsec, (<?>), optional, between, sepBy, choice)
import Text.Megaparsec.Char (alphaNumChar, char, letterChar, space1, asciiChar)
2024-07-23 22:44:42 +02:00
import qualified Text.Megaparsec.Char.Lexer as Lexer
import Control.Applicative (Alternative(..))
import Data.Maybe (isJust)
2024-07-24 01:22:20 +02:00
import Data.Functor (($>))
2024-07-23 22:44:42 +02:00
type Parser = Parsec Void Text
space :: Parser ()
space = Lexer.space space1 (Lexer.skipLineComment "//")
$ Lexer.skipBlockComment "/*" "*/"
lexeme :: forall a. Parser a -> Parser a
lexeme = Lexer.lexeme space
symbol :: Text -> Parser Text
symbol = Lexer.symbol space
blockP :: forall a. Parser a -> Parser a
blockP = between (symbol "{") (symbol "}")
procedureP :: Parser ()
procedureP = void $ symbol "proc"
parensP :: forall a. Parser a -> Parser a
parensP = between (symbol "(") (symbol ")")
2024-07-24 01:22:20 +02:00
bracketsP :: forall a. Parser a -> Parser a
bracketsP = between (symbol "[") (symbol "]")
2024-07-23 22:44:42 +02:00
colonP :: Parser ()
colonP = void $ symbol ":"
semicolonP :: Parser ()
semicolonP = void $ symbol ";"
identifierP :: Parser Identifier
identifierP =
let wordParser = (:) <$> letterChar <*> many alphaNumChar <?> "identifier"
in fmap Identifier $ lexeme $ Text.pack <$> wordParser
typeExpressionP :: Parser TypeExpression
typeExpressionP = arrayTypeExpression
<|> NamedType <$> identifierP
<?> "type expression"
where
arrayTypeExpression = flip ArrayType
2024-07-24 01:22:20 +02:00
<$> (symbol "array" *> bracketsP (lexeme Lexer.decimal))
2024-07-23 22:44:42 +02:00
<*> (symbol "of" *> typeExpressionP)
typeDefinitionP :: Parser Declaration
typeDefinitionP = TypeDefinition
<$> (symbol "type" *> identifierP)
<*> (symbol "=" *> typeExpressionP)
<?> "type definition"
variableDeclarationP :: Parser VariableDeclaration
variableDeclarationP = VariableDeclaration
<$> (symbol "var" *> identifierP)
<*> (colonP *> typeExpressionP)
<* semicolonP
<?> "variable declaration"
parameterP :: Parser Parameter
parameterP = paramCons
<$> optional (symbol "ref")
<*> identifierP
<*> (colonP *> typeExpressionP)
where
paramCons ref name typeName = Parameter name typeName (isJust ref)
parametersP :: Parser [Parameter]
parametersP = parensP $ sepBy parameterP (symbol ",")
2024-07-24 01:22:20 +02:00
literalP :: Parser Literal
literalP
= HexadecimalLiteral <$> Lexer.hexadecimal
<|> IntegerLiteral <$> Lexer.decimal
<|> CharacterLiteral <$> charP
<|> BooleanLiteral <$> (symbol "true" $> True)
<|> BooleanLiteral <$> (symbol "false" $> False)
where
-- TODO: Escape characters.
charP = fromIntegral . fromEnum
<$> between (char '\'') (char '\'') asciiChar
termP :: Parser Expression
termP = choice
[ parensP expressionP
, VariableExpression <$> identifierP
, LiteralExpression <$> literalP
]
operatorTable :: [[Operator Parser Expression]]
operatorTable =
[ [Postfix (ArrayExpression <$> bracketsP expressionP)]
, unaryOperator
, factoryOperator
, termOperator
, comparisonOperator
]
where
unaryOperator =
[ prefix "-" NegationExpression
, prefix "+" id
]
factoryOperator =
[ binary "*" ProductExpression
, binary "/" DivisionExpression
]
termOperator =
[ binary "+" SumExpression
, binary "-" SubtractionExpression
]
comparisonOperator =
[ binary "<" LessExpression
, binary "<=" LessOrEqualExpression
, binary ">" GreaterExpression
, binary ">=" GreaterOrEqualExpression
, binary "=" EqualExpression
, binary "#" NonEqualExpression
]
prefix name f = Prefix (f <$ symbol name)
binary name f = InfixL (f <$ symbol name)
expressionP :: Parser Expression
expressionP = makeExprParser termP operatorTable
statementP :: Parser Statement
statementP
= EmptyStatement <$ semicolonP
<|> AssignmentStatement <$> expressionP <* symbol ":=" <*> expressionP
<|> CompoundStatement <$> blockP (many statementP)
<?> "statement" -- TODO: further statements
2024-07-23 22:44:42 +02:00
procedureDefinitionP :: Parser Declaration
procedureDefinitionP = ProcedureDefinition
<$> (procedureP *> identifierP)
<*> parametersP
<*> blockP (many variableDeclarationP)
<*> pure mempty -- TODO
<?> "procedure definition"
declarationP :: Parser Declaration
declarationP = typeDefinitionP <|> procedureDefinitionP
programP :: Parser Program
programP = Program <$> many declarationP