Still trying to make parser work
authorunc0rr
Fri, 06 Jan 2017 01:00:21 +0300
changeset 12136 a4d22f197bd2
parent 12135 6273f89ab13d
child 12137 1525923cd7e3
Still trying to make parser work
gameServer2/src/protocol/hwprotocol.lalrpop
gameServer2/src/protocol/lexer.rs
gameServer2/src/protocol/messages.rs
gameServer2/src/protocol/mod.rs
gameServer2/src/server/coretypes.rs
--- a/gameServer2/src/protocol/hwprotocol.lalrpop	Thu Jan 05 19:07:01 2017 +0300
+++ b/gameServer2/src/protocol/hwprotocol.lalrpop	Fri Jan 06 01:00:21 2017 +0300
@@ -1,17 +1,40 @@
-use std::str;
+use std::string;
+use std::str::FromStr;
 
 use super::messages::HWProtocolMessage::*;
-use super::messages::HWProtocolMessage;
+use super::messages::*;
 
 grammar;
 
-pub ProtocolMessage: HWProtocolMessage<'input> = {
-    "NICK" <s:Str> => Nick(s),
+pub ProtocolMessage: HWProtocolMessage = {
+    <SpecificMessage> "\n\n",
+};
+
+SpecificMessage: HWProtocolMessage = {
+    "NICK" "\n" <ProtocolString> => Nick(<>),
+    "PONG" => Pong,
+    "PING" => Ping,
+    "PROTO" "\n" <Num32> => Proto(<>),
 };
 
-Str: &'input str = {
-    <s:r"[^\n]\n"> => s,
+Num32: u32 =
+    <Digit*> => number(<>);
+
+ProtocolString: String =
+    <ProtocolChar*> => <>.join("");
+
+ProtocolChar: &'input str =
+    r"[^\n]" => <>;
+
+Digit: u8 = {
+    "0" => 0,
+    "1" => 1,
+    "2" => 2,
+    "3" => 3,
+    "4" => 4,
+    "5" => 5,
+    "6" => 6,
+    "7" => 7,
+    "8" => 8,
+    "9" => 9,
 };
-
-
-//Num32: i32 = <s:r"[0-9]+"> => i32::from_str(s).unwrap();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/protocol/lexer.rs	Fri Jan 06 01:00:21 2017 +0300
@@ -0,0 +1,38 @@
+pub type Spanned<Tok, Loc, Error> = Result<(Loc, Tok, Loc), Error>;
+
+#[derive(Debug)]
+pub enum Tok {
+    Linefeed,
+}
+
+#[derive(Debug)]
+pub enum LexicalError {
+    // Not possible
+}
+
+use std::str::CharIndices;
+
+pub struct Lexer<'input> {
+    chars: CharIndices<'input>,
+}
+
+impl<'input> Lexer<'input> {
+    pub fn new(input: &'input str) -> Self {
+        Lexer { chars: input.char_indices() }
+    }
+}
+
+impl<'input> Iterator for Lexer<'input> {
+    type Item = Spanned<Tok, usize, LexicalError>;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        loop {
+            match self.chars.next() {
+                Some((i, '\n')) => return Some(Ok((i, Tok::Linefeed, i+1))),
+
+                None => return None, // End of file
+                _ => continue, // Comment; skip this character
+            }
+        }
+    }
+}
--- a/gameServer2/src/protocol/messages.rs	Thu Jan 05 19:07:01 2017 +0300
+++ b/gameServer2/src/protocol/messages.rs	Fri Jan 06 01:00:21 2017 +0300
@@ -1,46 +1,49 @@
 use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo};
+use std;
+use std::ops;
+use std::convert::From;
 
