run cargo fmt

This commit is contained in:
Skgland
2023-08-26 01:29:53 +02:00
committed by Bennet Bleßmann
parent 5585e83fd6
commit b2130c2a48
50 changed files with 10821 additions and 9258 deletions

View File

@@ -258,7 +258,8 @@ impl GenContext {
pub struct OpDesc {
prec: B11,
spec: B8,
#[allow(unused)] padding: B13,
#[allow(unused)]
padding: B13,
}
impl OpDesc {
@@ -417,13 +418,13 @@ pub enum ParserError {
impl ParserError {
pub fn line_and_col_num(&self) -> Option<(usize, usize)> {
match self {
&ParserError::BackQuotedString(line_num, col_num) |
&ParserError::IncompleteReduction(line_num, col_num) |
&ParserError::MissingQuote(line_num, col_num) |
&ParserError::NonPrologChar(line_num, col_num) |
&ParserError::ParseBigInt(line_num, col_num) |
&ParserError::UnexpectedChar(_, line_num, col_num) |
&ParserError::Utf8Error(line_num, col_num) => Some((line_num, col_num)),
&ParserError::BackQuotedString(line_num, col_num)
| &ParserError::IncompleteReduction(line_num, col_num)
| &ParserError::MissingQuote(line_num, col_num)
| &ParserError::NonPrologChar(line_num, col_num)
| &ParserError::ParseBigInt(line_num, col_num)
| &ParserError::UnexpectedChar(_, line_num, col_num)
| &ParserError::Utf8Error(line_num, col_num) => Some((line_num, col_num)),
_ => None,
}
}
@@ -432,8 +433,12 @@ impl ParserError {
match self {
ParserError::BackQuotedString(..) => atom!("back_quoted_string"),
ParserError::IncompleteReduction(..) => atom!("incomplete_reduction"),
ParserError::InvalidSingleQuotedCharacter(..) => atom!("invalid_single_quoted_character"),
ParserError::IO(e) if e.kind() == ErrorKind::UnexpectedEof => atom!("unexpected_end_of_file"),
ParserError::InvalidSingleQuotedCharacter(..) => {
atom!("invalid_single_quoted_character")
}
ParserError::IO(e) if e.kind() == ErrorKind::UnexpectedEof => {
atom!("unexpected_end_of_file")
}
ParserError::IO(_) => atom!("input_output_error"),
ParserError::LexicalError(_) => atom!("lexical_error"),
ParserError::MissingQuote(..) => atom!("missing_quote"),
@@ -522,9 +527,12 @@ pub enum Fixity {
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct Fixnum {
num: B56,
#[allow(unused)] f: bool,
#[allow(unused)] m: bool,
#[allow(unused)] tag: B6,
#[allow(unused)]
f: bool,
#[allow(unused)]
m: bool,
#[allow(unused)]
tag: B6,
}
impl Fixnum {
@@ -631,7 +639,6 @@ impl Literal {
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VarPtr(Rc<RefCell<Var>>);

View File

@@ -20,7 +20,7 @@ use std::str;
pub struct CharReader<R> {
inner: R,
buf: SmallVec<[u8;32]>,
buf: SmallVec<[u8; 32]>,
pos: usize,
}
@@ -78,7 +78,7 @@ pub trait CharRead {
self.consume(c.len_utf8());
Some(Ok(c))
}
result => result
result => result,
}
}
@@ -161,9 +161,7 @@ impl<R: Read> CharRead for CharReader<R> {
return Some(Ok(c));
}
Err(e) => {
e
}
Err(e) => e,
};
if buf.len() - e.valid_up_to() >= 4 {
@@ -192,13 +190,15 @@ impl<R: Read> CharRead for CharReader<R> {
// the buffer, it will be returned on the next
// loop.
return Some(Err(io::Error::new(io::ErrorKind::InvalidData,
BadUtf8Error { bytes: badbytes })));
return Some(Err(io::Error::new(
io::ErrorKind::InvalidData,
BadUtf8Error { bytes: badbytes },
)));
} else {
if self.pos >= self.buf.len() {
return None;
} else if self.buf.len() - self.pos >= 4 {
return match str::from_utf8(&self.buf[self.pos .. e.valid_up_to()]) {
return match str::from_utf8(&self.buf[self.pos..e.valid_up_to()]) {
Ok(s) => {
let mut chars = s.chars();
let c = chars.next().unwrap();
@@ -206,10 +206,12 @@ impl<R: Read> CharRead for CharReader<R> {
Some(Ok(c))
}
Err(e) => {
let badbytes = self.buf[self.pos .. e.valid_up_to()].to_vec();
let badbytes = self.buf[self.pos..e.valid_up_to()].to_vec();
Some(Err(io::Error::new(io::ErrorKind::InvalidData,
BadUtf8Error { bytes: badbytes })))
Some(Err(io::Error::new(
io::ErrorKind::InvalidData,
BadUtf8Error { bytes: badbytes },
)))
}
};
} else {
@@ -223,7 +225,7 @@ impl<R: Read> CharRead for CharReader<R> {
let buf_len = self.buf.len();
let mut word = [0u8;4];
let mut word = [0u8; 4];
let word_slice = &mut word[buf_len..4];
match self.inner.read(word_slice) {
@@ -250,7 +252,7 @@ impl<R: Read> CharRead for CharReader<R> {
let c_len = c.len_utf8();
let mut shifted_slice = [0u8; 32];
shifted_slice[0..src_len].copy_from_slice(&self.buf[self.pos .. self.buf.len()]);
shifted_slice[0..src_len].copy_from_slice(&self.buf[self.pos..self.buf.len()]);
self.buf.resize(c_len, 0);
self.buf.extend_from_slice(&shifted_slice[0..src_len]);
@@ -311,7 +313,10 @@ impl<R: Read> Read for CharReader<R> {
}
if !buf.is_empty() {
Err(io::Error::new(ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
Err(io::Error::new(
ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
))
} else {
Ok(())
}
@@ -364,12 +369,14 @@ where
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("CharReader")
.field("reader", &self.inner)
.field("buf", &format_args!("{}/{}", self.buf.capacity() - self.pos, self.buf.len()))
.field(
"buf",
&format_args!("{}/{}", self.buf.capacity() - self.pos, self.buf.len()),
)
.finish()
}
}
#[cfg(test)]
mod tests {
use crate::parser::char_reader::*;

View File

@@ -86,14 +86,14 @@ impl<'a, R: CharRead> Lexer<'a, R> {
pub fn lookahead_char(&mut self) -> Result<char, ParserError> {
match self.reader.peek_char() {
Some(Ok(c)) => Ok(c),
_ => Err(ParserError::unexpected_eof())
_ => Err(ParserError::unexpected_eof()),
}
}
pub fn read_char(&mut self) -> Result<char, ParserError> {
match self.reader.read_char() {
Some(Ok(c)) => Ok(c),
_ => Err(ParserError::unexpected_eof())
_ => Err(ParserError::unexpected_eof()),
}
}
@@ -168,13 +168,15 @@ impl<'a, R: CharRead> Lexer<'a, R> {
match comment_loop() {
Err(e) if e.is_unexpected_eof() => {
return Err(ParserError::IncompleteReduction(self.line_num, self.col_num));
return Err(ParserError::IncompleteReduction(
self.line_num,
self.col_num,
));
}
Err(e) => {
return Err(e);
}
Ok(_) => {
}
Ok(_) => {}
}
if prolog_char!(c) {
@@ -362,7 +364,10 @@ impl<'a, R: CharRead> Lexer<'a, R> {
if hexadecimal_digit_char!(c) {
self.escape_sequence_to_char(|c| hexadecimal_digit_char!(c), 16)
} else {
Err(ParserError::IncompleteReduction(self.line_num, self.col_num))
Err(ParserError::IncompleteReduction(
self.line_num,
self.col_num,
))
}
}
@@ -395,7 +400,10 @@ impl<'a, R: CharRead> Lexer<'a, R> {
},
)
} else {
Err(ParserError::IncompleteReduction(self.line_num, self.col_num))
Err(ParserError::IncompleteReduction(
self.line_num,
self.col_num,
))
}
}
@@ -463,9 +471,12 @@ impl<'a, R: CharRead> Lexer<'a, R> {
.map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)))
.or_else(|_| {
Integer::from_str_radix(&token, 16)
.map(|n| Token::Literal(Literal::Integer(
arena_alloc!(n, &mut self.machine_st.arena)
)))
.map(|n| {
Token::Literal(Literal::Integer(arena_alloc!(
n,
&mut self.machine_st.arena
)))
})
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
})
} else {
@@ -495,9 +506,12 @@ impl<'a, R: CharRead> Lexer<'a, R> {
.map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)))
.or_else(|_| {
Integer::from_str_radix(&token, 8)
.map(|n| Token::Literal(Literal::Integer(
arena_alloc!(n, &mut self.machine_st.arena)
)))
.map(|n| {
Token::Literal(Literal::Integer(arena_alloc!(
n,
&mut self.machine_st.arena
)))
})
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
})
} else {
@@ -527,9 +541,12 @@ impl<'a, R: CharRead> Lexer<'a, R> {
.map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)))
.or_else(|_| {
Integer::from_str_radix(&token, 2)
.map(|n| Token::Literal(Literal::Integer(
arena_alloc!(n, &mut self.machine_st.arena)
)))
.map(|n| {
Token::Literal(Literal::Integer(arena_alloc!(
n,
&mut self.machine_st.arena
)))
})
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
})
} else {
@@ -629,7 +646,10 @@ impl<'a, R: CharRead> Lexer<'a, R> {
fn vacate_with_float(&mut self, mut token: String) -> Result<Token, ParserError> {
self.return_char(token.pop().unwrap());
let n = parse_lossy::<f64, _>(token.as_bytes())?;
Ok(Token::Literal(Literal::from(float_alloc!(n, self.machine_st.arena))))
Ok(Token::Literal(Literal::from(float_alloc!(
n,
self.machine_st.arena
))))
}
fn skip_underscore_in_number(&mut self) -> Result<char, ParserError> {
@@ -675,7 +695,10 @@ impl<'a, R: CharRead> Lexer<'a, R> {
token
.parse::<Integer>()
.map(|n| {
Token::Literal(Literal::Integer(arena_alloc!(n, &mut self.machine_st.arena)))
Token::Literal(Literal::Integer(arena_alloc!(
n,
&mut self.machine_st.arena
)))
})
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
})
@@ -740,17 +763,19 @@ impl<'a, R: CharRead> Lexer<'a, R> {
}
let n = parse_lossy::<f64, _>(token.as_bytes())?;
Ok(Token::Literal(Literal::from(
float_alloc!(n, self.machine_st.arena)
)))
Ok(Token::Literal(Literal::from(float_alloc!(
n,
self.machine_st.arena
))))
} else {
return Ok(self.vacate_with_float(token)?);
}
} else {
let n = parse_lossy::<f64, _>(token.as_bytes())?;
Ok(Token::Literal(Literal::from(
float_alloc!(n, self.machine_st.arena)
)))
Ok(Token::Literal(Literal::from(float_alloc!(
n,
self.machine_st.arena
))))
}
} else {
self.return_char('.');
@@ -761,7 +786,10 @@ impl<'a, R: CharRead> Lexer<'a, R> {
token
.parse::<Integer>()
.map(|n| {
Token::Literal(Literal::Integer(arena_alloc!(n, &mut self.machine_st.arena)))
Token::Literal(Literal::Integer(arena_alloc!(
n,
&mut self.machine_st.arena
)))
})
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
})
@@ -772,7 +800,9 @@ impl<'a, R: CharRead> Lexer<'a, R> {
self.hexadecimal_constant(c).or_else(|e| {
if let ParserError::ParseBigInt(..) = e {
i64::from_str_radix(&token, 10)
.map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)))
.map(|n| {
Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena))
})
.or_else(|_| {
token
.parse::<Integer>()
@@ -794,7 +824,9 @@ impl<'a, R: CharRead> Lexer<'a, R> {
self.octal_constant(c).or_else(|e| {
if let ParserError::ParseBigInt(..) = e {
i64::from_str_radix(&token, 10)
.map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)))
.map(|n| {
Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena))
})
.or_else(|_| {
token
.parse::<Integer>()
@@ -816,7 +848,9 @@ impl<'a, R: CharRead> Lexer<'a, R> {
self.binary_constant(c).or_else(|e| {
if let ParserError::ParseBigInt(..) = e {
i64::from_str_radix(&token, 10)
.map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)))
.map(|n| {
Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena))
})
.or_else(|_| {
token
.parse::<Integer>()
@@ -856,15 +890,16 @@ impl<'a, R: CharRead> Lexer<'a, R> {
.map(|c| Token::Literal(Literal::Fixnum(Fixnum::build_with(c as i64))))
.or_else(|err| {
match err {
ParserError::UnexpectedChar('\'', ..) => {
}
ParserError::UnexpectedChar('\'', ..) => {}
err => return Err(err),
}
self.return_char(c);
i64::from_str_radix(&token, 10)
.map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)))
.map(|n| {
Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena))
})
.or_else(|_| {
token
.parse::<Integer>()
@@ -901,7 +936,10 @@ impl<'a, R: CharRead> Lexer<'a, R> {
token
.parse::<Integer>()
.map(|n| {
Token::Literal(Literal::Integer(arena_alloc!(n, &mut self.machine_st.arena)))
Token::Literal(Literal::Integer(arena_alloc!(
n,
&mut self.machine_st.arena
)))
})
.map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num))
})
@@ -940,11 +978,12 @@ impl<'a, R: CharRead> Lexer<'a, R> {
pub fn scan_for_layout(&mut self) -> Result<bool, ParserError> {
match self.lookahead_char() {
Err(e) => {
Err(e)
}
Err(e) => Err(e),
Ok(c) => {
let mut layout_info = LayoutInfo { inserted: false, more: true };
let mut layout_info = LayoutInfo {
inserted: false,
more: true,
};
let mut cr = Some(c);
loop {
@@ -1054,7 +1093,7 @@ impl<'a, R: CharRead> Lexer<'a, R> {
}
if c == '\u{0}' {
return Err(ParserError::unexpected_eof())
return Err(ParserError::unexpected_eof());
}
self.name_token(c)

View File

@@ -7,13 +7,14 @@ macro_rules! char_class {
#[macro_export]
macro_rules! alpha_char {
($c: expr) => {
(!$c.is_numeric() &&
!$c.is_whitespace() &&
!$c.is_control() &&
!$crate::graphic_token_char!($c) &&
!$crate::layout_char!($c) &&
!$crate::meta_char!($c) &&
!$crate::solo_char!($c)) || $c == '_'
(!$c.is_numeric()
&& !$c.is_whitespace()
&& !$c.is_control()
&& !$crate::graphic_token_char!($c)
&& !$crate::layout_char!($c)
&& !$crate::meta_char!($c)
&& !$crate::solo_char!($c))
|| $c == '_'
};
}

View File

@@ -7,7 +7,6 @@ use crate::parser::ast::*;
use crate::parser::char_reader::*;
use crate::parser::lexer::*;
use std::cell::Cell;
use std::mem;
use std::ops::Neg;
@@ -120,26 +119,17 @@ pub(crate) fn as_partial_string(
}
match &tail {
Term::AnonVar | Term::Var(..) => {
Ok((string, Some(Box::new(tail))))
}
Term::Literal(_, Literal::Atom(atom!("[]"))) => {
Ok((string, None))
}
Term::AnonVar | Term::Var(..) => Ok((string, Some(Box::new(tail)))),
Term::Literal(_, Literal::Atom(atom!("[]"))) => Ok((string, None)),
Term::Literal(_, Literal::String(tail)) => {
string += tail.as_str();
Ok((string, None))
}
_ => {
Ok((string, Some(Box::new(tail))))
}
_ => Ok((string, Some(Box::new(tail)))),
}
}
pub fn get_op_desc(
name: Atom,
op_dir: &CompositeOpDir,
) -> Option<CompositeOpDesc> {
pub fn get_op_desc(name: Atom, op_dir: &CompositeOpDir) -> Option<CompositeOpDesc> {
let mut op_desc = CompositeOpDesc {
pre: 0,
inf: 0,
@@ -409,7 +399,8 @@ impl<'a, R: CharRead> Parser<'a, R> {
}
fn promote_atom_op(&mut self, atom: Atom, priority: usize, assoc: u32) {
self.terms.push(Term::Literal(Cell::default(), Literal::Atom(atom)));
self.terms
.push(Term::Literal(Cell::default(), Literal::Atom(atom)));
self.stack.push(TokenDesc {
tt: TokenType::Term,
priority,
@@ -419,7 +410,9 @@ impl<'a, R: CharRead> Parser<'a, R> {
fn shift(&mut self, token: Token, priority: usize, spec: Specifier) {
let tt = match token {
Token::Literal(Literal::String(s)) if self.lexer.machine_st.flags.double_quotes.is_codes() => {
Token::Literal(Literal::String(s))
if self.lexer.machine_st.flags.double_quotes.is_codes() =>
{
let mut list = Term::Literal(Cell::default(), Literal::Atom(atom!("[]")));
for c in s.as_str().chars().rev() {
@@ -436,7 +429,9 @@ impl<'a, R: CharRead> Parser<'a, R> {
self.terms.push(list);
TokenType::Term
}
Token::Literal(Literal::String(s)) if self.lexer.machine_st.flags.double_quotes.is_chars() => {
Token::Literal(Literal::String(s))
if self.lexer.machine_st.flags.double_quotes.is_chars() =>
{
self.terms.push(Term::CompleteString(Cell::default(), s));
TokenType::Term
}
@@ -588,20 +583,19 @@ impl<'a, R: CharRead> Parser<'a, R> {
let tail = subterms.pop().unwrap();
let head = subterms.pop().unwrap();
self.terms.push(
match as_partial_string(head, tail) {
Ok((string_buf, Some(tail))) => {
Term::PartialString(Cell::default(), string_buf, tail)
}
Ok((string_buf, None)) => {
let atom = self.lexer.machine_st.atom_tbl.build_with(&string_buf);
Term::CompleteString(Cell::default(), atom)
}
Err(term) => term,
},
);
self.terms.push(match as_partial_string(head, tail) {
Ok((string_buf, Some(tail))) => {
Term::PartialString(Cell::default(), string_buf, tail)
}
Ok((string_buf, None)) => {
let atom = self.lexer.machine_st.atom_tbl.build_with(&string_buf);
Term::CompleteString(Cell::default(), atom)
}
Err(term) => term,
});
} else {
self.terms.push(Term::Clause(Cell::default(), name, subterms));
self.terms
.push(Term::Clause(Cell::default(), name, subterms));
}
if let Some(&mut TokenDesc {
@@ -695,7 +689,8 @@ impl<'a, R: CharRead> Parser<'a, R> {
td.tt = TokenType::Term;
td.priority = 0;
self.terms.push(Term::Literal(Cell::default(), Literal::Atom(atom!("[]"))));
self.terms
.push(Term::Literal(Cell::default(), Literal::Atom(atom!("[]"))));
return Ok(true);
}
}
@@ -736,8 +731,8 @@ impl<'a, R: CharRead> Parser<'a, R> {
if arity > self.terms.len() {
return Err(ParserError::IncompleteReduction(
self.lexer.line_num,
self.lexer.col_num
))
self.lexer.col_num,
));
}
let idx = self.terms.len() - arity;
@@ -755,18 +750,16 @@ impl<'a, R: CharRead> Parser<'a, R> {
});
self.terms.push(match list {
Term::Cons(_, head, tail) => {
match as_partial_string(*head, *tail) {
Ok((string_buf, Some(tail))) => {
Term::PartialString(Cell::default(), string_buf, tail)
}
Ok((string_buf, None)) => {
let atom = self.lexer.machine_st.atom_tbl.build_with(&string_buf);
Term::CompleteString(Cell::default(), atom)
}
Err(term) => term,
Term::Cons(_, head, tail) => match as_partial_string(*head, *tail) {
Ok((string_buf, Some(tail))) => {
Term::PartialString(Cell::default(), string_buf, tail)
}
}
Ok((string_buf, None)) => {
let atom = self.lexer.machine_st.atom_tbl.build_with(&string_buf);
Term::CompleteString(Cell::default(), atom)
}
Err(term) => term,
},
term => term,
});
@@ -784,10 +777,7 @@ impl<'a, R: CharRead> Parser<'a, R> {
td.priority = 0;
td.spec = TERM;
let term = Term::Literal(
Cell::default(),
Literal::Atom(atom!("{}")),
);
let term = Term::Literal(Cell::default(), Literal::Atom(atom!("{}")));
self.terms.push(term);
return Ok(true);
@@ -818,11 +808,8 @@ impl<'a, R: CharRead> Parser<'a, R> {
}
};
self.terms.push(Term::Clause(
Cell::default(),
atom!("{}"),
vec![term],
));
self.terms
.push(Term::Clause(Cell::default(), atom!("{}"), vec![term]));
return Ok(true);
}
@@ -933,7 +920,8 @@ impl<'a, R: CharRead> Parser<'a, R> {
if let Some(term) = self.terms.last().cloned() {
match term {
Term::Literal(_, Literal::Atom(name))
if name == atom!("-") && (is_prefix!(desc.spec) || is_negate!(desc.spec)) =>
if name == atom!("-")
&& (is_prefix!(desc.spec) || is_negate!(desc.spec)) =>
{
self.stack.pop();
self.terms.pop();
@@ -1069,7 +1057,11 @@ impl<'a, R: CharRead> Parser<'a, R> {
}
// on success, returns the parsed term and the number of lines read.
pub fn read_term(&mut self, op_dir: &CompositeOpDir, tokens: Tokens) -> Result<Term, ParserError> {
pub fn read_term(
&mut self,
op_dir: &CompositeOpDir,
tokens: Tokens,
) -> Result<Term, ParserError> {
self.tokens = match tokens {
Tokens::Default => read_tokens(&mut self.lexer)?,
Tokens::Provided(tokens) => tokens,