Skip to content

Commit

Permalink
feat: add self parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Feb 20, 2024
1 parent 55a3412 commit 45436b6
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 8 deletions.
27 changes: 27 additions & 0 deletions crates/mun_syntax/src/ast/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,33 @@ 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<Self> {
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)]
Expand Down
6 changes: 6 additions & 0 deletions crates/mun_syntax/src/grammar.ron
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Grammar(

"PARAM_LIST",
"PARAM",
"SELF_PARAM",

"STRUCT_DEF",
"TYPE_ALIAS_DEF",
Expand Down Expand Up @@ -215,6 +216,11 @@ Grammar(
"TypeAscriptionOwner"
],
),
"SelfParam": (
traits: [
"TypeAscriptionOwner",
]
),
"StructDef": (
options: ["MemoryTypeSpecifier"],
traits: [
Expand Down
6 changes: 3 additions & 3 deletions crates/mun_syntax/src/parsing/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down
24 changes: 23 additions & 1 deletion crates/mun_syntax/src/parsing/grammar/params.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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);
}

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");
Expand All @@ -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);
}
2 changes: 2 additions & 0 deletions crates/mun_syntax/src/syntax_kind/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub enum SyntaxKind {
VISIBILITY,
PARAM_LIST,
PARAM,
SELF_PARAM,
STRUCT_DEF,
TYPE_ALIAS_DEF,
MEMORY_TYPE_SPECIFIER,
Expand Down Expand Up @@ -589,6 +590,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" },
Expand Down
88 changes: 84 additions & 4 deletions crates/mun_syntax/src/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
[email protected]
[email protected] "\n "
[email protected] "fn"
Expand Down Expand Up @@ -1319,8 +1322,85 @@ fn function_calls() {
[email protected] ")"
[email protected] "\n "
[email protected] "}"
[email protected] "\n "
"#);
[email protected]
[email protected] "\n "
[email protected] "fn"
[email protected] " "
[email protected]
[email protected] "baz"
[email protected]
[email protected] "("
[email protected]
[email protected]
[email protected] "self"
[email protected] ")"
[email protected] " "
[email protected]
[email protected] "{"
[email protected] " "
[email protected] "}"
[email protected]
[email protected] "\n "
[email protected] "fn"
[email protected] " "
[email protected]
[email protected] "qux"
[email protected]
[email protected] "("
[email protected]
[email protected]
[email protected] "self"
[email protected] ","
[email protected] " "
[email protected]
[email protected]
[email protected]
[email protected] "i"
[email protected] ":"
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "number"
[email protected] ")"
[email protected] " "
[email protected]
[email protected] "{"
[email protected] " "
[email protected] "}"
[email protected]
[email protected] "\n "
[email protected] "fn"
[email protected] " "
[email protected]
[email protected] "foo"
[email protected]
[email protected] "("
[email protected]
[email protected]
[email protected] "self"
[email protected] " "
[email protected]
[email protected]
[email protected]
[email protected] "i"
[email protected] ":"
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "number"
[email protected] ")"
[email protected] " "
[email protected]
[email protected] "{"
[email protected] " "
[email protected] "}"
[email protected] " "
[email protected] "// error: expected comma"
[email protected] "\n "
error Offset(137): expected COMMA
"###);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions crates/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {

fn reformat(text: impl std::fmt::Display) -> Result<String> {
let mut rustfmt = Command::new("rustfmt")
.arg("+nightly")
//.arg("--config-path")
//.arg(project_root().join("rustfmt.toml"))
.stdin(Stdio::piped())
Expand Down

0 comments on commit 45436b6

Please sign in to comment.