pire-0.2.4

Copyright(c) Andreas Reuleaux 2015 - 2018
LicenseBSD2
MaintainerAndreas Reuleaux <rx@a-rx.info>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Refactor.Decorate

Description

decorate the syntax tree with exact position information, used subsequently to calculate token ranges, and allow for navigation in the white space aware syntax tree

Synopsis

Documentation

class Deco t where Source #

Minimal complete definition

deco

Methods

deco :: MonadState Delta m => t -> m (t, Delta) Source #

Instances

decorate :: (Deco b, Deco a, Bitraversable t) => t a b -> Delta -> t (a, Delta) (b, Delta) Source #

decorate token trees with exact position info (relying on bitraversal, and on the fact the every token is recorded now with its textual repr), decorate is just a shortcut for evalState (bitraverse deco deco...), creates a tree of pairs

(more fine grained than (Pos ...), wrapped around only some of the exprs by the parser), and works after parsing as well

>>> runState (bitraverse deco deco $ snd $ parse expr_ "f arg") $ beginning
(Pair (Pair (Id ("f",Lines 0 0 0 0)) (Ws (" ",Lines 0 1 0 0))) (Pair (Id ("arg",Lines 0 2 0 0)) (Ws ("",Lines 0 5 0 0))),Lines 0 5 0 0)
>>> evalState (bitraverse deco deco $ snd $ parse expr_ "f arg") $ beginning
Pair (Pair (Id ("f",Lines 0 0 0 0)) (Ws (" ",Lines 0 1 0 0))) (Pair (Id ("arg",Lines 0 2 0 0)) (Ws ("",Lines 0 5 0 0)))

and works with String exprs as well

>>> evalState (bitraverse deco deco $ t2s $ snd $ parse expr_ "f arg") $ beginning
Pair (Pair (Id ("f",Lines 0 0 0 0)) (Ws (" ",Lines 0 1 0 0))) (Pair (Id ("arg",Lines 0 2 0 0)) (Ws ("",Lines 0 5 0 0)))
>>> decorate (snd $ parse expr_ "\\  x . f  a") $ beginning
Node [Pair (Token ("\\",Lines 0 0 0 0)) (Ws ("  ",Lines 0 1 0 0)),Node [Pair (Binder ("x",Lines 0 3 0 0)) (Ws (" ",Lines 0 4 0 0))],Pair (Token (".",Lines 0 5 0 0)) (Ws (" ",Lines 0 6 0 0)),Abstract [("x",Lines 0 7 0 0)] (Scope (Pair (Pair (Id (F (Id ("f",Lines 0 8 0 0)))) (Ws ("  ",Lines 0 9 0 0))) (Pair (Id (F (Id ("a",Lines 0 11 0 0)))) (Ws ("",Lines 0 12 0 0)))))]

watch out however for bound vars (they have no textual repr in the scope, and thus shift the result)

>>> decorate (snd $ parse expr_ "\\  x . x  a") $ beginning
Node [Pair (Token ("\\",Lines 0 0 0 0)) (Ws ("  ",Lines 0 1 0 0)),Node [Pair (Binder ("x",Lines 0 3 0 0)) (Ws (" ",Lines 0 4 0 0))],Pair (Token (".",Lines 0 5 0 0)) (Ws (" ",Lines 0 6 0 0)),Abstract [("x",Lines 0 7 0 0)] (Scope (Pair (Pair (Id (B 0)) (Ws ("  ",Lines 0 8 0 0))) (Pair (Id (F (Id ("a",Lines 0 10 0 0)))) (Ws ("",Lines 0 11 0 0)))))]

but get exact positions, even in the case of bound vars, w/ the help of wrap:

>>> decorate (wrap $ snd $ parse expr_ "\\  x . x  a") $ beginning
Node [Pair (Token ("\\",Lines 0 0 0 0)) (Ws ("  ",Lines 0 1 0 0)),Node [Pair (Binder ("x",Lines 0 3 0 0)) (Ws (" ",Lines 0 4 0 0))],Pair (Token (".",Lines 0 5 0 0)) (Ws (" ",Lines 0 6 0 0)),Abstract_ (Scope (Pair (Pair (Bnd_ ("x",Lines 0 7 0 0) (Id (B 0))) (Ws ("  ",Lines 0 8 0 0))) (Pair (Id (F (Id ("a",Lines 0 10 0 0)))) (Ws ("",Lines 0 11 0 0)))))]

decoWithDummy :: (Deco t, MonadState Delta m) => t -> m (t, Delta) Source #