-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
semver: minor
- Loading branch information
Showing
6 changed files
with
219 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::lexer::{TokenKind, TokenValue}; | ||
|
||
use super::{ast::Type, lookup::BindingPower, Parser}; | ||
use miette::Result; | ||
|
||
pub fn parse<'de>(parser: &mut Parser<'de>, binding_power: BindingPower) -> Result<Type<'de>> { | ||
let token = match parser.lexer.peek().as_ref() { | ||
Some(Ok(token)) => token, | ||
Some(Err(err)) => return Err(miette::miette!(err.to_string())), // FIXME: better error handling | ||
None => { | ||
return Err(miette::miette! { | ||
help = "expected a type", | ||
"expected a type" | ||
}) | ||
} | ||
}; | ||
|
||
let handler = parser | ||
.lookup | ||
.type_lookup | ||
.get(&token.kind) | ||
.ok_or(miette::miette! { | ||
labels = vec![token.label("expected a type")], | ||
help = format!("{} is not a type", token.kind), | ||
"expected a type, found {}", token.kind | ||
})?; | ||
let mut lhs = handler(parser)?; | ||
|
||
let mut next_token = parser.lexer.peek(); | ||
|
||
while let Some(token) = next_token { | ||
let token = match token { | ||
Ok(token) => token, | ||
Err(err) => return Err(miette::miette!(err.to_string())), // FIXME: better error handling | ||
}; | ||
|
||
let token_binding_power = { | ||
let binding_power_lookup = parser.lookup.binding_power_lookup.clone(); | ||
binding_power_lookup | ||
.get(&token.kind) | ||
.unwrap_or(&BindingPower::None) | ||
.clone() | ||
}; | ||
|
||
if binding_power > token_binding_power { | ||
break; | ||
} | ||
|
||
let handler = match parser.lookup.left_type_lookup.get(&token.kind) { | ||
Some(handler) => handler, | ||
None => break, | ||
}; | ||
|
||
parser.lexer.next(); | ||
|
||
lhs = handler(parser, lhs, token_binding_power)?; | ||
|
||
next_token = parser.lexer.peek(); | ||
} | ||
|
||
Ok(lhs) | ||
} | ||
|
||
pub fn identifier<'de>(parser: &mut Parser<'de>) -> Result<Type<'de>> { | ||
let token = parser | ||
.lexer | ||
.expect(TokenKind::Identifier, "expected an identifier")?; | ||
|
||
let identifier = match token.value { | ||
TokenValue::Identifier(identifier) => identifier, | ||
_ => unreachable!(), | ||
}; | ||
|
||
Ok(Type::Symbol(identifier)) | ||
} |