Skip to main content

nash_parse/
keyword.rs

1//! Keyword handling for Nash.
2//!
3//! Ported from Elm's `Parse/Keyword.hs` and `Parse/Variable.hs`.
4
5use crate::Parser;
6
7/// Reserved words that cannot be used as variable names.
8pub const RESERVED: &[&str] = &[
9    "if",
10    "then",
11    "else",
12    "case",
13    "of",
14    "let",
15    "in",
16    "do",
17    "type",
18    "module",
19    "where",
20    "import",
21    "exposing",
22    "as",
23    "trait",
24    "impl",
25    "comptime",
26    "assert",
27    "fail",
28    "todo",
29    "trace",
30    "tests",
31    "validator",
32];
33
34/// Check if a name is a reserved keyword.
35#[inline]
36pub fn is_reserved(name: &str) -> bool {
37    RESERVED.contains(&name)
38}
39
40use crate::{Col, Row};
41
42impl<'a> Parser<'a> {
43    /// Parse the `if` keyword.
44    ///
45    /// Mirrors Elm's `Keyword.if_`.
46    pub fn keyword_if<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
47        self.keyword(b"if", to_error)
48    }
49
50    /// Parse the `then` keyword.
51    ///
52    /// Mirrors Elm's `Keyword.then_`.
53    pub fn keyword_then<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
54        self.keyword(b"then", to_error)
55    }
56
57    /// Parse the `else` keyword.
58    ///
59    /// Mirrors Elm's `Keyword.else_`.
60    pub fn keyword_else<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
61        self.keyword(b"else", to_error)
62    }
63
64    /// Parse the `case` keyword.
65    ///
66    /// Mirrors Elm's `Keyword.case_`.
67    pub fn keyword_case<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
68        self.keyword(b"case", to_error)
69    }
70
71    /// Parse the `of` keyword.
72    ///
73    /// Mirrors Elm's `Keyword.of_`.
74    pub fn keyword_of<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
75        self.keyword(b"of", to_error)
76    }
77
78    /// Parse the `let` keyword.
79    ///
80    /// Mirrors Elm's `Keyword.let_`.
81    pub fn keyword_let<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
82        self.keyword(b"let", to_error)
83    }
84
85    /// Parse the `in` keyword.
86    ///
87    /// Mirrors Elm's `Keyword.in_`.
88    pub fn keyword_in<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
89        self.keyword(b"in", to_error)
90    }
91
92    pub fn keyword_do<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
93        self.keyword(b"do", to_error)
94    }
95
96    pub fn keyword_assert<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
97        self.keyword(b"assert", to_error)
98    }
99
100    pub fn keyword_fail<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
101        self.keyword(b"fail", to_error)
102    }
103
104    pub fn keyword_todo<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
105        self.keyword(b"todo", to_error)
106    }
107
108    pub fn keyword_trace<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
109        self.keyword(b"trace", to_error)
110    }
111
112    pub fn keyword_comptime<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
113        self.keyword(b"comptime", to_error)
114    }
115
116    /// Parse the `type` keyword.
117    ///
118    /// Mirrors Elm's `Keyword.type_`.
119    pub fn keyword_type<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
120        self.keyword(b"type", to_error)
121    }
122
123    /// Parse the `alias` keyword.
124    ///
125    /// Mirrors Elm's `Keyword.alias_`.
126    pub fn keyword_alias<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
127        self.keyword(b"alias", to_error)
128    }
129
130    /// Parse the `infix` keyword.
131    ///
132    /// Mirrors Elm's `Keyword.infix_`.
133    pub fn keyword_infix<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
134        self.keyword(b"infix", to_error)
135    }
136
137    /// Parse the `left` keyword (for infix associativity).
138    ///
139    /// Mirrors Elm's `Keyword.left_`.
140    pub fn keyword_left<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
141        self.keyword(b"left", to_error)
142    }
143
144    /// Parse the `right` keyword (for infix associativity).
145    ///
146    /// Mirrors Elm's `Keyword.right_`.
147    pub fn keyword_right<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
148        self.keyword(b"right", to_error)
149    }
150
151    /// Parse the `non` keyword (for infix associativity).
152    ///
153    /// Mirrors Elm's `Keyword.non_`.
154    pub fn keyword_non<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
155        self.keyword(b"non", to_error)
156    }
157
158    /// Parse the `import` keyword.
159    ///
160    /// Mirrors Elm's `Keyword.import_`.
161    pub fn keyword_import<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
162        self.keyword(b"import", to_error)
163    }
164
165    /// Parse the `as` keyword.
166    ///
167    /// Mirrors Elm's `Keyword.as_`.
168    pub fn keyword_as<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
169        self.keyword(b"as", to_error)
170    }
171
172    /// Parse the `exposing` keyword.
173    ///
174    /// Mirrors Elm's `Keyword.exposing_`.
175    pub fn keyword_exposing<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
176        self.keyword(b"exposing", to_error)
177    }
178
179    /// Parse the `module` keyword.
180    ///
181    /// Mirrors Elm's `Keyword.module_`.
182    pub fn keyword_module<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
183        self.keyword(b"module", to_error)
184    }
185
186    pub fn keyword_trait<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
187        self.keyword(b"trait", to_error)
188    }
189
190    pub fn keyword_where<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
191        self.keyword(b"where", to_error)
192    }
193
194    pub fn keyword_impl<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
195        self.keyword(b"impl", to_error)
196    }
197
198    pub fn keyword_validator<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
199        self.keyword(b"validator", to_error)
200    }
201
202    pub fn keyword_tests<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
203        self.keyword(b"tests", to_error)
204    }
205
206    pub fn keyword_test<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
207        self.keyword(b"test", to_error)
208    }
209
210    pub fn keyword_prop<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
211        self.keyword(b"prop", to_error)
212    }
213
214    pub fn keyword_via<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
215        self.keyword(b"via", to_error)
216    }
217
218    pub fn keyword_once<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
219        self.keyword(b"once", to_error)
220    }
221
222    pub fn keyword_within<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
223        self.keyword(b"within", to_error)
224    }
225
226    pub fn keyword_cpu<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
227        self.keyword(b"cpu", to_error)
228    }
229
230    pub fn keyword_mem<E>(&mut self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
231        self.keyword(b"mem", to_error)
232    }
233
234    /// Generic keyword parser that checks bytes match and no identifier continuation follows.
235    ///
236    /// Mirrors Elm's `k2`, `k3`, `k4` etc. but generalized.
237    fn keyword<E>(&mut self, kw: &[u8], to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
238        // Check if we have enough bytes
239        if self.pos + kw.len() > self.src.len() {
240            let (row, col) = self.position();
241            return Err(to_error(row, col));
242        }
243
244        // Check if bytes match
245        if &self.src[self.pos..self.pos + kw.len()] != kw {
246            let (row, col) = self.position();
247            return Err(to_error(row, col));
248        }
249
250        // Check that no identifier continuation follows (like Elm's getInnerWidth == 0)
251        let next_pos = self.pos + kw.len();
252        if next_pos < self.src.len() {
253            let next_byte = self.src[next_pos];
254            if is_inner_char(next_byte) {
255                let (row, col) = self.position();
256                return Err(to_error(row, col));
257            }
258        }
259
260        // Success - advance position and update column
261        for _ in 0..kw.len() {
262            self.advance();
263        }
264        Ok(())
265    }
266}
267
268/// Check if a byte is a valid identifier continuation character.
269///
270/// Matches: a-z, A-Z, 0-9, _
271#[inline]
272fn is_inner_char(b: u8) -> bool {
273    b.is_ascii_alphanumeric() || b == b'_'
274}