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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2017 Issei Horie
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub fn tokenize(input: &str) -> Vec<String> {

    let mut token: String = String::from("");    
    let mut v: Vec<String> = Vec::new();
    let mut chars = input.chars();
    let mut is_string = false;
    
    loop {
        if is_string == true {
            match chars.next() {
                Some(c)  => {
                    match c {
                        '"' => {
                            if token.is_empty() == false {
                                v.push(token);
                            }
                            token = String::from("");
                            is_string = false;
                        },
                        _ => {
                            token.push(c);
                        }
                    };
                },
                None => {
                    break;
                },
            };
            continue;
        }
        match chars.next() {            
            Some(c) => {
                match c {
                    ' ' => {
                        if token.is_empty() == false {
                            v.push(token);
                        }
                        token = String::from("");
                    },
                    '=' => {
                        if token.is_empty() == false {
                            v.push(token);
                        }
                        token = String::from("=");
                        v.push(token);
                        token = String::from("");
                    },
                    '+' => {
                        if token.is_empty() == false {
                            v.push(token);
                        }
                        token = String::from("+");
                        v.push(token);
                        token = String::from("");
                    },                
                    '"' => {
                        if token.is_empty() == false {
                            v.push(token);
                        }
                        token = String::from("");
                        is_string = true;
                    },
                    '\n' => {
                        if token.is_empty() == false {
                            v.push(token);
                        }
                        break;
                    },
                    _ => {
                        token.push(c);
                    }
                }
            },
            None => {
                if token.is_empty() == false {
                    v.push(token);
                }
                break;
            },
        }
    }
    
    v
}


#[test]
fn tokenize_empty() {
    let expected: Vec<String> = Vec::new();
    let got = tokenize("");
    assert_eq!(got, expected);
}

#[test]
fn tokenize_one_token() {
    {
        let expected: Vec<String> = vec!["cd".to_string()];
        let got = tokenize("cd");
        assert_eq!(got, expected);
    }

    {
        let expected: Vec<String> = vec!["ls".to_string()];
        let got = tokenize("ls\n");
        assert_eq!(got, expected);
    }
}

#[test]
fn tokenize_two_token() {
    let expected: Vec<String> = vec!["cd".to_string(), "directory".to_string()];
    let got = tokenize("cd directory");
    assert_eq!(got, expected);
}

#[test]
fn tokenize_three_token() {
    {
        let expected: Vec<String> = vec![
            "ls".to_string(),
            "-l".to_string(),
            "directory".to_string(),
        ];
        let got = tokenize("ls -l directory");
        assert_eq!(got, expected);
    }

    {
        let expected: Vec<String> = vec![
            "PS1".to_string(),
            "=".to_string(),
            ">".to_string(),
        ];
        let got = tokenize("PS1 = \">\"");
        assert_eq!(got, expected);
    }
}

#[test]
fn tokenize_four_token() {
    {
        let expected: Vec<String> = vec![
            "alias".to_string(),
            "emacs".to_string(),
            "=".to_string(),
            "emacs -nw".to_string()
        ];
        let got = tokenize("alias emacs = \"emacs -nw\"");
        assert_eq!(got, expected);
    }
    {
        let expected: Vec<String> = vec![
            "println".to_string(),
            "(".to_string(),
            "Hello, World!".to_string(),
            ")".to_string(),
        ];
        let got = tokenize("println(\"Hello, World!\")");
    }
}