1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 09:15:01 +03:00
Commit Graph

8 Commits

Author SHA1 Message Date
Patrick Thomson
bdc5db555b rename and prune 2018-11-05 16:56:49 -05:00
Patrick Thomson
292a268b00 better name for infix operator 2018-11-04 14:46:19 -05:00
Patrick Thomson
7ea52dbfe3 Give Control.Matching API better ergonomics.
Given that @tclem and I have found the matcher API frustrating, I've
taken a stab at improving its ergonomics, and I've found some success
in separating composition of matchers from predicate-based narrowing
thereof.

The biggest change here is the elimination of the old `match`
combinator, which proved to be clumsy in that it complected narrowing
and composition. Top-down matching combinators are now written with
the `need` combinator and the `>>>` combinator, which is more readable
and more versatile. Here's a matcher that accepts functions with
Python docstrings:

```haskell
docstringMatcher :: ( Decl.Function :< fs
                    , [] :< fs
                    , Lit.TextElement :< fs
                    , term ~ Term (Sum fs) ann
                    ) => Matcher term term
docstringMatcher = target <*
               (need Decl.functionBody
                >>> narrow @[]
                >>> mhead
                >>> narrow @Lit.TextElement
                >>> ensure Lit.isTripleQuoted))
```

Pretty readable, right? Each step of the tree regular expression -
choosing function bodies, ensuring said bodies are lists, examining
the first element, and choosing only TextElements containing
triple-quoted strings - is made implicit. The old way would have
looked something like this:

```haskell
docstringMatcher = target <* match Decl.functionBody
                           $ narrow
                           $ matchM listToMaybe
                           $ target <* ensure Lit.isTripleQuoted
```
which is a good deal more disorganized and less flexible
in the quite-common case of applying functions during a
matching pass. Separating the act of composition from
function application is a big win here.

Further comments are inline.
2018-11-02 19:25:29 -04:00
Patrick Thomson
6b476d0eb7 Add 'purely' combinator to Matching and rename it.
@tclem and I found ourselves wanting an arrow-like combinator that
promotes a given function to a Matcher. While I think an Arrow
instance is going a little overboard, there's no harm in adding a
'purely' function, the naming of which is commensurate with the
rewriting DSL.

This also renames the module, since there's not anything really
abstract about matching (indeed, it is quite concrete).
2018-10-30 11:04:11 -04:00
Patrick Thomson
f541c42da8 Fix literals to store text internally. 2018-05-30 22:32:22 -04:00
Rob Rix
ebc8e01cc4 Use our own local definition of Sum. 2018-05-02 12:00:15 -04:00
Timothy Clem
10d13cd424 Bring preludes back for testing, clean up Util 2018-04-22 07:47:59 -07:00
Patrick Thomson
034bb562d4 Introduce tree-automata DSL for filtering and matching ASTs.
This patch adds the `Matcher` monad, which is capable of filtering any
recursive data structure, bottom-up, yielding a list of (or an
optional) result. These functions are probably going to be used over
`Term` values, so API is provided to wrap common projection functions.

The API was more or less copied directly from that of Clang's AST
matching facilities.

There are a lot of things we can do in the future:
* Binding results yielded in matchers to associated names, for future
  transformation stages to look up and modify.
* Actual transformation stages.
* Optimizations. This is not very fast.

A million thanks to @robrix, whose sage advice managed to turn my
kooky idea for an API into something really special and exciting.
2018-03-27 16:14:30 -04:00