1use nash_region::Located;
12use nash_source::{Exposing, Import};
13
14use crate::Parser;
15use crate::error;
16
17impl<'a> Parser<'a> {
18 pub fn import(&mut self) -> Result<&'a Import<'a>, error::Module<'a>> {
25 self.keyword_import(error::Module::ImportStart)?;
27
28 self.chomp_and_check_indent(error::Module::Space, error::Module::ImportIndentName)?;
29
30 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 self.chomp(error::Module::Space)?;
37
38 self.import_help(module_name)
40 }
41
42 fn import_help(
46 &mut self,
47 module_name: &'a Located<&'a str>,
48 ) -> Result<&'a Import<'a>, error::Module<'a>> {
49 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 self.one_of(
61 error::Module::ImportAs,
62 vec![
63 Box::new(|p: &mut Parser<'a>| p.import_as(module_name)),
65 Box::new(|p: &mut Parser<'a>| p.import_exposing(module_name, None)),
67 ],
68 )
69 }
70
71 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 self.chomp(error::Module::Space)?;
86
87 if self.col <= self.indent {
89 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 self.import_exposing(module_name, Some(alias))
99 }
100 }
101
102 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 let exposing = self.specialize(
119 |bump, err, row, col| error::Module::ImportExposingList(bump.alloc(err), row, col),
120 |p| p.exposing(),
121 )?;
122
123 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 pub(crate) fn module_name<E>(
143 &mut self,
144 to_error: impl FnOnce(usize, usize) -> E,
145 ) -> Result<&'a str, E> {
146 let (row, col) = self.position();
147 let start_pos = self.pos;
148
149 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 loop {
160 if self.peek() == Some(b'.') {
161 if let Some(next) = self.peek_at(1)
163 && next.is_ascii_uppercase()
164 {
165 self.advance(); self.advance(); self.chomp_inner_chars();
168 continue;
169 }
170 }
171 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);
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}