More use of Token instead of TokenKind

This commit is contained in:
greg 2019-01-05 17:50:54 -08:00
parent 22f2750853
commit 846eeae04c

View File

@ -73,8 +73,8 @@ impl TokenHandler {
fn peek_token(&mut self) -> Token {
self.tokens.peek().map(|t: &Token| { t.clone()}).unwrap_or(Token { kind: TokenKind::EOF, offset: self.end_of_file})
}
fn next(&mut self) -> TokenKind {
self.tokens.next().map(|ref t| { t.kind.clone() }).unwrap_or(TokenKind::EOF)
fn next(&mut self) -> Token {
self.tokens.next().unwrap_or(Token { kind: TokenKind::EOF, offset: self.end_of_file})
}
}
@ -91,7 +91,7 @@ impl Parser {
fn peek(&mut self) -> TokenKind {
self.token_handler.peek()
}
fn next(&mut self) -> TokenKind {
fn next(&mut self) -> Token {
self.token_handler.next()
}
@ -572,7 +572,8 @@ impl Parser {
if precedence >= new_precedence {
break;
}
let operation = match BinOp::from_sigil_token(&self.next()) {
let next_tok = self.next();
let operation = match BinOp::from_sigil_token(&next_tok.kind) {
Some(sigil) => sigil,
None => unreachable!()
};
@ -587,7 +588,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() {
let sigil = match self.next().kind {
Operator(op) => op,
_ => unreachable!(),
};
@ -826,7 +827,7 @@ impl Parser {
Guard::Pat(pat)
},
ref tok if BinOp::from_sigil_token(tok).is_some() => {
let op = BinOp::from_sigil_token(&self.next()).unwrap();
let op = BinOp::from_sigil_token(&self.next().kind).unwrap();
let precedence = op.get_precedence();
let Expression(expr, _) = self.precedence_expr(precedence)?;
Guard::HalfExpr(HalfExpr { op: Some(op), expr })
@ -1001,9 +1002,10 @@ impl Parser {
#[recursive_descent_method]
fn identifier(&mut self) -> ParseResult<Rc<String>> {
match self.next() {
let tok = self.next();
match tok.get_kind() {
Identifier(s) => Ok(s),
p => ParseError::new(&format!("Expected an identifier, got {:?}", p)),
p => ParseError::new_with_token(&format!("Expected an identifier, got {:?}", p), tok),
}
}
@ -1041,7 +1043,8 @@ impl Parser {
#[recursive_descent_method]
fn int_literal(&mut self) -> ParseResult<Expression> {
use self::ExpressionType::*;
match self.next() {
let tok = self.next();
match tok.get_kind() {
BinNumberSigil => {
let digits = self.digits()?;
let n = parse_binary(digits)?;
@ -1052,7 +1055,7 @@ impl Parser {
let n = parse_hex(digits)?;
Ok(Expression(NatLiteral(n), None))
},
_ => return ParseError::new("Expected '0x' or '0b'"),
_ => return ParseError::new_with_token("Expected '0x' or '0b'", tok),
}
}