Start interpreting pest output
This commit is contained in:
parent
1e58672e21
commit
6e53446a08
3 changed files with 35 additions and 7 deletions
|
|
@ -1,11 +1,8 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use pest::Parser;
|
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
use parse::{MyParser, Rule};
|
|
||||||
|
|
||||||
mod commands;
|
mod commands;
|
||||||
mod parse;
|
mod parse;
|
||||||
|
|
||||||
|
|
@ -18,7 +15,7 @@ pub struct Opt {
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let opt = Opt::from_args();
|
let opt = Opt::from_args();
|
||||||
let content = fs::read_to_string(&opt.file)?;
|
let content = fs::read_to_string(&opt.file)?;
|
||||||
let parsed = MyParser::parse(Rule::file, &content)?.next().unwrap();
|
let commands = parse::parse(&content)?;
|
||||||
println!("{:#?}", parsed);
|
println!("{:#?}", commands);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
35
src/parse.rs
35
src/parse.rs
|
|
@ -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)]
|
#[derive(pest_derive::Parser)]
|
||||||
#[grammar = "parse/grammar.pest"]
|
#[grammar = "parse/todayfile.pest"]
|
||||||
pub struct MyParser;
|
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!()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue