Another approach to parsing two-lines protocol commands qmlfrontend
authorunc0rr
Sun, 22 Nov 2015 18:06:32 +0300
branchqmlfrontend
changeset 11425 2947f06e8533
parent 11424 86c13e5662f1
child 11426 ab6a6d9ebfc0
Another approach to parsing two-lines protocol commands
hedgewars/uFLNet.pas
hedgewars/uFLNetProtocol.pas
hedgewars/uFLNetTypes.pas
tools/protocolParser.hs
--- a/hedgewars/uFLNet.pas	Sat Nov 21 23:56:41 2015 +0300
+++ b/hedgewars/uFLNet.pas	Sun Nov 22 18:06:32 2015 +0300
@@ -32,8 +32,7 @@
 procedure handleTail; forward;
 function getShortString: shortstring; forward;
 
-// generated stuff here
-const letters: array[0..212] of char = ('A', 'S', 'K', 'P', 'A', 'S', 'S', 'W',
+const letters: array[0..224] of char = ('A', 'S', 'K', 'P', 'A', 'S', 'S', 'W',
     'O', 'R', 'D', #10, 'B', 'A', 'N', 'L', 'I', 'S', 'T', #10, 'Y', 'E', #10, 'C',
     'H', 'A', 'T', #10, 'L', 'I', 'E', 'N', 'T', '_', 'F', 'L', 'A', 'G', 'S', #10,
     'O', 'N', 'N', 'E', 'C', 'T', 'E', 'D', #10, 'E', 'M', #10, 'R', 'R', 'O', 'R',
@@ -42,21 +41,24 @@
     'E', 'F', 'T', #10, 'O', 'B', 'B', 'Y', ':', 'J', 'O', 'I', 'N', 'E', 'D', #10,
     'L', 'E', 'F', 'T', #10, 'N', 'I', 'C', 'K', #10, 'O', 'T', 'I', 'C', 'E', #10,
     'P', 'I', 'N', 'G', #10, 'R', 'O', 'T', 'O', #10, 'R', 'O', 'O', 'M', 'S', #10,
-    #10, 'U', 'N', 'D', '_', 'F', 'I', 'N', 'I', 'S', 'H', 'E', 'D', #10, 'U', 'N',
-    '_', 'G', 'A', 'M', 'E', #10, 'S', 'E', 'R', 'V', 'E', 'R', '_', 'A', 'U', 'T',
-    'H', #10, 'M', 'E', 'S', 'S', 'A', 'G', 'E', #10, 'V', 'A', 'R', 'S', #10, 'T',
-    'E', 'A', 'M', '_', 'A', 'C', 'C', 'E', 'P', 'T', 'E', 'D', #10, 'C', 'O', 'L',
-    'O', 'R', #10, 'W', 'A', 'R', 'N', 'I', 'N', 'G', #10, #0, #10);
-const commands: array[0..212] of integer = (12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-    -40, 11, 7, 0, 0, 0, 0, 0, -39, 0, 0, -38, 26, 4, 0, 0, -37, 12, 0, 0, 0, 0, 0,
-    0, 0, 0, 0, 0, -36, 0, 0, 0, 0, 0, 0, 0, 0, -35, 8, 2, -34, 0, 0, 0, 0, -33, 7,
-    0, 0, 0, 0, 0, -32, 5, 0, 0, 0, -31, 11, 0, 0, 0, 3, 0, -30, 0, 0, 0, -29, 7, 0,
-    0, 0, 0, 0, -28, 22, 4, 0, 0, -27, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, -26, 0, 0,
-    0, 0, -25, 11, 4, 0, 0, -24, 0, 0, 0, 0, 0, -23, 10, 4, 0, 0, -22, 0, 0, 0, 0,
-    -21, 28, 19, 5, 3, 2, -19, -20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -18, 0, 0, 0,
-    0, 0, 0, 0, -17, 25, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, -16, 8, 0, 0, 0, 0, 0, 0,
-    -15, 0, 0, 0, 0, -14, 20, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, -13, 0, 0, 0, 0,
-    0, -12, 8, 0, 0, 0, 0, 0, 0, -11, 0, -10);
+    #10, 'A', 'D', 'D', #10, 'D', 'E', 'L', #10, 'U', 'P', 'D', #10, 'U', 'N', 'D',
+    '_', 'F', 'I', 'N', 'I', 'S', 'H', 'E', 'D', #10, 'U', 'N', '_', 'G', 'A', 'M',
+    'E', #10, 'S', 'E', 'R', 'V', 'E', 'R', '_', 'A', 'U', 'T', 'H', #10, 'M', 'E',
+    'S', 'S', 'A', 'G', 'E', #10, 'V', 'A', 'R', 'S', #10, 'T', 'E', 'A', 'M', '_',
+    'A', 'C', 'C', 'E', 'P', 'T', 'E', 'D', #10, 'C', 'O', 'L', 'O', 'R', #10, 'W',
+    'A', 'R', 'N', 'I', 'N', 'G', #10, #0, #10);
+const commands: array[0..224] of integer = (12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    -42, 11, 7, 0, 0, 0, 0, 0, -41, 0, 0, -40, 26, 4, 0, 0, -39, 12, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, -38, 0, 0, 0, 0, 0, 0, 0, 0, -37, 8, 2, -36, 0, 0, 0, 0, -35, 7,
+    0, 0, 0, 0, 0, -34, 5, 0, 0, 0, -33, 11, 0, 0, 0, 3, 0, -32, 0, 0, 0, -31, 7, 0,
+    0, 0, 0, 0, -30, 22, 4, 0, 0, -29, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, -28, 0, 0,
+    0, 0, -27, 11, 4, 0, 0, -26, 0, 0, 0, 0, 0, -25, 10, 4, 0, 0, -24, 0, 0, 0, 0,
+    -23, 40, 31, 17, 0, 2, -22, 0, 4, 0, 0, -21, 4, 0, 0, -20, 0, 0, 0, -19, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -18, 0, 0, 0, 0, 0, 0, 0, -17, 25, 0, 0, 0, 0, 0,
+    0, 5, 0, 0, 0, -16, 8, 0, 0, 0, 0, 0, 0, -15, 0, 0, 0, 0, -14, 20, 0, 0, 0, 0,
+    9, 0, 0, 0, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, -12, 8, 0, 0, 0, 0, 0, 0, -11, 0,
+    -10);
+
 
 procedure handler_;
 begin
@@ -172,21 +174,20 @@
     handleTail()
 end;
 
-const handlers: array[0..30] of PHandler = (@handler__UNKNOWN_, @handler_L, @handler_MS, @handler_S,
-    @handler_SL, @handler_L, @handler_S, @handler_, @handler_, @handler_MS,
-    @handler_MS, @handler_i, @handler_MS, @handler_L, @handler_S, @handler_SL,
-    @handler_MS, @handler_SMS, @handler_, @handler_S, @handler_MS, @handler_MS,
-    @handler_MS, @handler_L, @handler_ML, @handler__i, @handler_SMS, @handler_SL,
-    @handler_SL, @handler_MS, @handler_S);
-const net2cmd: array[0..30] of TCmdType = (cmd_WARNING, cmd_WARNING,
+const handlers: array[0..32] of PHandler = (@handler__UNKNOWN_, @handler_L,
+    @handler_MS, @handler_S, @handler_SL, @handler_L, @handler_S, @handler_,
+    @handler_, @handler_MS, @handler_S, @handler_MS, @handler_MS, @handler_i,
+    @handler_MS, @handler_L, @handler_S, @handler_SL, @handler_MS, @handler_SMS,
+    @handler_, @handler_S, @handler_MS, @handler_MS, @handler_MS, @handler_L,
+    @handler_ML, @handler__i, @handler_SMS, @handler_SL, @handler_SL, @handler_MS,
+    @handler_S);
+const net2cmd: array[0..32] of TCmdType = (cmd_WARNING, cmd_WARNING,
     cmd_TEAM_COLOR, cmd_TEAM_ACCEPTED, cmd_SERVER_VARS, cmd_SERVER_MESSAGE,
-    cmd_SERVER_AUTH, cmd_RUN_GAME, cmd_ROUND_FINISHED, cmd_ROOMS, cmd_ROOM,
-    cmd_PROTO, cmd_PING, cmd_NOTICE, cmd_NICK, cmd_LOBBY_LEFT, cmd_LOBBY_JOINED,
-    cmd_LEFT, cmd_KICKED, cmd_JOINING, cmd_JOINED, cmd_INFO, cmd_HH_NUM, cmd_ERROR,
-    cmd_EM, cmd_CONNECTED, cmd_CLIENT_FLAGS, cmd_CHAT, cmd_BYE, cmd_BANLIST,
-    cmd_ASKPASSWORD);
-
-
+    cmd_SERVER_AUTH, cmd_RUN_GAME, cmd_ROUND_FINISHED, cmd_ROOM_UPD, cmd_ROOM_DEL,
+    cmd_ROOM_ADD, cmd_ROOMS, cmd_PROTO, cmd_PING, cmd_NOTICE, cmd_NICK,
+    cmd_LOBBY_LEFT, cmd_LOBBY_JOINED, cmd_LEFT, cmd_KICKED, cmd_JOINING, cmd_JOINED,
+    cmd_INFO, cmd_HH_NUM, cmd_ERROR, cmd_EM, cmd_CONNECTED, cmd_CLIENT_FLAGS,
+    cmd_CHAT, cmd_BYE, cmd_BANLIST, cmd_ASKPASSWORD);
 
 // end of generated stuff
 procedure handleTail;
--- a/hedgewars/uFLNetProtocol.pas	Sat Nov 21 23:56:41 2015 +0300
+++ b/hedgewars/uFLNetProtocol.pas	Sun Nov 22 18:06:32 2015 +0300
@@ -128,7 +128,7 @@
 
 procedure handler_LOBBY_JOINED_s(var s: TCmdParamS);
 begin
-    if s.str1 = 'qmlfrontend' then 
+    if s.str1 = 'qmlfrontend' then
     begin
         sendUI(mtMoveToLobby, nil, 0);
         sendNet('LIST');
@@ -164,44 +164,46 @@
 begin
 end;
 
-type TRoomAction = (raUnknown, raAdd, raUpdate, raRemove);
-const raRoomInfoLength: array[TRoomAction] of integer = (1, 9, 10, 1);
-const raRoomAction: array[TRoomAction] of TMessageType = (mtAddRoom, mtAddRoom, mtUpdateRoom, mtRemoveRoom);
 var roomInfo: string;
     roomLinesCount: integer;
-    roomAction: TRoomAction;
 
-procedure handler_ROOM(var p: TCmdParam);
+procedure handler_ROOM_ADD(var p: TCmdParam);
 begin
     roomInfo:= '';
-    roomLinesCount:= 0;
-    roomAction:= raUnknown
+    roomLinesCount:= 0
 end;
 
-procedure handler_ROOM_s(var s: TCmdParamS);
+procedure handler_ROOM_ADD_s(var s: TCmdParamS);
 begin
-    if roomAction = raUnknown then
+    roomInfo:= roomInfo + s.str1 + #10;
+    inc(roomLinesCount);
+
+    if roomLinesCount = 9 then
     begin
-        if s.str1 = 'ADD' then
-            roomAction:= raAdd
-        else
-            if s.str1 = 'UPD' then
-                roomAction:= raUpdate
-            else
-                if s.str1 = 'DEL' then
-                    roomAction:= raRemove
-    end
-    else begin
-        roomInfo:= roomInfo + s.str1 + #10;
-        inc(roomLinesCount);
+        sendUI(mtAddRoom, @roomInfo[1], length(roomInfo) - 1);
+        roomInfo:= '';
+        roomLinesCount:= 0
+    end;
+end;
+
+procedure handler_ROOM_DEL(var p: TCmdParamS);
+begin
+    sendUI(mtRemoveRoom, @p.str1[1], length(p.str1));
+end;
 
-        if roomLinesCount = raRoomInfoLength[roomAction] then
-        begin
-            sendUI(raRoomAction[roomAction], @roomInfo[1], length(roomInfo) - 1);
-            roomLinesCount:= 0;
-            roomInfo:= ''
-        end;
-    end;
+procedure handler_ROOM_UPD(var p: TCmdParam);
+begin
+    roomInfo:= '';
+    roomLinesCount:= 0
+end;
+
+procedure handler_ROOM_UPD_s(var s: TCmdParamS);
+begin
+    roomInfo:= roomInfo + s.str1 + #10;
+    inc(roomLinesCount);
+
+    if roomLinesCount = 10 then
+        sendUI(mtUpdateRoom, @roomInfo[1], length(roomInfo) - 1);
 end;
 
 procedure handler_ROOMS(var p: TCmdParam);
@@ -271,8 +273,10 @@
     PHandler(@handler_LEFT_s), PHandler(@handler_LOBBY_JOINED),
     PHandler(@handler_LOBBY_JOINED_s), PHandler(@handler_LOBBY_LEFT),
     PHandler(@handler_NICK), PHandler(@handler_NOTICE), PHandler(@handler_PING),
-    PHandler(@handler_PING_s), PHandler(@handler_PROTO), PHandler(@handler_ROOM),
-    PHandler(@handler_ROOM_s), PHandler(@handler_ROOMS), PHandler(@handler_ROOMS_s),
+    PHandler(@handler_PING_s), PHandler(@handler_PROTO), PHandler(@handler_ROOMS),
+    PHandler(@handler_ROOMS_s), PHandler(@handler_ROOM_ADD),
+    PHandler(@handler_ROOM_ADD_s), PHandler(@handler_ROOM_DEL),
+    PHandler(@handler_ROOM_UPD), PHandler(@handler_ROOM_UPD_s),
     PHandler(@handler_ROUND_FINISHED), PHandler(@handler_RUN_GAME),
     PHandler(@handler_SERVER_AUTH), PHandler(@handler_SERVER_MESSAGE),
     PHandler(@handler_SERVER_VARS), PHandler(@handler_TEAM_ACCEPTED),
@@ -303,7 +307,7 @@
     begin
         isInRoom:= false;
         s:= 'PART';
-        if length(msg) > 0 then 
+        if length(msg) > 0 then
             s:= s + #10 + msg;
         sendNet(s);
         sendUI(mtMoveToLobby, nil, 0);
--- a/hedgewars/uFLNetTypes.pas	Sat Nov 21 23:56:41 2015 +0300
+++ b/hedgewars/uFLNetTypes.pas	Sun Nov 22 18:06:32 2015 +0300
@@ -6,10 +6,10 @@
     cmd_ERROR, cmd_HH_NUM, cmd_HH_NUM_s, cmd_INFO, cmd_INFO_s, cmd_JOINED,
     cmd_JOINED_s, cmd_JOINING, cmd_KICKED, cmd_LEFT, cmd_LEFT_s, cmd_LOBBY_JOINED,
     cmd_LOBBY_JOINED_s, cmd_LOBBY_LEFT, cmd_NICK, cmd_NOTICE, cmd_PING, cmd_PING_s,
-    cmd_PROTO, cmd_ROOM, cmd_ROOM_s, cmd_ROOMS, cmd_ROOMS_s, cmd_ROUND_FINISHED,
-    cmd_RUN_GAME, cmd_SERVER_AUTH, cmd_SERVER_MESSAGE, cmd_SERVER_VARS,
-    cmd_TEAM_ACCEPTED, cmd_TEAM_COLOR, cmd_TEAM_COLOR_s, cmd_WARNING);
-
+    cmd_PROTO, cmd_ROOMS, cmd_ROOMS_s, cmd_ROOM_ADD, cmd_ROOM_ADD_s, cmd_ROOM_DEL,
+    cmd_ROOM_UPD, cmd_ROOM_UPD_s, cmd_ROUND_FINISHED, cmd_RUN_GAME, cmd_SERVER_AUTH,
+    cmd_SERVER_MESSAGE, cmd_SERVER_VARS, cmd_TEAM_ACCEPTED, cmd_TEAM_COLOR,
+    cmd_TEAM_COLOR_s, cmd_WARNING);
 
     type TCmdParam = packed record
         cmd: TCmdType;
