]> Witch of Git - ivy/blob - src/main.rs
Parse S-expressions into the abstract syntax tree
[ivy] / src / main.rs
1 use ess::{self, parser::ParseError, Sexp};
2 use std::io::{self, Read};
3
4 mod ast;
5 use ast::Ast;
6
7 fn parse(input: &str) -> Result<Sexp, ParseError> {
8 let (sexp, rest) = ess::parse_one(input)?;
9 if !rest.trim().is_empty() {
10 return Err(ParseError::Unexpected('\0', sexp.get_loc().1));
11 }
12 Ok(sexp)
13 }
14
15 fn main() -> io::Result<()> {
16 let stdin = std::io::stdin();
17 let mut text = String::new();
18 stdin.lock().read_to_string(&mut text)?;
19 let sexp = match parse(&text) {
20 Ok(item) => item,
21 Err(err) => {
22 println!("{:?}", err);
23 std::process::exit(1);
24 }
25 };
26 let ast = match Ast::parse(&sexp) {
27 Ok(ast) => ast,
28 Err(err) => {
29 println!("{}", err);
30 std::process::exit(1);
31 }
32 };
33 println!("{:#?}", ast);
34 Ok(())
35 }