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
use crate::parse::ParseErr;

pub fn split(expr: &str) -> Result<Vec<&str>, ParseErr> {
    let mut begin = 0;
    let mut len = 0;
    let mut items = Vec::new();
    let mut string = false;
    let mut escape = false;
    let mut line_comment = false;
    let mut comment = 0;
    while begin + len < expr.len() {
        let c = expr[begin + len..begin + len + 1].chars().next().unwrap();
        if line_comment {
            if c == '\n' {
                line_comment = false;
            }
            begin += 1;
        } else if comment > 0 {
            if begin + len + 1 < expr.len() && &expr[begin + len..begin + len + 2] == "|#" {
                comment -= 1;
                begin += 1;
            } else if begin + len + 1 < expr.len() && &expr[begin + len..begin + len + 2] == "#|" {
                comment += 1;
                begin += 1;
            }
            begin += 1;
        } else if escape {
            len += 1;
            escape = false;
        } else if c == '\\' {
            escape = true;
            len += 1;
        } else if string {
            if c == '"' {
                string = false;
            }
            len += 1;
        } else if c == '"' && !string {
            string = true;
            len += 1;
        } else if c == ';' {
            if len > 0 {
                items.push(&expr[begin..begin + len]);
            }
            line_comment = true;
            begin += len + 1;
            len = 0;
        } else if begin + len + 1 < expr.len() && &expr[begin + len..begin + len + 2] == "#|" {
            if len > 0 {
                items.push(&expr[begin..begin + len]);
            }
            comment = 1;
            begin += len + 2;
            len = 0;
        } else if begin + len + 1 < expr.len() && &expr[begin + len..begin + len + 2] == "|#" {
            return Err(ParseErr::NoCommentStart);
        } else if "()[] \t\n".contains(c) {
            if len > 0 {
                items.push(&expr[begin..begin + len]);
            }
            begin += len;
            if "()[]".contains(c) {
                items.push(&expr[begin..begin + 1]);
            }
            len = 0;
            begin += 1;
        } else if c == '\'' {
            begin += 1;
            items.push("'");
        } else if c == ',' {
            begin += 1;
            items.push(",");
        } else if c == '`' {
            begin += 1;
            items.push("`");
        } else {
            len += 1;
        }
    }
    if string {
        if &expr[begin..begin + 1] == "\"" {
            Err(ParseErr::UnterminatedString(begin))
        } else {
            Err(ParseErr::IncorrectSpacing(begin))
        }
    } else if comment > 0 {
        Err(ParseErr::UnterminatedComment)
    } else {
        if expr.len() > begin && !line_comment {
            items.push(&expr[begin..begin + len]);
        }
        Ok(items)
    }
}

#[cfg(test)]
mod test {
    macro_rules! test {
        ( $input:tt -> $( $output:tt )* ) => {
            assert_eq!(split($input).ok().unwrap(), vec![ $( $output ),* ] as Vec<&str>);
        }
    }

    use super::*;
    #[test]
    pub fn simple_splits() {
        test!("(f a b)" -> "(" "f" "a" "b" ")");
        test!("()()()" -> "(" ")" "(" ")" "(" ")");
        test!("((())" -> "(" "(" "(" ")" ")");
        test!(") abc(" -> ")" "abc" "(");
        test!("abc de (a cfg (b d)) )" -> "abc" "de" "(" "a" "cfg" "(" "b" "d" ")" ")" ")");
        test!("+-123 // <e> (%1 11>1) ?~" -> "+-123" "//" "<e>" "(" "%1" "11>1" ")" "?~");
        test!("(f args ...)" -> "(" "f" "args" "..." ")");
    }

    #[test]
    pub fn string_split() {
        test!("\"\"" -> "\"\"");
        test!("\"abcdef\"" -> "\"abcdef\"");
        test!("(abc \"def\" ghi)" -> "(" "abc" "\"def\"" "ghi" ")");
        test!("(\"(\")" -> "(" "\"(\"" ")");
    }

    #[test]
    pub fn char_escape_and_literals() {
        test!("(\\a" -> "(" "\\a");
        test!("\\'" -> "\\'");
        test!("\\\"" -> "\\\"");
        test!("(abc de (f #\\\\) #\\\") (gh #\\) (#\\i ())" -> "(" "abc" "de" "(" "f" "#\\\\" ")" "#\\\"" ")" "(" "gh" "#\\)" "(" "#\\i" "(" ")" ")");
        test!("#true #f #\\t #\\em #\\tab" -> "#true" "#f" "#\\t" "#\\em" "#\\tab");
    }

    #[test]
    pub fn comments() {
        test!(";abc\ndef;a" -> "def");
        test!(";;; x y z \n a b c \n ;e" -> "a" "b" "c");
        test!("this is an#|~~ inline |#comment" -> "this" "is" "an" "comment");
        test!("[.(.#|.|#.].)" -> "[" "." "(" "." "." "]" "." ")");
        test!("#|comment|#" -> );
        test!("a#| #| c|# d|# f" -> "a" "f");
    }

    #[test]
    pub fn quotes() {
        test!("'a" -> "'" "a");
        test!("`(ab ,c d)" -> "`" "(" "ab" "," "c" "d" ")");
        test!("ab `c" -> "ab" "`" "c");
    }
}