diff --git a/bang.cabal b/bang.cabal index 54d1794..7509191 100644 --- a/bang.cabal +++ b/bang.cabal @@ -39,6 +39,7 @@ executable bang Bang.Syntax.Lexer, Bang.Syntax.Location, Bang.Syntax.Name, + Bang.Syntax.Parser, Bang.Syntax.Token, Paths_bang diff --git a/src/Bang/Syntax/Parser.y b/src/Bang/Syntax/Parser.y new file mode 100644 index 0000000..05e0e00 --- /dev/null +++ b/src/Bang/Syntax/Parser.y @@ -0,0 +1,76 @@ +{ +{-# OPTION_GHC -w #-} +{-# LANGUAGE DeriveFunctor #-} +{-# LANGAUGE OverloadedStrings #-} +module Bang.Syntax.Parser( + parseModule + , parseExpression + , lexWithLayout + ) + where + +import Bang.Syntax.Lexer +import Bang.Syntax.Location +import Bang.Syntax.Token +import MonadLib + +} + +%tokentype { Located Token } + +%token + '::' { Located $$ (OpIdent "::") } + +%monad { Parser } +%error { parseError } + +%name top_module + +%% + +top_module :: { () } + : '::' { () } + +{ + +newtype Parser a = Parser { + unParser :: StateT ParserState (ExceptionT Error Id) a + } + deriving (Functor, Applicative, Monad) + +data ParserState = ParserState { + psPrecTable :: Map Text Word + , psPosition :: Location + } + +instance StateM Parser ParserState where + get = Parser get + set = Parser . set + +instance ExceptionM Parser Error where + raise = Parser . raise + +instance RunExceptionM Parser Error where + try m = Parser (try (unParser m)) + +lexWithLayout :: Source -> Position -> L.Text -> [Located Token] +lexWithLayout = undefined + +parseModule :: Source -> L.Text -> Parser Module +parseModule = undefined + +parseExpression :: Source -> L.Text -> Parser Module +parseModule = undefined + +parseError :: [Located Token] -> Parser a +parseError toks = + case toks of + [] -> + raise (Error + addError Nothing ErrParser "Unexpected end of file." + (Located p (ErrorTok _) : _) -> + addError (Just p) ErrLexer "Lexer error" + (Located p _ : _) -> + addError (Just p) ErrParser "Parser error" + +} diff --git a/src/Main.hs b/src/Main.hs index ac5ac1d..969880d 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,6 +1,7 @@ import Bang.CommandLine import Bang.Syntax.Lexer import Bang.Syntax.Location +import Bang.Syntax.Parser import Control.Exception(tryJust) import Control.Monad(guard) import qualified Data.Text.Lazy.IO as T @@ -24,3 +25,5 @@ runLexer _cmd opts = Right txt -> do let tokens = lexer (File path) (Just initialPosition) txt mapM_ (putStrLn . show) tokens + +