-
-pub enum HWProtocolMessage<'a> {
+#[derive(PartialEq, Debug)]
+pub enum HWProtocolMessage {
     // core
     Ping,
     Pong,
-    Quit(Option<&'a str>),
-    //Cmd(&'a str, Vec<&'a str>),
-    Global(&'a str),
-    Watch(&'a str),
+    Quit(Option<String>),
+    //Cmd(String, Vec<String>),
+    Global(String),
+    Watch(String),
     ToggleServerRegisteredOnly,
     SuperPower,
-    Info(&'a str),
+    Info(String),
     // not entered state
-    Nick(&'a str),
+    Nick(String),
     Proto(u32),
-    Password(&'a str, &'a str),
-    Checker(&'a str),
+    Password(String, String),
+    Checker(String),
     // lobby
     List,
-    Chat(&'a str),
-    CreateRoom(&'a str, Option<&'a str>),
-    Join(&'a str, Option<&'a str>),
-    Follow(&'a str),
-    Rnd(Vec<&'a str>),
-    Kick(&'a str),
-    Ban(&'a str, &'a str, u32),
-    BanIP(&'a str, &'a str, u32),
-    BanNick(&'a str, &'a str, u32),
+    Chat(String),
+    CreateRoom(String, Option<String>),
+    Join(String, Option<String>),
+    Follow(String),
+    Rnd(Vec<String>),
+    Kick(String),
+    Ban(String, String, u32),
+    BanIP(String, String, u32),
+    BanNick(String, String, u32),
     BanList,
-    Unban(&'a str),
+    Unban(String),
     SetServerVar(ServerVar),
     GetServerVar,
     RestartServer,
     Stats,
     // in room
-    Part(Option<&'a str>),
+    Part(Option<String>),
     Cfg(GameCfg),
     AddTeam(TeamInfo),
-    RemoveTeam(&'a str),
-    SetHedgehogsNumber(&'a str, u8),
-    SetTeamColor(&'a str, u8),
+    RemoveTeam(String),
+    SetHedgehogsNumber(String, u8),
+    SetTeamColor(String, u8),
     ToggleReady,
     StartGame,
     EngineMessage,
@@ -48,18 +51,31 @@
     ToggleRestrictJoin,
     ToggleRestrictTeams,
     ToggleRegisteredOnly,
-    RoomName(&'a str),
-    Delegate(&'a str),
-    TeamChat(&'a str),
+    RoomName(String),
+    Delegate(String),
+    TeamChat(String),
     MaxTeams(u8),
     Fix,
     Unfix,
-    Greeting(&'a str),
-    CallVote(Option<(&'a str, Option<&'a str>)>),
-    Vote(&'a str),
-    ForceVote(&'a str),
-    Save(&'a str, &'a str),
-    Delete(&'a str, &'a str),
-    SaveRoom(&'a str),
-    LoadRoom(&'a str),
+    Greeting(String),
+    CallVote(Option<(String, Option<String>)>),
+    Vote(String),
+    ForceVote(String),
+    Save(String, String),
+    Delete(String, String),
+    SaveRoom(String),
+    LoadRoom(String),
 }
+
+pub fn number<T: From<u8>
+                + std::default::Default
+                + std::ops::MulAssign
+                + std::ops::AddAssign>
+    (digits: Vec<u8>) -> T {
+    let mut value: T = T::default();
+    for digit in digits {
+        value *= T::from(10);
+        value += T::from(digit);
+    }
+    value
+}
--- a/gameServer2/src/protocol/mod.rs	Thu Jan 05 19:07:01 2017 +0300
+++ b/gameServer2/src/protocol/mod.rs	Fri Jan 06 01:00:21 2017 +0300
@@ -4,6 +4,7 @@
 
 mod messages;
 mod hwprotocol;
+mod lexer;
 
 pub struct FrameDecoder {
     buf: netbuf::Buf,
@@ -24,3 +25,9 @@
         &self.buf[..]
     }
 }
+
+#[test]
+fn testparser() {
+    assert_eq!(messages::HWProtocolMessage::Nick("hey".to_string()),
+               hwprotocol::parse_ProtocolMessage("NICK\nhey\n\n").unwrap());
+}
--- a/gameServer2/src/server/coretypes.rs	Thu Jan 05 19:07:01 2017 +0300
+++ b/gameServer2/src/server/coretypes.rs	Fri Jan 06 01:00:21 2017 +0300
@@ -1,13 +1,16 @@
+#[derive(PartialEq, Debug)]
 pub enum ServerVar {
     MOTDNew(String),
     MOTDOld(String),
     LatestProto(u32),
 }
 
+#[derive(PartialEq, Debug)]
 pub enum GameCfg {
 
 }
 
+#[derive(PartialEq, Debug)]
 pub struct TeamInfo {
     name: String,
     color: u8,
@@ -20,6 +23,7 @@
     hedgehogs: [HedgehogInfo; 8],
 }
 
+#[derive(PartialEq, Debug)]
 pub struct HedgehogInfo {
     name: String,
     hat: String,