-- | @futhark query@
module Futhark.CLI.Query (main) where

import Futhark.Compiler
import Futhark.Util.Loc
import Futhark.Util.Options
import Language.Futhark.Query
import Language.Futhark.Syntax
import Text.Read (readMaybe)

-- | Run @futhark query@.
main :: String -> [String] -> IO ()
main :: [Char] -> [[Char]] -> IO ()
main = ()
-> [FunOptDescr ()]
-> [Char]
-> ([[Char]] -> () -> Maybe (IO ()))
-> [Char]
-> [[Char]]
-> IO ()
forall cfg.
cfg
-> [FunOptDescr cfg]
-> [Char]
-> ([[Char]] -> cfg -> Maybe (IO ()))
-> [Char]
-> [[Char]]
-> IO ()
mainWithOptions () [] [Char]
"program line col" (([[Char]] -> () -> Maybe (IO ())) -> [Char] -> [[Char]] -> IO ())
-> ([[Char]] -> () -> Maybe (IO ())) -> [Char] -> [[Char]] -> IO ()
forall a b. (a -> b) -> a -> b
$ \[[Char]]
args () ->
  case [[Char]]
args of
    [[Char]
file, [Char]
line, [Char]
col] -> do
      Int
line' <- [Char] -> Maybe Int
forall a. Read a => [Char] -> Maybe a
readMaybe [Char]
line
      Int
col' <- [Char] -> Maybe Int
forall a. Read a => [Char] -> Maybe a
readMaybe [Char]
col
      IO () -> Maybe (IO ())
forall a. a -> Maybe a
Just (IO () -> Maybe (IO ())) -> IO () -> Maybe (IO ())
forall a b. (a -> b) -> a -> b
$ do
        (Warnings
_, Imports
imports, VNameSource
_) <- [Char] -> IO (Warnings, Imports, VNameSource)
forall (m :: * -> *).
MonadIO m =>
[Char] -> m (Warnings, Imports, VNameSource)
readProgramOrDie [Char]
file
        -- The 'offset' part of the Pos is not used and can be arbitrary.
        case Imports -> Pos -> Maybe AtPos
atPos Imports
imports (Pos -> Maybe AtPos) -> Pos -> Maybe AtPos
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> Int -> Int -> Pos
Pos [Char]
file Int
line' Int
col' Int
0 of
          Maybe AtPos
Nothing -> [Char] -> IO ()
putStrLn [Char]
"No information available."
          Just (AtName QualName VName
qn Maybe BoundTo
def Loc
loc) -> do
            [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Name: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ QualName VName -> [Char]
forall a. Pretty a => a -> [Char]
prettyString QualName VName
qn
            [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Position: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SrcLoc -> [Char]
forall a. Located a => a -> [Char]
locStr (Loc -> SrcLoc
forall a. Located a => a -> SrcLoc
srclocOf Loc
loc)
            case Maybe BoundTo
def of
              Maybe BoundTo
Nothing -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
              Just (BoundTerm StructType
t Loc
defloc) -> do
                [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Type: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ StructType -> [Char]
forall a. Pretty a => a -> [Char]
prettyString StructType
t
                [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Definition: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SrcLoc -> [Char]
forall a. Located a => a -> [Char]
locStr (Loc -> SrcLoc
forall a. Located a => a -> SrcLoc
srclocOf Loc
defloc)
              Just (BoundType Loc
defloc) ->
                [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Definition: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SrcLoc -> [Char]
forall a. Located a => a -> [Char]
locStr (Loc -> SrcLoc
forall a. Located a => a -> SrcLoc
srclocOf Loc
defloc)
              Just (BoundModule Loc
defloc) ->
                [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Definition: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SrcLoc -> [Char]
forall a. Located a => a -> [Char]
locStr (Loc -> SrcLoc
forall a. Located a => a -> SrcLoc
srclocOf Loc
defloc)
              Just (BoundModuleType Loc
defloc) ->
                [Char] -> IO ()
putStrLn ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Definition: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SrcLoc -> [Char]
forall a. Located a => a -> [Char]
locStr (Loc -> SrcLoc
forall a. Located a => a -> SrcLoc
srclocOf Loc
defloc)
    [[Char]]
_ -> Maybe (IO ())
forall a. Maybe a
Nothing