Skip to content

Commit

Permalink
Add missing attributes parsing/lowering
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Jan 17, 2025
1 parent a9b68f8 commit 13033c4
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 359 deletions.
2 changes: 2 additions & 0 deletions crates/hir/src/hir_def/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,7 @@ impl<'db> FieldDefListId<'db> {

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FieldDef<'db> {
pub attributes: AttrListId<'db>,
pub name: Partial<IdentId<'db>>,
pub ty: Partial<TypeId<'db>>,
pub vis: Visibility,
Expand All @@ -1106,6 +1107,7 @@ pub struct VariantDefListId<'db> {

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VariantDef<'db> {
pub attributes: AttrListId<'db>,
pub name: Partial<IdentId<'db>>,
pub kind: VariantKind<'db>,
}
Expand Down
16 changes: 14 additions & 2 deletions crates/hir/src/lower/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ impl<'db> FieldDefListId<'db> {

impl<'db> FieldDef<'db> {
fn lower_ast(ctxt: &mut FileLowerCtxt<'db>, ast: ast::RecordFieldDef) -> Self {
let attributes = AttrListId::lower_ast_opt(ctxt, ast.attr_list());
let name = IdentId::lower_token_partial(ctxt, ast.name());
let ty = TypeId::lower_ast_partial(ctxt, ast.ty());
let vis = if ast.pub_kw().is_some() {
Expand All @@ -400,7 +401,12 @@ impl<'db> FieldDef<'db> {
Visibility::Private
};

Self { name, ty, vis }
Self {
attributes,
name,
ty,
vis,
}
}
}

Expand All @@ -421,12 +427,18 @@ impl<'db> VariantDefListId<'db> {

impl<'db> VariantDef<'db> {
fn lower_ast(ctxt: &mut FileLowerCtxt<'db>, ast: ast::VariantDef) -> Self {
let attributes = AttrListId::lower_ast_opt(ctxt, ast.attr_list());
let name = IdentId::lower_token_partial(ctxt, ast.name());
let kind = match ast.kind() {
ast::VariantKind::Unit => VariantKind::Unit,
ast::VariantKind::Tuple(t) => VariantKind::Tuple(TupleTypeId::lower_ast(ctxt, t)),
ast::VariantKind::Record(r) => VariantKind::Record(FieldDefListId::lower_ast(ctxt, r)),
};
Self { name, kind }

Self {
attributes,
name,
kind,
}
}
}
13 changes: 7 additions & 6 deletions crates/parser2/src/ast/item.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rowan::ast::{support, AstNode};

use super::{ast_node, TraitRef, TupleType};
use crate::{FeLang, SyntaxKind as SK, SyntaxToken};

use rowan::ast::{support, AstNode};

ast_node! {
/// The top-level node of the AST tree.
pub struct Root,
Expand Down Expand Up @@ -334,6 +334,7 @@ ast_node! {
pub struct RecordFieldDef,
SK::RecordFieldDef,
}
impl super::AttrListOwner for RecordFieldDef {}
impl RecordFieldDef {
/// Returns the pub keyword if exists.
pub fn pub_kw(&self) -> Option<SyntaxToken> {
Expand Down Expand Up @@ -362,6 +363,7 @@ ast_node! {
pub struct VariantDef,
SK::VariantDef,
}
impl super::AttrListOwner for VariantDef {}
impl VariantDef {
/// Returns the name of the variant.
/// `Foo` in `Foo(i32, u32)`
Expand Down Expand Up @@ -458,16 +460,15 @@ pub enum ItemKind {

#[cfg(test)]
mod tests {
use wasm_bindgen_test::wasm_bindgen_test;

use super::*;
use crate::{
ast::{prelude::*, ExprKind, TypeKind},
lexer::Lexer,
parser::{ItemListScope, Parser},
};

use super::*;

use wasm_bindgen_test::wasm_bindgen_test;

fn parse_item<T>(source: &str) -> T
where
T: TryFrom<ItemKind, Error = &'static str>,
Expand Down
10 changes: 6 additions & 4 deletions crates/parser2/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::{cell::Cell, convert::Infallible, rc::Rc};

use unwrap_infallible::UnwrapInfallible;

use crate::{parser::func::FuncScope, ExpectedKind, SyntaxKind};

use super::{
attr, define_scope,
attr::{self, parse_attr_list},
define_scope,
expr::parse_expr,
func::FuncDefScope,
param::{parse_generic_params_opt, parse_where_clause_opt, TraitRefScope},
Expand All @@ -16,6 +15,7 @@ use super::{
use_tree::UseTreeScope,
ErrProof, Parser, Recovery,
};
use crate::{parser::func::FuncScope, ExpectedKind, SyntaxKind};

define_scope! {
#[doc(hidden)]
Expand Down Expand Up @@ -318,6 +318,7 @@ define_scope! { VariantDefScope, VariantDef }
impl super::Parse for VariantDefScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parse_attr_list(parser)?;
parser.bump_or_recover(SyntaxKind::Ident, "expected ident for the variant name")?;

if parser.current_kind() == Some(SyntaxKind::LParen) {
Expand Down Expand Up @@ -471,8 +472,9 @@ impl super::Parse for ConstScope {
type Error = Recovery<ErrProof>;

fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.bump_expected(SyntaxKind::ConstKw);
parse_attr_list(parser)?;

parser.bump_expected(SyntaxKind::ConstKw);
parser.set_newline_as_trivia(false);
parser.set_scope_recovery_stack(&[SyntaxKind::Ident, SyntaxKind::Colon, SyntaxKind::Eq]);

Expand Down
3 changes: 3 additions & 0 deletions crates/parser2/test_files/syntax_node/items/enums.fe
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ enum RecordVariants {
enum Option<T>
where T: Clone
{
/// Some value of type `T`
Some(T),

/// No value.
None,
}

Expand Down
Loading

0 comments on commit 13033c4

Please sign in to comment.