summaryrefslogtreecommitdiff
path: root/interactive-edit-lens/src/Main.hs
blob: f7bc80638148b835202b8626a91f20f1e1ba247c (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
{-# LANGUAGE OverloadedStrings
           , ExistentialQuantification
  #-}

module Main where

import Interact
import Control.DFST.Lens
import Control.DFST

import Data.Map (Map)
import qualified Data.Map as Map

import Data.Set (Set)
import qualified Data.Set as Set

import Data.Sequence (Seq)
import qualified Data.Sequence as Seq

import Data.Char

import System.Environment
import System.Exit

import Debug.Trace

data SomeDFST = forall state. (Ord state, Show state) => SomeDFST { someDFST :: DFST state Char Char }

data JsonNewlState = JNUndeterminedStructure | JNOutsideStructure | JNInsideStructure | JNInsideString | JNEscape
  deriving (Eq, Ord, Show, Read)

dfstMap :: String -> Maybe SomeDFST
dfstMap "double" = Just . SomeDFST $ DFST
  { stInitial = ()
  , stTransition = mconcat
    [ Map.fromList 
        [(((), sym), ((), Seq.fromList [sym, sym])) | sym <- ['a'..'z'] ++ ['A'..'Z'] ++ ['!', ' ']] -- sym <- [minBound..maxBound], isPrint sym]
    , Map.singleton ((), '\n') ((), Seq.singleton '\n')
    ]
  , stAccept = Set.singleton ()
  }
dfstMap "id" = Just . SomeDFST $ DFST
  { stInitial = ()
  , stTransition = Map.fromList 
      [(((), sym), ((), Seq.singleton sym)) | sym <- ['a'..'z'] ++ ['A'..'Z'] ++ ['!', ' ']] -- sym <- [minBound..maxBound], isPrint sym]
  , stAccept = Set.singleton ()
  }
dfstMap "alternate" = Just . SomeDFST $ DFST
  { stInitial = 0 :: Int
  , stTransition = mconcat
    [ Map.fromList [((0, sym), (1, Seq.singleton sym)) | sym <- ['a'..'z'] ++ ['A'..'Z'] ++ ['!', ' ']] -- sym <- [minBound..maxBound], isPrint sym]
    , Map.fromList [((1, sym), (0, Seq.fromList [toUpper sym, toUpper sym])) | sym <- ['a'..'z'] ++ ['A'..'Z'] ++ ['!', ' ']] -- sym <- [minBound..maxBound], isPrint sym]
    ]
  , stAccept = Set.fromList [0]
  }
dfstMap "json-newl" = Just . SomeDFST $ DFST
  { stInitial = JNOutsideStructure
  , stTransition = mconcat
    [ Map.fromList [((jnOutside, sym), (jnOutside, Seq.empty)) | sym <- whitespace, jnOutside <- [JNOutsideStructure, JNUndeterminedStructure]]
    , Map.fromList [((jnOutside, sym), (JNInsideStructure, Seq.fromList [sym, ' '])) | sym <- "[{", jnOutside <- [JNOutsideStructure, JNInsideStructure, JNUndeterminedStructure]]
    , Map.fromList [((JNInsideStructure, sym), (JNInsideStructure, Seq.empty)) | sym <- whitespace]
    , Map.fromList [((jnInside, sym), (JNUndeterminedStructure, Seq.fromList ['\n', sym])) | sym <- "]}", jnInside <- [JNInsideStructure, JNUndeterminedStructure]]
    , Map.fromList [((jnInside, ','), (JNInsideStructure, Seq.fromList "\n, ")) | jnInside <- [JNInsideStructure, JNUndeterminedStructure] ]
    , Map.fromList [((jnInside, ':'), (JNInsideStructure, Seq.fromList " : ")) | jnInside <- [JNInsideStructure, JNUndeterminedStructure] ]
    , Map.fromList [((jn, '"'), (JNInsideString, Seq.singleton '"')) | jn <- [JNUndeterminedStructure, JNInsideStructure]]
    , Map.fromList [((JNInsideString, sym), (JNInsideString, Seq.singleton sym)) | sym <- ['a'..'z'] ++ ['A'..'Z'] ++ ",.!?: "]
    , Map.singleton (JNInsideString, '"') (JNUndeterminedStructure, Seq.singleton '"')
    , Map.singleton (JNInsideString, '\\') (JNEscape, Seq.singleton '\\')
    , Map.singleton (JNEscape, '"') (JNInsideString, Seq.singleton '"')
    ]
  , stAccept = Set.fromList [JNOutsideStructure, JNUndeterminedStructure]
  }
  where
    whitespace = toEnum <$> [0x0020, 0x0009, 0x000a, 0x000d]
dfstMap _ = Nothing

main :: IO ()
main = do
  args <- getArgs

  dfst <- case args of
    [name] | Just dfst <- dfstMap name
      -> return dfst
    _ -> exitWith $ ExitFailure 2

  interactiveEditLens' dfst

interactiveEditLens' :: SomeDFST -> IO ()
interactiveEditLens' (SomeDFST dfst)
  = interactiveEditLens id $ InteractConfig
      { icfgInitial = InitialEmpty
      , icfgLens = dfstLens dfst
      , icfgCompileError = Nothing
      }