Start interpreting pest output

This commit is contained in:
Joscha 2021-11-18 21:59:18 +01:00
parent 1e58672e21
commit 6e53446a08
3 changed files with 35 additions and 7 deletions

View file

@ -1,11 +1,8 @@
use std::fs;
use std::path::PathBuf;
use pest::Parser;
use structopt::StructOpt;
use parse::{MyParser, Rule};
mod commands;
mod parse;
@ -18,7 +15,7 @@ pub struct Opt {
fn main() -> anyhow::Result<()> {
let opt = Opt::from_args();
let content = fs::read_to_string(&opt.file)?;
let parsed = MyParser::parse(Rule::file, &content)?.next().unwrap();
println!("{:#?}", parsed);
let commands = parse::parse(&content)?;
println!("{:#?}", commands);
Ok(())
}

View file

@ -1,3 +1,34 @@
use std::result;
use pest::error::Error;
use pest::iterators::Pair;
use pest::Parser;
use crate::commands::{Command, Task};
#[derive(pest_derive::Parser)]
#[grammar = "parse/grammar.pest"]
pub struct MyParser;
#[grammar = "parse/todayfile.pest"]
struct TodayfileParser;
type Result<T> = result::Result<T, Error<Rule>>;
pub fn parse(input: &str) -> Result<Vec<Command>> {
let mut pairs = TodayfileParser::parse(Rule::file, input)?;
let file = pairs.next().unwrap();
let commands = file.into_inner();
commands.map(parse_command).collect()
}
fn parse_command(p: Pair<Rule>) -> Result<Command> {
match p.as_rule() {
Rule::task => parse_task(p).map(Command::Task),
Rule::note => todo!(),
Rule::birthday => todo!(),
_ => unreachable!(),
}
}
fn parse_task(p: Pair<Rule>) -> Result<Task> {
dbg!(p);
todo!()
}