pub mod ast;
pub mod error;
pub mod parser;
pub mod utils;
pub mod warning;
#[cfg(test)]
mod tests;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use nom::types::CompleteStr;
use nom_locate::LocatedSpan;
use crate::parser::ast::Ast;
use crate::parser::error::Errors;
use crate::parser::warning::Warnings;
pub type Span<'a> = LocatedSpan<CompleteStr<'a>>;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Position {
pub line: u32,
pub column: usize,
pub offset: usize,
}
pub fn position<'a>(span: &Span<'a>) -> Position {
Position {
line: span.line,
column: span.get_utf8_column(),
offset: span.offset,
}
}
#[derive(Debug)]
pub struct Parsed {
pub ast: Ast,
pub warnings: Warnings,
}
pub fn parse<'a, P: AsRef<Path>>(path: P) -> Result<Parsed, Errors> {
let path = path.as_ref();
let mut file = File::open(&path).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
let ast = match parser::parse(Span::new(CompleteStr(&content))) {
Ok((_, ast)) => ast,
Err(_) => panic!(),
};
let errors = ast.errors();
let warnings = ast.warnings();
if errors.is_empty() {
Ok(Parsed {
ast,
warnings: Warnings {
path: PathBuf::from(&path),
warnings,
content,
},
})
} else {
Err(Errors {
path: PathBuf::from(&path),
content,
errors,
})
}
}