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
169
170
171
172
173
174
175
176
177
use std::collections::VecDeque;
use std::{error, fmt};

use super::{Expression, Language};

#[derive(Debug, Clone)]
pub struct ParseError {
    /// The location of the original string where parsing failed.
    pub location: usize,
    /// A message associated with the parse error.
    pub msg: &'static str,
}
impl ParseError {
    fn new(location: usize, msg: &'static str) -> Self {
        Self { location, msg }
    }
}
impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "{} at index {}", self.msg, self.location)
    }
}
impl error::Error for ParseError {
    fn description(&self) -> &str {
        "could not parse expression"
    }
}

pub fn parse(dsl: &Language, inp: &str) -> Result<Expression, ParseError> {
    let s = inp.trim_left();
    let offset = inp.len() - s.len();
    streaming_parse(dsl, s, offset).and_then(move |(di, expr)| {
        if s[di..].chars().all(char::is_whitespace) {
            Ok(expr)
        } else {
            Err(ParseError::new(
                offset + di,
                "expected end of expression, found more tokens",
            ))
        }
    })
}

/// inp must not have leading whitespace. Does not invent.
fn streaming_parse(
    dsl: &Language,
    inp: &str,
    offset: usize, // for good error messages
) -> Result<(usize, Expression), ParseError> {
    let init: Option<Result<(usize, Expression), ParseError>> = None;

    let abstraction = || {
        inp.find('(')
            .and_then(|i| {
                if inp[..i].chars().all(char::is_whitespace) {
                    Some(i + 1)
                } else {
                    None
                }
            })
            .and_then(|di| match inp[di..].find(char::is_whitespace) {
                Some(ndi) if &inp[di..di + ndi] == "lambda" || &inp[di..di + ndi] == "λ" => {
                    Some(di + ndi)
                }
                _ => None,
            })
            .map(|mut di| {
                // skip spaces
                di += inp[di..].chars().take_while(|c| c.is_whitespace()).count();
                // parse body
                let (ndi, body) = streaming_parse(dsl, &inp[di..], offset + di)?;
                di += ndi;
                // check if complete
                inp[di..]
                    .chars()
                    .nth(0)
                    .and_then(|c| if c == ')' { Some(di + 1) } else { None })
                    .ok_or_else(|| ParseError::new(offset + di, "incomplete application"))
                    .map(|di| (di, Expression::Abstraction(Box::new(body))))
            })
    };
    let application = || {
        inp.find('(')
            .and_then(|i| {
                if inp[..i].chars().all(char::is_whitespace) {
                    Some(i + 1)
                } else {
                    None
                }
            })
            .map(|mut di| {
                let mut items = VecDeque::new();
                loop {
                    // parse expr
                    let (ndi, expr) = streaming_parse(dsl, &inp[di..], offset + di)?;
                    items.push_back(expr);
                    di += ndi;
                    // skip spaces
                    di += inp[di..].chars().take_while(|c| c.is_whitespace()).count();
                    // check if complete
                    match inp[di..].chars().nth(0) {
                        None => break Err(ParseError::new(offset + di, "incomplete application")),
                        Some(')') => {
                            di += 1;
                            break if let Some(init) = items.pop_front() {
                                let app = items.into_iter().fold(init, |a, v| {
                                    Expression::Application(Box::new(a), Box::new(v))
                                });
                                Ok((di, app))
                            } else {
                                Err(ParseError::new(offset + di, "empty application"))
                            };
                        }
                        _ => (),
                    }
                }
            })
    };
    let index = || {
        if inp.chars().nth(0) == Some('$') && inp.len() > 1 {
            inp[1..]
                .find(|c: char| c.is_whitespace() || c == ')')
                .and_then(|i| inp[1..1 + i].parse::<usize>().ok().map(|num| (1 + i, num)))
                .map(|(di, num)| Ok((di, Expression::Index(num))))
        } else {
            None
        }
    };
    let invented = || {
        if inp.chars().take(2).collect::<String>() == "#(" {
            Some(1)
        } else {
            None
        }.map(|mut di| {
            let (ndi, expr) = streaming_parse(dsl, &inp[di..], offset + di)?;
            di += ndi;
            if let Some(num) = dsl.invented.iter().position(|&(ref x, _, _)| x == &expr) {
                Ok((di, Expression::Invented(num)))
            } else {
                Err(ParseError::new(
                    offset + di,
                    "invented expr is unfamiliar to context",
                ))
            }
        })
    };
    let primitive = || {
        match inp.find(|c: char| c.is_whitespace() || c == ')') {
            None if !inp.is_empty() => Some(inp.len()),
            Some(next) if next > 0 => Some(next),
            _ => None,
        }.map(|di| {
            if let Some(num) = dsl.primitives
                .iter()
                .position(|&(ref name, _, _)| name == &inp[..di])
            {
                Ok((di, Expression::Primitive(num)))
            } else {
                Err(ParseError::new(offset + di, "unexpected end of expression"))
            }
        })
    };
    // These parsers return None if the expr isn't applicable
    // or Some(Err(..)) if the expr applied but was invalid.
    // Ordering is intentional.
    init.or_else(abstraction)
        .or_else(application)
        .or_else(index)
        .or_else(invented)
        .or_else(primitive)
        .unwrap_or_else(|| {
            Err(ParseError::new(
                offset,
                "could not parse any expression variant",
            ))
        })
}