Skip to content

Commit

Permalink
add true and false as keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed Nov 29, 2024
1 parent d8e6d82 commit b5e7c59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/ast/data/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum Token {
KwConst,
#[regex(r"continue", priority = 4)]
KwContinue,
#[regex(r"false", priority = 4)]
KwFalse,
#[regex(r"fn", priority = 4)]
KwFn,
#[regex(r"if", priority = 4)]
Expand All @@ -27,6 +29,8 @@ pub enum Token {
KwReturn,
#[regex(r"static", priority = 4)]
KwStatic,
#[regex(r"true", priority = 4)]
KwTrue,

#[regex(r"\[", priority = 3)]
OpBracket,
Expand Down Expand Up @@ -226,6 +230,8 @@ impl core::fmt::Display for Token {
Token::GreaterThan => write!(f, ">"),
Token::LessThan => write!(f, "<"),
Token::Ampersand => write!(f, "&"),
Token::KwFalse => write!(f, "false"),
Token::KwTrue => write!(f, "true"),
}
}
}
19 changes: 17 additions & 2 deletions src/ast/parsing/lone_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Parsing of individual [TokenTree::Lone] token values.
//!
//! * Notably: [`num_lit_p`], [`ident_p`], and [`register_p`] can match against
//! varying inputs, and the captured input is [FileSpanned] as their output.
//! * The [`num_lit_p`], [`ident_p`], [`register_p`], and [`bool_p`] parsers can
//! match against varying inputs, and the captured input is [FileSpanned] as
//! their output.
//! * The rest of them only parse for exactly one value (eg: a single period or
//! a single comma), and so all those parsers output nothing.
Expand Down Expand Up @@ -57,6 +58,20 @@ where
.as_context()
}

/// Parses either `Lone(bool)` value.
pub fn bool_p<'src, I>(
) -> impl Parser<'src, I, FileSpanned<bool>, ErrRichTokenTree<'src>> + Clone
where
I: BorrowInput<'src, Token = TokenTree, Span = FileSpan> + ValueInput<'src>,
{
select! {
Lone(KwTrue) = ex => FileSpanned::new(true, ex.span()),
Lone(KwFalse) = ex => FileSpanned::new(false, ex.span()),
}
.labelled("bool")
.as_context()
}

/// Parses a `Lone(KwBreak)`, which is then discarded.
pub fn kw_break_p<'src, I>(
) -> impl Parser<'src, I, (), ErrRichTokenTree<'src>> + Clone
Expand Down

0 comments on commit b5e7c59

Please sign in to comment.