mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 11:52:19 +00:00
Module Params' proposal import syntax
I previously implemented the syntax in "Proposal: Module and Package Changes" [1]:
```
import [map, map2] from JsonDecode as JD
```
However, we decided [2] to use the one that appears in "Proposal: Module Params" [3]:
```
import JsonDecode as JD exposing [map, map2]
```
The new implementation also now supports comments and newlines between all the tokens.
[1] https://docs.google.com/document/d/1E_77fO-44BtoBtXoVeWyGh1xN2KRTWTu8q6i25RNNx0/edit
[2] 405410612
[3] https://docs.google.com/document/d/110MwQi7Dpo1Y69ECFXyyvDWzF4OYv1BLojIm08qDTvg/edit
This commit is contained in:
parent
5cd084b73c
commit
c56091ee3e
20 changed files with 767 additions and 204 deletions
|
@ -456,11 +456,14 @@ pub enum ValueDef<'a> {
|
|||
preceding_comment: Region,
|
||||
},
|
||||
|
||||
/// e.g. `import [Req] as Http from InternalHttp`.
|
||||
/// e.g. `import InternalHttp as Http exposing [Req]`.
|
||||
ModuleImport {
|
||||
name: Loc<crate::header::ModuleName<'a>>,
|
||||
alias: Option<Loc<crate::header::ModuleName<'a>>>,
|
||||
exposed: Collection<'a, Loc<Spaced<'a, crate::header::ExposedName<'a>>>>,
|
||||
name: Loc<Spaced<'a, crate::header::ModuleName<'a>>>,
|
||||
alias: Option<Loc<Spaced<'a, crate::header::ModuleName<'a>>>>,
|
||||
exposed: Option<(
|
||||
&'a [CommentOrNewline<'a>],
|
||||
Collection<'a, Loc<Spaced<'a, crate::header::ExposedName<'a>>>>,
|
||||
)>,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -1803,7 +1806,10 @@ impl<'a> Malformed for ValueDef<'a> {
|
|||
name,
|
||||
alias,
|
||||
exposed: _,
|
||||
} => name.value.contains_dot() || alias.map_or(false, |x| x.value.contains_dot()),
|
||||
} => {
|
||||
name.value.item().contains_dot()
|
||||
|| alias.map_or(false, |x| x.value.item().contains_dot())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -838,54 +838,53 @@ pub fn parse_single_def<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn import<'a>() -> impl Parser<'a, ValueDef<'a>, EImport> {
|
||||
map!(
|
||||
skip_first!(
|
||||
crate::parser::keyword_e(crate::keyword::IMPORT, EImport::Import),
|
||||
and!(
|
||||
crate::parser::keyword_e(crate::keyword::IMPORT, EImport::Import),
|
||||
spaces()
|
||||
),
|
||||
and!(
|
||||
optional(skip_second!(
|
||||
skip_second!(
|
||||
collection_trailing_sep_e!(
|
||||
word1(b'[', EImport::ExposedListStart),
|
||||
loc!(import_exposed_name()),
|
||||
word1(b',', EImport::ExposedListEnd),
|
||||
word1(b']', EImport::ExposedListEnd),
|
||||
Spaced::SpaceBefore
|
||||
),
|
||||
spaces()
|
||||
),
|
||||
skip_second!(
|
||||
crate::parser::keyword_e(crate::keyword::FROM, EImport::From),
|
||||
spaces()
|
||||
)
|
||||
)),
|
||||
spaces_around(loc!(map!(
|
||||
crate::module::module_name_help(EImport::ModuleName),
|
||||
Spaced::Item
|
||||
))),
|
||||
and!(
|
||||
loc!(crate::module::module_name_help(EImport::ModuleName)),
|
||||
optional(backtrackable(skip_first!(
|
||||
optional(skip_first!(
|
||||
crate::parser::keyword_e(crate::keyword::AS, EImport::As),
|
||||
spaces_around(loc!(map!(
|
||||
crate::module::module_name_help(EImport::ModuleName),
|
||||
Spaced::Item
|
||||
)))
|
||||
)),
|
||||
optional(skip_first!(
|
||||
crate::parser::keyword_e(crate::keyword::EXPOSING, EImport::Exposing),
|
||||
and!(
|
||||
spaces(),
|
||||
and!(
|
||||
crate::parser::keyword_e(crate::keyword::AS, EImport::As),
|
||||
spaces()
|
||||
collection_trailing_sep_e!(
|
||||
word1(b'[', EImport::ExposedListStart),
|
||||
loc!(import_exposed_name()),
|
||||
word1(b',', EImport::ExposedListEnd),
|
||||
word1(b']', EImport::ExposedListEnd),
|
||||
Spaced::SpaceBefore
|
||||
)
|
||||
),
|
||||
loc!(crate::module::module_name_help(EImport::Alias))
|
||||
)))
|
||||
)
|
||||
))
|
||||
)
|
||||
)
|
||||
),
|
||||
|(exposed, (name, alias)): (
|
||||
Option<Collection<'a, Loc<Spaced<'a, crate::header::ExposedName<'a>>>>>,
|
||||
_
|
||||
|(name, (alias, exposed)): (
|
||||
Loc<Spaced<'a, crate::header::ModuleName<'a>>>,
|
||||
(
|
||||
Option<Loc<Spaced<'a, crate::header::ModuleName<'a>>>>,
|
||||
Option<(
|
||||
&'a [CommentOrNewline<'a>],
|
||||
Collection<'a, Loc<Spaced<'a, crate::header::ExposedName<'a>>>>
|
||||
)>
|
||||
)
|
||||
)| {
|
||||
ValueDef::ModuleImport {
|
||||
exposed: exposed.unwrap_or_else(Collection::empty),
|
||||
name,
|
||||
alias,
|
||||
exposed,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
|
@ -12,7 +12,7 @@ pub const EXPECT_FX: &str = "expect-fx";
|
|||
pub const CRASH: &str = "crash";
|
||||
|
||||
// These keywords are valid in imports
|
||||
pub const FROM: &str = "from";
|
||||
pub const EXPOSING: &str = "exposing";
|
||||
|
||||
// These keywords are valid in types
|
||||
pub const IMPLEMENTS: &str = "implements";
|
||||
|
|
|
@ -522,13 +522,13 @@ pub enum EExpect<'a> {
|
|||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum EImport {
|
||||
Import(Position),
|
||||
ExposedListStart(Position),
|
||||
ExposedName(Position),
|
||||
ExposedListEnd(Position),
|
||||
From(Position),
|
||||
ModuleName(Position),
|
||||
As(Position),
|
||||
Alias(Position),
|
||||
Exposing(Position),
|
||||
ExposedListStart(Position),
|
||||
ExposedName(Position),
|
||||
ExposedListEnd(Position),
|
||||
Space(BadInputError, Position),
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue