improve ParseResult usage

This commit is contained in:
mos 2024-07-27 16:45:32 +02:00
parent 3c55dc8d45
commit 8e0e1dcca5
1 changed files with 6 additions and 6 deletions

View File

@ -5,7 +5,7 @@ pub mod bp;
pub mod de; pub mod de;
pub mod error; pub mod error;
type ParseResult = Result<Node, ParseError>; type ParseResult<T> = Result<T, ParseError>;
#[derive(Debug)] #[derive(Debug)]
pub enum ParseError { pub enum ParseError {
@ -163,11 +163,11 @@ impl<'a> Parser<'a> {
} }
} }
fn peek(&self) -> Result<&Symbol, ParseError> { fn peek(&self) -> ParseResult<&Symbol> {
self.read().ok_or(ParseError::Eof) self.read().ok_or(ParseError::Eof)
} }
fn expect(&self, sym: Symbol, err: ParseError) -> Result<(), ParseError> { fn expect(&self, sym: Symbol, err: ParseError) -> ParseResult<()> {
if *self.peek()? != sym { if *self.peek()? != sym {
Err(err) Err(err)
} else { } else {
@ -179,7 +179,7 @@ impl<'a> Parser<'a> {
self.pos += 1; self.pos += 1;
} }
fn parse_term(&mut self) -> Result<Node, ParseError> { fn parse_term(&mut self) -> ParseResult<Node> {
match self.peek()? { match self.peek()? {
Symbol::Int(n) => Ok(Node::new_op(NodeOp::Const(*n, false))), Symbol::Int(n) => Ok(Node::new_op(NodeOp::Const(*n, false))),
Symbol::SInt(n) => Ok(Node::new_op(NodeOp::Const(*n, true))), Symbol::SInt(n) => Ok(Node::new_op(NodeOp::Const(*n, true))),
@ -212,7 +212,7 @@ impl<'a> Parser<'a> {
} }
} }
fn parse_expr(&mut self) -> ParseResult { fn parse_expr(&mut self) -> ParseResult<Node> {
let mut lhs = self.parse_term()?; let mut lhs = self.parse_term()?;
if lhs.op == NodeOp::Data || lhs.op == NodeOp::List { if lhs.op == NodeOp::Data || lhs.op == NodeOp::List {
@ -248,7 +248,7 @@ impl<'a> Parser<'a> {
Err(ParseError::Eof) Err(ParseError::Eof)
} }
pub fn parse(&mut self) -> Result<Vec<Node>, ParseError> { pub fn parse(&mut self) -> ParseResult<Vec<Node>> {
let mut node_tree = vec![]; let mut node_tree = vec![];
loop { loop {