#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeywordKind {
Declaration,
Statement,
Modifier,
Type,
Value,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Keyword {
pub word: &'static str,
pub kind: KeywordKind,
pub opens_block: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ContextualPosition {
ChoiceOption,
ImportAlias,
ImportNamespace,
EachStatement,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ContextualKeyword {
pub word: &'static str,
pub position: ContextualPosition,
}
pub const CONTEXTUAL_KEYWORDS: &[ContextualKeyword] = &[
ContextualKeyword {
word: "once",
position: ContextualPosition::ChoiceOption,
},
ContextualKeyword {
word: "as",
position: ContextualPosition::ImportAlias,
},
ContextualKeyword {
word: "std",
position: ContextualPosition::ImportNamespace,
},
ContextualKeyword {
word: "each",
position: ContextualPosition::EachStatement,
},
];
const fn kw(word: &'static str, kind: KeywordKind, opens_block: bool) -> Keyword {
Keyword {
word,
kind,
opens_block,
}
}
use KeywordKind::{Declaration, Modifier, Statement, Type, Value};
pub const KEYWORDS: &[Keyword] = &[
kw("identity", Declaration, false),
kw("label", Declaration, true),
kw("jump", Statement, false),
kw("call", Statement, false),
kw("choice", Statement, true),
kw("import", Declaration, false),
kw("end", Statement, false),
kw("save", Declaration, false),
kw("persistent", Declaration, false),
kw("var", Modifier, false),
kw("let", Statement, false),
kw("fn", Declaration, true),
kw("if", Statement, true),
kw("else", Statement, true),
kw("while", Statement, true),
kw("for", Statement, true),
kw("in", Modifier, false),
kw("match", Statement, true),
kw("break", Statement, false),
kw("continue", Statement, false),
kw("true", Value, false),
kw("false", Value, false),
kw("enum", Declaration, true),
kw("struct", Declaration, true),
kw("return", Statement, false),
kw("Some", Value, false),
kw("None", Value, false),
kw("int", Type, false),
kw("float", Type, false),
kw("bool", Type, false),
kw("str", Type, false),
kw("list", Type, false),
kw("map", Type, false),
kw("Option", Type, false),
kw("Identity", Type, false),
];
pub fn keyword(word: &str) -> Option<&'static Keyword> {
KEYWORDS.iter().find(|k| k.word == word)
}
pub fn is_reserved(word: &str) -> bool {
keyword(word).is_some()
}
pub(crate) fn starts_block(word: &str) -> bool {
keyword(word).is_some_and(|k| k.opens_block)
}
pub(crate) fn is_declaration(word: &str) -> bool {
keyword(word).is_some_and(|k| k.kind == Declaration)
}
pub(crate) fn is_block_statement(word: &str) -> bool {
keyword(word).is_some_and(|k| k.kind == Statement && k.opens_block)
}