aboutsummaryrefslogtreecommitdiff
path: root/apps/cassiopeia/src/format/lexer.rs
blob: f062ca4238c13ff1c3fd4e640fe115f897a52ca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! Cassiopeia file lexer

use logos::Logos;

/// A basic line lexer type
///
/// This lexer distinguishes between comments, and keyword lines.  It
/// does not attempt to parse the line specifics.  This is what the
/// content lexer is for.
#[derive(Logos, Debug, PartialEq)]
enum Line {

    #[token("HEADER")]
    Header,

    #[token("START")]
    Start,

    #[token("STOP")]
    Stop,

    #[token("INVOICE")]
    Invoice,
    
    #[regex(r"\w+=[^,$]+[,$]")]
    HeaderData,

    // FIXME: this will have a leading whitespace that we could remove
    // with ^\w, but logos does not support this at the moment
    #[regex(r"[0-9-:+ ]+")]
    Date,

    #[token(" ", logos::skip)]
    Space,
    
    #[error]
    Error,
}


// pub fn test_this() {
//     // let mut lex = Line::lexer("HEADER version=0.0.0,location=Berlin,");
//     let mut lex = Line::lexer("START 2020-11-11 13:00:00+01:00");

//     while let Some(t) = lex.next() {
//         println!("{:?}: {}", t, lex.slice());
//     }
// }


#[test]
fn basic_header() {
    let mut lex = Line::lexer("HEADER version=0.0.0,location=Berlin Lichtenberg,");

    assert_eq!(lex.next(), Some(Line::Header));
    assert_eq!(lex.span(), 0..6);
    assert_eq!(lex.slice(), "HEADER");

    assert_eq!(lex.next(), Some(Line::HeaderData));
    assert_eq!(lex.span(), 7..21);
    assert_eq!(lex.slice(), "version=0.0.0,");

    assert_eq!(lex.next(), Some(Line::HeaderData));
    assert_eq!(lex.span(), 21..49);
    assert_eq!(lex.slice(), "location=Berlin Lichtenberg,");

    assert_eq!(lex.next(), None);
}


#[test]
fn basic_start() {
    let mut lex = Line::lexer("START 2020-11-11 13:00:00+01:00");

    assert_eq!(lex.next(), Some(Line::Start));
    assert_eq!(lex.span(), 0..5);
    assert_eq!(lex.slice(), "START");

    assert_eq!(lex.next(), Some(Line::Date));
    assert_eq!(lex.span(), 5..31);
    assert_eq!(lex.slice(), " 2020-11-11 13:00:00+01:00");

    assert_eq!(lex.next(), None);
}


#[test]
fn basic_stop() {
    let mut lex = Line::lexer("STOP 2020-11-11 13:00:00+01:00");

    assert_eq!(lex.next(), Some(Line::Stop));
    assert_eq!(lex.span(), 0..4);
    assert_eq!(lex.slice(), "STOP");

    assert_eq!(lex.next(), Some(Line::Date));
    assert_eq!(lex.span(), 4..30);
    assert_eq!(lex.slice(), " 2020-11-11 13:00:00+01:00");

    assert_eq!(lex.next(), None);
}


#[test]
fn basic_invoice() {
    let mut lex = Line::lexer("INVOICE 2020-11-11 13:00:00+01:00");

    assert_eq!(lex.next(), Some(Line::Invoice));
    assert_eq!(lex.span(), 0..7);
    assert_eq!(lex.slice(), "INVOICE");

    assert_eq!(lex.next(), Some(Line::Date));
    assert_eq!(lex.span(), 7..33);
    assert_eq!(lex.slice(), " 2020-11-11 13:00:00+01:00");

    assert_eq!(lex.next(), None);
}