Remove parsing logic
This commit is contained in:
parent
d8ec2e6c3a
commit
f6c249393b
7 changed files with 1 additions and 286 deletions
29
src/main.rs
29
src/main.rs
|
|
@ -1,14 +1,8 @@
|
|||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
|
||||
use structopt::StructOpt;
|
||||
|
||||
use crate::parse::Parser;
|
||||
use crate::source::SourceFiles;
|
||||
|
||||
mod commands;
|
||||
mod parse;
|
||||
mod source;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
pub struct Opt {
|
||||
|
|
@ -18,26 +12,5 @@ pub struct Opt {
|
|||
|
||||
fn main() {
|
||||
let opt = Opt::from_args();
|
||||
|
||||
let mut files = SourceFiles::new();
|
||||
|
||||
let (file, content) = match files.load(&opt.file) {
|
||||
Ok(result) => result,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to load file: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let mut parser = Parser::new(file, content);
|
||||
|
||||
let commands = match parser.parse(parse::parse_commands) {
|
||||
Ok(result) => result,
|
||||
Err(es) => {
|
||||
files.emit_all(&es);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
println!("{:#?}", commands);
|
||||
println!("{:#?}", opt);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
mod commands;
|
||||
mod parser;
|
||||
|
||||
pub use commands::parse as parse_commands;
|
||||
pub use parser::{ParseError, ParseResult, Parser};
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
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")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,107 +0,0 @@
|
|||
use std::cmp::min;
|
||||
|
||||
use codespan_reporting::diagnostic::{Diagnostic, Label};
|
||||
|
||||
use crate::source::{SourceFile, SourceSpan};
|
||||
|
||||
// TODO Add warnings for things like trailing whitespace
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ParseError(SourceSpan, String);
|
||||
|
||||
impl From<&ParseError> for Diagnostic<usize> {
|
||||
fn from(e: &ParseError) -> Self {
|
||||
Self::error()
|
||||
.with_message(&e.1)
|
||||
.with_labels(vec![Label::primary(e.0.file_id(), e.0.range())])
|
||||
}
|
||||
}
|
||||
|
||||
pub type ParseResult<T> = Result<T, ParseError>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Parser<'a> {
|
||||
file: SourceFile,
|
||||
content: &'a str,
|
||||
offset: usize,
|
||||
errors: Vec<ParseError>,
|
||||
}
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
pub fn new(file: SourceFile, content: &'a str) -> Self {
|
||||
Self {
|
||||
file,
|
||||
content,
|
||||
offset: 0,
|
||||
errors: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn peek(&self, amount: usize) -> &'a str {
|
||||
// self.offset is always a valid start since take() ensures it is in the
|
||||
// range 0..self.content.len()
|
||||
let end = min(self.content.len(), self.offset + amount);
|
||||
&self.content[self.offset..end]
|
||||
}
|
||||
|
||||
pub fn peek_rest(&self) -> &'a str {
|
||||
// self.offset is always a valid start since take() ensures it is in the
|
||||
// range 0..self.content.len()
|
||||
&self.content[self.offset..]
|
||||
}
|
||||
|
||||
pub fn peek_line(&self) -> &'a str {
|
||||
if let Some(i) = self.peek_rest().find('\n') {
|
||||
self.peek(i)
|
||||
} else {
|
||||
self.peek_rest()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn at(&self) -> usize {
|
||||
self.offset
|
||||
}
|
||||
|
||||
pub fn at_eof(&self) -> bool {
|
||||
self.offset >= self.content.len()
|
||||
}
|
||||
|
||||
pub fn take(&mut self, amount: usize) {
|
||||
// Ensure the offset always stays in the range 0..self.content.len()
|
||||
self.offset = min(self.content.len(), self.offset + amount);
|
||||
}
|
||||
|
||||
pub fn take_line(&mut self) {
|
||||
self.take(self.peek_line().len() + 1);
|
||||
}
|
||||
|
||||
fn error(&self, at: usize, error: impl ToString) -> ParseError {
|
||||
ParseError(self.file.span(at..at), error.to_string())
|
||||
}
|
||||
|
||||
pub fn uncritical(&mut self, at: usize, error: impl ToString) {
|
||||
self.errors.push(self.error(at, error));
|
||||
}
|
||||
|
||||
pub fn critical<T>(&self, at: usize, error: impl ToString) -> ParseResult<T> {
|
||||
Err(self.error(at, error))
|
||||
}
|
||||
|
||||
pub fn parse<T>(
|
||||
&mut self,
|
||||
f: impl FnOnce(&mut Self) -> ParseResult<T>,
|
||||
) -> Result<T, Vec<ParseError>> {
|
||||
match f(self) {
|
||||
Ok(result) => {
|
||||
if self.errors.is_empty() {
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
self.errors.push(error);
|
||||
}
|
||||
};
|
||||
|
||||
Err(self.errors.split_off(0))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,82 +0,0 @@
|
|||
use std::collections::HashMap;
|
||||
use std::ops::Range;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{fs, io};
|
||||
|
||||
use codespan_reporting::diagnostic::Diagnostic;
|
||||
use codespan_reporting::files::SimpleFiles;
|
||||
use codespan_reporting::term;
|
||||
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SourceFiles {
|
||||
files: SimpleFiles<String, String>,
|
||||
files_by_path: HashMap<PathBuf, usize>,
|
||||
}
|
||||
|
||||
impl SourceFiles {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
files: SimpleFiles::new(),
|
||||
files_by_path: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(&mut self, path: &Path) -> io::Result<(SourceFile, &str)> {
|
||||
let canonical_path = path.canonicalize()?;
|
||||
let file_id = if let Some(&file_id) = self.files_by_path.get(&canonical_path) {
|
||||
file_id
|
||||
} else {
|
||||
let name = path.as_os_str().to_string_lossy().into_owned();
|
||||
let content = fs::read_to_string(path)?;
|
||||
self.files.add(name, content)
|
||||
};
|
||||
|
||||
let content = self.files.get(file_id).unwrap().source() as &str;
|
||||
Ok((SourceFile(file_id), content))
|
||||
}
|
||||
|
||||
pub fn emit_all<'a, T>(&self, diagnostics: &'a [T])
|
||||
where
|
||||
&'a T: Into<Diagnostic<usize>>,
|
||||
{
|
||||
let stderr = StandardStream::stderr(ColorChoice::Auto);
|
||||
let config = term::Config::default();
|
||||
|
||||
for diagnostic in diagnostics {
|
||||
let diagnostic: Diagnostic<usize> = diagnostic.into();
|
||||
term::emit(&mut stderr.lock(), &config, &self.files, &diagnostic)
|
||||
.expect("failed to print errors");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct SourceFile(usize);
|
||||
|
||||
impl SourceFile {
|
||||
pub fn span(&self, range: Range<usize>) -> SourceSpan {
|
||||
SourceSpan {
|
||||
file_id: self.0,
|
||||
start: range.start,
|
||||
end: range.end,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct SourceSpan {
|
||||
file_id: usize,
|
||||
start: usize,
|
||||
end: usize,
|
||||
}
|
||||
|
||||
impl SourceSpan {
|
||||
pub fn file_id(&self) -> usize {
|
||||
self.file_id
|
||||
}
|
||||
|
||||
pub fn range(&self) -> Range<usize> {
|
||||
self.start..self.end
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue