parser: make iterator type more generic
This commit is contained in:
parent
b336f2f847
commit
4b879a5516
1 changed files with 19 additions and 27 deletions
|
@ -1,7 +1,7 @@
|
|||
use {
|
||||
crate::{
|
||||
expr::{LTExpr, Statement, TypeError, TypeTag},
|
||||
lexer::{LTIRLexer, LTIRToken, LexError},
|
||||
lexer::{LTIRLexer, LTIRToken, LexError, InputRegionTag},
|
||||
},
|
||||
std::{
|
||||
iter::Peekable,
|
||||
|
@ -19,11 +19,10 @@ pub enum ParseError {
|
|||
}
|
||||
|
||||
pub fn parse_expect<It>(
|
||||
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||
tokens: &mut Peekable<It>,
|
||||
expected_token: LTIRToken,
|
||||
) -> Result<(), ParseError>
|
||||
where
|
||||
It: Iterator<Item = char>,
|
||||
where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
|
||||
{
|
||||
match tokens.next() {
|
||||
Some((region, Ok(t))) => {
|
||||
|
@ -40,9 +39,8 @@ where
|
|||
|
||||
/* parse symbol name
|
||||
*/
|
||||
pub fn parse_symbol<It>(tokens: &mut Peekable<LTIRLexer<It>>) -> Result<String, ParseError>
|
||||
where
|
||||
It: Iterator<Item = char>,
|
||||
pub fn parse_symbol<It>(tokens: &mut Peekable<It>) -> Result<String, ParseError>
|
||||
where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
|
||||
{
|
||||
match tokens.next() {
|
||||
Some((region, Ok(LTIRToken::Symbol(name)))) => Ok(name),
|
||||
|
@ -57,10 +55,9 @@ where
|
|||
*/
|
||||
pub fn parse_type_tag<It>(
|
||||
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||
tokens: &mut Peekable<It>,
|
||||
) -> Result<Option<laddertypes::TypeTerm>, ParseError>
|
||||
where
|
||||
It: Iterator<Item = char>,
|
||||
where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
|
||||
{
|
||||
if let Some((region, peektok)) = tokens.peek().clone() {
|
||||
match peektok.clone() {
|
||||
|
@ -111,9 +108,9 @@ impl VariableBinding {
|
|||
*/
|
||||
pub fn parse_binding_expr<It>(
|
||||
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||
tokens: &mut Peekable<It>,
|
||||
) -> Result< VariableBinding, ParseError>
|
||||
where It: Iterator<Item = char>
|
||||
where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
|
||||
{
|
||||
if let Some((region, peektok)) = tokens.peek().clone() {
|
||||
match peektok {
|
||||
|
@ -142,10 +139,9 @@ where It: Iterator<Item = char>
|
|||
*/
|
||||
pub fn parse_binding_block<It>(
|
||||
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||
tokens: &mut Peekable<It>,
|
||||
) -> Result< Vec<VariableBinding>, ParseError>
|
||||
where
|
||||
It: Iterator<Item = char>,
|
||||
where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
|
||||
{
|
||||
let _ = parse_expect(tokens, LTIRToken::BlockOpen)?;
|
||||
|
||||
|
@ -173,10 +169,9 @@ where
|
|||
|
||||
pub fn parse_statement<It>(
|
||||
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||
tokens: &mut Peekable<It>,
|
||||
) -> Result<crate::expr::Statement, ParseError>
|
||||
where
|
||||
It: Iterator<Item = char>,
|
||||
where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
|
||||
{
|
||||
if let Some((region, peektok)) = tokens.peek() {
|
||||
match peektok {
|
||||
|
@ -251,10 +246,9 @@ where
|
|||
|
||||
pub fn parse_statement_block<It>(
|
||||
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||
tokens: &mut Peekable<It>,
|
||||
) -> Result<Vec<Statement>, ParseError>
|
||||
where
|
||||
It: Iterator<Item = char>,
|
||||
where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
|
||||
{
|
||||
let _ = parse_expect(tokens, LTIRToken::BlockOpen)?;
|
||||
|
||||
|
@ -278,10 +272,9 @@ where
|
|||
}
|
||||
|
||||
pub fn parse_atom<It>(
|
||||
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||
tokens: &mut Peekable<It>,
|
||||
) -> Result<crate::expr::LTExpr, ParseError>
|
||||
where
|
||||
It: Iterator<Item = char>,
|
||||
where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
|
||||
{
|
||||
match tokens.next() {
|
||||
Some((region, Ok(LTIRToken::Symbol(sym)))) => Ok(LTExpr::symbol(sym.as_str())),
|
||||
|
@ -295,10 +288,9 @@ where
|
|||
|
||||
pub fn parse_expr<It>(
|
||||
typectx: &Arc<RwLock<laddertypes::dict::TypeDict>>,
|
||||
tokens: &mut Peekable<LTIRLexer<It>>,
|
||||
tokens: &mut Peekable<It>,
|
||||
) -> Result<crate::expr::LTExpr, ParseError>
|
||||
where
|
||||
It: Iterator<Item = char>,
|
||||
where It: Iterator<Item = (InputRegionTag, Result<LTIRToken, LexError>)>
|
||||
{
|
||||
let mut children = Vec::new();
|
||||
|
||||
|
|
Loading…
Reference in a new issue