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