0 | module Text.Molfile.Parser.Stack
  1 |
  2 | import Data.Array.Mutable
  3 | import Data.SortedMap as SM
  4 |
  5 | import Syntax.T1
  6 |
  7 | import Text.ILex.Derive
  8 |
  9 | import public Text.ILex
 10 | import public Text.Molfile.Types
 11 |
 12 | %default total
 13 | %language ElabReflection
 14 |
 15 | --------------------------------------------------------------------------------
 16 | -- Stack and State
 17 | --------------------------------------------------------------------------------
 18 |
 19 | %runElab deriveParserState "CSz" "CST"
 20 |   [ "CErr", "H1", "H2", "H3", "Counts", "EndMol", "CDone" -- general states
 21 |   , "SData", "SDValue" -- SD Files
 22 |
 23 |   -- V2000 States
 24 |   , "Coords2", "Sym2", "Chrg2", "Bnd2", "Prop2"
 25 |
 26 |   -- V3000 States
 27 |   , "CountsV3", "ACount", "BCount", "CountEnd", "EmptyV3"
 28 |   , "Atom3", "Index3", "Coords3", "Sym3", "AAMap", "Prop3", "AtomEnd"
 29 |   , "BondBegin", "BondEnd", "Bnd3", "BndProp3"
 30 |   , "RestV3", "SGroup"
 31 |   ]
 32 |
 33 | ||| A molecular graph in the making.
 34 | public export
 35 | record MGraph (q : Type) where
 36 |   constructor MG
 37 |   atoms   : Nat
 38 |   atom    : Ref q (Fin $ S atoms)
 39 |   indices : Ref q (SortedMap Nat $ Fin (S atoms))
 40 |   bond    : Ref q (Maybe $ Edge (S atoms) MolBond)
 41 |   graph   : MArray q (S atoms) (Adj (S atoms) MolBond MolAtom)
 42 |
 43 | export
 44 | mgraph : (atoms : Nat) -> F1 q (MGraph q)
 45 | mgraph atoms = T1.do
 46 |   atm <- ref1 FZ
 47 |   ixs <- ref1 SM.empty
 48 |   bnd <- ref1 Nothing
 49 |   g   <- marray1 (S atoms) (A (cast {from = Elem} C) empty)
 50 |   pure (MG atoms atm ixs bnd g)
 51 |
 52 | public export
 53 | record CSTCK (q : Type) where
 54 |   constructor CK
 55 |   -- text position
 56 |   line_      : Ref q Nat
 57 |   col_       : Ref q Nat
 58 |   positions_ : Ref q (SnocList Position)
 59 |
 60 |   -- headers
 61 |   h1,h2,h3   : Ref q MolLine
 62 |
 63 |   -- graphs
 64 |   mgraph     : Ref q (MGraph q)
 65 |   stack_     : Ref q (SnocList Molfile)
 66 |   groups     : Ref q (SortedMap Nat String)
 67 |   count      : Ref q Nat
 68 |   isEmpty    : Ref q Bool
 69 |
 70 |   -- sdata
 71 |   sdhead     : Ref q SDHeader
 72 |   sdvals     : Ref q (SnocList StructureData)
 73 |
 74 |   -- utilities
 75 |   error_     : Ref q (Maybe $ BoundedErr MolErr)
 76 |   bytes_     : Ref q ByteString
 77 |   strings_   : Ref q (SnocList String)
 78 |   pos        : Ref q Nat
 79 |
 80 | %runElab derive "CSTCK" [HasPosition,HasError,HasBytes,HasStack,HasStringLits]
 81 |
 82 | export
 83 | init : F1 q (CSTCK q)
 84 | init = T1.do
 85 |   l   <- ref1 Z
 86 |   c   <- ref1 Z
 87 |   ps  <- ref1 [<]
 88 |   h1  <- ref1 ""
 89 |   h2  <- ref1 ""
 90 |   h3  <- ref1 ""
 91 |   mg  <- mgraph 0
 92 |   gr  <- ref1 mg
 93 |   gs  <- ref1 [<]
 94 |   grp <- ref1 SM.empty
 95 |   cnt <- ref1 Z
 96 |   ie  <- ref1 False
 97 |   sdh <- ref1 ""
 98 |   sdd <- ref1 [<]
 99 |   err <- ref1 Nothing
100 |   bs  <- ref1 empty
101 |   str <- ref1 [<]
102 |   pos <- ref1 Z
103 |   pure (CK l c ps h1 h2 h3 gr gs grp cnt ie sdh sdd err bs str pos)
104 |