gameServer2/src/protocol/lexer.rs
author unc0rr
Fri, 06 Jan 2017 01:00:21 +0300
changeset 12131 a4d22f197bd2
permissions -rw-r--r--
Still trying to make parser work
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12131
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
     1
pub type Spanned<Tok, Loc, Error> = Result<(Loc, Tok, Loc), Error>;
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
     2
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
     3
#[derive(Debug)]
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
     4
pub enum Tok {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
     5
    Linefeed,
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
     6
}
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
     7
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
     8
#[derive(Debug)]
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
     9
pub enum LexicalError {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    10
    // Not possible
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    11
}
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    12
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    13
use std::str::CharIndices;
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    14
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    15
pub struct Lexer<'input> {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    16
    chars: CharIndices<'input>,
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    17
}
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    18
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    19
impl<'input> Lexer<'input> {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    20
    pub fn new(input: &'input str) -> Self {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    21
        Lexer { chars: input.char_indices() }
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    22
    }
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    23
}
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    24
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    25
impl<'input> Iterator for Lexer<'input> {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    26
    type Item = Spanned<Tok, usize, LexicalError>;
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    27
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    28
    fn next(&mut self) -> Option<Self::Item> {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    29
        loop {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    30
            match self.chars.next() {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    31
                Some((i, '\n')) => return Some(Ok((i, Tok::Linefeed, i+1))),
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    32
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    33
                None => return None, // End of file
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    34
                _ => continue, // Comment; skip this character
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    35
            }
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    36
        }
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    37
    }
a4d22f197bd2 Still trying to make parser work
unc0rr
parents:
diff changeset
    38
}