Extract parser into its own submodule

This commit is contained in:
Joscha 2021-11-08 12:22:51 +00:00
parent dc24bbc883
commit d8ec2e6c3a
6 changed files with 43 additions and 56 deletions

View file

@ -3,11 +3,11 @@ use std::process;
use structopt::StructOpt;
use crate::parser::Parser;
use crate::parse::Parser;
use crate::source::SourceFiles;
mod commands;
mod parser;
mod parse;
mod source;
#[derive(Debug, StructOpt)]
@ -31,7 +31,7 @@ fn main() {
let mut parser = Parser::new(file, content);
let commands = match parser.parse(parser::commands::parse) {
let commands = match parser.parse(parse::parse_commands) {
Ok(result) => result,
Err(es) => {
files.emit_all(&es);

5
src/parse.rs Normal file
View file

@ -0,0 +1,5 @@
mod commands;
mod parser;
pub use commands::parse as parse_commands;
pub use parser::{ParseError, ParseResult, Parser};

34
src/parse/commands.rs Normal file
View file

@ -0,0 +1,34 @@
use crate::commands::Command;
use super::{ParseResult, Parser};
pub fn parse(p: &mut Parser<'_>) -> ParseResult<Vec<Command>> {
let mut commands = vec![];
skip_empty_lines(p);
while !p.at_eof() {
// Commands consume all their trailing lines, including empty ones
commands.push(parse_command(p)?);
}
Ok(commands)
}
fn skip_empty_lines(p: &mut Parser<'_>) {
while p.peek_line().chars().all(|c| c.is_whitespace()) {
p.take_line();
}
}
fn parse_command(p: &mut Parser<'_>) -> ParseResult<Command> {
let rest = p.peek_rest();
if rest.starts_with("TASK") {
todo!() // TODO Implement parsing TASK command
} else if rest.starts_with("NOTE") {
todo!() // TODO Implement parsing NOTE command
} else if rest.starts_with("BIRTHDAY") {
todo!() // TODO Implement parsing BIRTHDAY command
} else {
p.critical(p.at(), "Expected command")
}
}

View file

@ -4,9 +4,6 @@ use codespan_reporting::diagnostic::{Diagnostic, Label};
use crate::source::{SourceFile, SourceSpan};
pub mod commands;
mod task;
// TODO Add warnings for things like trailing whitespace
#[derive(Debug)]
@ -20,7 +17,7 @@ impl From<&ParseError> for Diagnostic<usize> {
}
}
type ParseResult<T> = Result<T, ParseError>;
pub type ParseResult<T> = Result<T, ParseError>;
#[derive(Debug)]
pub struct Parser<'a> {
@ -107,39 +104,4 @@ impl<'a> Parser<'a> {
Err(self.errors.split_off(0))
}
// fn parse_commands(&mut self) -> ParseResult<Vec<Command>> {
// let mut commands = vec![];
// self.skip_empty_lines();
// while !self.at_eof() {
// commands.push(self.parse_command()?);
// }
// if !self.at_eof() {
// self.uncritical(self.offset, "Expected EOF");
// }
// Ok(commands)
// }
// fn skip_empty_lines(&mut self) {
// while self.peek_line().chars().all(|c| c.is_whitespace()) {
// self.take_line();
// }
// }
// fn parse_command(&mut self) -> ParseResult<Command> {
// let rest = self.peek_rest();
// if rest.starts_with("TASK") {
// let task = self.parse_task()?;
// Ok(Command::Task(task))
// } else if rest.starts_with("NOTE") {
// todo!() // TODO Implement parsing NOTE command
// } else if rest.starts_with("BIRTHDAY") {
// todo!() // TODO Implement parsing BIRTHDAY command
// } else {
// self.critical(self.offset, "Expected command")
// }
// }
}

View file

@ -1,7 +0,0 @@
use crate::commands::Command;
use super::{ParseResult, Parser};
pub fn parse(p: &mut Parser<'_>) -> ParseResult<Vec<Command>> {
todo!()
}

View file

@ -1,7 +0,0 @@
use crate::commands::Task;
use super::{ParseResult, Parser};
fn parse_task(p: &mut Parser<'_>) -> ParseResult<Task> {
todo!()
}