From d8ec2e6c3a577e048a3a278ae5ef96227c4398a7 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 8 Nov 2021 12:22:51 +0000 Subject: [PATCH] Extract parser into its own submodule --- src/main.rs | 6 +++--- src/parse.rs | 5 +++++ src/parse/commands.rs | 34 +++++++++++++++++++++++++++++++++ src/{ => parse}/parser.rs | 40 +-------------------------------------- src/parser/commands.rs | 7 ------- src/parser/task.rs | 7 ------- 6 files changed, 43 insertions(+), 56 deletions(-) create mode 100644 src/parse.rs create mode 100644 src/parse/commands.rs rename src/{ => parse}/parser.rs (69%) delete mode 100644 src/parser/commands.rs delete mode 100644 src/parser/task.rs diff --git a/src/main.rs b/src/main.rs index 03308b1..4ac6e0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); diff --git a/src/parse.rs b/src/parse.rs new file mode 100644 index 0000000..1d571b0 --- /dev/null +++ b/src/parse.rs @@ -0,0 +1,5 @@ +mod commands; +mod parser; + +pub use commands::parse as parse_commands; +pub use parser::{ParseError, ParseResult, Parser}; diff --git a/src/parse/commands.rs b/src/parse/commands.rs new file mode 100644 index 0000000..0ed21df --- /dev/null +++ b/src/parse/commands.rs @@ -0,0 +1,34 @@ +use crate::commands::Command; + +use super::{ParseResult, Parser}; + +pub fn parse(p: &mut Parser<'_>) -> ParseResult> { + 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 { + 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") + } +} diff --git a/src/parser.rs b/src/parse/parser.rs similarity index 69% rename from src/parser.rs rename to src/parse/parser.rs index 18aefc9..87f5f2f 100644 --- a/src/parser.rs +++ b/src/parse/parser.rs @@ -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 { } } -type ParseResult = Result; +pub type ParseResult = Result; #[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> { - // 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 { - // 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") - // } - // } } diff --git a/src/parser/commands.rs b/src/parser/commands.rs deleted file mode 100644 index e3766e9..0000000 --- a/src/parser/commands.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::commands::Command; - -use super::{ParseResult, Parser}; - -pub fn parse(p: &mut Parser<'_>) -> ParseResult> { - todo!() -} diff --git a/src/parser/task.rs b/src/parser/task.rs deleted file mode 100644 index a2f2e23..0000000 --- a/src/parser/task.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::commands::Task; - -use super::{ParseResult, Parser}; - -fn parse_task(p: &mut Parser<'_>) -> ParseResult { - todo!() -}