Skip to content

Commit

Permalink
🐛 Fix keyword parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Philogy committed Oct 8, 2024
1 parent 7fe5a5e commit f3a09a1
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ fn text(
just(literal).to(token).labelled(label)
}

fn keyword(literal: &'static str, token: Token) -> impl Parser<char, Token, Error = Simple<char>> {
text(literal, token, literal)
}

fn keywords() -> impl Parser<char, Token, Error = Simple<char>> {
keyword("op", Token::Op)
.or(keyword("dependency", Token::Dependency))
.or(keyword("fn", Token::Fn))
.or(keyword("stack", Token::Stack))
.or(keyword("reads", Token::Reads))
.or(keyword("writes", Token::Writes))
.or(keyword("extern", Token::External))
.or(keyword("const", Token::Const))
}

fn symbols() -> impl Parser<char, Token, Error = Simple<char>> {
text("->", Token::Arrow, "arrow")
.or(text("(", Token::OpenRound, "open round bracket"))
Expand Down Expand Up @@ -71,7 +56,16 @@ fn ident() -> impl Parser<char, Token, Error = Simple<char>> {
)
.chain::<char, Vec<_>, _>(filter(|c: &char| c == &'\'').repeated())
.collect()
.map(Token::Ident)
.map(|name: String| match name.as_str() {
"dependency" => Token::Dependency,
"fn" => Token::Fn,
"stack" => Token::Stack,
"reads" => Token::Reads,
"writes" => Token::Writes,
"extern" => Token::External,
"const" => Token::Const,
_ => Token::Ident(name),
})
}

pub fn lexer() -> impl Parser<char, Vec<Spanned<Token>>, Error = Simple<char>> {
Expand All @@ -84,7 +78,7 @@ pub fn lexer() -> impl Parser<char, Vec<Spanned<Token>>, Error = Simple<char>> {
.padded()
.labelled("comment");

let token = keywords().or(symbols()).or(number()).or(ident());
let token = symbols().or(number()).or(ident());

token
.map_with_span(Spanned::new)
Expand Down

0 comments on commit f3a09a1

Please sign in to comment.