我試圖做一個簡單的LISP解析器,但是我卡在了將令牌向量轉換成AST節點樹的步驟。構建一個向量樹
我創建了樹的根,然後在當前想要添加下一個節點的樹中維護一堆引用。問題是不管我嘗試什麼,似乎借用檢查器認爲我所指的東西不夠長。
這是代碼:
pub fn parse(tokens: &Vec<Token>) -> Node {
let mut root: Vec<Node> = vec![];
{
tokens.into_iter().fold(vec![&mut root], handle_token);
}
Node::List(root)
}
fn handle_token<'a>(mut stack: Vec<&'a mut Vec<Node>>, token: &Token) -> Vec<&'a mut Vec<Node>> {
if *token == Token::LParen {
let new_node = Node::List(vec![]); // Create the new node
stack[0].push(new_node); // Add it to the tree
match stack[0][0] {
Node::List(ref mut the_vec) => stack.push(the_vec), // Finally, add a mutable reference to the new vector so that subsequent nodes will become children of this Node
_ => panic!(),
};
} else if *token == Token::RParen {
stack.pop();
} else {
match *token {
Token::Identifier(ref identifier) => {
stack[0].push(Node::Identifier(identifier.to_owned()))
}
Token::Number(number) => stack[0].push(Node::Number(number)),
Token::Str(ref s) => stack[0].push(Node::Str(s.to_owned())),
Token::EOF => {}
_ => panic!(),
}
}
stack
}
這是編譯器輸出:
error: `stack` does not live long enough
--> src/parser.rs:30:15
|
30 | match stack[0][0] {
| ^^^^^ does not live long enough
...
47 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the block at 26:96...
--> src/parser.rs:26:97
|
26 | fn handle_token<'a>(mut stack: Vec<&'a mut Vec<Node>>, token: &Token) -> Vec<&'a mut Vec<Node>> {
| ^
這個研究了一下後,就好像我試圖做一些完全不地道,以鐵鏽,但我不確定。有沒有簡單的方法來完成這項工作,還是我需要重新考慮這個問題?
我試圖減少的問題to a minimal example:
enum Token {
Start,
End,
Value(i32),
}
enum Node {
List(Vec<Node>),
Value(i32),
}
fn main() {
let v = vec![Token::Start, Token::Value(1), Token::End];
parse(&v);
}
fn parse(tokens: &Vec<Token>) -> Node {
let mut root: Vec<Node> = vec![];
{
tokens.into_iter().fold(vec![&mut root], handle_token);
}
Node::List(root)
}
fn handle_token<'a>(mut stack: Vec<&'a mut Vec<Node>>, token: &Token) -> Vec<&'a mut Vec<Node>> {
match *token {
Token::Start => {
stack[0].push(Node::List(vec![])); // Add the new node to the tree
match stack[0][0] {
Node::List(ref mut the_vec) => stack.push(the_vec), // Add a mutable reference to the new vector so that subsequent nodes will become children of this Node
_ => panic!(),
};
},
Token::End => { stack.pop(); },
Token::Value(v) => stack[0].push(Node::Value(v)),
}
stack
}
它看起來像你第二次添加一些東西到堆棧,而不刪除第一個。但是數據應該有1個所有者,而不是2個。你可以嘗試創建一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)?這將使建議替代方案變得更容易。 – wimh