1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum KeywordKind {
7 Declaration,
9 Statement,
11 Modifier,
13 Type,
15 Value,
17}
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub struct Keyword {
22 pub word: &'static str,
24 pub kind: KeywordKind,
26 pub opens_block: bool,
28}
29
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub enum ContextualPosition {
33 ChoiceOption,
35 ImportAlias,
37 ImportNamespace,
39 EachStatement,
41}
42
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45pub struct ContextualKeyword {
46 pub word: &'static str,
48 pub position: ContextualPosition,
50}
51
52pub const CONTEXTUAL_KEYWORDS: &[ContextualKeyword] = &[
54 ContextualKeyword {
55 word: "once",
56 position: ContextualPosition::ChoiceOption,
57 },
58 ContextualKeyword {
59 word: "as",
60 position: ContextualPosition::ImportAlias,
61 },
62 ContextualKeyword {
63 word: "std",
64 position: ContextualPosition::ImportNamespace,
65 },
66 ContextualKeyword {
67 word: "each",
68 position: ContextualPosition::EachStatement,
69 },
70];
71
72const fn kw(word: &'static str, kind: KeywordKind, opens_block: bool) -> Keyword {
73 Keyword {
74 word,
75 kind,
76 opens_block,
77 }
78}
79
80use KeywordKind::{Declaration, Modifier, Statement, Type, Value};
81
82pub const KEYWORDS: &[Keyword] = &[
84 kw("identity", Declaration, false),
85 kw("label", Declaration, true),
86 kw("jump", Statement, false),
87 kw("call", Statement, false),
88 kw("choice", Statement, true),
89 kw("import", Declaration, false),
90 kw("end", Statement, false),
91 kw("save", Declaration, false),
92 kw("persistent", Declaration, false),
93 kw("var", Modifier, false),
94 kw("let", Statement, false),
95 kw("fn", Declaration, true),
96 kw("if", Statement, true),
97 kw("else", Statement, true),
98 kw("while", Statement, true),
99 kw("for", Statement, true),
100 kw("in", Modifier, false),
101 kw("match", Statement, true),
102 kw("break", Statement, false),
103 kw("continue", Statement, false),
104 kw("true", Value, false),
105 kw("false", Value, false),
106 kw("enum", Declaration, true),
107 kw("struct", Declaration, true),
108 kw("return", Statement, false),
109 kw("Some", Value, false),
110 kw("None", Value, false),
111 kw("int", Type, false),
112 kw("float", Type, false),
113 kw("bool", Type, false),
114 kw("str", Type, false),
115 kw("list", Type, false),
116 kw("map", Type, false),
117 kw("Option", Type, false),
118 kw("Identity", Type, false),
119];
120
121pub fn keyword(word: &str) -> Option<&'static Keyword> {
123 KEYWORDS.iter().find(|k| k.word == word)
124}
125
126pub fn is_reserved(word: &str) -> bool {
128 keyword(word).is_some()
129}
130
131pub(crate) fn starts_block(word: &str) -> bool {
133 keyword(word).is_some_and(|k| k.opens_block)
134}
135
136pub(crate) fn is_declaration(word: &str) -> bool {
138 keyword(word).is_some_and(|k| k.kind == Declaration)
139}
140
141pub(crate) fn is_block_statement(word: &str) -> bool {
143 keyword(word).is_some_and(|k| k.kind == Statement && k.opens_block)
144}