parser: improve readability i guess
This commit is contained in:
parent
1f86885424
commit
83441a7b57
1 changed files with 18 additions and 21 deletions
|
@ -15,23 +15,23 @@ impl <'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_node(&mut self, first: Token) -> Result<Node, SyntaxError> {
|
fn parse_node(&mut self) -> Option<Result<Node, SyntaxError>> {
|
||||||
match first.kind {
|
Some(self.lexer.next()?.and_then(|token| match token.kind {
|
||||||
TokenKind::Ident(name) => self.require_kind(
|
TokenKind::Ident(name) => {
|
||||||
TokenKind::Eq
|
self.require_kind(TokenKind::Eq ).and_then(|_| {
|
||||||
).and_then(|_| {
|
self.require_next()
|
||||||
self.require_next()
|
}).and_then(|token| match token.kind {
|
||||||
}).and_then(|token| match token.kind {
|
TokenKind::TrueKeyword => self.node_result(name, NodeVal::Bool(true)),
|
||||||
TokenKind::TrueKeyword => self.make_node_result(name, NodeVal::Bool(true)),
|
TokenKind::FalseKeyword => self.node_result(name, NodeVal::Bool(false)),
|
||||||
TokenKind::FalseKeyword => self.make_node_result(name, NodeVal::Bool(false)),
|
TokenKind::IntLiteral(i) => self.node_result(name, NodeVal::Int(i)),
|
||||||
TokenKind::IntLiteral(i) => self.make_node_result(name, NodeVal::Int(i)),
|
TokenKind::StringLiteral(s) => self.node_result(name, NodeVal::String(s)),
|
||||||
TokenKind::StringLiteral(s) => self.make_node_result(name, NodeVal::String(String::from(s))),
|
_ => self.lexer.syntax_error(String::from("Expected a value")),
|
||||||
_ => self.lexer.syntax_error(String::from("Expected a value")),
|
}).and_then(|node| {
|
||||||
}).and_then(|node| {
|
self.require_kind(TokenKind::Semi).and_then(|_| Ok(node))
|
||||||
self.require_kind(TokenKind::Semi).and_then(|_| Ok(node))
|
})
|
||||||
}),
|
},
|
||||||
_ => self.lexer.syntax_error(String::from("Expected identifier")),
|
_ => self.lexer.syntax_error(String::from("Expected identifier")),
|
||||||
}
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn require_kind(&mut self, kind: TokenKind) -> Result<Token, SyntaxError> {
|
fn require_kind(&mut self, kind: TokenKind) -> Result<Token, SyntaxError> {
|
||||||
|
@ -51,7 +51,7 @@ impl <'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_node_result<T>(&self, name: String, val: NodeVal) -> Result<Node, T> {
|
fn node_result<T>(&self, name: String, val: NodeVal) -> Result<Node, T> {
|
||||||
Ok(self.make_node(name, val))
|
Ok(self.make_node(name, val))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,10 +67,7 @@ impl <'a> Iterator for Parser<'a> {
|
||||||
type Item = Result<Node, SyntaxError>;
|
type Item = Result<Node, SyntaxError>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Result<Node, SyntaxError>> {
|
fn next(&mut self) -> Option<Result<Node, SyntaxError>> {
|
||||||
Some(match self.lexer.next()? {
|
self.parse_node()
|
||||||
Ok(t) => self.parse_node(t),
|
|
||||||
Err(e) => Err(e),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue