Skip to main content

yomi_core/syntax/
keywords.rs

1//! The table of reserved words (section 1.6 of `spec/language/01-lexical.md`). The parser, the registry, the REPL and
2//! tools look only at this table.
3
4/// The category of a reserved word.
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum KeywordKind {
7    /// Words that start declarations (`label`, `fn`, `save`, and so on). Cannot be written inside a label.
8    Declaration,
9    /// Words that start statements (`jump`, `if`, `return`, and so on).
10    Statement,
11    /// Words that appear in the middle of syntax (`var`, `in`). Never at the start of a statement.
12    Modifier,
13    /// Type names.
14    Type,
15    /// Words that make values (`true`, `false`, `Some`, `None`).
16    Value,
17}
18
19/// One reserved word.
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub struct Keyword {
22    /// The word.
23    pub word: &'static str,
24    /// The category.
25    pub kind: KeywordKind,
26    /// Whether it is the first word of syntax that opens a block with `{` at the end of the line.
27    pub opens_block: bool,
28}
29
30/// Positions where a contextual word has meaning.
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub enum ContextualPosition {
33    /// The start of an option in `choice { }`.
34    ChoiceOption,
35    /// Before the alias of an `import`.
36    ImportAlias,
37    /// The start of the path of an `import` (the namespace of standard packages).
38    ImportNamespace,
39    /// After the `.` in the statement `expr.each { name =>`.
40    EachStatement,
41}
42
43/// A contextual word. Not a reserved word; it has meaning only in certain positions.
44#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45pub struct ContextualKeyword {
46    /// The word.
47    pub word: &'static str,
48    /// The position where it has meaning.
49    pub position: ContextualPosition,
50}
51
52/// The list of contextual words. In the same order as section 1.6 of the spec.
53pub 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
82/// The list of reserved words. In the same order as section 1.6 of the spec.
83pub 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
121/// The entry for the word (`None` if not a reserved word).
122pub fn keyword(word: &str) -> Option<&'static Keyword> {
123    KEYWORDS.iter().find(|k| k.word == word)
124}
125
126/// Whether it is a reserved word.
127pub fn is_reserved(word: &str) -> bool {
128    keyword(word).is_some()
129}
130
131/// Whether it is the first word of syntax that opens a block with `{` at the end of the line.
132pub(crate) fn starts_block(word: &str) -> bool {
133    keyword(word).is_some_and(|k| k.opens_block)
134}
135
136/// Whether it is the first word of a declaration that cannot be written inside a label.
137pub(crate) fn is_declaration(word: &str) -> bool {
138    keyword(word).is_some_and(|k| k.kind == Declaration)
139}
140
141/// Whether it is the first word of a statement that opens a block (`if`, `else`, `while`, `for`, `match`, `choice`).
142pub(crate) fn is_block_statement(word: &str) -> bool {
143    keyword(word).is_some_and(|k| k.kind == Statement && k.opens_block)
144}