Clean up location handling, which wsa kind of a pain.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use lalrpop_util::lalrpop_mod;
|
||||
use logos::Logos;
|
||||
|
||||
mod token_stream;
|
||||
mod location;
|
||||
mod tokens;
|
||||
lalrpop_mod!(
|
||||
#[allow(clippy::just_underscores_and_digits)]
|
||||
@@ -10,20 +11,64 @@ lalrpop_mod!(
|
||||
mod ast;
|
||||
|
||||
pub use crate::syntax::ast::*;
|
||||
pub use crate::syntax::location::Location;
|
||||
use crate::syntax::parser::ProgramParser;
|
||||
use crate::syntax::token_stream::TokenStream;
|
||||
pub use crate::syntax::token_stream::{LexerError, Location};
|
||||
pub use crate::syntax::tokens::Token;
|
||||
pub use crate::syntax::tokens::{LexerError, Token};
|
||||
use lalrpop_util::ParseError;
|
||||
#[cfg(test)]
|
||||
use std::str::FromStr;
|
||||
|
||||
type ParserError = ParseError<Location, Token, LexerError>;
|
||||
#[derive(Debug)]
|
||||
pub enum ParserError {
|
||||
InvalidToken(Location),
|
||||
UnrecognizedEOF(Location, Vec<String>),
|
||||
UnrecognizedToken(Location, Location, Token, Vec<String>),
|
||||
ExtraToken(Location, Token, Location),
|
||||
LexFailure(Location),
|
||||
}
|
||||
|
||||
impl ParserError {
|
||||
fn convert(file_idx: usize, err: ParseError<usize, Token, LexerError>) -> Self {
|
||||
match err {
|
||||
ParseError::InvalidToken { location } => {
|
||||
ParserError::InvalidToken(Location::new(file_idx, location))
|
||||
}
|
||||
ParseError::UnrecognizedEOF { location, expected } => {
|
||||
ParserError::UnrecognizedEOF(Location::new(file_idx, location), expected)
|
||||
}
|
||||
ParseError::UnrecognizedToken {
|
||||
token: (start, token, end),
|
||||
expected,
|
||||
} => ParserError::UnrecognizedToken(
|
||||
Location::new(file_idx, start),
|
||||
Location::new(file_idx, end),
|
||||
token,
|
||||
expected,
|
||||
),
|
||||
ParseError::ExtraToken {
|
||||
token: (start, token, end),
|
||||
} => ParserError::ExtraToken(
|
||||
Location::new(file_idx, start),
|
||||
token,
|
||||
Location::new(file_idx, end),
|
||||
),
|
||||
ParseError::User { error } => match error {
|
||||
LexerError::LexFailure(offset) => {
|
||||
ParserError::LexFailure(Location::new(file_idx, offset))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Program {
|
||||
pub fn parse(file_idx: usize, buffer: &str) -> Result<Program, ParserError> {
|
||||
let lexer = TokenStream::new(file_idx, buffer);
|
||||
ProgramParser::new().parse(lexer)
|
||||
let lexer = Token::lexer(buffer)
|
||||
.spanned()
|
||||
.map(|(token, range)| (range.start, token, range.end));
|
||||
ProgramParser::new()
|
||||
.parse(file_idx, lexer)
|
||||
.map_err(|e| ParserError::convert(file_idx, e))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,23 +89,23 @@ fn order_of_operations() {
|
||||
Program::from_str(muladd1).unwrap(),
|
||||
Program {
|
||||
statements: vec![Statement::Binding(
|
||||
Location::InFile(testfile, 0),
|
||||
Location::new(testfile, 0),
|
||||
"x".to_string(),
|
||||
Expression::Primitive(
|
||||
Location::InFile(testfile, 6),
|
||||
Location::new(testfile, 6),
|
||||
"+".to_string(),
|
||||
vec![
|
||||
Expression::Value(Location::InFile(testfile, 4), Value::Number(None, 1)),
|
||||
Expression::Value(Location::new(testfile, 4), Value::Number(None, 1)),
|
||||
Expression::Primitive(
|
||||
Location::InFile(testfile, 10),
|
||||
Location::new(testfile, 10),
|
||||
"*".to_string(),
|
||||
vec![
|
||||
Expression::Value(
|
||||
Location::InFile(testfile, 8),
|
||||
Location::new(testfile, 8),
|
||||
Value::Number(None, 2),
|
||||
),
|
||||
Expression::Value(
|
||||
Location::InFile(testfile, 12),
|
||||
Location::new(testfile, 12),
|
||||
Value::Number(None, 3),
|
||||
),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user