tools/protocolParser.hs
author unc0rr
Sat, 09 May 2015 23:30:19 +0300
branchqmlfrontend
changeset 10925 be9ce3dc3739
parent 10908 1bd7a3a28b18
child 10927 336f5ad638be
permissions -rw-r--r--
Produce some useful code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
     1
module Main where
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
     2
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
     3
import Text.PrettyPrint.HughesPJ
10904
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
     4
import qualified Data.MultiMap as MM
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
     5
import Data.Maybe
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
     6
import Data.List
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
     7
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
     8
data HWProtocol = Command String [CmdParam]
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
     9
data CmdParam = Skip
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    10
              | SS
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    11
              | LS
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    12
              | IntP
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    13
              | Many [CmdParam]
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    14
data ClientStates = NotConnected
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    15
                  | JustConnected
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    16
                  | ServerAuth
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    17
                  | Lobby
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    18
10906
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    19
data ParseTree = PTPrefix String [ParseTree]
10902
29519fe63fdd Will use own tree type
unc0rr
parents: 10900
diff changeset
    20
               | PTCommand HWProtocol
29519fe63fdd Will use own tree type
unc0rr
parents: 10900
diff changeset
    21
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    22
cmd = Command
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    23
cmd1 s p = Command s [p]
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    24
cmd2 s p1 p2 = Command s [p1, p2]
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    25
10902
29519fe63fdd Will use own tree type
unc0rr
parents: 10900
diff changeset
    26
breakCmd (Command (c:cs) params) = (c, Command cs params)
29519fe63fdd Will use own tree type
unc0rr
parents: 10900
diff changeset
    27
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    28
commands = [
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    29
        cmd "CONNECTED" [Skip, IntP]
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    30
        , cmd1 "NICK" SS
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    31
        , cmd1 "PROTO" IntP
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    32
        , cmd1 "ASKPASSWORD" SS
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    33
        , cmd1 "SERVER_AUTH" SS
10904
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    34
        , cmd1 "JOINING" SS
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    35
        , cmd1 "BANLIST" $ Many [SS]
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    36
        , cmd1 "JOINED" $ Many [SS]
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    37
        , cmd1 "LOBBY:JOINED" $ Many [SS]
10904
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    38
        , cmd2 "LOBBY:LEFT" SS LS
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    39
        , cmd2 "CLIENT_FLAGS" SS $ Many [SS]
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    40
        , cmd2 "LEFT" SS $ Many [SS]
10902
29519fe63fdd Will use own tree type
unc0rr
parents: 10900
diff changeset
    41
        , cmd1 "SERVER_MESSAGE" LS
10904
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    42
        , cmd1 "EM" $ Many [LS]
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    43
        , cmd1 "PING" $ Many [SS]
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    44
        , cmd2 "CHAT" SS LS
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    45
        , cmd2 "SERVER_VARS" SS LS
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    46
        , cmd2 "BYE" SS LS
10908
1bd7a3a28b18 Try another approach, WIP
unc0rr
parents: 10906
diff changeset
    47
        , cmd1 "INFO" $ Many [SS]
10904
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    48
        , cmd "KICKED" []
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    49
    ]
f373838129c2 Some futher work on flib net client part
unc0rr
parents:
diff changeset
    50
10904
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    51
groupByFirstChar :: [HWProtocol] -> [(Char, [HWProtocol])]
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    52
groupByFirstChar = MM.assocs . MM.fromList . map breakCmd
10902
29519fe63fdd Will use own tree type
unc0rr
parents: 10900
diff changeset
    53
10925
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    54
buildParseTree cmds = [PTPrefix "!" $ bpt cmds]
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    55
bpt cmds = if isJust emptyNamed then cmdLeaf $ fromJust emptyNamed else subtree
10904
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    56
    where
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    57
        emptyNamed = find (\(_, (Command n _:_)) -> null n) assocs
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    58
        assocs = groupByFirstChar cmds
10906
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    59
        subtree = map buildsub assocs
10925
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    60
        buildsub (c, cmds) = let st = bpt cmds in if null $ drop 1 st then maybeMerge c st else PTPrefix [c] st
10906
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    61
        maybeMerge c cmd@[PTCommand _] = PTPrefix [c] cmd
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    62
        maybeMerge c cmd@[PTPrefix s ss] = PTPrefix (c:s) ss
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    63
        cmdLeaf (c, (hwc:_)) = [PTPrefix [c] [PTCommand hwc]]
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    64
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    65
dumpTree = vcat . map dt
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    66
    where
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    67
    dt (PTPrefix s st) = text s $$ (nest 1 $ vcat $ map dt st)
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    68
    dt _ = empty
10904
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    69
10908
1bd7a3a28b18 Try another approach, WIP
unc0rr
parents: 10906
diff changeset
    70
pas2 = buildSwitch $ buildParseTree commands
10906
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    71
    where
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    72
        buildSwitch cmds = text "case getNextChar of" $$ (nest 4 . vcat $ map buildCase cmds) $$ elsePart
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    73
        buildCase (PTCommand _ ) = text "#10: <call cmd handler>;"
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    74
        buildCase (PTPrefix (s:ss) cmds) = quotes (char s) <> text ": " <> consumePrefix ss (buildSwitch cmds)
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    75
        consumePrefix "" = id
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    76
        consumePrefix str = (text "consume" <> (parens . quotes $ text str) <> semi $$)
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    77
        zeroChar = text "#0: state:= pstDisconnected;"
13fde38281fc Rendering some code
unc0rr
parents: 10904
diff changeset
    78
        elsePart = text "else <unknown cmd> end;"
10904
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    79
10925
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    80
renderArrays letters commands = l $+$ s
10908
1bd7a3a28b18 Try another approach, WIP
unc0rr
parents: 10906
diff changeset
    81
    where
10925
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    82
        maybeQuotes s = if null $ tail s then quotes $ text s else text s
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    83
        l = text "const letters: array[0.." <> (int $ length letters - 1) <> text "] of char = "
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    84
            <> parens (hsep . punctuate comma $ map maybeQuotes letters) <> semi
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    85
        s = text "const commands: array[0.." <> (int $ length commands - 1) <> text "] of integer = "
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    86
            <> parens (hsep . punctuate comma $ map text commands) <> semi
10908
1bd7a3a28b18 Try another approach, WIP
unc0rr
parents: 10906
diff changeset
    87
10925
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    88
pas = uncurry renderArrays $ buildTables $ buildParseTree commands
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    89
    where
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    90
        buildTables cmds = let (_, _, _, t1, t2) = foldr walk (0, [0], -10, [], []) cmds in (tail t1, tail t2)
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    91
        walk (PTCommand _ ) (lc, s:sh, pc, tbl1, tbl2) = (lc, 2:sh, pc - 1, "#10":"0":tbl1, "0":(show pc):tbl2)
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    92
        walk (PTPrefix prefix cmds) l = lvldown $ foldr fpf (foldr walk (lvlup l) cmds) prefix
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    93
        lvlup (lc, sh, pc, tbl1, tbl2) = (lc, 0:sh, pc, tbl1, tbl2)
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    94
        lvldown (lc, s1:s2:sh, pc, tbl1, t:tbl2) = (lc, s1+s2:sh, pc, tbl1, show s1:tbl2)
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    95
        fpf c (lc, s:sh, pc, tbl1, tbl2) = (lc + 1, s+1:sh, pc, [c]:tbl1, "0":tbl2)
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    96
be9ce3dc3739 Produce some useful code
unc0rr
parents: 10908
diff changeset
    97
main = putStrLn $ renderStyle style{lineLength = 80} pas