A fairly major refactor / simplification.

This commit is contained in:
2023-01-16 20:11:06 -08:00
parent 2e82fcf343
commit afcf3c65cd
26 changed files with 800 additions and 1132 deletions

View File

@@ -1,14 +1,16 @@
use codespan_reporting::files::SimpleFiles;
use lalrpop_util::lalrpop_mod;
use logos::Logos;
mod location;
mod simplify;
mod tokens;
lalrpop_mod!(
#[allow(clippy::just_underscores_and_digits)]
parser,
"/syntax/parser.rs"
);
mod ast;
pub mod ast;
pub use crate::syntax::ast::*;
pub use crate::syntax::location::Location;
@@ -17,14 +19,24 @@ pub use crate::syntax::tokens::{LexerError, Token};
use lalrpop_util::ParseError;
#[cfg(test)]
use std::str::FromStr;
use thiserror::Error;
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum ParserError {
#[error("Invalid token")]
InvalidToken(Location),
#[error("Unrecognized EOF")]
UnrecognizedEOF(Location, Vec<String>),
#[error("Unrecognized token")]
UnrecognizedToken(Location, Location, Token, Vec<String>),
#[error("Extra token")]
ExtraToken(Location, Token, Location),
#[error("Lexing failure")]
LexFailure(Location),
#[error("File database error")]
FileDatabaseError(#[from] codespan_reporting::files::Error),
#[error("Read error")]
ReadError(#[from] std::io::Error),
}
impl ParserError {
@@ -62,6 +74,16 @@ impl ParserError {
}
impl Program {
pub fn parse_file(
file_database: &mut SimpleFiles<String, String>,
file_name: &str,
) -> Result<Self, ParserError> {
let file_contents = std::fs::read_to_string(file_name)?;
let file_handle = file_database.add(file_name.to_string(), file_contents);
let file_db_info = file_database.get(file_handle)?;
Program::parse(file_handle, file_db_info.source())
}
pub fn parse(file_idx: usize, buffer: &str) -> Result<Program, ParserError> {
let lexer = Token::lexer(buffer)
.spanned()