summary refs log tree commit diff
path: root/src/main.rs
blob: d499ad99a50165d88ded9d9e95f5244e95a33f80 (plain)
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
pub mod error;

use error::Error;
use std::io;
use std::io::prelude::*;

#[macro_use] extern crate lalrpop_util;

lalrpop_mod!(pub commandline);
lalrpop_mod!(pub path);


pub type Result<T> = std::result::Result<T, Error>;


pub enum Input {
    String(String),
    End,
}

pub struct GenericPath {
    absolute: bool,
    directory: bool,
    components: Vec<GenericPathComponent>,
}

pub enum GenericPathComponent {
    CurrentDirectory,
    ParentDirectory,
    Entry(String),
}


fn main() -> Result<()> {
    std::process::exit(match repl() {
        Ok(()) => 0,
        Err(ref e) => {
            eprintln!("{}", e);
            1
        }
    })
}


fn repl() -> Result<()> {
    println!("Hello, terminal!");

    loop {
        prompt()?;

        let input = read()?;
        match input {
            Input::String(string) => execute(&string)?,
            Input::End => break,
        }
    }

    Ok(())
}


fn prompt() -> Result<()> {
    print!("$ ");
    io::stdout().flush()?;

    Ok(())
}


fn read() -> Result<Input> {
    let mut input = String::new();
    let n_bytes = io::stdin().read_line(&mut input)?;

    if n_bytes == 0 {
        Ok(Input::End)
    } else {
        Ok(Input::String(input))
    }
}


fn execute(input: &str) -> Result<()> {
    let invocation = commandline::InvocationParser::new().parse(input)?;

    println!("{}", input);

    match invocation.as_slice() {
      ["paths", path_list, ..] => {
        println!("{:?}", path_list);
        match path::PathListParser::new().parse(path_list) {
          Ok(parsed_paths) => {
            println!("paths '{:?}'", parsed_paths);
          },
          Err(_) => {
            match path::PathListAllowingEmptyPathsParser::new()
              .parse(path_list)
            {
              Ok(parsed) => {
                println!("path list has empty component");
                println!("{:?}", parsed);
              },
              Err(error) => {
                println!("path list is okay, does not have empty component");
                println!("{:?}", error);
              },
            }
          },
        }
      },
      _ => {
        println!("invocation '{:?}'", invocation);
      }
    }

    Ok(())
}