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

Parsing attribute calls with generic args. #719

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions crates/parser/src/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,40 @@ fn infix_op(
par.error(span, "floats not supported");
return Err(ParseFailed);
}
Expr::Call {
func,
generic_args,
args,
} => {
let func_span = left.span + func.span;
let func = Box::new(Node::new(
Expr::Attribute {
value: Box::new(left),
attr: {
if let Expr::Name(name) = func.kind {
Node::new(name, func.span)
} else {
par.fancy_error(
"failed to parse attribute expression",
vec![Label::primary(func.span, "expected a name")],
vec![],
);
return Err(ParseFailed);
}
},
},
func_span,
));

Node::new(
Expr::Call {
func,
generic_args,
args,
},
span,
)
}
_ => {
par.fancy_error(
"failed to parse attribute expression",
Expand Down
2 changes: 2 additions & 0 deletions crates/parser/tests/cases/parse_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ macro_rules! test_parse {

test_parse! { expr_call1, expressions::parse_expr, "foo()" }
test_parse! { expr_call2, expressions::parse_expr, "foo(1,2,x:3)" }
test_parse! { expr_call3, expressions::parse_expr, "bing.foo<Bar>(x:3)" }
test_parse! { expr_call4, expressions::parse_expr, "bang.bing.foo<Bar, Baz>(26, 42)" }
test_parse! { expr_attr1, expressions::parse_expr, "foo.bar[0][y]" }
test_parse! { expr_attr2, expressions::parse_expr, "a[x].b[y](1)" }
test_parse! { expr_num1, expressions::parse_expr, "12345" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
source: crates/parser/tests/cases/parse_ast.rs
expression: "ast_string(stringify!(expr_call3), expressions::parse_expr,\n \"bing.foo<Bar>(x:3)\")"

---
Node(
kind: Call(
func: Node(
kind: Attribute(
value: Node(
kind: Name("bing"),
span: Span(
start: 0,
end: 4,
),
),
attr: Node(
kind: "foo",
span: Span(
start: 5,
end: 8,
),
),
),
span: Span(
start: 0,
end: 8,
),
),
generic_args: Some(Node(
kind: [
TypeDesc(Node(
kind: Base(
base: "Bar",
),
span: Span(
start: 9,
end: 12,
),
)),
],
span: Span(
start: 8,
end: 13,
),
)),
args: Node(
kind: [
Node(
kind: CallArg(
label: Some(Node(
kind: "x",
span: Span(
start: 14,
end: 15,
),
)),
value: Node(
kind: Num("3"),
span: Span(
start: 16,
end: 17,
),
),
),
span: Span(
start: 14,
end: 17,
),
),
],
span: Span(
start: 13,
end: 18,
),
),
),
span: Span(
start: 0,
end: 18,
),
)
116 changes: 116 additions & 0 deletions crates/parser/tests/cases/snapshots/cases__parse_ast__expr_call4.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
source: crates/parser/tests/cases/parse_ast.rs
expression: "ast_string(stringify!(expr_call4), expressions::parse_expr,\n \"bang.bing.foo<Bar, Baz>(26, 42)\")"

---
Node(
kind: Call(
func: Node(
kind: Attribute(
value: Node(
kind: Attribute(
value: Node(
kind: Name("bang"),
span: Span(
start: 0,
end: 4,
),
),
attr: Node(
kind: "bing",
span: Span(
start: 5,
end: 9,
),
),
),
span: Span(
start: 0,
end: 9,
),
),
attr: Node(
kind: "foo",
span: Span(
start: 10,
end: 13,
),
),
),
span: Span(
start: 0,
end: 13,
),
),
generic_args: Some(Node(
kind: [
TypeDesc(Node(
kind: Base(
base: "Bar",
),
span: Span(
start: 14,
end: 17,
),
)),
TypeDesc(Node(
kind: Base(
base: "Baz",
),
span: Span(
start: 19,
end: 22,
),
)),
],
span: Span(
start: 13,
end: 23,
),
)),
args: Node(
kind: [
Node(
kind: CallArg(
label: None,
value: Node(
kind: Num("26"),
span: Span(
start: 24,
end: 26,
),
),
),
span: Span(
start: 24,
end: 26,
),
),
Node(
kind: CallArg(
label: None,
value: Node(
kind: Num("42"),
span: Span(
start: 28,
end: 30,
),
),
),
span: Span(
start: 28,
end: 30,
),
),
],
span: Span(
start: 23,
end: 31,
),
),
),
span: Span(
start: 0,
end: 31,
),
)
1 change: 1 addition & 0 deletions newsfragments/719.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for parsing of attribute calls with generic arguments (e.g. `foo.bar<Baz>()`).