rust/hedgewars-engine-messages/src/parser.rs
author unC0Rr
Wed, 14 Nov 2018 16:58:22 +0100
changeset 14216 0f7b92397b9e
parent 14215 1c55c54013c4
child 14219 77b95406631e
permissions -rw-r--r--
Parse some synced messages, tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14216
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
     1
use nom::{*, Err::Error};
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     2
use std::str;
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     3
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     4
use super::messages::{*, EngineMessage::*, UnsyncedEngineMessage::*, SyncedEngineMessage::*, ConfigEngineMessage::*, KeystrokeAction::*};
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     5
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
     6
14216
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
     7
macro_rules! eof_slice (
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
     8
  ($i:expr,) => (
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
     9
    {
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    10
      if ($i).input_len() == 0 {
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    11
        Ok(($i, $i))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    12
      } else {
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    13
        Err(Error(error_position!($i, ErrorKind::Eof::<u32>)))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    14
      }
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    15
    }
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    16
  );
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    17
);
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    18
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    19
named!(unrecognized_message<&[u8], EngineMessage>,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    20
    do_parse!(rest >> (Unknown))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    21
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    22
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    23
named!(string_tail<&[u8], String>, map!(map_res!(rest, str::from_utf8), String::from));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    24
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    25
named!(length_without_timestamp<&[u8], usize>,
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    26
    map_opt!(rest_len, |l| if l > 2 { Some(l - 2) } else { None } )
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    27
);
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    28
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    29
named!(synced_message<&[u8], SyncedEngineMessage>, alt!(
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    30
      do_parse!(tag!("L") >> (Left(Press)))
14216
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    31
      | do_parse!(tag!("l") >> ( Left(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    32
      | do_parse!(tag!("R") >> ( Right(Press) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    33
      | do_parse!(tag!("r") >> ( Right(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    34
      | do_parse!(tag!("U") >> ( Up(Press) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    35
      | do_parse!(tag!("u") >> ( Up(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    36
      | do_parse!(tag!("D") >> ( Down(Press) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    37
      | do_parse!(tag!("d") >> ( Down(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    38
      | do_parse!(tag!("Z") >> ( Precise(Press) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    39
      | do_parse!(tag!("z") >> ( Precise(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    40
      | do_parse!(tag!("A") >> ( Attack(Press) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    41
      | do_parse!(tag!("a") >> ( Attack(Release) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    42
      | do_parse!(tag!("N") >> ( NextTurn ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    43
      | do_parse!(tag!("j") >> ( LongJump ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    44
      | do_parse!(tag!("J") >> ( HighJump ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    45
      | do_parse!(tag!("S") >> ( Switch ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    46
      | do_parse!(tag!(",") >> ( Skip ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    47
      | do_parse!(tag!("1") >> ( Timer(1) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    48
      | do_parse!(tag!("2") >> ( Timer(2) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    49
      | do_parse!(tag!("3") >> ( Timer(3) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    50
      | do_parse!(tag!("4") >> ( Timer(4) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    51
      | do_parse!(tag!("5") >> ( Timer(5) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    52
      | do_parse!(tag!("p") >> x: be_i24 >> y: be_i24 >> ( Put(x, y) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    53
      | do_parse!(tag!("P") >> x: be_i24 >> y: be_i24 >> ( CursorMove(x, y) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    54
      | do_parse!(tag!("f") >> s: string_tail >> ( SyncedEngineMessage::TeamControlLost(s) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    55
      | do_parse!(tag!("g") >> s: string_tail >> ( SyncedEngineMessage::TeamControlGained(s) ))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    56
      /*
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    57
    Slot(u8),
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    58
    SetWeapon(u8),
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    59
          */
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    60
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    61
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    62
named!(unsynced_message<&[u8], UnsyncedEngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    63
      do_parse!(tag!("?") >> (Ping))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    64
    | do_parse!(tag!("!") >> (Ping))
14216
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    65
    | do_parse!(tag!("esay ") >> s: string_tail >> (Say(s)))
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    66
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    67
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    68
named!(config_message<&[u8], ConfigEngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    69
    do_parse!(tag!("C") >> (ConfigRequest))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    70
));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    71
14216
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    72
named!(timestamped_message<&[u8], (SyncedEngineMessage, u16)>,
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    73
    do_parse!(msg: length_value!(length_without_timestamp, terminated!(synced_message, eof_slice!()))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    74
        >> timestamp: be_u16
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    75
        >> ((msg, timestamp))
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    76
    )
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    77
);
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    78
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    79
named!(unwrapped_message<&[u8], EngineMessage>,
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    80
    alt!(
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    81
        map!(timestamped_message, |(m, t)| Synced(m, t as u32))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    82
        | do_parse!(tag!("#") >> (Synced(TimeWrap, 65535)))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    83
        | map!(unsynced_message, |m| Unsynced(m))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    84
        | map!(config_message, |m| Config(m))
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    85
        | unrecognized_message
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    86
));
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    87
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    88
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    89
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    90
named!(length_specifier<&[u8], u16>, alt!(
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    91
    verify!(map!(take!(1), |a : &[u8]| a[0] as u16), |l| l < 64)
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    92
    | map!(take!(2), |a| (a[0] as u16 - 64) * 256 + a[1] as u16 + 64)
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    93
    )
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    94
);
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    95
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    96
named!(empty_message<&[u8], EngineMessage>,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    97
    do_parse!(tag!("\0") >> (Empty))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    98
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    99
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
   100
named!(non_empty_message<&[u8], EngineMessage>,
14216
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
   101
    length_value!(length_specifier, terminated!(unwrapped_message, eof_slice!())));
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   102
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   103
named!(message<&[u8], EngineMessage>, alt!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   104
      empty_message
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   105
    | non_empty_message
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   106
    )
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   107
);
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   108
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   109
named!(pub extract_messages<&[u8], Vec<EngineMessage> >, many0!(complete!(message)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   110
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   111
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   112
fn parse_length() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   113
    assert_eq!(length_specifier(b"\x01"), Ok((&b""[..], 1)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   114
    assert_eq!(length_specifier(b"\x00"), Ok((&b""[..], 0)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   115
    assert_eq!(length_specifier(b"\x3f"), Ok((&b""[..], 63)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   116
    assert_eq!(length_specifier(b"\x40\x00"), Ok((&b""[..], 64)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   117
    assert_eq!(length_specifier(b"\xff\xff"), Ok((&b""[..], 49215)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   118
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   119
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   120
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   121
fn parse_synced_messages() {
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
   122
    assert_eq!(message(b"\x03L\x01\x02"), Ok((&b""[..], Synced(Left(Press), 258))));
14216
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
   123
    assert_eq!(message(b"\x01#"), Ok((&b""[..], Synced(TimeWrap, 65535))));
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   124
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   125
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   126
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   127
fn parse_unsynced_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   128
    assert_eq!(message(b"\x0aesay hello"), Ok((&b""[..], Unsynced(Say(String::from("hello"))))));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   129
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   130
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   131
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   132
fn parse_incorrect_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   133
    assert_eq!(message(b"\x00"), Ok((&b""[..], Empty)));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   134
    assert_eq!(message(b"\x01\x00"), Ok((&b""[..], Unknown)));
14216
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
   135
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
   136
    // garbage after correct message
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
   137
    assert_eq!(message(b"\x04La\x01\x02"), Ok((&b""[..], Unknown)));
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   138
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   139
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   140
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   141
fn parse_config_messages() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   142
    assert_eq!(
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   143
        message(b"\x01C"),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   144
        Ok((
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   145
            &b""[..],
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   146
            Config(ConfigRequest)
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   147
        ))
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   148
    );
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   149
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   150
#[test]
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   151
fn parse_test_general() {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   152
    assert_eq!(string_tail(b"abc"), Ok((&b""[..], String::from("abc"))));
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   153
}