--- a/tools/protocolParser.hs	Sat Nov 21 23:56:41 2015 +0300
+++ b/tools/protocolParser.hs	Sun Nov 22 18:06:32 2015 +0300
@@ -89,7 +89,9 @@
         , cmd2 "SERVER_VARS" SS LS
         , cmd2 "BYE" SS LS
         , cmd1 "INFO" $ Many [SS]
-        , cmd1 "ROOM" $ Many [SS]
+        , cmd1 "ROOM~ADD" $ Many [SS]
+        , cmd1 "ROOM~UPD" $ Many [SS]
+        , cmd1 "ROOM~DEL" SS
         , cmd1 "ROOMS" $ Many [SS]
         , cmd "KICKED" []
         , cmd "RUN_GAME" []
@@ -127,7 +129,7 @@
         maybeMerge c [] = PTPrefix [c] []
         cmdLeaf ([(c, hwc:assocs1)], assocs2)
             | null assocs1 = PTPrefix [c] [hwc] : map buildsub assocs2
-            | otherwise = [buildsub (c, assocs1)] ++ [PTPrefix [] [hwc]] ++ map buildsub assocs2
+            | otherwise = error "not supported" --[buildsub (c, assocs1)] ++ [PTPrefix [] [hwc]] ++ map buildsub assocs2
 
 dumpTree = vcat . map dt
     where
@@ -137,6 +139,7 @@
 renderArrays (letters, commands, handlers) = vcat $ punctuate (char '\n') [grr, cmds, l, s, c, bodies, structs, realHandlers, realHandlersArray]
     where
         maybeQuotes "$" = text "#0"
+        maybeQuotes "~" = text "#10"
         maybeQuotes s = if null $ tail s then quotes $ text s else text s
         l = text "const letters: array[0.." <> (int $ length letters - 1) <> text "] of char = "
             <> parens (hsep . punctuate comma $ map maybeQuotes letters) <> semi