coping with already existing state (nested states), any advice?

Issue #3 new
Andreas Reuleaux
created an issue

I have tried your indentation-trifecta package (read Michael Adam's paper), and like it a lot: works great for me, thanks a lot.

ie I when I use it in the style suggested in the docs :

I have for example a function decls for parsing decls that requires IndentationParsing m now:

  decls :: (
    TokenParsing m
    , LookAheadParsing m
    , DeltaParsing m
    , IndentationParsing m
    ) => m [Exp Text Text]
  decls = ...

and I can use it as

  parse decls "some long input string"

with

  parse parser s = fromSuccess $ parseString parser' beginning s
    where
      parser' = runInnerParser $ evalTokenIndentationParserT p indentst

with indentst as

indentst = mkIndentationState 0 infIndentation True Gt

and some more details ommitted here (fromSuccess, runInnerParser, beginning etc), but should be obvious: I just run the given evalTokenIndentationParserT on my parser decls, and everthing else set up accordingly.

Now my original parser decls (before switching to IndentationParsing) had state already, decls originally read:

  decls :: (
    TokenParsing m
    , LookAheadParsing m
    , DeltaParsing m
    , MonadState TTState m
    ) => m [Exp Text Text]
  decls = ...

and I would run it as

  oldparse p s = fromSuccess $ parseString p' beginning s
    where
      p' = runInnerParser $ evalStateT p ttst

and this state was useful to me: would provide fresh variables eg etc:

  data TTState = TTState {

    , fresh_vars :: [String]
    , fresh_idx :: Integer

    } deriving (Show)

and I wonder what to do in that situation: basically I have two states now: your IndentationState and my old TTState.

While I can run

  runInnerParser $ evalStateT (evalTokenIndentationParserT p indentst) ttst

I still cannot use my decls requiring two TTState and IndentationState:

  decls :: (
    TokenParsing m
    , LookAheadParsing m
    , DeltaParsing m

    , IndentationParsing m
    , MonadState TTState m

    ) => m [Exp Text Text]
  decls = ...

well makes sense, two nested states is kind of strange, thus I am trying to wrap your IndentationState in my given TTState like so:

  data TTState = TTState {

    , fresh_vars :: [String]
    , fresh_idx :: Integer

    , idstate :: IndentationState

    } deriving (Show)

and now what: how do I declare my ttdefault of type TTState to obey the IndentationParsing interface ?

  instance IndentationParsing ttdefault where
     ...

maybe it's just a simple matter of lifting some given functions, maybe it's not possible at all...?

Any attempt / recherche of mine in this regard yields only hints how to cope with a given State..., eg.

hoist, embed in the mmorph package

but your IndentationState package seems to be written deliberatly only in terms of a state transformer.

thanks a lot.

-Andreas

Comments (2)

  1. Log in to comment