diff --git a/src/Data/Reprinting/Token.hs b/src/Data/Reprinting/Token.hs index fa8dd1642..904dafc3a 100644 --- a/src/Data/Reprinting/Token.hs +++ b/src/Data/Reprinting/Token.hs @@ -4,6 +4,7 @@ module Data.Reprinting.Token , isControl , Element (..) , Control (..) + , Flow (..) ) where import Data.Text (Text) @@ -31,15 +32,38 @@ isControl _ = False -- and are interpreted into language-specific representations at a -- later point in the reprinting pipeline. data Element - = Run Text -- ^ A literal chunk of text. - | Truth Bool -- ^ A boolean value. - | Nullity -- ^ @null@ or @nil@ or some other zero value. - | Sep -- ^ Some sort of delimiter, interpreted in some 'Context'. - | Sym -- ^ Some sort of symbol, interpreted in some 'Context'. - | Then + = Run Text -- ^ A literal chunk of text. + | Truth Bool -- ^ A boolean value. + | Nullity -- ^ @null@ or @nil@ or some other zero value. + | Sep -- ^ Some sort of delimiter, interpreted in some 'Context'. + | Sym -- ^ Some sort of symbol, interpreted in some 'Context'. + | Open -- ^ The beginning of some 'Context', such as an @[@ or @{@. + | Close -- ^ The opposite of 'Open'. + | Access -- ^ Member/method access + | Resolve -- ^ Namespace/package resolution + | Assign -- ^ Variable binding + | Self -- ^ @self@ or @this@ + | Superclass -- ^ @super@ + | Flow Flow -- ^ Control-flow token (@if@, @else@, @for@...) + | Extends -- ^ Subclassing indicator (syntax varies) + deriving (Eq, Show) + +-- | Helper datum to corral control-flow entities like @while@, @for@, +-- etc. Usually corresponds to a keyword in a given language. +data Flow + = Break + | Continue | Else - | Open -- ^ The beginning of some 'Context', such as an @[@ or @{@. - | Close -- ^ The opposite of 'TOpen'. + | For + | Foreach + | In -- ^ Usually associated with 'Foreach' loops + | Rescue -- ^ AKA @catch@ in most languages + | Retry + | Switch -- ^ AKA @case@ + | Then -- ^ The true-branch of @if@-statements + | Try + | While + | Yield deriving (Eq, Show) -- | 'Control' tokens describe information about some AST's context. diff --git a/src/Data/Syntax/Statement.hs b/src/Data/Syntax/Statement.hs index d7151f39e..a14daf0b9 100644 --- a/src/Data/Syntax/Statement.hs +++ b/src/Data/Syntax/Statement.hs @@ -14,7 +14,7 @@ import Data.Abstract.Evaluatable as Abstract import Control.Abstract.ScopeGraph import Data.JSON.Fields import Diffing.Algorithm -import Reprinting.Tokenize +import Reprinting.Tokenize (Tokenize (..), imperative, within', yield) import qualified Data.Reprinting.Token as Token import qualified Data.Reprinting.Scope as Scope @@ -57,9 +57,9 @@ instance Evaluatable If where instance Tokenize If where tokenize If{..} = within' Scope.If $ do ifCondition - yield Token.Then + yield (Token.Flow Token.Then) ifThenBody - yield Token.Else + yield (Token.Flow Token.Else) ifElseBody -- | Else statement. The else condition is any term, that upon successful completion, continues evaluation to the elseBody, e.g. `for ... else` in Python. diff --git a/src/Language/Python/PrettyPrint.hs b/src/Language/Python/PrettyPrint.hs index b0bca195d..e144bb0f8 100644 --- a/src/Language/Python/PrettyPrint.hs +++ b/src/Language/Python/PrettyPrint.hs @@ -35,10 +35,10 @@ step (Defer el cs) = case (el, cs) of (Close, Imperative:Return:_) -> pure () -- Don't hardwarp or indent for return statements -- If statements - (Open, If:_) -> emit "if" *> space - (Then, If:_) -> emit ":" - (Else, If:xs) -> endContext (imperativeDepth xs) *> emit "else:" - (Close, If:_) -> pure () + (Open, If:_) -> emit "if" *> space + (Flow Then, If:_) -> emit ":" + (Flow Else, If:xs) -> endContext (imperativeDepth xs) *> emit "else:" + (Close, If:_) -> pure () -- Booleans (Truth True, _) -> emit "True"