mirror of
https://github.com/github/semantic.git
synced 2024-12-30 10:27:45 +03:00
Add 'Flow' to represent control-flow constructs.
This commit is contained in:
parent
54b8f5248b
commit
2974b34c17
@ -4,6 +4,7 @@ module Data.Reprinting.Token
|
||||
, isControl
|
||||
, Element (..)
|
||||
, Control (..)
|
||||
, Flow (..)
|
||||
) where
|
||||
|
||||
import Data.Text (Text)
|
||||
@ -36,10 +37,33 @@ data Element
|
||||
| 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
|
||||
| Else
|
||||
| Open -- ^ The beginning of some 'Context', such as an @[@ or @{@.
|
||||
| Close -- ^ The opposite of 'TOpen'.
|
||||
| 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
|
||||
| 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.
|
||||
|
@ -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.
|
||||
|
@ -36,8 +36,8 @@ step (Defer el cs) = case (el, cs) of
|
||||
|
||||
-- If statements
|
||||
(Open, If:_) -> emit "if" *> space
|
||||
(Then, If:_) -> emit ":"
|
||||
(Else, If:xs) -> endContext (imperativeDepth xs) *> emit "else:"
|
||||
(Flow Then, If:_) -> emit ":"
|
||||
(Flow Else, If:xs) -> endContext (imperativeDepth xs) *> emit "else:"
|
||||
(Close, If:_) -> pure ()
|
||||
|
||||
-- Booleans
|
||||
|
Loading…
Reference in New Issue
Block a user