gpt4 book ai didi

iterator - 错误 : cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495]

转载 作者:行者123 更新时间:2023-11-29 08:14:36 25 4
gpt4 key购买 nike

首先:我完全了解这篇文章:Cannot infer appropriate lifetime for autoref in Iterator impl而且这个问题可能与我的相似。但是,我无法使用此线程的知识来使用它。

代码:

use std::str::Chars;

use super::token::*;
use super::token_stream::TokenStream;

pub struct Lexer<'a> {
input: Chars<'a>,
buffer: String,
cur_char: char
}

impl<'a> Lexer<'a> {
pub fn new(iterator: Chars<'a>) -> Lexer {
let mut lexer = Lexer {
input: iterator,
buffer: String::new(),
cur_char: '\0' };
lexer.consume_next();
lexer
}

pub fn new_from_str(content : &str) -> Lexer {
Lexer::new(content.chars())
}

fn consume_next(&mut self) -> char {
let next = self.input.next();
if let Some(c) = next {
self.buffer.push(c);
self.cur_char = c;
}
else {
self.cur_char = '\0';
}
self.current_char()
}

fn clear_buffer(&mut self) {
self.buffer.clear();
}

fn current_char(&self) -> char {
self.cur_char
}

fn scan_line_comment(&self) -> Token { Token::EndOfFile }
fn scan_multi_line_comment(&self) -> Token { Token::EndOfFile }


fn scan_identifier(&self) -> Token { Token::EndOfFile }
fn scan_char_literal(&self) -> Token { Token::EndOfFile }
fn scan_string_literal(&self) -> Token { Token::EndOfFile }
fn scan_number_literal(&self) -> Token { Token::EndOfFile }

fn consume_and_return<'b>(&mut self, token: Token<'b>) -> Token<'b> {
self.consume_next();
token
}
}

