Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Data/Kicad/PcbnewExpr/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ parseWithFilename filename =
fromSExpr :: SExpr -> Either String PcbnewExpr
fromSExpr (List _ (Atom pos kw:sxs)) = case kw of
"module" -> PcbnewExprModule <$> asPcbnewModule sxs
"kicad_pcb" -> PcbnewExprKicadPcb <$> asPcbnewKicadPcb sxs
"pad" -> PcbnewExprItem <$> asPcbnewPad sxs
"fp_text" -> PcbnewExprItem <$> asPcbnewFpText sxs
"fp_arc" -> PcbnewExprItem <$> asPcbnewFpArc sxs
Expand Down Expand Up @@ -99,6 +100,15 @@ fromSExpr sx@(Atom _ s) = case s of
_ -> expecting "'italic' or 'hide' or 'locked' " sx
fromSExpr x = expecting "List _ with a key or a string atom" x

asPcbnewKicadPcb :: [SExpr] -> Either String PcbnewKicadPcb
asPcbnewKicadPcb sxs = interpretRest sxs defaultPcbnewKicadPcb
where
interpretRest [] pcb = Right pcb
interpretRest (sx:sxs) pcb = case fromSExpr sx of
Right (PcbnewExprAttribute attr) ->
interpretRest sxs (over kicadPcbAttrs (++[attr]) pcb)
_ -> expecting "kicad_pcb attributes or items" sx

asPcbnewModule :: [SExpr] -> Either String PcbnewModule
asPcbnewModule (Atom _ n:xs) =
interpretRest xs defaultPcbnewModule { pcbnewModuleName = n }
Expand Down
31 changes: 31 additions & 0 deletions Data/Kicad/PcbnewExpr/PcbnewExpr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Data.Kicad.PcbnewExpr.PcbnewExpr
-- * Types
PcbnewExpr(..)
, PcbnewModule(..)
, PcbnewKicadPcb(..)
, PcbnewItem(..)
, PcbnewAttribute(..)
-- * Attribute types
Expand All @@ -22,6 +23,7 @@ module Data.Kicad.PcbnewExpr.PcbnewExpr
, fpTextJustify
, moduleItems
, moduleAttrs
, kicadPcbAttrs
, itemLayers
, padAttributes
, atP
Expand All @@ -43,6 +45,7 @@ module Data.Kicad.PcbnewExpr.PcbnewExpr
, justifyToString
-- * Default (empty) instances
, defaultPcbnewModule
, defaultPcbnewKicadPcb
, defaultPcbnewFpText
, defaultPcbnewFpLine
, defaultPcbnewFpCircle
Expand All @@ -66,12 +69,14 @@ import Data.Kicad.SExpr.SExpr
import Data.Kicad.Util

data PcbnewExpr = PcbnewExprModule PcbnewModule
| PcbnewExprKicadPcb PcbnewKicadPcb
| PcbnewExprItem PcbnewItem
| PcbnewExprAttribute PcbnewAttribute
deriving (Show, Eq)

instance AEq PcbnewExpr where
PcbnewExprModule x ~== PcbnewExprModule y = x ~== y
PcbnewExprKicadPcb x ~== PcbnewExprKicadPcb y = x ~== y
PcbnewExprItem x ~== PcbnewExprItem y = x ~== y
PcbnewExprAttribute x ~== PcbnewExprAttribute y = x ~== y
_ ~== _ = False
Expand All @@ -88,6 +93,29 @@ data PcbnewModule = PcbnewModule { pcbnewModuleName :: String
}
deriving (Show, Eq)

data PcbnewKicadPcb = PcbnewKicadPcb
{ pcbnewKicadPcbAttrs :: [PcbnewAttribute]
, pcbnewKicadPcbNets :: [PcbnewItem]
, pcbnewKicadPcbModules :: [PcbnewModule]
, pcbnewKicadPcbItems :: [PcbnewItem]
}
deriving (Show, Eq)

defaultPcbnewKicadPcb :: PcbnewKicadPcb
defaultPcbnewKicadPcb = PcbnewKicadPcb
{ pcbnewKicadPcbAttrs = []
, pcbnewKicadPcbNets = []
, pcbnewKicadPcbModules = []
, pcbnewKicadPcbItems = []
}

instance AEq PcbnewKicadPcb where
PcbnewKicadPcb as1 ns1 ms1 is1 ~== PcbnewKicadPcb as2 ns2 ms2 is2 =
as1 ~== as2
&& ns1 ~== ns2
&& ms1 ~== ms2
&& is1 ~== is2


pos :: SourcePos
pos = newPos "" 0 0
Expand All @@ -109,6 +137,9 @@ moduleItems f (PcbnewModule n l a i) = PcbnewModule n l a `fmap` f i
moduleAttrs :: Functor f => LensLike' f PcbnewModule [PcbnewAttribute]
moduleAttrs f (PcbnewModule n l a i) = (\a' -> PcbnewModule n l a' i) `fmap` f a

kicadPcbAttrs :: Functor f => LensLike' f PcbnewKicadPcb [PcbnewAttribute]
kicadPcbAttrs f (PcbnewKicadPcb a n m i) = (\a' -> PcbnewKicadPcb a' n m i) `fmap` f a

instance AEq PcbnewModule where
PcbnewModule n1 l1 as1 is1 ~== PcbnewModule n2 l2 as2 is2 =
n1 == n2
Expand Down