Skip to content

Add pub and use keywords and parse them#256

Merged
KyrylR merged 1 commit intoBlockstreamResearch:dev/importsfrom
LesterEvSe:feature/parse-use-pub-keywords
Mar 27, 2026
Merged

Add pub and use keywords and parse them#256
KyrylR merged 1 commit intoBlockstreamResearch:dev/importsfrom
LesterEvSe:feature/parse-use-pub-keywords

Conversation

@LesterEvSe
Copy link
Copy Markdown
Collaborator

@LesterEvSe LesterEvSe commented Mar 26, 2026

This PR introduces the baseline architecture for dependency and import resolution. As part of the larger module system roadmap, this is a scoped implementation focused strictly on parsing use statements and visibility.

@LesterEvSe LesterEvSe requested a review from KyrylR March 26, 2026 16:18
@LesterEvSe LesterEvSe self-assigned this Mar 26, 2026
@LesterEvSe LesterEvSe requested a review from delta1 as a code owner March 26, 2026 16:18
@LesterEvSe LesterEvSe added the enhancement New feature or request label Mar 26, 2026
src/lexer.rs Outdated
pub type Spanned<T> = (T, SimpleSpan);
pub type Tokens<'src> = Vec<(Token<'src>, crate::error::Span)>;

/// # Architecture Note: Omitted Keywords
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is needed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It explains why the compiler does not need the super and crate keywords at all.

src/parse.rs Outdated
TypeAlias(TypeAlias),
/// A function.
Function(Function),
/// Use keyword to load other items
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What other items means?

src/parse.rs Outdated
Comment on lines +61 to +91
/// Definition of a declaration
#[derive(Clone, Debug)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct UseDecl {
visibility: Visibility,
path: Vec<Identifier>,
items: UseItems,
span: Span,
}

impl UseDecl {
/// Access the visibility of the use declaration.
pub fn visibility(&self) -> &Visibility {
&self.visibility
}

/// Access the visibility of the function.
pub fn path(&self) -> &Vec<Identifier> {
&self.path
}

/// Access the visibility of the function.
pub fn items(&self) -> &UseItems {
&self.items
}

/// Access the span of the use declaration.
pub fn span(&self) -> &Span {
&self.span
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add docs for self-explanatory fns

src/parse.rs Outdated
}

impl Function {
/// Access the visibility of the function.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

src/ast.rs Outdated
parse::Item::Function(function) => {
Function::analyze(function, ty, scope).map(Self::Function)
}
parse::Item::Use(_) => todo!(),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do the reject gracefully, just in case

src/parse.rs Outdated
Comment on lines 637 to 639
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visibility is doped here

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for type aliases

@LesterEvSe LesterEvSe force-pushed the feature/parse-use-pub-keywords branch from e378410 to 5bcf426 Compare March 27, 2026 10:07
@LesterEvSe LesterEvSe requested a review from KyrylR March 27, 2026 10:07
@LesterEvSe LesterEvSe force-pushed the feature/parse-use-pub-keywords branch from 5bcf426 to 4c8ee5e Compare March 27, 2026 12:09
Comment on lines +62 to +69
#[derive(Clone, Debug)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct UseDecl {
visibility: Visibility,
path: Vec<Identifier>,
items: UseItems,
span: Span,
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a rust doc explaining what is an intention for those fields?

Comment on lines +90 to +96

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum UseItems {
Single(Identifier),
List(Vec<Identifier>),
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

src/parse.rs Outdated
Comment on lines +108 to +114
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum Visibility {
Public,
#[default]
Private,
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this above UseDecl?

@LesterEvSe LesterEvSe force-pushed the feature/parse-use-pub-keywords branch 2 times, most recently from 577e0a7 to 2328407 Compare March 27, 2026 12:29
@LesterEvSe LesterEvSe requested a review from KyrylR March 27, 2026 12:29
@KyrylR
Copy link
Copy Markdown
Collaborator

KyrylR commented Mar 27, 2026

Pls add description

@KyrylR
Copy link
Copy Markdown
Collaborator

KyrylR commented Mar 27, 2026

Why lexer::is_keyword() was not updated?

@KyrylR
Copy link
Copy Markdown
Collaborator

KyrylR commented Mar 27, 2026

Why use bar; is not allowed? But with arbitrary it is allowed...

@LesterEvSe LesterEvSe force-pushed the feature/parse-use-pub-keywords branch from 2328407 to 0656bb2 Compare March 27, 2026 15:18
@LesterEvSe
Copy link
Copy Markdown
Collaborator Author

Why use bar; is not allowed? But with arbitrary it is allowed...

I have updated this code to require at least three path segments. This is necessary because we cannot load specific functionality without at least three items in the path. The first is the alias, the second is the specific file, and the third is the specific function or item being imported.

This logic enforced in the following segment of parse.rs (impl ChumskyParser for UseDecl)

let path = Identifier::parser()
    .then_ignore(just(Token::DoubleColon))
    .repeated()
    .at_least(2)
    .collect::<Vec<_>>();

@LesterEvSe LesterEvSe force-pushed the feature/parse-use-pub-keywords branch from 0656bb2 to 5fcfb76 Compare March 27, 2026 15:54
@LesterEvSe LesterEvSe force-pushed the feature/parse-use-pub-keywords branch from 5fcfb76 to 0f853eb Compare March 27, 2026 16:08
Copy link
Copy Markdown
Collaborator

@KyrylR KyrylR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 0f853eb

It is a good start, though it is not merged into master, so I won't use the merge script

@KyrylR KyrylR merged commit 0f853eb into BlockstreamResearch:dev/imports Mar 27, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants