parser: make iterator type more generic

This commit is contained in:
Michael Sippel 2024-05-15 23:46:07 +02:00
parent b336f2f847
commit 4b879a5516
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -1,7 +1,7 @@
use { use {
crate::{ crate::{
expr::{LTExpr, Statement, TypeError, TypeTag}, expr::{LTExpr, Statement, TypeError, TypeTag},
lexer::{LTIRLexer, LTIRToken, LexError}, lexer::{LTIRLexer, LTIRToken, LexError, InputRegionTag},
}, },
std::{ std::{
iter::Peekable, iter::Peekable,
@ -19,11 +19,10 @@ pub enum ParseError {
} }
pub fn parse_expect<It>( pub fn parse_expect<It>(
tokens: &mut Peekable<LTIRLexer<It>>, tokens: &mut Peekable<It>,
expected_token: LTIRToken, expected_token: LTIRToken,
) -> Result<(), ParseError> ) -> Result<(), ParseError>
where where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
It: Iterator<Item = char>,
{ {
match tokens.next() { match tokens.next() {
Some((region, Ok(t))) => { Some((region, Ok(t))) => {
@ -40,9 +39,8 @@ where
/* parse symbol name /* parse symbol name
*/ */
pub fn parse_symbol<It>(tokens: &mut Peekable<LTIRLexer<It>>) -> Result<String, ParseError> pub fn parse_symbol<It>(tokens: &mut Peekable<It>) -> Result<String, ParseError>
where where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
It: Iterator<Item = char>,
{ {
match tokens.next() { match tokens.next() {
Some((region, Ok(LTIRToken::Symbol(name)))) => Ok(name), Some((region, Ok(LTIRToken::Symbol(name)))) => Ok(name),
@ -57,10 +55,9 @@ where
*/ */
pub fn parse_type_tag<It>( pub fn parse_type_tag<It>(
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>, typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
tokens: &mut Peekable<LTIRLexer<It>>, tokens: &mut Peekable<It>,
) -> Result<Option<laddertypes::TypeTerm>, ParseError> ) -> Result<Option<laddertypes::TypeTerm>, ParseError>
where where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
It: Iterator<Item = char>,
{ {
if let Some((region, peektok)) = tokens.peek().clone() { if let Some((region, peektok)) = tokens.peek().clone() {
match peektok.clone() { match peektok.clone() {
@ -111,9 +108,9 @@ impl VariableBinding {
*/ */
pub fn parse_binding_expr<It>( pub fn parse_binding_expr<It>(
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>, typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
tokens: &mut Peekable<LTIRLexer<It>>, tokens: &mut Peekable<It>,
) -> Result< VariableBinding, ParseError> ) -> Result< VariableBinding, ParseError>
where It: Iterator<Item = char> where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
{ {
if let Some((region, peektok)) = tokens.peek().clone() { if let Some((region, peektok)) = tokens.peek().clone() {
match peektok { match peektok {
@ -142,10 +139,9 @@ where It: Iterator<Item = char>
*/ */
pub fn parse_binding_block<It>( pub fn parse_binding_block<It>(
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>, typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
tokens: &mut Peekable<LTIRLexer<It>>, tokens: &mut Peekable<It>,
) -> Result< Vec<VariableBinding>, ParseError> ) -> Result< Vec<VariableBinding>, ParseError>
where where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
It: Iterator<Item = char>,
{ {
let _ = parse_expect(tokens, LTIRToken::BlockOpen)?; let _ = parse_expect(tokens, LTIRToken::BlockOpen)?;
@ -173,10 +169,9 @@ where
pub fn parse_statement<It>( pub fn parse_statement<It>(
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>, typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
tokens: &mut Peekable<LTIRLexer<It>>, tokens: &mut Peekable<It>,
) -> Result<crate::expr::Statement, ParseError> ) -> Result<crate::expr::Statement, ParseError>
where where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
It: Iterator<Item = char>,
{ {
if let Some((region, peektok)) = tokens.peek() { if let Some((region, peektok)) = tokens.peek() {
match peektok { match peektok {
@ -251,10 +246,9 @@ where
pub fn parse_statement_block<It>( pub fn parse_statement_block<It>(
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>, typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
tokens: &mut Peekable<LTIRLexer<It>>, tokens: &mut Peekable<It>,
) -> Result<Vec<Statement>, ParseError> ) -> Result<Vec<Statement>, ParseError>
where where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
It: Iterator<Item = char>,
{ {
let _ = parse_expect(tokens, LTIRToken::BlockOpen)?; let _ = parse_expect(tokens, LTIRToken::BlockOpen)?;
@ -278,10 +272,9 @@ where
} }
pub fn parse_atom<It>( pub fn parse_atom<It>(
tokens: &mut Peekable<LTIRLexer<It>>, tokens: &mut Peekable<It>,
) -> Result<crate::expr::LTExpr, ParseError> ) -> Result<crate::expr::LTExpr, ParseError>
where where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
It: Iterator<Item = char>,
{ {
match tokens.next() { match tokens.next() {
Some((region, Ok(LTIRToken::Symbol(sym)))) => Ok(LTExpr::symbol(sym.as_str())), Some((region, Ok(LTIRToken::Symbol(sym)))) => Ok(LTExpr::symbol(sym.as_str())),
@ -295,10 +288,9 @@ where
pub fn parse_expr<It>( pub fn parse_expr<It>(
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>, typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
tokens: &mut Peekable<LTIRLexer<It>>, tokens: &mut Peekable<It>,
) -> Result<crate::expr::LTExpr, ParseError> ) -> Result<crate::expr::LTExpr, ParseError>
where where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
It: Iterator<Item = char>,
{ {
let mut children = Vec::new(); let mut children = Vec::new();