Parse more stuff

This commit is contained in:
Frederik Hanghøj Iversen 2019-10-16 22:12:30 +02:00
parent 81b058f033
commit ebf62b8d4f
2 changed files with 58 additions and 5 deletions

View file

@ -9,6 +9,10 @@ module Data.Language.Ruby.AST
, Name(..)
, Send(..)
, Namespace(..)
, RBlock(..)
, Casgn(..)
, RArray(..)
, Anything(..)
) where
import Data.Aeson (parseJSON, Value(..), withArray)
@ -49,9 +53,10 @@ data Statement
| StmtFunction Function
| StmtSend Send
| StmtRBlock RBlock
-- TODO: We should also handle modules here. Otherwise we cannot
-- cover the case where a function references a module.
| StmtConst Namespace
| StmtCasgn Casgn
| StmtArray RArray
-- TODO Get rid of this
| StmtAnything Anything
deriving stock instance Show Statement
@ -77,8 +82,45 @@ instance FromJSON Statement where
<|> (StmtSend <$> parseJSON v)
<|> (StmtRBlock <$> parseJSON v)
<|> (StmtConst <$> parseJSON v)
<|> (StmtCasgn <$> parseJSON v)
<|> (StmtArray <$> parseJSON v)
<|> (StmtAnything <$> parseJSON v)
data Casgn = Casgn
{ name :: Name
, statement :: Statement
}
deriving stock instance Show Casgn
deriving stock instance Ord Casgn
deriving stock instance Eq Casgn
deriving stock instance Generic Casgn
instance ToJSON Casgn where
toEncoding = Aeson.genericToEncoding aesonOptions
instance FromJSON Casgn where
parseJSON = withArray "Module" $ \case
[String "casgn", _, name, statement]
-> Casgn
<$> parseJSON name
<*> parseJSON statement
_ -> empty
data RArray = RArray
{ statements :: [Statement]
}
deriving stock instance Show RArray
deriving stock instance Ord RArray
deriving stock instance Eq RArray
deriving stock instance Generic RArray
instance ToJSON RArray where
toEncoding = Aeson.genericToEncoding aesonOptions
instance FromJSON RArray where
parseJSON = withArray "RArray" $ \as -> case Vector.toList as of
String "array":xs
-> RArray
<$> traverse parseJSON xs
_ -> empty
newtype Anything = Anything Value
deriving stock instance Show Anything

View file

@ -186,9 +186,20 @@ instance References Statement where
StmtFunction f -> entries f
StmtSend s -> entries s
StmtConst c -> entries c
-- TODO:
StmtRBlock{} -> pure ()
StmtAnything{} -> pure ()
StmtRBlock b -> entries b
StmtCasgn c -> entries c
StmtArray a -> entries a
StmtAnything a -> entries a
-- TODO
instance References Ruby.RBlock where
entries = const $ pure ()
instance References Ruby.Casgn where
entries = const $ pure ()
instance References Ruby.RArray where
entries = const $ pure ()
instance References Ruby.Anything where
entries = const $ pure ()
instance References Ruby.Namespace where
entries = \case