impl<'a> TokenStream for Lexer<'a> {
fn next_token(&mut self) -> Token {
match self.current_char() {
/* Skip whitespace */
' ' |
'\r' |
'\n' |
'\t' => {
self.clear_buffer();
self.consume_and_return(Token::Whitespace)
}

/* Opening delimiters */
'(' => self.consume_and_return(Token::OpenDelim(DelimitToken::Paren)),
'[' => self.consume_and_return(Token::OpenDelim(DelimitToken::Bracket)),
'{' => self.consume_and_return(Token::OpenDelim(DelimitToken::Brace)),

/* Opening delimiters */
')' => self.consume_and_return(Token::CloseDelim(DelimitToken::Paren)),
']' => self.consume_and_return(Token::CloseDelim(DelimitToken::Bracket)),
'}' => self.consume_and_return(Token::CloseDelim(DelimitToken::Brace)),

/* Special tokens which aren't the beginning
of any other token */
'?' => self.consume_and_return(Token::Question),
';' => self.consume_and_return(Token::SemiColon),
',' => self.consume_and_return(Token::Comma),

/* Dot, DotDot and DotDotDot tokens */
'.' => match self.consume_next() {
'.' => match self.consume_next() {
'.' => self.consume_and_return(Token::DotDotDot),
_ => Token::DotDot
},
_ => Token::Dot
},

/* Tokens starting with '+' */
'+' => match self.consume_next() {
'=' => self.consume_and_return(Token::BinOpEq(BinOpToken::Plus)),
_ => Token::BinOp(BinOpToken::Plus)
},

/* Tokens starting with '-' */
'-' => match self.consume_next() {
'=' => self.consume_and_return(Token::BinOpEq(BinOpToken::Minus)),
'>' => self.consume_and_return(Token::Arrow),
_ => Token::BinOp(BinOpToken::Minus)
},

/* Tokens starting with '*' */
'*' => match self.consume_next() {
'=' => self.consume_and_return(Token::BinOpEq(BinOpToken::Star)),
_ => return Token::BinOp(BinOpToken::Star)
},

/* Tokens starting with '/' */
'/' => match self.consume_next() {
'=' => self.consume_and_return(Token::BinOpEq(BinOpToken::Slash)),
'/' => self.scan_line_comment(),
'*' => self.scan_multi_line_comment(),
_ => Token::BinOp(BinOpToken::Slash)
},

/* Tokens starting with '%' */
'%' => match self.consume_next() {
'=' => self.consume_and_return(Token::BinOpEq(BinOpToken::Percent)),
_ => Token::BinOp(BinOpToken::Percent)
},

/* Tokens starting with '^' */
'^' => match self.consume_next() {
'=' => self.consume_and_return(Token::BinOpEq(BinOpToken::Caret)),
_ => return Token::BinOp(BinOpToken::Caret)
},

/* Tokens starting with '!' */
'!' => match self.consume_next() {
'=' => self.consume_and_return(Token::RelOp(RelOpToken::NotEq)),
_ => Token::Exclamation
},

/* Tokens starting with '=' */
'=' => match self.consume_next() {
'=' => self.consume_and_return(Token::RelOp(RelOpToken::EqEq)),
_ => Token::Eq
},

/* Tokens starting with '&' */
'&' => match self.consume_next() {
'&' => self.consume_and_return(Token::LogicalOp(LogicalOpToken::AndAnd)),
'=' => self.consume_and_return(Token::BinOpEq(BinOpToken::And)),
_ => Token::BinOp(BinOpToken::And)
},

/* Tokens starting with '|' */
'|' => match self.consume_next() {
'|' => self.consume_and_return(Token::LogicalOp(LogicalOpToken::OrOr)),
'=' => self.consume_and_return(Token::BinOpEq(BinOpToken::Or)),
_ => Token::BinOp(BinOpToken::Or)
},

/* Tokens starting with '<' */
'<' => match self.consume_next() {
'<' => match self.consume_next() {
'=' => self.consume_and_return(Token::BinOpEq(BinOpToken::Shl)),
_ => Token::BinOp(BinOpToken::Shl)
},
'=' => self.consume_and_return(Token::RelOp(RelOpToken::LessEq)),
_ => Token::RelOp(RelOpToken::LessThan)
},

/* Tokens starting with '>' */
'>' => match self.consume_next() {
'>' => match self.consume_next() {
'=' => self.consume_and_return(Token::BinOpEq(BinOpToken::Shr)),
_ => Token::BinOp(BinOpToken::Shr)
},
'=' => self.consume_and_return(Token::RelOp(RelOpToken::GreaterEq)),
_ => Token::RelOp(RelOpToken::GreaterThan)
},

/* Char and string literals */
'\'' => self.scan_char_literal(),
'\"' => self.scan_string_literal(),

/* Integer- and float literals and identifiers */
'0' ... '9' => self.scan_number_literal(),
'a' ... 'z' |
'A' ... 'Z' => self.scan_identifier(),

/* When end of iterator has been reached */
_ => Token::EndOfFile
}
}
}

