forked from facebookincubator/retrie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Retrie.hs
163 lines (157 loc) · 4.09 KB
/
Retrie.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
-- Copyright (c) Facebook, Inc. and its affiliates.
--
-- This source code is licensed under the MIT license found in the
-- LICENSE file in the root directory of this source tree.
--
-- | This module provides the external interface for using Retrie as a library.
-- All other modules should be considered internal, with APIs that are subject
-- to change without notice.
{-# LANGUAGE RecordWildCards #-}
module Retrie
( -- * Scripting
runScript
, runScriptWithModifiedOptions
-- ** Parsing Rewrites
-- *** Imports
, parseImports
-- *** Queries
, parseQueries
, QuerySpec(..)
-- *** Rewrites
, parseRewrites
, RewriteSpec(..)
, QualifiedName
-- ** Retrie Computations
, Retrie
-- *** Applying Rewrites
, apply
, applyWithStrategy
, applyWithUpdate
, applyWithUpdateAndStrategy
, addImports
-- *** Control Flow
, ifChanged
, iterateR
-- *** Focusing
, focus
-- *** Querying the AST
, query
, queryWithUpdate
-- *** Traversal Strategies
, bottomUp
, topDown
, topDownPrune
-- * Advanced Scripting
-- $advanced
-- ** Side Conditions
, MatchResultTransformer
, defaultTransformer
, MatchResult(..)
-- * Annotated ASTs
, Annotated
, astA
-- ** Type Synonyms
, AnnotatedHsDecl
, AnnotatedHsExpr
, AnnotatedHsType
, AnnotatedImports
, AnnotatedModule
, AnnotatedPat
, AnnotatedStmt
-- ** Parsing
-- | Note: These parsers do not re-associate infix operators.
-- To do so, use 'Retrie.ExactPrint.fix'. For example:
--
-- > do
-- > expr <- parseExpr "f <$> x <*> y"
-- > e <- transformA expr (fix (fixityEnv opts))
--
, LibDir
, parseDecl
, parseExpr
, parsePattern
, parseStmt
, parseType
-- ** Operations
, transformA
, graftA
, pruneA
, trimA
, printA
-- ** Util
-- | Collection of miscellaneous helpers for manipulating the GHC AST.
, module Retrie.Expr
-- * Types
-- ** Context
, Context(..)
, ContextUpdater
, updateContext
-- ** Options
, Options
, Options_(..)
, Verbosity(..)
-- ** Quantifiers
, module Retrie.Quantifiers
-- ** Queries
, Query(..)
-- ** Rewrites
, Rewrite
, Template(..)
, mkRewrite
, ppRewrite
, addRewriteImports
, setRewriteTransformer
-- ** Substitution
, subst
-- | See "Retrie.Substitution" for the 'Substitution' type.
, module Retrie.Substitution
-- ** Universe
, Universe
, Matchable(..)
, toURewrite
, fromURewrite
-- * GHC API
-- | "Retrie.GHC" re-exports the GHC API, with some helpers for consistency
-- across versions.
, module Retrie.GHC
) where
import Retrie.Context
import Retrie.ExactPrint.Annotated hiding (unsafeMkA)
import Retrie.ExactPrint
import Retrie.Expr
import Retrie.Fixity
import Retrie.GHC
import Retrie.Monad
import Retrie.Options
import Retrie.Quantifiers
import Retrie.Query
( QuerySpec(..)
, parseQuerySpecs
)
import Retrie.Rewrites
import Retrie.Run
import Retrie.Subst
import Retrie.Substitution
import Retrie.SYB
import Retrie.Types
import Retrie.Universe
import Retrie.Util
-- | Create 'Rewrite's from string specifications of rewrites.
parseRewrites :: LibDir -> Options -> [RewriteSpec] -> IO [Rewrite Universe]
parseRewrites = parseRewritesInternal
-- | Create 'Query's from string specifications of expressions/types/statements.
parseQueries
:: LibDir -> Options -> [(Quantifiers, QuerySpec, v)] -> IO [Query Universe v]
parseQueries libdir Options{..} = parseQuerySpecs libdir fixityEnv
-- $advanced
-- For advanced rewriting, Retrie provides the notion of a
-- 'MatchResultTransformer'. This is a callback function that is provided the
-- result of matching the left-hand side of an equation. Whatever the callback
-- returns is used to perform the actual rewrite.
--
-- The callback has access to the 'Context' of the match, the generated
-- 'Substitution', and 'IO'. Helper libraries such as 'Annotated' and 'subst'
-- make it possible to define complex transformers without too much tedium.
--
-- Transformers can check side conditions by examining the 'MatchResult' and
-- returning 'NoMatch' when conditions do not hold.