Nix.Match
Description
A set of functions for matching on Nix expression trees and extracting the values of sub-trees.
Synopsis
- match :: Matchable t => WithHoles t v -> Fix t -> Maybe [(v, Fix t)]
- findMatches :: Matchable t => WithHoles t v -> Fix t -> [(Fix t, [(v, Fix t)])]
- class Traversable t => Matchable t where
- zipMatchLeft :: t a -> t b -> Maybe (t (a, b))
- class (Traversable t, Generic1 t) => GMatchable t where
- gZipMatchLeft :: t a -> t b -> Maybe (t (a, b))
- data WithHoles t v
- addHoles :: NExpr -> WithHoles NExprF Text
- addHolesLoc :: NExprLoc -> WithHoles NExprLocF Text
- isOptionalPath :: NAttrPath r -> Maybe (NAttrPath r)
Documentation
match :: Matchable t => WithHoles t v -> Fix t -> Maybe [(v, Fix t)] Source #
Match a tree with holes against a tree without holes, returning the values of the holes if it matches.
NExprF and
NExprLocF are both instances of
Matchable.
NExprLocF does
not require the annotations to match. Please see the
Matchable instance
documentation for
NExprF for more details.
>>>
import Nix.TH
>>>
match (addHoles [nix|{foo = x: ^foo; bar = ^bar;}|]) [nix|{foo = x: "hello"; bar = "world"; baz = "!";}|]Just [("bar",Fix (NStr (DoubleQuoted [Plain "world"]))),("foo",Fix (NStr (DoubleQuoted [Plain "hello"])))]
Arguments
Find all the needles in a haystack, returning the matched expression as well as their filled holes. Results are returned productively in preorder.
>>>
import Nix.TH
>>>
import Control.Arrow
>>>
pretty = prettyNix *** (fmap @[] (fmap @((,) Text) prettyNix))
>>>
pretty <$> findMatches (addHoles [nix|{x=^x;}|]) [nix|{x=1;a={x=2;};}|][({ x = 1; a = { x = 2; }; },[("x",1)]),({ x = 2; },[("x",2)])]
class Traversable t => Matchable t where Source #
Minimal complete definition
Nothing
Methods
zipMatchLeft :: t a -> t b -> Maybe (t (a, b)) Source #
Match one level of structure, returning the matched structure with sub structures to match. Needle is the first argument, matchee is the second.
Unlike the
Unifiable class in the "unification-fd" package, this doesn't
have to be a commutative operation, the needle will always be the first
parameter and instances are free to treat if differently if appropriate.
default zipMatchLeft :: (Generic1 t, GMatchable (Rep1 t)) => t a -> t b -> Maybe (t (a, b)) Source #
Instances
class (Traversable t, Generic1 t) => GMatchable t where Source #
A class used in the
default definition for
zipMatchLeft
Methods
gZipMatchLeft :: t a -> t b -> Maybe (t (a, b)) Source #