summaryrefslogtreecommitdiff
path: root/src/GF/IDE/IDECommands.hs
blob: 56d392a71dd92271cba89161f492fa34df6bcd41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
----------------------------------------------------------------------
-- |
-- Module      : IDECommands
-- Maintainer  : AR
-- Stability   : (stable)
-- Portability : (portable)
--
-- > CVS $Date: 2005/11/14 16:03:41 $ 
-- > CVS $Author: aarne $
-- > CVS $Revision: 1.2 $
--
-- Commands usable in grammar-writing IDE.
-----------------------------------------------------------------------------

module GF.IDE.IDECommands where

import GF.Infra.Ident (Ident, identC)
import GF.Compile.ShellState
import qualified GF.Shell.ShellCommands as S
import qualified GF.Shell.Commands as E
import qualified GF.Shell.CommandL as PE
import GF.UseGrammar.Session
import GF.UseGrammar.Custom
import GF.Grammar.PrGrammar

import GF.Infra.Option
import GF.Data.Operations
import GF.Infra.Modules
import GF.Infra.UseIO

data IDEState = IDE {
  ideShellState :: ShellState, 
  ideAbstract   :: Maybe Ident,
  ideConcretes  :: [Ident],
  ideCurrentCnc :: Maybe Ident,
  ideCurrentLin :: Maybe Ident, -- lin or lincat
  ideSState     :: Maybe SState
  }

emptyIDEState :: ShellState -> IDEState
emptyIDEState shst = IDE shst Nothing [] Nothing Nothing Nothing

data IDECommand =
    IDEInit
  | IDEAbstract Ident
  | IDEConcrete Ident
  | IDELin      Ident
  | IDEShell    String -- S.Command
  | IDEEdit     String -- E.Command
  | IDEQuit
  | IDEVoid     String -- the given command itself maybe


execIDECommand :: IDECommand -> IDEState -> IOE IDEState
execIDECommand c state = case c of
  IDEInit -> 
    return $ emptyIDEState env
  IDEAbstract a -> 
    return $ state {ideAbstract = Just a} ---- check a exists or import it
  IDEConcrete a -> 
    return $ state {ideCurrentCnc = Just a} ---- check a exists or import it
  IDELin a -> 
    return $ state {ideCurrentLin = Just a} ---- check a exists
  IDEEdit s ->
    execEdit s
  IDEShell s ->
    execShell s
  IDEVoid s  -> ioeErr $ fail s
  _ -> ioeErr $ fail "command not implemented"

 where
   env = ideShellState state
   sstate = maybe initSState id $ ideSState state

   execShell s = execEdit $ "gf" +++ s

   execEdit s = ioeIO $ do
     (env',sstate') <- E.execCommand env (PE.pCommand s) sstate
     return $ state {ideShellState = env', ideSState = Just sstate'}

   putMsg = putStrLn ---- XML

pCommands :: String -> [IDECommand]
pCommands = map pCommand . concatMap (chunks ";;" . words) . lines

pCommand :: [String] -> IDECommand
pCommand ws = case ws of
  "gf"       : s     -> IDEShell $ unwords s
  "edit"     : s     -> IDEEdit  $ unwords s
  "abstract" : a : _ -> IDEAbstract $ identC a
  "concrete" : a : _ -> IDEConcrete $ identC a
  "lin"      : a : _ -> IDELin      $ identC a
  "empty"    : _     -> IDEInit
  "quit"     : _     -> IDEQuit
  _                  -> IDEVoid $ unwords ws