From b68a9522b257364f75082000cc7ce3950bb4b8e8 Mon Sep 17 00:00:00 2001 From: Wodann Date: Wed, 21 Feb 2024 13:20:39 -0600 Subject: [PATCH] feat: add self parameter (#557) --- .github/workflows/ci.yml | 31 +- crates/mun_syntax/src/ast/generated.rs | 341 ++++++++---------- crates/mun_syntax/src/ast/generated.rs.tera | 6 +- crates/mun_syntax/src/grammar.ron | 6 + crates/mun_syntax/src/parsing/grammar.rs | 6 +- .../mun_syntax/src/parsing/grammar/params.rs | 24 +- .../mun_syntax/src/syntax_kind/generated.rs | 19 +- .../src/syntax_kind/generated.rs.tera | 3 +- crates/mun_syntax/src/tests/parser.rs | 88 ++++- crates/tools/src/lib.rs | 1 + 10 files changed, 305 insertions(+), 220 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed552f91..76d383e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -82,12 +82,19 @@ jobs: - name: Install LLVM uses: ./.github/actions/install-llvm - - name: Install toolchain + - name: Install nightly rustfmt + uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + profile: minimal + components: rustfmt + + - name: Install stable toolchain uses: actions-rs/toolchain@v1 with: profile: minimal override: true - components: rustfmt, llvm-tools-preview + components: llvm-tools-preview - uses: Swatinem/rust-cache@v2 if: matrix.os != 'windows-latest' @@ -169,13 +176,19 @@ jobs: - name: Install LLVM uses: ./.github/actions/install-llvm - - name: Install toolchain + - name: Install nightly rustfmt uses: actions-rs/toolchain@v1 with: + toolchain: nightly profile: minimal - override: true components: rustfmt + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + override: true + - uses: Swatinem/rust-cache@v2 - name: Cargo build @@ -219,12 +232,18 @@ jobs: - name: Install LLVM uses: ./.github/actions/install-llvm - - name: Install toolchain + - name: Install nightly rustfmt + uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + profile: minimal + components: rustfmt + + - name: Install stable toolchain uses: actions-rs/toolchain@v1 with: profile: minimal override: true - components: rustfmt - uses: Swatinem/rust-cache@v2 diff --git a/crates/mun_syntax/src/ast/generated.rs b/crates/mun_syntax/src/ast/generated.rs index df6695b9..74ec0e48 100644 --- a/crates/mun_syntax/src/ast/generated.rs +++ b/crates/mun_syntax/src/ast/generated.rs @@ -1,8 +1,7 @@ //! Generated file, do not edit by hand, see `crate/ra_tools/src/codegen` -#![cfg_attr(rustfmt, rustfmt_skip)] -// This file is automatically generated based on the file `./generated.rs.tera` when `cargo gen-syntax` is run -// Do not edit manually +// This file is automatically generated based on the file `./generated.rs.tera` +// when `cargo gen-syntax` is run Do not edit manually //! This module contains auto-generated Rust AST. Like `SyntaxNode`s, AST nodes //! are generic over ownership: `X<'a>` things are `Copy` references, `XNode` @@ -14,8 +13,7 @@ use crate::{ ast::{self, AstNode, AstToken}, SyntaxKind::{self, *}, - SyntaxNode, - SyntaxToken + SyntaxNode, SyntaxToken, }; // ArgList @@ -27,9 +25,7 @@ pub struct ArgList { impl AstNode for ArgList { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - ARG_LIST - ) + matches!(kind, ARG_LIST) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -57,9 +53,7 @@ pub struct ArrayExpr { impl AstNode for ArrayExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - ARRAY_EXPR - ) + matches!(kind, ARRAY_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -87,9 +81,7 @@ pub struct ArrayType { impl AstNode for ArrayType { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - ARRAY_TYPE - ) + matches!(kind, ARRAY_TYPE) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -117,9 +109,7 @@ pub struct AssociatedItem { impl AstNode for AssociatedItem { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - FUNCTION_DEF - ) + matches!(kind, FUNCTION_DEF) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -145,13 +135,14 @@ impl From for AssociatedItem { impl AssociatedItem { pub fn kind(&self) -> AssociatedItemKind { match self.syntax.kind() { - FUNCTION_DEF => AssociatedItemKind::FunctionDef(FunctionDef::cast(self.syntax.clone()).unwrap()), + FUNCTION_DEF => { + AssociatedItemKind::FunctionDef(FunctionDef::cast(self.syntax.clone()).unwrap()) + } _ => unreachable!(), } } } - impl AssociatedItem {} // AssociatedItemList @@ -163,9 +154,7 @@ pub struct AssociatedItemList { impl AstNode for AssociatedItemList { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - ASSOCIATED_ITEM_LIST - ) + matches!(kind, ASSOCIATED_ITEM_LIST) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -193,9 +182,7 @@ pub struct BinExpr { impl AstNode for BinExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - BIN_EXPR - ) + matches!(kind, BIN_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -219,9 +206,7 @@ pub struct BindPat { impl AstNode for BindPat { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - BIND_PAT - ) + matches!(kind, BIND_PAT) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -250,9 +235,7 @@ pub struct BlockExpr { impl AstNode for BlockExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - BLOCK_EXPR - ) + matches!(kind, BLOCK_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -284,9 +267,7 @@ pub struct BreakExpr { impl AstNode for BreakExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - BREAK_EXPR - ) + matches!(kind, BREAK_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -314,9 +295,7 @@ pub struct CallExpr { impl AstNode for CallExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - CALL_EXPR - ) + matches!(kind, CALL_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -345,9 +324,7 @@ pub struct Condition { impl AstNode for Condition { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - CONDITION - ) + matches!(kind, CONDITION) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -379,8 +356,24 @@ pub struct Expr { impl AstNode for Expr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - LITERAL | PREFIX_EXPR | PATH_EXPR | BIN_EXPR | PAREN_EXPR | CALL_EXPR | FIELD_EXPR | IF_EXPR | LOOP_EXPR | WHILE_EXPR | RETURN_EXPR | BREAK_EXPR | BLOCK_EXPR | ARRAY_EXPR | INDEX_EXPR | RECORD_LIT + matches!( + kind, + LITERAL + | PREFIX_EXPR + | PATH_EXPR + | BIN_EXPR + | PAREN_EXPR + | CALL_EXPR + | FIELD_EXPR + | IF_EXPR + | LOOP_EXPR + | WHILE_EXPR + | RETURN_EXPR + | BREAK_EXPR + | BLOCK_EXPR + | ARRAY_EXPR + | INDEX_EXPR + | RECORD_LIT ) } fn cast(syntax: SyntaxNode) -> Option { @@ -518,7 +511,6 @@ impl Expr { } } - impl Expr {} // ExprStmt @@ -530,9 +522,7 @@ pub struct ExprStmt { impl AstNode for ExprStmt { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - EXPR_STMT - ) + matches!(kind, EXPR_STMT) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -560,9 +550,7 @@ pub struct FieldExpr { impl AstNode for FieldExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - FIELD_EXPR - ) + matches!(kind, FIELD_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -594,9 +582,7 @@ pub struct FunctionDef { impl AstNode for FunctionDef { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - FUNCTION_DEF - ) + matches!(kind, FUNCTION_DEF) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -636,9 +622,7 @@ pub struct IfExpr { impl AstNode for IfExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - IF_EXPR - ) + matches!(kind, IF_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -666,9 +650,7 @@ pub struct Impl { impl AstNode for Impl { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - IMPL - ) + matches!(kind, IMPL) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -702,9 +684,7 @@ pub struct IndexExpr { impl AstNode for IndexExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - INDEX_EXPR - ) + matches!(kind, INDEX_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -728,9 +708,7 @@ pub struct LetStmt { impl AstNode for LetStmt { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - LET_STMT - ) + matches!(kind, LET_STMT) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -763,9 +741,7 @@ pub struct Literal { impl AstNode for Literal { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - LITERAL - ) + matches!(kind, LITERAL) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -789,9 +765,7 @@ pub struct LoopExpr { impl AstNode for LoopExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - LOOP_EXPR - ) + matches!(kind, LOOP_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -816,9 +790,7 @@ pub struct MemoryTypeSpecifier { impl AstNode for MemoryTypeSpecifier { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - MEMORY_TYPE_SPECIFIER - ) + matches!(kind, MEMORY_TYPE_SPECIFIER) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -842,8 +814,9 @@ pub struct ModuleItem { impl AstNode for ModuleItem { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - USE | FUNCTION_DEF | STRUCT_DEF | TYPE_ALIAS_DEF | IMPL + matches!( + kind, + USE | FUNCTION_DEF | STRUCT_DEF | TYPE_ALIAS_DEF | IMPL ) } fn cast(syntax: SyntaxNode) -> Option { @@ -895,16 +868,19 @@ impl ModuleItem { pub fn kind(&self) -> ModuleItemKind { match self.syntax.kind() { USE => ModuleItemKind::Use(Use::cast(self.syntax.clone()).unwrap()), - FUNCTION_DEF => ModuleItemKind::FunctionDef(FunctionDef::cast(self.syntax.clone()).unwrap()), + FUNCTION_DEF => { + ModuleItemKind::FunctionDef(FunctionDef::cast(self.syntax.clone()).unwrap()) + } STRUCT_DEF => ModuleItemKind::StructDef(StructDef::cast(self.syntax.clone()).unwrap()), - TYPE_ALIAS_DEF => ModuleItemKind::TypeAliasDef(TypeAliasDef::cast(self.syntax.clone()).unwrap()), + TYPE_ALIAS_DEF => { + ModuleItemKind::TypeAliasDef(TypeAliasDef::cast(self.syntax.clone()).unwrap()) + } IMPL => ModuleItemKind::Impl(Impl::cast(self.syntax.clone()).unwrap()), _ => unreachable!(), } } } - impl ModuleItem {} // Name @@ -916,9 +892,7 @@ pub struct Name { impl AstNode for Name { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - NAME - ) + matches!(kind, NAME) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -942,9 +916,7 @@ pub struct NameRef { impl AstNode for NameRef { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - NAME_REF - ) + matches!(kind, NAME_REF) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -968,9 +940,7 @@ pub struct NeverType { impl AstNode for NeverType { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - NEVER_TYPE - ) + matches!(kind, NEVER_TYPE) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -994,9 +964,7 @@ pub struct Param { impl AstNode for Param { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - PARAM - ) + matches!(kind, PARAM) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1025,9 +993,7 @@ pub struct ParamList { impl AstNode for ParamList { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - PARAM_LIST - ) + matches!(kind, PARAM_LIST) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1055,9 +1021,7 @@ pub struct ParenExpr { impl AstNode for ParenExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - PAREN_EXPR - ) + matches!(kind, PAREN_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1085,9 +1049,7 @@ pub struct Pat { impl AstNode for Pat { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - BIND_PAT | PLACEHOLDER_PAT - ) + matches!(kind, BIND_PAT | PLACEHOLDER_PAT) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1120,13 +1082,14 @@ impl Pat { pub fn kind(&self) -> PatKind { match self.syntax.kind() { BIND_PAT => PatKind::BindPat(BindPat::cast(self.syntax.clone()).unwrap()), - PLACEHOLDER_PAT => PatKind::PlaceholderPat(PlaceholderPat::cast(self.syntax.clone()).unwrap()), + PLACEHOLDER_PAT => { + PatKind::PlaceholderPat(PlaceholderPat::cast(self.syntax.clone()).unwrap()) + } _ => unreachable!(), } } } - impl Pat {} // Path @@ -1138,9 +1101,7 @@ pub struct Path { impl AstNode for Path { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - PATH - ) + matches!(kind, PATH) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1172,9 +1133,7 @@ pub struct PathExpr { impl AstNode for PathExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - PATH_EXPR - ) + matches!(kind, PATH_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1202,9 +1161,7 @@ pub struct PathSegment { impl AstNode for PathSegment { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - PATH_SEGMENT - ) + matches!(kind, PATH_SEGMENT) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1232,9 +1189,7 @@ pub struct PathType { impl AstNode for PathType { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - PATH_TYPE - ) + matches!(kind, PATH_TYPE) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1262,9 +1217,7 @@ pub struct PlaceholderPat { impl AstNode for PlaceholderPat { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - PLACEHOLDER_PAT - ) + matches!(kind, PLACEHOLDER_PAT) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1288,9 +1241,7 @@ pub struct PrefixExpr { impl AstNode for PrefixExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - PREFIX_EXPR - ) + matches!(kind, PREFIX_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1318,9 +1269,7 @@ pub struct RecordField { impl AstNode for RecordField { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - RECORD_FIELD - ) + matches!(kind, RECORD_FIELD) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1352,9 +1301,7 @@ pub struct RecordFieldDef { impl AstNode for RecordFieldDef { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - RECORD_FIELD_DEF - ) + matches!(kind, RECORD_FIELD_DEF) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1382,9 +1329,7 @@ pub struct RecordFieldDefList { impl AstNode for RecordFieldDefList { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - RECORD_FIELD_DEF_LIST - ) + matches!(kind, RECORD_FIELD_DEF_LIST) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1412,9 +1357,7 @@ pub struct RecordFieldList { impl AstNode for RecordFieldList { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - RECORD_FIELD_LIST - ) + matches!(kind, RECORD_FIELD_LIST) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1446,9 +1389,7 @@ pub struct RecordLit { impl AstNode for RecordLit { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - RECORD_LIT - ) + matches!(kind, RECORD_LIT) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1480,9 +1421,7 @@ pub struct Rename { impl AstNode for Rename { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - RENAME - ) + matches!(kind, RENAME) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1507,9 +1446,7 @@ pub struct RetType { impl AstNode for RetType { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - RET_TYPE - ) + matches!(kind, RET_TYPE) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1537,9 +1474,7 @@ pub struct ReturnExpr { impl AstNode for ReturnExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - RETURN_EXPR - ) + matches!(kind, RETURN_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1558,6 +1493,31 @@ impl ReturnExpr { } } +// SelfParam + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct SelfParam { + pub(crate) syntax: SyntaxNode, +} + +impl AstNode for SelfParam { + fn can_cast(kind: SyntaxKind) -> bool { + matches!(kind, SELF_PARAM) + } + fn cast(syntax: SyntaxNode) -> Option { + if Self::can_cast(syntax.kind()) { + Some(SelfParam { syntax }) + } else { + None + } + } + fn syntax(&self) -> &SyntaxNode { + &self.syntax + } +} +impl ast::TypeAscriptionOwner for SelfParam {} +impl SelfParam {} + // SourceFile #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1567,9 +1527,7 @@ pub struct SourceFile { impl AstNode for SourceFile { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - SOURCE_FILE - ) + matches!(kind, SOURCE_FILE) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1595,9 +1553,7 @@ pub struct Stmt { impl AstNode for Stmt { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - LET_STMT | EXPR_STMT - ) + matches!(kind, LET_STMT | EXPR_STMT) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1636,7 +1592,6 @@ impl Stmt { } } - impl Stmt {} // StructDef @@ -1648,9 +1603,7 @@ pub struct StructDef { impl AstNode for StructDef { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - STRUCT_DEF - ) + matches!(kind, STRUCT_DEF) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1681,9 +1634,7 @@ pub struct TupleFieldDef { impl AstNode for TupleFieldDef { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - TUPLE_FIELD_DEF - ) + matches!(kind, TUPLE_FIELD_DEF) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1712,9 +1663,7 @@ pub struct TupleFieldDefList { impl AstNode for TupleFieldDefList { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - TUPLE_FIELD_DEF_LIST - ) + matches!(kind, TUPLE_FIELD_DEF_LIST) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1742,9 +1691,7 @@ pub struct TypeAliasDef { impl AstNode for TypeAliasDef { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - TYPE_ALIAS_DEF - ) + matches!(kind, TYPE_ALIAS_DEF) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1775,9 +1722,7 @@ pub struct TypeRef { impl AstNode for TypeRef { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - PATH_TYPE | ARRAY_TYPE | NEVER_TYPE - ) + matches!(kind, PATH_TYPE | ARRAY_TYPE | NEVER_TYPE) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1823,7 +1768,6 @@ impl TypeRef { } } - impl TypeRef {} // Use @@ -1835,9 +1779,7 @@ pub struct Use { impl AstNode for Use { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - USE - ) + matches!(kind, USE) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1866,9 +1808,7 @@ pub struct UseTree { impl AstNode for UseTree { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - USE_TREE - ) + matches!(kind, USE_TREE) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1904,9 +1844,7 @@ pub struct UseTreeList { impl AstNode for UseTreeList { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - USE_TREE_LIST - ) + matches!(kind, USE_TREE_LIST) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1934,9 +1872,7 @@ pub struct Visibility { impl AstNode for Visibility { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - VISIBILITY - ) + matches!(kind, VISIBILITY) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1960,9 +1896,7 @@ pub struct WhileExpr { impl AstNode for WhileExpr { fn can_cast(kind: SyntaxKind) -> bool { - matches!(kind, - WHILE_EXPR - ) + matches!(kind, WHILE_EXPR) } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { @@ -1982,7 +1916,6 @@ impl WhileExpr { } } - // IntNumber #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1995,11 +1928,19 @@ impl std::fmt::Display for IntNumber { } } impl AstToken for IntNumber { - fn can_cast(kind: SyntaxKind) -> bool { kind == INT_NUMBER } + fn can_cast(kind: SyntaxKind) -> bool { + kind == INT_NUMBER + } fn cast(syntax: SyntaxToken) -> Option { - if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None } + if Self::can_cast(syntax.kind()) { + Some(Self { syntax }) + } else { + None + } + } + fn syntax(&self) -> &SyntaxToken { + &self.syntax } - fn syntax(&self) -> &SyntaxToken { &self.syntax } } // FloatNumber @@ -2014,11 +1955,19 @@ impl std::fmt::Display for FloatNumber { } } impl AstToken for FloatNumber { - fn can_cast(kind: SyntaxKind) -> bool { kind == FLOAT_NUMBER } + fn can_cast(kind: SyntaxKind) -> bool { + kind == FLOAT_NUMBER + } fn cast(syntax: SyntaxToken) -> Option { - if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None } + if Self::can_cast(syntax.kind()) { + Some(Self { syntax }) + } else { + None + } + } + fn syntax(&self) -> &SyntaxToken { + &self.syntax } - fn syntax(&self) -> &SyntaxToken { &self.syntax } } // String @@ -2033,11 +1982,17 @@ impl std::fmt::Display for String { } } impl AstToken for String { - fn can_cast(kind: SyntaxKind) -> bool { kind == STRING } + fn can_cast(kind: SyntaxKind) -> bool { + kind == STRING + } fn cast(syntax: SyntaxToken) -> Option { - if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None } + if Self::can_cast(syntax.kind()) { + Some(Self { syntax }) + } else { + None + } + } + fn syntax(&self) -> &SyntaxToken { + &self.syntax } - fn syntax(&self) -> &SyntaxToken { &self.syntax } } - - diff --git a/crates/mun_syntax/src/ast/generated.rs.tera b/crates/mun_syntax/src/ast/generated.rs.tera index ed9c96f2..3a5f2c8e 100644 --- a/crates/mun_syntax/src/ast/generated.rs.tera +++ b/crates/mun_syntax/src/ast/generated.rs.tera @@ -1,7 +1,6 @@ {# THIS File is not automatically generated: the below applies to the result of this template -#}#![cfg_attr(rustfmt, rustfmt_skip)] -// This file is automatically generated based on the file `./generated.rs.tera` when `cargo gen-syntax` is run +#}// This file is automatically generated based on the file `./generated.rs.tera` when `cargo gen-syntax` is run // Do not edit manually //! This module contains auto-generated Rust AST. Like `SyntaxNode`s, AST nodes @@ -14,8 +13,7 @@ the below applies to the result of this template use crate::{ ast::{self, AstNode, AstToken}, SyntaxKind::{self, *}, - SyntaxNode, - SyntaxToken + SyntaxNode, SyntaxToken }; {% for node, methods in ast %}// {{ node }} diff --git a/crates/mun_syntax/src/grammar.ron b/crates/mun_syntax/src/grammar.ron index b32cafaa..a7806cd2 100644 --- a/crates/mun_syntax/src/grammar.ron +++ b/crates/mun_syntax/src/grammar.ron @@ -129,6 +129,7 @@ Grammar( "PARAM_LIST", "PARAM", + "SELF_PARAM", "STRUCT_DEF", "TYPE_ALIAS_DEF", @@ -215,6 +216,11 @@ Grammar( "TypeAscriptionOwner" ], ), + "SelfParam": ( + traits: [ + "TypeAscriptionOwner", + ] + ), "StructDef": ( options: ["MemoryTypeSpecifier"], traits: [ diff --git a/crates/mun_syntax/src/parsing/grammar.rs b/crates/mun_syntax/src/parsing/grammar.rs index 191a21fe..9dfb5d5c 100644 --- a/crates/mun_syntax/src/parsing/grammar.rs +++ b/crates/mun_syntax/src/parsing/grammar.rs @@ -17,9 +17,9 @@ use super::{ LOOP_EXPR, MEMORY_TYPE_SPECIFIER, NAME, NAME_REF, NEVER_TYPE, PARAM, PARAM_LIST, PAREN_EXPR, PATH, PATH_EXPR, PATH_SEGMENT, PATH_TYPE, PLACEHOLDER_PAT, PREFIX_EXPR, RECORD_FIELD, RECORD_FIELD_DEF, RECORD_FIELD_DEF_LIST, RECORD_FIELD_LIST, RECORD_LIT, - RENAME, RETURN_EXPR, RET_TYPE, SOURCE_FILE, STRING, STRUCT_DEF, TUPLE_FIELD_DEF, - TUPLE_FIELD_DEF_LIST, TYPE_ALIAS_DEF, USE, USE_TREE, USE_TREE_LIST, VALUE_KW, VISIBILITY, - WHILE_EXPR, + RENAME, RETURN_EXPR, RET_TYPE, SELF_PARAM, SOURCE_FILE, STRING, STRUCT_DEF, + TUPLE_FIELD_DEF, TUPLE_FIELD_DEF_LIST, TYPE_ALIAS_DEF, USE, USE_TREE, USE_TREE_LIST, + VALUE_KW, VISIBILITY, WHILE_EXPR, }, }; diff --git a/crates/mun_syntax/src/parsing/grammar/params.rs b/crates/mun_syntax/src/parsing/grammar/params.rs index 0f005e40..f59fb996 100644 --- a/crates/mun_syntax/src/parsing/grammar/params.rs +++ b/crates/mun_syntax/src/parsing/grammar/params.rs @@ -1,4 +1,4 @@ -use super::{patterns, types, Parser, TokenSet, EOF, PARAM, PARAM_LIST}; +use super::{patterns, types, Parser, TokenSet, EOF, NAME, PARAM, PARAM_LIST, SELF_PARAM}; pub(super) fn param_list(p: &mut Parser<'_>) { list(p); @@ -6,8 +6,12 @@ pub(super) fn param_list(p: &mut Parser<'_>) { fn list(p: &mut Parser<'_>) { assert!(p.at(T!['('])); + let m = p.start(); p.bump(T!['(']); + + opt_self_param(p); + while !p.at(EOF) && !p.at(T![')']) { if !p.at_ts(VALUE_PARAMETER_FIRST) { p.error("expected value parameter"); @@ -30,3 +34,21 @@ fn param(p: &mut Parser<'_>) { types::ascription(p); m.complete(p, PARAM); } + +fn opt_self_param(p: &mut Parser<'_>) { + if p.at(T![self]) { + let m = p.start(); + self_as_name(p); + m.complete(p, SELF_PARAM); + + if !p.at(T![')']) { + p.expect(T![,]); + } + } +} + +fn self_as_name(p: &mut Parser<'_>) { + let m = p.start(); + p.bump(T![self]); + m.complete(p, NAME); +} diff --git a/crates/mun_syntax/src/syntax_kind/generated.rs b/crates/mun_syntax/src/syntax_kind/generated.rs index 43a5a909..8200e59c 100644 --- a/crates/mun_syntax/src/syntax_kind/generated.rs +++ b/crates/mun_syntax/src/syntax_kind/generated.rs @@ -1,10 +1,16 @@ //! Generated file, do not edit by hand, see `crate/ra_tools/src/codegen` -#![cfg_attr(rustfmt, rustfmt_skip)] -// This file is automatically generated based on the file `./generated.rs.tera` when `cargo gen-syntax` is run -// Do not edit manually +// This file is automatically generated based on the file `./generated.rs.tera` +// when `cargo gen-syntax` is run Do not edit manually -#![allow(bad_style, missing_docs, unreachable_pub, clippy::enum_glob_use, clippy::manual_non_exhaustive, clippy::upper_case_acronyms)] +#![allow( + bad_style, + missing_docs, + unreachable_pub, + clippy::enum_glob_use, + clippy::manual_non_exhaustive, + clippy::upper_case_acronyms +)] use super::SyntaxInfo; /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. @@ -109,6 +115,7 @@ pub enum SyntaxKind { VISIBILITY, PARAM_LIST, PARAM, + SELF_PARAM, STRUCT_DEF, TYPE_ALIAS_DEF, MEMORY_TYPE_SPECIFIER, @@ -589,6 +596,7 @@ impl SyntaxKind { VISIBILITY => &SyntaxInfo { name: "VISIBILITY" }, PARAM_LIST => &SyntaxInfo { name: "PARAM_LIST" }, PARAM => &SyntaxInfo { name: "PARAM" }, + SELF_PARAM => &SyntaxInfo { name: "SELF_PARAM" }, STRUCT_DEF => &SyntaxInfo { name: "STRUCT_DEF" }, TYPE_ALIAS_DEF => &SyntaxInfo { name: "TYPE_ALIAS_DEF" }, MEMORY_TYPE_SPECIFIER => &SyntaxInfo { name: "MEMORY_TYPE_SPECIFIER" }, @@ -705,6 +713,3 @@ impl SyntaxKind { Some(tok) } } - - - diff --git a/crates/mun_syntax/src/syntax_kind/generated.rs.tera b/crates/mun_syntax/src/syntax_kind/generated.rs.tera index 3ae9ff05..21aab27e 100644 --- a/crates/mun_syntax/src/syntax_kind/generated.rs.tera +++ b/crates/mun_syntax/src/syntax_kind/generated.rs.tera @@ -1,7 +1,6 @@ {# THIS File is not automatically generated: the below applies to the result of this template -#}#![cfg_attr(rustfmt, rustfmt_skip)] -// This file is automatically generated based on the file `./generated.rs.tera` when `cargo gen-syntax` is run +#}// This file is automatically generated based on the file `./generated.rs.tera` when `cargo gen-syntax` is run // Do not edit manually #![allow(bad_style, missing_docs, unreachable_pub, clippy::enum_glob_use, clippy::manual_non_exhaustive, clippy::upper_case_acronyms)] diff --git a/crates/mun_syntax/src/tests/parser.rs b/crates/mun_syntax/src/tests/parser.rs index 5b789836..d77fb9fc 100644 --- a/crates/mun_syntax/src/tests/parser.rs +++ b/crates/mun_syntax/src/tests/parser.rs @@ -1249,9 +1249,12 @@ fn function_calls() { fn foo(i:number) { bar(i+1) } + fn baz(self) { } + fn qux(self, i:number) { } + fn foo(self i:number) { } // error: expected comma "#, - ).debug_dump(), @r#" - SOURCE_FILE@0..74 + ).debug_dump(), @r###" + SOURCE_FILE@0..181 FUNCTION_DEF@0..25 WHITESPACE@0..5 "\n " FN_KW@5..7 "fn" @@ -1319,8 +1322,85 @@ fn function_calls() { R_PAREN@62..63 ")" WHITESPACE@63..68 "\n " R_CURLY@68..69 "}" - WHITESPACE@69..74 "\n " - "#); + FUNCTION_DEF@69..90 + WHITESPACE@69..74 "\n " + FN_KW@74..76 "fn" + WHITESPACE@76..77 " " + NAME@77..80 + IDENT@77..80 "baz" + PARAM_LIST@80..86 + L_PAREN@80..81 "(" + SELF_PARAM@81..85 + NAME@81..85 + SELF_KW@81..85 "self" + R_PAREN@85..86 ")" + WHITESPACE@86..87 " " + BLOCK_EXPR@87..90 + L_CURLY@87..88 "{" + WHITESPACE@88..89 " " + R_CURLY@89..90 "}" + FUNCTION_DEF@90..121 + WHITESPACE@90..95 "\n " + FN_KW@95..97 "fn" + WHITESPACE@97..98 " " + NAME@98..101 + IDENT@98..101 "qux" + PARAM_LIST@101..117 + L_PAREN@101..102 "(" + SELF_PARAM@102..106 + NAME@102..106 + SELF_KW@102..106 "self" + COMMA@106..107 "," + WHITESPACE@107..108 " " + PARAM@108..116 + BIND_PAT@108..109 + NAME@108..109 + IDENT@108..109 "i" + COLON@109..110 ":" + PATH_TYPE@110..116 + PATH@110..116 + PATH_SEGMENT@110..116 + NAME_REF@110..116 + IDENT@110..116 "number" + R_PAREN@116..117 ")" + WHITESPACE@117..118 " " + BLOCK_EXPR@118..121 + L_CURLY@118..119 "{" + WHITESPACE@119..120 " " + R_CURLY@120..121 "}" + FUNCTION_DEF@121..151 + WHITESPACE@121..126 "\n " + FN_KW@126..128 "fn" + WHITESPACE@128..129 " " + NAME@129..132 + IDENT@129..132 "foo" + PARAM_LIST@132..147 + L_PAREN@132..133 "(" + SELF_PARAM@133..137 + NAME@133..137 + SELF_KW@133..137 "self" + WHITESPACE@137..138 " " + PARAM@138..146 + BIND_PAT@138..139 + NAME@138..139 + IDENT@138..139 "i" + COLON@139..140 ":" + PATH_TYPE@140..146 + PATH@140..146 + PATH_SEGMENT@140..146 + NAME_REF@140..146 + IDENT@140..146 "number" + R_PAREN@146..147 ")" + WHITESPACE@147..148 " " + BLOCK_EXPR@148..151 + L_CURLY@148..149 "{" + WHITESPACE@149..150 " " + R_CURLY@150..151 "}" + WHITESPACE@151..152 " " + COMMENT@152..176 "// error: expected comma" + WHITESPACE@176..181 "\n " + error Offset(137): expected COMMA + "###); } #[test] diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index cd7882f2..05c7f7f0 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -36,6 +36,7 @@ fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { fn reformat(text: impl std::fmt::Display) -> Result { let mut rustfmt = Command::new("rustfmt") + .arg("+nightly") //.arg("--config-path") //.arg(project_root().join("rustfmt.toml")) .stdin(Stdio::piped())