1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 13:51:44 +03:00

Add 'Flow' to represent control-flow constructs.

This commit is contained in:
Patrick Thomson 2018-11-19 16:05:01 -05:00
parent 54b8f5248b
commit 2974b34c17
3 changed files with 39 additions and 15 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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"