summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud.bailly@iohk.io>2024-09-24 16:26:40 +0200
committerArnaud Bailly <arnaud.bailly@iohk.io>2024-09-24 16:26:40 +0200
commit91356e9786cce67ed49b4c25b1b66ec9f1d17db2 (patch)
tree8d47b0432dc6f72f5ca33edc233b36b902306284 /rust
parent5e5c4f2c47ec867d698c001cfe8d097be5744a57 (diff)
downloadlambda-nantes-91356e9786cce67ed49b4c25b1b66ec9f1d17db2.tar.gz
Inline next token advance
Diffstat (limited to 'rust')
-rw-r--r--rust/src/parser.rs31
1 files changed, 16 insertions, 15 deletions
diff --git a/rust/src/parser.rs b/rust/src/parser.rs
index 11948c1..c85a425 100644
--- a/rust/src/parser.rs
+++ b/rust/src/parser.rs
@@ -14,7 +14,7 @@ struct Parser {
}
impl Parser {
- fn expect(&self, token: Token) -> Result<(), String> {
+ fn expect(&mut self, token: Token) -> Result<(), String> {
if self.tokens.get(self.index) == Some(&token) {
Ok(())
} else {
@@ -24,14 +24,9 @@ impl Parser {
self.tokens.get(self.index)
))
}
- }
-
- fn next(&mut self) {
- self.index += 1;
- }
-
- fn backtrack(&mut self) {
- self.index -= 1;
+ .map(|_| {
+ self.next();
+ })
}
fn expect_symbol(&mut self) -> Result<String, String> {
@@ -40,6 +35,18 @@ impl Parser {
} else {
Err("Expected a symbol".to_string())
}
+ .map(|s| {
+ self.next();
+ s
+ })
+ }
+
+ fn next(&mut self) {
+ self.index += 1;
+ }
+
+ fn backtrack(&mut self) {
+ self.index -= 1;
}
}
@@ -59,32 +66,26 @@ fn parse_expression(parser: &mut Parser) -> Result<Value, String> {
fn parse_abstraction(parser: &mut Parser) -> Result<Value, String> {
parser.expect(Token::LParen)?;
- parser.next();
parser.expect(Token::Lambda).map_err(|e| {
parser.backtrack();
e.to_string()
})?;
- parser.next();
let var = parse_variable(parser)?;
let body = parse_expression(parser)?;
parser.expect(Token::RParen)?;
- parser.next();
Ok(Value::Lam(var, Box::new(body)))
}
fn parse_variable(parser: &mut Parser) -> Result<String, String> {
let var = parser.expect_symbol()?;
- parser.next();
Ok(var)
}
fn parse_application(parser: &mut Parser) -> Result<Value, String> {
parser.expect(Token::LParen)?;
- parser.next();
let left = parse_expression(parser)?;
let right = parse_expression(parser)?;
parser.expect(Token::RParen)?;
- parser.next();
Ok(Value::App(Box::new(left), Box::new(right)))
}