mirror of
https://github.com/roc-lang/roc.git
synced 2025-11-02 22:01:20 +00:00
Extract ModuleImport/IngestedFileImport into structs for reuse
This commit is contained in:
parent
4d6e641864
commit
2d93f0c3f1
10 changed files with 917 additions and 816 deletions
|
|
@ -459,25 +459,31 @@ pub enum ValueDef<'a> {
|
|||
},
|
||||
|
||||
/// e.g. `import InternalHttp as Http exposing [Req]`.
|
||||
ModuleImport {
|
||||
before_name: &'a [CommentOrNewline<'a>],
|
||||
name: Loc<ImportedModuleName<'a>>,
|
||||
alias: Option<header::KeywordItem<'a, ImportAsKeyword, Loc<ImportAlias<'a>>>>,
|
||||
exposed: Option<
|
||||
header::KeywordItem<
|
||||
'a,
|
||||
ImportExposingKeyword,
|
||||
Collection<'a, Loc<Spaced<'a, header::ExposedName<'a>>>>,
|
||||
>,
|
||||
>,
|
||||
},
|
||||
ModuleImport(ModuleImport<'a>),
|
||||
|
||||
/// e.g. `import "path/to/my/file.txt" as myFile : Str`
|
||||
IngestedFileImport {
|
||||
before_path: &'a [CommentOrNewline<'a>],
|
||||
path: Loc<StrLiteral<'a>>,
|
||||
name: header::KeywordItem<'a, ImportAsKeyword, Loc<Spaced<'a, header::TypedIdent<'a>>>>,
|
||||
},
|
||||
IngestedFileImport(IngestedFileImport<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct ModuleImport<'a> {
|
||||
pub before_name: &'a [CommentOrNewline<'a>],
|
||||
pub name: Loc<ImportedModuleName<'a>>,
|
||||
pub alias: Option<header::KeywordItem<'a, ImportAsKeyword, Loc<ImportAlias<'a>>>>,
|
||||
pub exposed: Option<
|
||||
header::KeywordItem<
|
||||
'a,
|
||||
ImportExposingKeyword,
|
||||
Collection<'a, Loc<Spaced<'a, header::ExposedName<'a>>>>,
|
||||
>,
|
||||
>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct IngestedFileImport<'a> {
|
||||
pub before_path: &'a [CommentOrNewline<'a>],
|
||||
pub path: Loc<StrLiteral<'a>>,
|
||||
pub name: header::KeywordItem<'a, ImportAsKeyword, Loc<Spaced<'a, header::TypedIdent<'a>>>>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
|
|
@ -1848,17 +1854,17 @@ impl<'a> Malformed for ValueDef<'a> {
|
|||
condition,
|
||||
preceding_comment: _,
|
||||
} => condition.is_malformed(),
|
||||
ValueDef::ModuleImport {
|
||||
ValueDef::ModuleImport(ModuleImport {
|
||||
before_name: _,
|
||||
name: _,
|
||||
alias: _,
|
||||
exposed: _,
|
||||
} => false,
|
||||
ValueDef::IngestedFileImport {
|
||||
}) => false,
|
||||
ValueDef::IngestedFileImport(IngestedFileImport {
|
||||
before_path: _,
|
||||
path,
|
||||
name: _,
|
||||
} => path.is_malformed(),
|
||||
}) => path.is_malformed(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use crate::ast::{
|
||||
AssignedField, Collection, CommentOrNewline, Defs, Expr, ExtractSpaces, Implements,
|
||||
ImplementsAbilities, ImportAlias, ImportAsKeyword, ImportExposingKeyword, ImportedModuleName,
|
||||
Pattern, RecordBuilderField, Spaceable, Spaced, Spaces, TypeAnnotation, TypeDef, TypeHeader,
|
||||
ValueDef,
|
||||
IngestedFileImport, ModuleImport, Pattern, RecordBuilderField, Spaceable, Spaced, Spaces,
|
||||
TypeAnnotation, TypeDef, TypeHeader, ValueDef,
|
||||
};
|
||||
use crate::blankspace::{
|
||||
space0_after_e, space0_around_e_no_after_indent_check, space0_around_ee, space0_before_e,
|
||||
|
|
@ -848,12 +848,15 @@ fn import<'a>() -> impl Parser<'a, ValueDef<'a>, EImport<'a>> {
|
|||
}
|
||||
|
||||
fn import_body<'a>() -> impl Parser<'a, ValueDef<'a>, EImport<'a>> {
|
||||
record!(ValueDef::ModuleImport {
|
||||
before_name: space0_e(EImport::IndentStart),
|
||||
name: loc!(imported_module_name()),
|
||||
alias: optional(backtrackable(import_as())),
|
||||
exposed: optional(backtrackable(import_exposing()))
|
||||
})
|
||||
map!(
|
||||
record!(ModuleImport {
|
||||
before_name: space0_e(EImport::IndentStart),
|
||||
name: loc!(imported_module_name()),
|
||||
alias: optional(backtrackable(import_as())),
|
||||
exposed: optional(backtrackable(import_exposing()))
|
||||
}),
|
||||
ValueDef::ModuleImport
|
||||
)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
|
@ -922,14 +925,17 @@ fn import_exposed_name<'a>(
|
|||
|
||||
#[inline(always)]
|
||||
fn import_ingested_file_body<'a>() -> impl Parser<'a, ValueDef<'a>, EImport<'a>> {
|
||||
record!(ValueDef::IngestedFileImport {
|
||||
before_path: space0_e(EImport::IndentStart),
|
||||
path: loc!(specialize(
|
||||
|_, pos| EImport::IngestedPath(pos),
|
||||
string_literal::parse_str_literal()
|
||||
)),
|
||||
name: import_ingested_file_as(),
|
||||
})
|
||||
map!(
|
||||
record!(IngestedFileImport {
|
||||
before_path: space0_e(EImport::IndentStart),
|
||||
path: loc!(specialize(
|
||||
|_, pos| EImport::IngestedPath(pos),
|
||||
string_literal::parse_str_literal()
|
||||
)),
|
||||
name: import_ingested_file_as(),
|
||||
}),
|
||||
ValueDef::IngestedFileImport
|
||||
)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue