Refactoring into Syntax Parser, also float parsing
This commit is contained in:
parent
212eecfdf7
commit
52e04d28cf
18 changed files with 287 additions and 176 deletions
|
@ -1,71 +0,0 @@
|
|||
module Ubc.Parse.Struct
|
||||
( Struct(..)
|
||||
, parseStruct
|
||||
)
|
||||
where
|
||||
|
||||
import Data.Functor ( (<&>) )
|
||||
import Control.Arrow ( (>>>) )
|
||||
|
||||
import Text.Parsec
|
||||
( char,
|
||||
choice,
|
||||
getState,
|
||||
lookAhead,
|
||||
many,
|
||||
modifyState,
|
||||
try,
|
||||
ParsecT )
|
||||
|
||||
import Ubc.Parse.ParserState (ParserState)
|
||||
import Ubc.Parse.Scope.StructScope (StructScope(..))
|
||||
import Ubc.Parse.Scope (Scope(..))
|
||||
import Ubc.Parse.Data.Struct (Struct(..))
|
||||
|
||||
import qualified Ubc.Parse.Language as UbcLanguage
|
||||
import qualified Ubc.Parse.Scope.StructScope as StructScope
|
||||
import qualified Ubc.Parse.ParserState as ParserState
|
||||
import qualified Ubc.Parse.Scope as Scope
|
||||
import qualified Ubc.Parse.VariableType as VariableType
|
||||
|
||||
|
||||
parseStruct :: Monad m => ParsecT String ParserState m Struct
|
||||
parseStruct = do
|
||||
_ <- UbcLanguage.reserved "struct"
|
||||
|
||||
structIdentifier <- UbcLanguage.identifier
|
||||
let structScope = StructScope
|
||||
{ structName = structIdentifier
|
||||
, variables = []
|
||||
}
|
||||
|
||||
modifyState (ParserState.pushScope . ScopeStruct $ structScope)
|
||||
|
||||
_ <- UbcLanguage.braces (many structMember)
|
||||
|
||||
structScope' <- getState <&> Scope.expectScopeStruct . snd . ParserState.popScope
|
||||
|
||||
return $ Struct (StructScope.structName structScope') (StructScope.variables structScope')
|
||||
|
||||
structMember :: Monad m => ParsecT String ParserState m ()
|
||||
structMember = choice [ structVariableOrFunction ]
|
||||
|
||||
structVariableOrFunction :: Monad m => ParsecT String ParserState m ()
|
||||
structVariableOrFunction = do
|
||||
(typeName, identifier) <- try $ do
|
||||
typeName <- UbcLanguage.typeName
|
||||
objectIdentifier <- UbcLanguage.identifier
|
||||
return (typeName, objectIdentifier)
|
||||
choice
|
||||
[ lookAhead (char ';') *> parseVariable typeName identifier
|
||||
] -- TODO: Functions on structs
|
||||
|
||||
parseVariable :: Monad m => String -> String -> ParsecT String ParserState m ()
|
||||
parseVariable variableType variableName = do
|
||||
_ <- UbcLanguage.semicolon
|
||||
|
||||
modifyState (ParserState.modifyScope (Scope.expectScopeStruct
|
||||
>>> StructScope.modifyVariables ((variableName, VariableType.fromString variableType):)
|
||||
>>> Scope.ScopeStruct
|
||||
)
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue