1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 06:41:45 +03:00

Derive Foldable instances for syntaxes.

This commit is contained in:
Rob Rix 2017-04-06 14:19:15 -04:00
parent b0700242ee
commit 5b1d68ae80
6 changed files with 38 additions and 32 deletions

View File

@ -8,13 +8,13 @@ import Prologue
-- Undifferentiated
newtype Leaf a = Leaf { leafContent :: ByteString }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Leaf where liftEq = genericLiftEq
instance Show1 Leaf where liftShowsPrec = genericLiftShowsPrec
newtype Branch a = Branch { branchElements :: [a] }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Branch where liftEq = genericLiftEq
instance Show1 Branch where liftShowsPrec = genericLiftShowsPrec
@ -24,7 +24,7 @@ instance Show1 Branch where liftShowsPrec = genericLiftShowsPrec
-- | An identifier of some other construct, whether a containing declaration (e.g. a class name) or a reference (e.g. a variable).
newtype Identifier a = Identifier ByteString
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Identifier where liftEq = genericLiftEq
instance Show1 Identifier where liftShowsPrec = genericLiftShowsPrec

View File

@ -7,7 +7,7 @@ import Prologue
-- | An unnested comment (line or block).
newtype Comment a = Comment { commentContent :: ByteString }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Comment where liftEq = genericLiftEq
instance Show1 Comment where liftShowsPrec = genericLiftShowsPrec

View File

@ -6,7 +6,7 @@ import GHC.Generics
import Prologue
data Function a = Function { functionName :: !a, functionParameters :: ![a], functionBody :: !a }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Function where liftEq = genericLiftEq
instance Show1 Function where liftShowsPrec = genericLiftShowsPrec
@ -14,7 +14,7 @@ instance Show1 Function where liftShowsPrec = genericLiftShowsPrec
-- TODO: How should we represent function types, where applicable?
data Method a = Method { methodName :: !a, methodParameters :: ![a], methodBody :: !a }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Method where liftEq = genericLiftEq
instance Show1 Method where liftShowsPrec = genericLiftShowsPrec
@ -24,7 +24,7 @@ instance Show1 Method where liftShowsPrec = genericLiftShowsPrec
data Class a = Class { classIdentifier :: !a, classSuperclasses :: ![a], classScope :: !a }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Class where liftEq = genericLiftEq
instance Show1 Class where liftShowsPrec = genericLiftShowsPrec
@ -34,14 +34,14 @@ instance Show1 Class where liftShowsPrec = genericLiftShowsPrec
-- | An ADT, i.e. a disjoint sum of products, like 'data' in Haskell, or 'enum' in Rust or Swift.
data Datatype a = Datatype { datatypeName :: !a, datatypeConstructors :: ![a] }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Data.Syntax.Declaration.Datatype where liftEq = genericLiftEq
instance Show1 Data.Syntax.Declaration.Datatype where liftShowsPrec = genericLiftShowsPrec
-- | A single constructor in a datatype, or equally a 'struct' in C, Rust, or Swift.
data Constructor a = Constructor { constructorName :: !a, constructorFields :: ![a] }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Data.Syntax.Declaration.Constructor where liftEq = genericLiftEq
instance Show1 Data.Syntax.Declaration.Constructor where liftShowsPrec = genericLiftShowsPrec

View File

@ -1,7 +1,13 @@
module Data.Syntax.Expression where
import Data.Functor.Classes.Eq.Generic
import Data.Functor.Classes.Show.Generic
import GHC.Generics
import Prologue
-- | Typical prefix function application, like `f(x)` in many languages, or `f x` in Haskell.
data Call a = Call { callFunction :: a, callParams :: [a] }
deriving (Eq, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Call where liftEq = genericLiftEq
instance Show1 Call where liftShowsPrec = genericLiftShowsPrec

View File

@ -11,7 +11,7 @@ import Prologue
-- Boolean
newtype Boolean a = Boolean Bool
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Boolean where liftEq = genericLiftEq
instance Show1 Boolean where liftShowsPrec = genericLiftShowsPrec
@ -21,7 +21,7 @@ instance Show1 Boolean where liftShowsPrec = genericLiftShowsPrec
-- | A literal integer of unspecified width. No particular base is implied.
newtype Integer a = Integer { integerContent :: ByteString }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Data.Syntax.Literal.Integer where liftEq = genericLiftEq
instance Show1 Data.Syntax.Literal.Integer where liftShowsPrec = genericLiftShowsPrec
@ -34,7 +34,7 @@ instance Show1 Data.Syntax.Literal.Integer where liftShowsPrec = genericLiftShow
-- Strings, symbols
newtype String a = String { stringElements :: [Union '[InterpolationElement, TextElement] a] }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 String where liftEq = genericLiftEq
instance Show1 String where liftShowsPrec = genericLiftShowsPrec
@ -43,7 +43,7 @@ instance Show1 String where liftShowsPrec = genericLiftShowsPrec
-- | An interpolation element within a string literal.
newtype InterpolationElement a = InterpolationElement { interpolationBody :: a }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 InterpolationElement where liftEq = genericLiftEq
instance Show1 InterpolationElement where liftShowsPrec = genericLiftShowsPrec
@ -51,14 +51,14 @@ instance Show1 InterpolationElement where liftShowsPrec = genericLiftShowsPrec
-- | A sequence of textual contents within a string literal.
newtype TextElement a = TextElement { textElementContent :: ByteString }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 TextElement where liftEq = genericLiftEq
instance Show1 TextElement where liftShowsPrec = genericLiftShowsPrec
newtype Symbol a = SymbolLiteral { symbolContent :: ByteString }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Symbol where liftEq = genericLiftEq
instance Show1 Symbol where liftShowsPrec = genericLiftShowsPrec
@ -69,21 +69,21 @@ instance Show1 Symbol where liftShowsPrec = genericLiftShowsPrec
-- Collections
newtype ArrayLiteral a = ArrayLiteral { arrayElements :: [Union '[Identity, Comment] a] }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 ArrayLiteral where liftEq = genericLiftEq
instance Show1 ArrayLiteral where liftShowsPrec = genericLiftShowsPrec
newtype HashLiteral a = HashLiteral { hashElements :: [Union '[KeyValue, Comment] a] }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 HashLiteral where liftEq = genericLiftEq
instance Show1 HashLiteral where liftShowsPrec = genericLiftShowsPrec
data KeyValue a = KeyValue { key :: !a, value :: !a }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 KeyValue where liftEq = genericLiftEq
instance Show1 KeyValue where liftShowsPrec = genericLiftShowsPrec

View File

@ -8,7 +8,7 @@ import Prologue
-- | Conditional. This must have an else block, which can be filled with some default value when omitted in the source, e.g. 'pure ()' for C-style if-without-else or 'pure Nothing' for Ruby-style, in both cases assuming some appropriate Applicative context into which the If will be lifted.
data If a = If { ifCondition :: !a, ifThenBody :: !a, ifElseBody :: !a }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 If where liftEq = genericLiftEq
instance Show1 If where liftShowsPrec = genericLiftShowsPrec
@ -17,14 +17,14 @@ instance Show1 If where liftShowsPrec = genericLiftShowsPrec
-- | A pattern-matching or computed jump control-flow statement, like 'switch' in C or JavaScript, or 'case' in Ruby or Haskell.
data Match with a = Switch { matchSubject :: !a, matchPatterns :: ![with a] }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 with => Eq1 (Match with) where liftEq = genericLiftEq
instance Show1 with => Show1 (Match with) where liftShowsPrec = genericLiftShowsPrec
-- | A pattern in a pattern-matching or computed jump control-flow statement, like 'case' in C or JavaScript, 'when' in Ruby, or the left-hand side of '->' in the body of Haskell 'case' expressions.
newtype Pattern a = Pattern a
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Pattern where liftEq = genericLiftEq
instance Show1 Pattern where liftShowsPrec = genericLiftShowsPrec
@ -33,13 +33,13 @@ instance Show1 Pattern where liftShowsPrec = genericLiftShowsPrec
-- Returns
newtype Return a = Return a
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Return where liftEq = genericLiftEq
instance Show1 Return where liftShowsPrec = genericLiftShowsPrec
newtype Yield a = Yield a
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Yield where liftEq = genericLiftEq
instance Show1 Yield where liftShowsPrec = genericLiftShowsPrec
@ -48,25 +48,25 @@ instance Show1 Yield where liftShowsPrec = genericLiftShowsPrec
-- Loops
data For a = For { forBefore :: !a, forCondition :: !a, forStep :: !a, forBody :: !a }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 For where liftEq = genericLiftEq
instance Show1 For where liftShowsPrec = genericLiftShowsPrec
data ForEach a = ForEach { forEachBinding :: !a, forEachSubject :: !a, forEachBody :: !a }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 ForEach where liftEq = genericLiftEq
instance Show1 ForEach where liftShowsPrec = genericLiftShowsPrec
data While a = While { whileCondition :: !a, whileBody :: !a }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 While where liftEq = genericLiftEq
instance Show1 While where liftShowsPrec = genericLiftShowsPrec
data DoWhile a = DoWhile { doWhileCondition :: !a, doWhileBody :: !a }
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 DoWhile where liftEq = genericLiftEq
instance Show1 DoWhile where liftShowsPrec = genericLiftShowsPrec
@ -75,13 +75,13 @@ instance Show1 DoWhile where liftShowsPrec = genericLiftShowsPrec
-- Exception handling
newtype Throw a = Throw a
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Throw where liftEq = genericLiftEq
instance Show1 Throw where liftShowsPrec = genericLiftShowsPrec
data Try with a = Try !a ![with a]
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
-- deriving instance (Eq a, Eq (with a)) => Eq (Try with a)
-- deriving instance (Show a, Show (with a)) => Show (Try with a)
@ -89,13 +89,13 @@ instance Eq1 with => Eq1 (Try with) where liftEq = genericLiftEq
instance Show1 with => Show1 (Try with) where liftShowsPrec = genericLiftShowsPrec
data Catch a = Catch !(Maybe a) !a
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Catch where liftEq = genericLiftEq
instance Show1 Catch where liftShowsPrec = genericLiftShowsPrec
newtype Finally a = Finally a
deriving (Eq, Generic1, Show)
deriving (Eq, Foldable, Generic1, Show)
instance Eq1 Finally where liftEq = genericLiftEq
instance Show1 Finally where liftShowsPrec = genericLiftShowsPrec