Make Parser class internally use IntoIterator
And wrap the next() and peek() methods
This commit is contained in:
parent
32a90b8103
commit
1af1589550
@ -1,5 +1,6 @@
|
|||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
use std::slice::Iter;
|
use std::slice::Iter;
|
||||||
|
use std::vec::IntoIter;
|
||||||
|
|
||||||
use tokenizer::Token;
|
use tokenizer::Token;
|
||||||
|
|
||||||
@ -25,15 +26,25 @@ pub type ParseResult<T> = Result<T, ParseError>;
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct Parser<'a> {
|
struct Parser {
|
||||||
tokens: Peekable<Iter<'a, Token>>
|
tokens: Peekable<IntoIter<Token>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
impl Parser {
|
||||||
|
fn next(&mut self) -> Option<Token> {
|
||||||
|
self.tokens.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lookahead(&mut self) -> Option<&Token> {
|
||||||
|
self.tokens.peek()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parser {
|
||||||
|
|
||||||
fn expect(&mut self, expected: Token) -> ParseResult<()> {
|
fn expect(&mut self, expected: Token) -> ParseResult<()> {
|
||||||
match self.tokens.next() {
|
match self.next() {
|
||||||
Some(next) if *next == expected => Ok(()),
|
Some(ref next) if *next == expected => Ok(()),
|
||||||
Some(next) => {
|
Some(next) => {
|
||||||
let err = format!("Expected {:?} but got {:?}", expected, next);
|
let err = format!("Expected {:?} but got {:?}", expected, next);
|
||||||
return Err(ParseError { err: err });
|
return Err(ParseError { err: err });
|
||||||
@ -53,13 +64,13 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn expr(&mut self) -> ParseResult<AST> {
|
fn expr(&mut self) -> ParseResult<AST> {
|
||||||
self.tokens.next();
|
self.next();
|
||||||
return Ok(AST::Number(5.0));
|
return Ok(AST::Number(5.0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(input: Vec<Token>) -> ParseResult<AST> {
|
pub fn parse(input: Vec<Token>) -> ParseResult<AST> {
|
||||||
let mut iter = input.iter().peekable();
|
let mut iter = input.into_iter().peekable();
|
||||||
let mut parser = Parser { tokens: iter };
|
let mut parser = Parser { tokens: iter };
|
||||||
return parser.parse();
|
return parser.parse();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user