From 46ae60eaca841b554ba20c6a2b7a15b43c12b4df Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 18 Dec 2018 13:51:16 +0100 Subject: Much ado about nothing --- interactive-edit-lens/src/Main.hs | 94 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 interactive-edit-lens/src/Main.hs (limited to 'interactive-edit-lens/src/Main.hs') diff --git a/interactive-edit-lens/src/Main.hs b/interactive-edit-lens/src/Main.hs new file mode 100644 index 0000000..f7bc806 --- /dev/null +++ b/interactive-edit-lens/src/Main.hs @@ -0,0 +1,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 + } -- cgit v1.2.3