Skip to content

Instantly share code, notes, and snippets.

@voidlizard
Created August 5, 2016 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voidlizard/28168c5666eee25e2c4afe4681a982e0 to your computer and use it in GitHub Desktop.
Save voidlizard/28168c5666eee25e2c4afe4681a982e0 to your computer and use it in GitHub Desktop.
module Recons.CLI.Dict where
import Data.Monoid ((<>))
import qualified Data.Map as M
import Recons.Types
type DictKey = Prefix
data DictValue = W Str -- static word
| Q Str -- arg with given name, should be queried
deriving Show
type DictEntry = (Prefix, [DictValue])
dictForCommand :: Cp -> [DictEntry]
dictForCommand cp = merge $ snd $ go [] cp
where
go :: Prefix -> Cp -> (Prefix, [DictEntry])
go p (P s) = (p <> [s], [(p, [W s])])
go p (Arg s) = (p, [(p, [Q s])])
go p (B a b) = (p, d1 <> d2)
where (p1, d1) = go p a
(p2, d2) = go p b
go p (S a b) = (p2, d1 <> d2)
where (p1, d1) = go p a
(p2, d2) = go p1 b
go p (O x) = go p x
go p (Discard x) = go p x
go p N = (p, [(mempty, mempty)])
merge = M.toList . M.fromListWith (<>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment