tools/protocolParser.hs
author unc0rr
Tue, 28 Apr 2015 23:26:12 +0300
branchqmlfrontend
changeset 10904 ce265b038220
parent 10902 29519fe63fdd
child 10906 13fde38281fc
permissions -rw-r--r--
Some more work on network client implementation
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
10902
29519fe63fdd Will use own tree type
unc0rr
parents: 10900
diff changeset
    19
data ParseTree = PTChar Char [ParseTree]
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
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    47
        , cmd "INFO" [SS, SS, SS, SS]
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
10904
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    54
buildParseTree cmds = if isJust emptyNamed then cmdLeaf $ fromJust emptyNamed else subtree
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    55
    where
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    56
        emptyNamed = find (\(_, (Command n _:_)) -> null n) assocs
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    57
        assocs = groupByFirstChar cmds
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    58
        subtree = map (\(c, cmds) -> PTChar c $ buildParseTree cmds) assocs
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    59
        cmdLeaf (c, (hwc:_)) = [PTChar c [PTCommand hwc]]
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    60
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    61
dumpTree (PTChar c st) = char c $$ (nest 2 $ vcat $ map dumpTree st)
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    62
dumpTree _ = empty
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    63
ce265b038220 Some more work on network client implementation
unc0rr
parents: 10902
diff changeset
    64
pas = vcat . map dumpTree $ buildParseTree commands
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    65
    
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    66
main = putStrLn $ render pas