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
extern crate lalrpop_util;
extern crate reproto_core as core;
extern crate reproto_ast as ast;
extern crate reproto_path_lexer as path_lexer;

mod parser;

use core::errors::{Error, Result};
use ast::PathSpec;

pub fn parse(input: &str) -> Result<PathSpec> {
    use self::path_lexer::Error::*;
    use lalrpop_util::ParseError::*;

    let lexer = path_lexer::path_lex(input);

    match parser::parse_path(lexer) {
        Ok(file) => Ok(file),
        Err(e) => match e {
            InvalidToken { location } => Err(Error::new(format!("Invalid token at char #{}", location))),
            UnrecognizedToken { token, expected } => {
                Err(Error::new(format!("Syntax error, got: {:?}, expected: {:?}", token, expected)))
            }
            User { error } => match error {
                Unexpected { pos } => {
                    return Err(Error::new(format!("Unexpected input at char #{}", pos)));
                }
            },
            _ => Err(Error::new("Parse error")),
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_path() {
        let spec = parse("/foo\\//{bar}_baz\\{\\}").unwrap();
        println!("spec = {:?}", spec);
    }
}