Skip to main content

nash_parse/
import.rs

1//! Import statement parsing for Nash.
2//!
3//! Ported from Elm's `Parse/Module.hs` (chompImport, chompAs, chompExposing).
4//!
5//! Parses import statements like:
6//! - `import List`
7//! - `import Json.Decode as Decode`
8//! - `import Html exposing (div, span)`
9//! - `import Platform.Cmd as Cmd exposing (Cmd)`
10
11use nash_region::Located;
12use nash_source::{Exposing, Import};
13
14use crate::Parser;
15use crate::error;
16
17impl<'a> Parser<'a> {
18    /// Parse an import statement.
19    ///
20    /// Mirrors Elm's `chompImport`:
21    /// ```text
22    /// import = 'import' module_name [ 'as' alias ] [ 'exposing' exposing_list ]
23    /// ```
24    pub fn import(&mut self) -> Result<&'a Import<'a>, error::Module<'a>> {
25        // Match 'import' keyword
26        self.keyword_import(error::Module::ImportStart)?;
27
28        self.chomp_and_check_indent(error::Module::Space, error::Module::ImportIndentName)?;
29
30        // Parse module name (e.g., "Json.Decode")
31        let start = self.get_position();
32        let name = self.module_name(error::Module::ImportName)?;
33        let module_name = self.add_end(start, name);
34
35        // Chomp whitespace (not indent-checked, could be end of line)
36        self.chomp(error::Module::Space)?;
37
38        // Check what comes next: fresh line, or continuation (as/exposing)
39        self.import_help(module_name)
40    }
41
42    /// Parse the continuation of an import after the module name.
43    ///
44    /// Handles: fresh line (done), `as Alias`, or `exposing (...)`.
45    fn import_help(
46        &mut self,
47        module_name: &'a Located<&'a str>,
48    ) -> Result<&'a Import<'a>, error::Module<'a>> {
49        // Try fresh line first (import done, no alias or exposing)
50        if self.col <= self.indent {
51            let default_exposing = self.alloc(Exposing::Explicit(&[]));
52            return Ok(self.alloc(Import {
53                import: module_name,
54                alias: None,
55                exposing: default_exposing,
56            }));
57        }
58
59        // Check that we're indented past the start
60        self.one_of(
61            error::Module::ImportAs,
62            vec![
63                // `as Alias [exposing (...)]`
64                Box::new(|p: &mut Parser<'a>| p.import_as(module_name)),
65                // `exposing (...)`
66                Box::new(|p: &mut Parser<'a>| p.import_exposing(module_name, None)),
67            ],
68        )
69    }
70
71    /// Parse `as Alias` part of an import.
72    ///
73    /// Mirrors Elm's `chompAs`.
74    fn import_as(
75        &mut self,
76        module_name: &'a Located<&'a str>,
77    ) -> Result<&'a Import<'a>, error::Module<'a>> {
78        self.keyword_as(error::Module::ImportAs)?;
79
80        self.chomp_and_check_indent(error::Module::Space, error::Module::ImportIndentAlias)?;
81
82        let alias = self.upper_name(error::Module::ImportAlias)?;
83
84        // Chomp whitespace
85        self.chomp(error::Module::Space)?;
86
87        // Check for exposing or end
88        if self.col <= self.indent {
89            // Fresh line - done
90            let default_exposing = self.alloc(Exposing::Explicit(&[]));
91            Ok(self.alloc(Import {
92                import: module_name,
93                alias: Some(alias),
94                exposing: default_exposing,
95            }))
96        } else {
97            // Must be exposing
98            self.import_exposing(module_name, Some(alias))
99        }
100    }
101
102    /// Parse `exposing (...)` part of an import.
103    ///
104    /// Mirrors Elm's `chompExposing`.
105    fn import_exposing(
106        &mut self,
107        module_name: &'a Located<&'a str>,
108        alias: Option<&'a str>,
109    ) -> Result<&'a Import<'a>, error::Module<'a>> {
110        self.keyword_exposing(error::Module::ImportExposing)?;
111
112        self.chomp_and_check_indent(
113            error::Module::Space,
114            error::Module::ImportIndentExposingList,
115        )?;
116
117        // Parse the exposing list, wrapping errors
118        let exposing = self.specialize(
119            |bump, err, row, col| error::Module::ImportExposingList(bump.alloc(err), row, col),
120            |p| p.exposing(),
121        )?;
122
123        // Check for fresh line (end of import)
124        self.chomp(error::Module::Space)?;
125        if self.col > self.indent {
126            return Err(error::Module::ImportEnd(self.row, self.col));
127        }
128
129        Ok(self.alloc(Import {
130            import: module_name,
131            alias,
132            exposing: self.alloc(exposing),
133        }))
134    }
135
136    /// Parse a module name like "Json.Decode".
137    ///
138    /// Mirrors Elm's `Var.moduleName`:
139    /// ```text
140    /// module_name = upper_var { '.' upper_var }
141    /// ```
142    pub(crate) fn module_name<E>(
143        &mut self,
144        to_error: impl FnOnce(u16, u16) -> E,
145    ) -> Result<&'a str, E> {
146        let (row, col) = self.position();
147        let start_pos = self.pos;
148
149        // Must start with uppercase
150        match self.peek() {
151            Some(b) if b.is_ascii_uppercase() => {
152                self.advance();
153                self.chomp_inner_chars();
154            }
155            _ => return Err(to_error(row, col)),
156        }
157
158        // Continue with .Upper segments
159        loop {
160            if self.peek() == Some(b'.') {
161                // Check if followed by uppercase
162                if let Some(next) = self.peek_at(1)
163                    && next.is_ascii_uppercase()
164                {
165                    self.advance(); // consume '.'
166                    self.advance(); // consume first upper char
167                    self.chomp_inner_chars();
168                    continue;
169                }
170            }
171            // Not a dot or not followed by uppercase - done
172            break;
173        }
174
175        Ok(self.slice_from(start_pos))
176    }
177}
178
179#[cfg(test)]
180mod tests {
181    use super::*;
182    use bumpalo::Bump;
183    use indoc::indoc;
184
185    macro_rules! assert_import_snapshot {
186        ($input:expr) => {{
187            let input = indoc!($input);
188            let bump = Bump::new();
189            let src = bump.alloc_str(input);
190            let mut parser = Parser::new(&bump, src.as_bytes());
191            let result = parser.import();
192            match result {
193                Ok(ref import) => {
194                    insta::with_settings!({
195                        description => format!("Code:\n\n{}", input),
196                        omit_expression => true,
197                    }, {
198                        insta::assert_debug_snapshot!(import);
199                    });
200                }
201                Err(e) => {
202                    panic!("Expected successful parse, got error: {:?}", e);
203                }
204            }
205        }};
206    }
207
208    #[test]
209    fn import_simple() {
210        assert_import_snapshot!("import Foo\n");
211    }
212
213    #[test]
214    fn import_dotted() {
215        assert_import_snapshot!("import Json.Decode\n");
216    }
217
218    #[test]
219    fn import_deeply_nested() {
220        assert_import_snapshot!("import Platform.Cmd.Extra\n");
221    }
222
223    #[test]
224    fn import_with_alias() {
225        assert_import_snapshot!("import Json.Decode as Decode\n");
226    }
227
228    #[test]
229    fn import_exposing_open() {
230        assert_import_snapshot!("import List exposing (..)\n");
231    }
232
233    #[test]
234    fn import_exposing_explicit() {
235        assert_import_snapshot!("import Html exposing (div, span)\n");
236    }
237
238    #[test]
239    fn import_full() {
240        assert_import_snapshot!("import Platform.Cmd as Cmd exposing (Cmd)\n");
241    }
242
243    #[test]
244    fn import_exposing_little_type() {
245        assert_import_snapshot!("import Prelude exposing (type option(..), map)\n");
246    }
247
248    #[test]
249    fn import_exposing_types() {
250        assert_import_snapshot!("import Maybe exposing (Maybe(..))\n");
251    }
252}