Move .next() onto token_handler

This commit is contained in:
greg 2019-01-05 18:29:24 -08:00
parent 821f321261
commit 2b338fd3c9

View File

@ -91,9 +91,6 @@ impl Parser {
fn peek(&mut self) -> TokenKind { fn peek(&mut self) -> TokenKind {
self.token_handler.peek() self.token_handler.peek()
} }
fn next(&mut self) -> Token {
self.token_handler.next()
}
pub fn parse(&mut self) -> ParseResult<AST> { pub fn parse(&mut self) -> ParseResult<AST> {
self.program() self.program()
@ -126,7 +123,7 @@ macro_rules! expect {
{ {
let tok = $self.token_handler.peek_token(); let tok = $self.token_handler.peek_token();
match tok.get_kind() { match tok.get_kind() {
$expected_kind if $cond => $self.next(), $expected_kind if $cond => $self.token_handler.next(),
actual_kind => { actual_kind => {
let msg = format!("Expected {}, got {:?}", print_token_pattern!($expected_kind), actual_kind); let msg = format!("Expected {}, got {:?}", print_token_pattern!($expected_kind), actual_kind);
return ParseError::new_with_token(&msg, tok); return ParseError::new_with_token(&msg, tok);
@ -155,13 +152,13 @@ macro_rules! delimited {
} }
if !$strictness { if !$strictness {
match peek { match peek {
$( $delim )|* => { $self.next(); continue }, $( $delim )|* => { $self.token_handler.next(); continue },
_ => () _ => ()
} }
} }
acc.push($self.$parse_fn()?); acc.push($self.$parse_fn()?);
match $self.peek() { match $self.peek() {
$( $delim )|* => { $self.next(); continue }, $( $delim )|* => { $self.token_handler.next(); continue },
_ if $strictness => break, _ if $strictness => break,
_ => continue, _ => continue,
}; };
@ -302,7 +299,7 @@ impl Parser {
match self.peek() { match self.peek() {
EOF => break, EOF => break,
Newline | Semicolon => { Newline | Semicolon => {
self.next(); self.token_handler.next();
continue; continue;
}, },
_ => statements.push( _ => statements.push(
@ -338,7 +335,7 @@ impl Parser {
self.type_alias() self.type_alias()
} else { } else {
let mutable = if let Keyword(Mut) = self.peek() { let mutable = if let Keyword(Mut) = self.peek() {
self.next(); self.token_handler.next();
true true
} else { } else {
false false
@ -365,7 +362,7 @@ impl Parser {
variants.push(self.variant_specifier()?); variants.push(self.variant_specifier()?);
loop { loop {
if let Pipe = self.peek() { if let Pipe = self.peek() {
self.next(); self.token_handler.next();
variants.push(self.variant_specifier()?); variants.push(self.variant_specifier()?);
} else { } else {
break; break;
@ -417,7 +414,7 @@ impl Parser {
let (name, operator) = match self.peek() { let (name, operator) = match self.peek() {
Operator(s) => { Operator(s) => {
let name = s.clone(); let name = s.clone();
self.next(); self.token_handler.next();
(name, true) (name, true)
}, },
_ => (self.identifier()?, false) _ => (self.identifier()?, false)
@ -456,7 +453,7 @@ impl Parser {
expect!(self, Keyword(Kw::Let)); expect!(self, Keyword(Kw::Let));
let constant = match self.peek() { let constant = match self.peek() {
Keyword(Kw::Mut) => { Keyword(Kw::Mut) => {
self.next(); self.token_handler.next();
false false
} }
_ => true _ => true
@ -486,7 +483,7 @@ impl Parser {
expect!(self, Keyword(Impl)); expect!(self, Keyword(Impl));
let first = self.type_name()?; let first = self.type_name()?;
let second = if let Keyword(For) = self.peek() { let second = if let Keyword(For) = self.peek() {
self.next(); self.token_handler.next();
Some(self.type_name()?) Some(self.type_name()?)
} else { } else {
None None
@ -572,7 +569,7 @@ impl Parser {
if precedence >= new_precedence { if precedence >= new_precedence {
break; break;
} }
let next_tok = self.next(); let next_tok = self.token_handler.next();
let operation = match BinOp::from_sigil_token(&next_tok.kind) { let operation = match BinOp::from_sigil_token(&next_tok.kind) {
Some(sigil) => sigil, Some(sigil) => sigil,
None => unreachable!() None => unreachable!()
@ -588,7 +585,7 @@ impl Parser {
fn prefix_expr(&mut self) -> ParseResult<Expression> { fn prefix_expr(&mut self) -> ParseResult<Expression> {
match self.peek() { match self.peek() {
Operator(ref op) if PrefixOp::is_prefix(&*op) => { Operator(ref op) if PrefixOp::is_prefix(&*op) => {
let sigil = match self.next().kind { let sigil = match self.token_handler.next().kind {
Operator(op) => op, Operator(op) => op,
_ => unreachable!(), _ => unreachable!(),
}; };
@ -773,7 +770,7 @@ impl Parser {
#[recursive_descent_method] #[recursive_descent_method]
fn else_clause(&mut self) -> ParseResult<Option<Block>> { fn else_clause(&mut self) -> ParseResult<Option<Block>> {
Ok(if let Keyword(Kw::Else) = self.peek() { Ok(if let Keyword(Kw::Else) = self.peek() {
self.next(); self.token_handler.next();
Some(self.expr_or_block()?) Some(self.expr_or_block()?)
} else { } else {
None None
@ -790,13 +787,13 @@ impl Parser {
loop { loop {
match self.peek() { match self.peek() {
RCurlyBrace | EOF => break, RCurlyBrace | EOF => break,
Semicolon | Newline => { self.next(); continue}, Semicolon | Newline => { self.token_handler.next(); continue},
_ => { _ => {
let guard_arm = self.guard_arm()?; let guard_arm = self.guard_arm()?;
guards.push(guard_arm); guards.push(guard_arm);
loop { loop {
match self.peek() { match self.peek() {
Semicolon | Newline => { self.next(); continue; }, Semicolon | Newline => { self.token_handler.next(); continue; },
_ => break, _ => break,
} }
} }
@ -823,12 +820,12 @@ impl Parser {
fn guard(&mut self) -> ParseResult<Guard> { fn guard(&mut self) -> ParseResult<Guard> {
Ok(match self.peek() { Ok(match self.peek() {
Keyword(Kw::Is) => { Keyword(Kw::Is) => {
self.next(); self.token_handler.next();
let pat = self.pattern()?; let pat = self.pattern()?;
Guard::Pat(pat) Guard::Pat(pat)
}, },
ref tok if BinOp::from_sigil_token(tok).is_some() => { ref tok if BinOp::from_sigil_token(tok).is_some() => {
let op = BinOp::from_sigil_token(&self.next().kind).unwrap(); let op = BinOp::from_sigil_token(&self.token_handler.next().kind).unwrap();
let precedence = op.get_precedence(); let precedence = op.get_precedence();
let Expression(expr, _) = self.precedence_expr(precedence)?; let Expression(expr, _) = self.precedence_expr(precedence)?;
Guard::HalfExpr(HalfExpr { op: Some(op), expr }) Guard::HalfExpr(HalfExpr { op: Some(op), expr })
@ -871,21 +868,21 @@ impl Parser {
} }
}, },
Keyword(Kw::True) => { Keyword(Kw::True) => {
self.next(); self.token_handler.next();
Pattern::Literal(PatternLiteral::BoolPattern(true)) Pattern::Literal(PatternLiteral::BoolPattern(true))
}, },
Keyword(Kw::False) => { Keyword(Kw::False) => {
self.next(); self.token_handler.next();
Pattern::Literal(PatternLiteral::BoolPattern(false)) Pattern::Literal(PatternLiteral::BoolPattern(false))
}, },
StrLiteral(s) => { StrLiteral(s) => {
self.next(); self.token_handler.next();
Pattern::Literal(PatternLiteral::StringPattern(s)) Pattern::Literal(PatternLiteral::StringPattern(s))
}, },
DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.signed_number_literal()?, DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.signed_number_literal()?,
Operator(ref op) if **op == "-" => self.signed_number_literal()?, Operator(ref op) if **op == "-" => self.signed_number_literal()?,
Underscore => { Underscore => {
self.next(); self.token_handler.next();
Pattern::Ignored Pattern::Ignored
}, },
other => return ParseError::new_with_token(&format!("{:?} is not a valid Pattern", other), tok) other => return ParseError::new_with_token(&format!("{:?} is not a valid Pattern", other), tok)
@ -897,7 +894,7 @@ impl Parser {
fn signed_number_literal(&mut self) -> ParseResult<Pattern> { fn signed_number_literal(&mut self) -> ParseResult<Pattern> {
let neg = match self.peek() { let neg = match self.peek() {
Operator(ref op) if **op == "-" => { Operator(ref op) if **op == "-" => {
self.next(); self.token_handler.next();
true true
}, },
_ => false _ => false
@ -994,7 +991,7 @@ impl Parser {
StatementBlock(statements.into_iter().map(|s| Node::new(s)).collect()) StatementBlock(statements.into_iter().map(|s| Node::new(s)).collect())
}, },
Keyword(Kw::Return) => { Keyword(Kw::Return) => {
self.next(); self.token_handler.next();
MonadicReturn(self.expression()?) MonadicReturn(self.expression()?)
}, },
_ => return ParseError::new_with_token("for expressions must end in a block or 'return'", tok), _ => return ParseError::new_with_token("for expressions must end in a block or 'return'", tok),
@ -1003,7 +1000,7 @@ impl Parser {
#[recursive_descent_method] #[recursive_descent_method]
fn identifier(&mut self) -> ParseResult<Rc<String>> { fn identifier(&mut self) -> ParseResult<Rc<String>> {
let tok = self.next(); let tok = self.token_handler.next();
match tok.get_kind() { match tok.get_kind() {
Identifier(s) => Ok(s), Identifier(s) => Ok(s),
p => ParseError::new_with_token(&format!("Expected an identifier, got {:?}", p), tok), p => ParseError::new_with_token(&format!("Expected an identifier, got {:?}", p), tok),
@ -1018,15 +1015,15 @@ impl Parser {
match tok.get_kind() { match tok.get_kind() {
DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.number_literal(), DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.number_literal(),
Keyword(Kw::True) => { Keyword(Kw::True) => {
self.next(); self.token_handler.next();
Ok(Expression(BoolLiteral(true), None)) Ok(Expression(BoolLiteral(true), None))
}, },
Keyword(Kw::False) => { Keyword(Kw::False) => {
self.next(); self.token_handler.next();
Ok(Expression(BoolLiteral(false), None)) Ok(Expression(BoolLiteral(false), None))
}, },
StrLiteral(s) => { StrLiteral(s) => {
self.next(); self.token_handler.next();
Ok(Expression(StringLiteral(s.clone()), None)) Ok(Expression(StringLiteral(s.clone()), None))
} }
e => ParseError::new_with_token(&format!("Expected a literal expression, got {:?}", e), tok), e => ParseError::new_with_token(&format!("Expected a literal expression, got {:?}", e), tok),
@ -1044,7 +1041,7 @@ impl Parser {
#[recursive_descent_method] #[recursive_descent_method]
fn int_literal(&mut self) -> ParseResult<Expression> { fn int_literal(&mut self) -> ParseResult<Expression> {
use self::ExpressionType::*; use self::ExpressionType::*;
let tok = self.next(); let tok = self.token_handler.next();
match tok.get_kind() { match tok.get_kind() {
BinNumberSigil => { BinNumberSigil => {
let digits = self.digits()?; let digits = self.digits()?;
@ -1065,7 +1062,7 @@ impl Parser {
use self::ExpressionType::*; use self::ExpressionType::*;
let mut digits = self.digits()?; let mut digits = self.digits()?;
if let Period = self.peek() { if let Period = self.peek() {
self.next(); self.token_handler.next();
digits.push_str("."); digits.push_str(".");
digits.push_str(&self.digits()?); digits.push_str(&self.digits()?);
match digits.parse::<f64>() { match digits.parse::<f64>() {
@ -1086,8 +1083,8 @@ impl Parser {
let mut ds = String::new(); let mut ds = String::new();
loop { loop {
match self.peek() { match self.peek() {
Underscore => { self.next(); continue; }, Underscore => { self.token_handler.next(); continue; },
DigitGroup(ref s) => { self.next(); ds.push_str(s)}, DigitGroup(ref s) => { self.token_handler.next(); ds.push_str(s)},
_ => break, _ => break,
} }
} }