{-# LANGUAGE OverloadedStrings #-}
module Tokstyle.Cimple.Analysis.ForLoops (analyse) where

import qualified Control.Monad.State.Lazy    as State
import           Data.Text                   (Text)
import qualified Data.Text                   as Text
import           Language.Cimple             (AssignOp (..), AstActions,
                                              Lexeme (..), Node (..),
                                              defaultActions, doNode,
                                              traverseAst)
import qualified Language.Cimple.Diagnostics as Diagnostics


linter :: AstActions [Text]
linter :: AstActions [Text]
linter = AstActions [Text]
forall a. IdentityActions (State a) () Text
defaultActions
    { doNode :: FilePath
-> Node () (Lexeme Text)
-> State [Text] (Node () (Lexeme Text))
-> State [Text] (Node () (Lexeme Text))
doNode = \FilePath
file Node () (Lexeme Text)
node State [Text] (Node () (Lexeme Text))
act ->
        case Node () (Lexeme Text)
node of
            ForStmt (VarDecl Node () (Lexeme Text)
_ty (Declarator (DeclSpecVar Lexeme Text
_i) (Just Node () (Lexeme Text)
_v))) Node () (Lexeme Text)
_ Node () (Lexeme Text)
_ [Node () (Lexeme Text)]
_ -> State [Text] (Node () (Lexeme Text))
act
            ForStmt (AssignExpr (VarExpr Lexeme Text
_i) AssignOp
AopEq Node () (Lexeme Text)
_v) Node () (Lexeme Text)
_ Node () (Lexeme Text)
_ [Node () (Lexeme Text)]
_ -> State [Text] (Node () (Lexeme Text))
act
            ForStmt Node () (Lexeme Text)
i Node () (Lexeme Text)
_ Node () (Lexeme Text)
_ [Node () (Lexeme Text)]
_ -> do
                FilePath -> Node () (Lexeme Text) -> Text -> DiagnosticsT [Text] ()
forall diags a.
HasDiagnostics diags =>
FilePath -> Node a (Lexeme Text) -> Text -> DiagnosticsT diags ()
warn FilePath
file Node () (Lexeme Text)
node (Text -> DiagnosticsT [Text] ())
-> (Node () (Lexeme Text) -> Text)
-> Node () (Lexeme Text)
-> DiagnosticsT [Text] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Text
Text.pack (FilePath -> Text)
-> (Node () (Lexeme Text) -> FilePath)
-> Node () (Lexeme Text)
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Node () (Lexeme Text) -> FilePath
forall a. Show a => a -> FilePath
show (Node () (Lexeme Text) -> DiagnosticsT [Text] ())
-> Node () (Lexeme Text) -> DiagnosticsT [Text] ()
forall a b. (a -> b) -> a -> b
$ Node () (Lexeme Text)
i
                State [Text] (Node () (Lexeme Text))
act

            Node () (Lexeme Text)
_ -> State [Text] (Node () (Lexeme Text))
act
    }
  where warn :: FilePath -> Node a (Lexeme Text) -> Text -> DiagnosticsT diags ()
warn FilePath
file Node a (Lexeme Text)
node = FilePath -> Lexeme Text -> Text -> DiagnosticsT diags ()
forall diags.
HasDiagnostics diags =>
FilePath -> Lexeme Text -> Text -> DiagnosticsT diags ()
Diagnostics.warn FilePath
file (Node a (Lexeme Text) -> Lexeme Text
forall a. Node a (Lexeme Text) -> Lexeme Text
Diagnostics.at Node a (Lexeme Text)
node)

analyse :: (FilePath, [Node () (Lexeme Text)]) -> [Text]
analyse :: (FilePath, [Node () (Lexeme Text)]) -> [Text]
analyse = [Text] -> [Text]
forall a. [a] -> [a]
reverse ([Text] -> [Text])
-> ((FilePath, [Node () (Lexeme Text)]) -> [Text])
-> (FilePath, [Node () (Lexeme Text)])
-> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State [Text] (FilePath, [Node () (Lexeme Text)])
 -> [Text] -> [Text])
-> [Text]
-> State [Text] (FilePath, [Node () (Lexeme Text)])
-> [Text]
forall a b c. (a -> b -> c) -> b -> a -> c
flip State [Text] (FilePath, [Node () (Lexeme Text)])
-> [Text] -> [Text]
forall s a. State s a -> s -> s
State.execState [] (State [Text] (FilePath, [Node () (Lexeme Text)]) -> [Text])
-> ((FilePath, [Node () (Lexeme Text)])
    -> State [Text] (FilePath, [Node () (Lexeme Text)]))
-> (FilePath, [Node () (Lexeme Text)])
-> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AstActions [Text]
-> (FilePath, [Node () (Lexeme Text)])
-> State [Text] (FilePath, [Node () (Lexeme Text)])
forall iattr oattr itext otext a (f :: * -> *).
(TraverseAst iattr oattr itext otext a a, Applicative f) =>
AstActions f iattr oattr itext otext -> a -> f a
traverseAst AstActions [Text]
linter