Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the simple stack machine display functions from other annotated prettyprinters #13

Closed
quchen opened this issue May 21, 2017 · 2 comments

Comments

@quchen
Copy link
Owner

quchen commented May 21, 2017

This is a simpler kind of stack machine based rendering, when all we’re concerned about is printing output, pushing and popping based on certain SimpleDoc constructors. This won’t allow fancy stack machines that peek, double-push and what not.

e.g. annotated-wl-pprint contains:

displayDecoratedA :: (Applicative f, Monoid b)
                  => (String -> f b) -> (a -> f b) -> (a -> f b)
                  -> SimpleDoc a -> f b
displayDecoratedA str start end sd = display [] sd
  where display []        SEmpty              = pure mempty
        display stk       (SChar c x)         = (str [c]) <++> (display stk x)
        display stk       (SText l s x)       = (str s) <++> (display stk x)
        display stk       (SLine ind x)       = (str ('\n':indentation ind)) <++> (display stk x)
        display stk       (SAnnotStart ann x) = (start ann) <++> (display (ann:stk) x)
        display (ann:stk) (SAnnotStop x)      = (end ann) <++> (display stk x)

        -- malformed documents
        display []        (SAnnotStop _)      = error "stack underflow"
        display stk       SEmpty              = error "stack not consumed by rendering"

        (<++>) = liftA2 mappend

Convinced this is a good idea in #12.

@minad
Copy link

minad commented May 21, 2017

where do you need double push and the additional power? just out of interest...

@quchen
Copy link
Owner Author

quchen commented May 22, 2017

Rendering to ANSI terminal directly, for example. When removing a style, you need to pop the current one, and then peek what the previous one was, in order to set the new one correctly.

@quchen quchen closed this as completed May 22, 2017
quchen added a commit that referenced this issue May 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants