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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::string::ToString;

#[derive(Debug, PartialEq, Eq)]
pub enum Tokens {
    Literal(String),
    // equals (=)
    Eq,
    // square brackets open
    Sbo,
    // curly brackets open
    Cbo,
    // square brakcets close
    Sbc,
    // curly brackets close
    Cbc,
    Hash,
    DoubleQuote,
    SingleQuote,
    LineBreak,
    TripleDoubleQuotes,
    Comma,
}

impl ToString for Tokens {
    fn to_string(&self) -> String {
        let x = match self {
            Tokens::Literal(x) => x.as_str(),
            Tokens::Eq => ("="),
            Tokens::Sbo => ("["),
            Tokens::Sbc => ("]"),
            Tokens::Cbo => ("{"),
            Tokens::Cbc => ("}"),
            Tokens::Hash => ("#"),
            Tokens::Comma => ",",
            Tokens::TripleDoubleQuotes => r#""""#,
            Tokens::DoubleQuote => (r#"""#),
            Tokens::SingleQuote => "'",
            Tokens::LineBreak => "\n",
        };
        String::from(x)
    }
}

pub struct Lexer;

#[macro_use]
macro_rules! push {
    ( $lexemes : expr, $context : expr, $new_token : expr ) => {
        if !$context.is_empty() {
            $lexemes.push(crate::lexer::Tokens::Literal($context));
            $context = String::new();
        }
        $lexemes.push($new_token);
    };
}

impl Lexer {
    pub fn lex(data: Vec<u8>) -> Vec<Tokens> {
        let mut lexemes: Vec<Tokens> = Vec::new();
        let mut peekable = data.into_iter().peekable();
        let mut literal_context = String::new();
        let mut lexing_literal = false;
        while let Some(val) = peekable.next() {
            match val {
                // brackets
                b'[' => {
                    push!(lexemes, literal_context, Tokens::Sbo);
                }
                b']' => {
                    push!(lexemes, literal_context, Tokens::Sbc);
                }
                b'{' => {
                    push!(lexemes, literal_context, Tokens::Cbo);
                }
                b'}' => {
                    push!(lexemes, literal_context, Tokens::Cbc);
                }
                // symbols
                b'=' => {
                    push!(lexemes, literal_context, Tokens::Eq);
                }
                b'#' => {
                    push!(lexemes, literal_context, Tokens::Hash);
                }
                b'"' => {
                    if let Some(b'"') = peekable.peek() {
                        peekable.next();
                        if let Some(b'"') = peekable.peek() {
                            peekable.next();
                            push!(lexemes, literal_context, Tokens::TripleDoubleQuotes);
                        } else {
                            push!(lexemes, literal_context, Tokens::DoubleQuote);
                        }
                    } else {
                        push!(lexemes, literal_context, Tokens::DoubleQuote);
                    }
                    if lexing_literal {
                        lexing_literal = false
                    } else {
                        lexing_literal = true;
                    }
                }
                b'\'' => {
                    push!(lexemes, literal_context, Tokens::SingleQuote);
                }
                b'\\' => {
                    if lexing_literal {
                        if let Some(x) = peekable.next() {
                            literal_context.push(x as char)
                        } else {
                            literal_context.push(val as char);
                        }
                    } else {
                        literal_context.push(val as char);
                    }
                }
                b'\n' => {
                    push!(lexemes, literal_context, Tokens::LineBreak);
                }
                b',' => {
                    push!(lexemes, literal_context, Tokens::Comma);
                }
                b' ' => {
                    if lexing_literal {
                        literal_context.push(val as char)
                    }
                }
                _ => {
                    literal_context.push(val as char);
                }
            };
        }
        lexemes
    }
}