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 , isControl
, Element (..) , Element (..)
, Control (..) , Control (..)
, Flow (..)
) where ) where
import Data.Text (Text) import Data.Text (Text)
@ -31,15 +32,38 @@ isControl _ = False
-- and are interpreted into language-specific representations at a -- and are interpreted into language-specific representations at a
-- later point in the reprinting pipeline. -- later point in the reprinting pipeline.
data Element data Element
= Run Text -- ^ A literal chunk of text. = Run Text -- ^ A literal chunk of text.
| Truth Bool -- ^ A boolean value. | Truth Bool -- ^ A boolean value.
| Nullity -- ^ @null@ or @nil@ or some other zero value. | Nullity -- ^ @null@ or @nil@ or some other zero value.
| Sep -- ^ Some sort of delimiter, interpreted in some 'Context'. | Sep -- ^ Some sort of delimiter, interpreted in some 'Context'.
| Sym -- ^ Some sort of symbol, interpreted in some 'Context'. | Sym -- ^ Some sort of symbol, interpreted in some 'Context'.
| Then | 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 | Else
| Open -- ^ The beginning of some 'Context', such as an @[@ or @{@. | For
| Close -- ^ The opposite of 'TOpen'. | 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) deriving (Eq, Show)
-- | 'Control' tokens describe information about some AST's context. -- | '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 Control.Abstract.ScopeGraph
import Data.JSON.Fields import Data.JSON.Fields
import Diffing.Algorithm import Diffing.Algorithm
import Reprinting.Tokenize import Reprinting.Tokenize (Tokenize (..), imperative, within', yield)
import qualified Data.Reprinting.Token as Token import qualified Data.Reprinting.Token as Token
import qualified Data.Reprinting.Scope as Scope import qualified Data.Reprinting.Scope as Scope
@ -57,9 +57,9 @@ instance Evaluatable If where
instance Tokenize If where instance Tokenize If where
tokenize If{..} = within' Scope.If $ do tokenize If{..} = within' Scope.If $ do
ifCondition ifCondition
yield Token.Then yield (Token.Flow Token.Then)
ifThenBody ifThenBody
yield Token.Else yield (Token.Flow Token.Else)
ifElseBody ifElseBody
-- | Else statement. The else condition is any term, that upon successful completion, continues evaluation to the elseBody, e.g. `for ... else` in Python. -- | 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 (Close, Imperative:Return:_) -> pure () -- Don't hardwarp or indent for return statements
-- If statements -- If statements
(Open, If:_) -> emit "if" *> space (Open, If:_) -> emit "if" *> space
(Then, If:_) -> emit ":" (Flow Then, If:_) -> emit ":"
(Else, If:xs) -> endContext (imperativeDepth xs) *> emit "else:" (Flow Else, If:xs) -> endContext (imperativeDepth xs) *> emit "else:"
(Close, If:_) -> pure () (Close, If:_) -> pure ()
-- Booleans -- Booleans
(Truth True, _) -> emit "True" (Truth True, _) -> emit "True"