impl<'a> Iterator for Lexer<'a> {
type Item = Token<'a>;

fn next(&mut self) -> Option<Self::Item> {
let token = self.next_token();
match token {
Token::EndOfFile => None,
_ => Some(token)
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use super::super::token::*;
use super::super::token_stream::TokenStream;

#[test]
fn simple_tokens() {
let solution = [
Token::OpenDelim(DelimitToken::Paren),
Token::CloseDelim(DelimitToken::Paren),

Token::OpenDelim(DelimitToken::Bracket),
Token::CloseDelim(DelimitToken::Bracket),

Token::OpenDelim(DelimitToken::Brace),
Token::CloseDelim(DelimitToken::Brace),

Token::Question,
Token::SemiColon,
Token::Comma,

Token::EndOfFile
];
let mut lexer = Lexer::new_from_str("()[]{}?;,");
for expected in &solution {
assert_eq!(lexer.next_token(), *expected);
}
}
}

Playground

及其依赖模块'Token':

#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
pub enum BinOpToken {
Plus, // +
Minus, // -
Star, // *
Slash, // /
Percent, // %
Caret, // ^
And, // &
Or, // |
Shl, // <<
Shr // >>
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
pub enum RelOpToken {
EqEq, // ==
NotEq, // !=
LessThan, // <
LessEq, // <=
GreaterThan, // >
GreaterEq // >=
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
pub enum LogicalOpToken {
AndAnd, // &&
OrOr // ||
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
pub enum DelimitToken {
Paren, // ( or )
Bracket, // [ or ]
Brace, // { or }
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
pub enum LiteralToken<'a> {
Char(&'a str), // e.g. 'a'
Integer(&'a str), // e.g. 5, 42, 1337, 0
Float(&'a str), // e.g. 0.1, 5.0, 13.37, 0.0
String(&'a str) // e.g. "Hello, World!"
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
pub enum Token<'a> {
/* Logical operators, e.g. && or || */
LogicalOp(LogicalOpToken),
/* Binary operators compatible with assignment, e.g. +, - */
BinOp(BinOpToken),
/* Binary assignment operators, e.g. +=, -= */
BinOpEq(BinOpToken),
/* Relational operators, e.g. <, <=, >, >=, ==, != */
RelOp(RelOpToken),

/* An opening delimiter, e.g. { or ( or [ */
OpenDelim(DelimitToken),

/* A closing delimiter, e.g. } or ) or ] */
CloseDelim(DelimitToken),

/* Identifiers with their given name */
Identifier(&'a str),
/* Literal token, e.g. an integer, float or string literal */
Literal(LiteralToken<'a>),

/* Special tokens */
Eq, // =
Colon, // :
SemiColon, // ;
ColonColon, // ::
Dot, // .
DotDot, // ..
DotDotDot, // ...
Comma, // ,
Exclamation, // !
Question, // ?
Arrow, // ->
FatArrow, // =>

/* Junk tokens which the parser doesn't require in order to parse the program. */
Whitespace,
Comment,

/* End of file (EOF) token indicating the end of stream for parsing */
EndOfFile
}

Playground

以及特征“TokenStream”:

pub use super::token::Token;

pub trait TokenStream {
fn next_token(&mut self) -> Token;
}

我收到以下错误:

src/parser/lexer.rs:202:20: 202:32 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495]
src/parser/lexer.rs:202 let token = self.next_token();
^~~~~~~~~~~~

我猜这是一辈子的问题。我的 next_token() 方法返回一个 Token,它的生命周期独立于 Self,但是我不确定我的注释是否正确。

我还尝试为 Iterator 中的 next() 方法做更多的注释,但都失败了......

当我在 Iterator 的实现中向 next() 方法的 &mut self 参数添加生命周期时出现此错误特点:

src/parser/lexer.rs:201:2: 207:3 error: method `next` has an incompatible type for trait:
expected bound lifetime parameter ,
found concrete lifetime [E0053]

最佳答案

我找到了解决问题的方法,现在一切都可以正常编译了。

该问题实际上是一个生命周期问题,但不仅限于 TokenStream 特征。我在整个代码的几个地方遇到了生命周期问题。

初始帖子中的长代码中的一些值得注意的地方:

词法分析器.rs:第 46 - 58 行

fn scan_line_comment<'b>(&self) -> Token<'b> { Token::EndOfFile }
fn scan_multi_line_comment<'b>(&self) -> Token<'b> { Token::EndOfFile }


fn scan_identifier<'b>(&self) -> Token<'b> { Token::EndOfFile }
fn scan_char_literal<'b>(&self) -> Token<'b> { Token::EndOfFile }
fn scan_string_literal<'b>(&self) -> Token<'b> { Token::EndOfFile }
fn scan_number_literal<'b>(&self) -> Token<'b> { Token::EndOfFile }

fn consume_and_return<'b>(&mut self, token: Token<'b>) -> Token<'b> {
self.consume_next();
token
}

我必须插入生存期 'b 以指定 Token 可能比 Lexer 实例存在时间更长。

TokenStream 需要一个新的生命周期参数,以便它也可以指定延长的生命周期:

pub trait TokenStream<'a> {
fn next_token(&mut self) -> Token<'a>;
}

LexerTokenStream 实现必须针对此更改进行调整:

impl<'a, 'b> TokenStream<'b> for Lexer<'a> {
fn next_token(&mut self) -> Token<'b> {
...
}
...
}

以及 LexerIterator 实现

impl<'a> Iterator for Lexer<'a> {
type Item = Token<'a>;

fn next(&mut self) -> Option<Self::Item> {
let token = self.next_token();
match token {
Token::EndOfFile => None,
_ => Some(token)
}
}
}

就是这样!

关于iterator - 错误 : cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35517514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com