Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better typed api #105

Merged
merged 33 commits into from
Jul 23, 2022
Merged

Conversation

oberblastmeister
Copy link
Contributor

Continuation of #36

src/ast.rs Outdated Show resolved Hide resolved
src/ast/nodes.rs Outdated Show resolved Hide resolved
src/ast/nodes.rs Outdated Show resolved Hide resolved
src/ast/nodes.rs Outdated Show resolved Hide resolved
src/ast/nodes.rs Outdated Show resolved Hide resolved
src/ast/nodes.rs Outdated Show resolved Hide resolved
src/ast/nodes.rs Outdated Show resolved Hide resolved
src/ast/nodes.rs Outdated Show resolved Hide resolved
src/ast/nodes.rs Outdated Show resolved Hide resolved
src/ast/nodes.rs Outdated Show resolved Hide resolved
@oberblastmeister
Copy link
Contributor Author

That was a lot of work but I think I finished.

@oberblastmeister
Copy link
Contributor Author

I'm going to remove Value, as it is replaced by LiteralKind. The library is focused on syntax and not semantics, so Value is not appropriate here. The current Value enum is also incorrect, as a value should also contain lambdas and records which are not present here. Also, it looks like you can't construct a string value, which is weird. Instead, I will add methods on the inner types of LiteralKind to return the "even more typed" types of the literals.

@oberblastmeister
Copy link
Contributor Author

@oxalica @zseri @Ma27 What are your thoughts?

examples/list-fns.rs Outdated Show resolved Hide resolved
src/ast.rs Outdated Show resolved Hide resolved
src/ast/expr_ext.rs Outdated Show resolved Hide resolved
Co-authored-by: Alain Zscheile <[email protected]>
Co-authored-by: Alain Zscheile <[email protected]>
@fogti
Copy link
Contributor

fogti commented Jul 19, 2022

Tests should be ported before this should/can be merged.

@oberblastmeister
Copy link
Contributor Author

The tests should be ported already, hence the massive diff

@oberblastmeister
Copy link
Contributor Author

Also rowan already has a pretty printer for the syntax tree, so we can remove our TextDump type. I was going to update the tests with the rowan pretty printer but the diff got too large. I will probably do that in another pr.

- Try to make the macro syntax more rust like.
- impl blocks should be written
- Implementing a trait that has all defaults should be done by hand
examples/list-fns.rs Outdated Show resolved Hide resolved
Co-authored-by: Alain Zscheile <[email protected]>
@@ -1,6 +1,20 @@
use std::{env, error::Error, fs};

use rnix::{types::*, NodeOrToken, SyntaxKind::*, SyntaxNode};
use rnix::ast::AstToken;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
use rnix::ast::AstToken;

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed.

src/ast.rs Outdated
Comment on lines 42 to 48
pub(super) fn children_tokens<N: AstNode>(parent: &N) -> impl Iterator<Item = SyntaxToken> {
parent.syntax().children_with_tokens().filter_map(SyntaxElement::into_token)
}

pub(super) fn children_tokens_u<N: AstNode>(parent: &N) -> impl Iterator<Item = SyntaxToken> {
parent.syntax().children_with_tokens().filter_map(SyntaxElement::into_token)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the intended difference between these two functions? The first one should ne ,pre typed, right? But it currently isn't?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is fixed now.

src/ast.rs Outdated Show resolved Hide resolved
src/ast.rs Outdated Show resolved Hide resolved
Comment on lines +27 to +50
pub fn from_kind(token: SyntaxKind) -> Option<Self> {
match token {
TOKEN_CONCAT => Some(BinOpKind::Concat),
TOKEN_QUESTION => Some(BinOpKind::IsSet),
TOKEN_UPDATE => Some(BinOpKind::Update),

TOKEN_ADD => Some(BinOpKind::Add),
TOKEN_SUB => Some(BinOpKind::Sub),
TOKEN_MUL => Some(BinOpKind::Mul),
TOKEN_DIV => Some(BinOpKind::Div),

TOKEN_AND => Some(BinOpKind::And),
TOKEN_EQUAL => Some(BinOpKind::Equal),
TOKEN_IMPLICATION => Some(BinOpKind::Implication),
TOKEN_LESS => Some(BinOpKind::Less),
TOKEN_LESS_OR_EQ => Some(BinOpKind::LessOrEq),
TOKEN_MORE => Some(BinOpKind::More),
TOKEN_MORE_OR_EQ => Some(BinOpKind::MoreOrEq),
TOKEN_NOT_EQUAL => Some(BinOpKind::NotEqual),
TOKEN_OR => Some(BinOpKind::Or),

_ => None,
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

imo this should be a core::convert::TryFrom impl

Copy link
Contributor

Choose a reason for hiding this comment

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

postponed.

Comment on lines +61 to +67
pub fn from_kind(kind: SyntaxKind) -> Option<Self> {
match kind {
TOKEN_INVERT => Some(UnaryOpKind::Invert),
TOKEN_SUB => Some(UnaryOpKind::Negate),
_ => None,
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

this should also be a TryFrom impl

Copy link
Contributor

Choose a reason for hiding this comment

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

postponed.

src/ast/str_util.rs Show resolved Hide resolved
src/ast/str_util.rs Show resolved Hide resolved
src/ast/str_util.rs Outdated Show resolved Hide resolved
src/ast/nodes.rs Show resolved Hide resolved
src/ast/tokens.rs Show resolved Hide resolved
@darichey
Copy link
Contributor

I think any problems with str_util.rs should be ignored in this PR, since it's just moved from the now-removed value.rs. Those can be a focus of a future PR.

@fogti
Copy link
Contributor

fogti commented Jul 22, 2022

ok

@fogti
Copy link
Contributor

fogti commented Jul 22, 2022

we do have indeed a problem with str_util.rs tho, because any changes in other PRs need to be manually ported/integrated into this one, without much assistance from git (because it doesn't recognize the file as "moved"), which is unfortunate.

examples/list-fns.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
@oberblastmeister
Copy link
Contributor Author

I wonder if we should be using TryFrom, our own trait, or just normal methods. All of our uses of TryFrom basically have type Error = () which makes it isomorphic to Option.

@fogti
Copy link
Contributor

fogti commented Jul 22, 2022

We could also try From<ExtType> for Option<OurType>, but it might not work due to orphan rules.

@oberblastmeister
Copy link
Contributor Author

I think this is basically ready. This pr is really big and I don't want it to drag on forever. It doesn't have to be perfect, as we can make followup prs. I think currently it is good enough. I want to get this in so I can merge other prs. So @oxalica @darichey @zseri final round of comments before I merge?

@oberblastmeister oberblastmeister merged commit 760780e into nix-community:master Jul 23, 2022
@Ma27
Copy link
Member

Ma27 commented Jul 23, 2022

Would you mind summarizing the changes in here, especially the breaking things? I actually wanted to review it as well, however I didn't have the time for it last week, next week perhaps.

The thing is, this may be fine implementation-wise, but to me it seems as if this change was punched through way too fast. Talking about the API changes etc. first might've been preferable.

@oberblastmeister
Copy link
Contributor Author

oberblastmeister commented Jul 23, 2022

I understand your concern, but we talked about the api a lot before merging this, hence the massive comment length. This pr was here for more than a year, and I didn't want to wait any longer, so I don't think it was too fast.

Summary

  • removes the types module and replaces it with ast
    • the ast modules has better types and sum types
    • biggest change is removes ParsedType in favor of Expr
  • removes the value module. methods on Value go on the individual types. For example, string related methods go on ast::Str
  • parse changed to Root::parse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants