Created
August 5, 2016 06:32
-
-
Save voidlizard/28168c5666eee25e2c4afe4681a982e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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