Skip to content

Commit

Permalink
feat(parser): update impl blocks with new syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Mar 10, 2024
1 parent 8c11c89 commit 9ae541d
Show file tree
Hide file tree
Showing 15 changed files with 649 additions and 576 deletions.
2 changes: 1 addition & 1 deletion crates/fuse-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ pub enum ConstructionField {
pub struct ImplStatement {
pub span: Span,
pub target: TypeAnnotation,
pub r#trait: Option<TypeAnnotation>,
pub r#trait: TypeAnnotation,
pub methods: Vec<ImplMethod>,
}

Expand Down
11 changes: 4 additions & 7 deletions crates/fuse-parser/src/parsers/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,10 @@ impl<'a> Parser<'a> {
// Consume the struct keyword.
self.consume();

let (target, r#trait) = {
let first_type = self.parse_type_annotation()?;
match self.consume_if(TokenKind::For) {
Some(_) => (self.parse_type_annotation()?, Some(first_type)),
None => (first_type, None),
}
};
let r#trait = self.parse_type_annotation()?;
self.consume_expect(TokenKind::For)?;
let target = self.parse_type_annotation()?;

let mut methods: Vec<ImplMethod> = Vec::new();
while !self.at(TokenKind::End) {
let modifier = self.try_parse_visibility_modifier();
Expand Down
33 changes: 33 additions & 0 deletions crates/fuse-parser/tests/cases/panic/impl-statement-01/ast.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: crates/fuse-parser/tests/cases/mod.rs
description: "impl Test\nend\n"
expression: parsed.chunk
input_file: crates/fuse-parser/tests/cases/pass/impl-statement-01/case.fuse
---
Some(Chunk(
span: Span(
start: 0,
end: 14,
),
body: Block(
statements: [
ImplStatement(ImplStatement(
span: Span(
start: 0,
end: 13,
),
target: TypeAnnotation(
identifier: Identifier(
span: Span(
start: 5,
end: 9,
),
name: Atom("Test"),
),
),
trait: None,
methods: [],
)),
],
),
))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
impl Test
end
30 changes: 30 additions & 0 deletions crates/fuse-parser/tests/cases/panic/impl-statement-01/errors.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/fuse-parser/tests/cases/mod.rs
description: "impl Test\nend\n"
expression: parsed.errors
input_file: crates/fuse-parser/tests/cases/panic/impl-statement-01/case.fuse
---
[
UnexpectedTokenKindError(
token: TokenReference(
token: Token(
span: Span(
start: 10,
end: 13,
),
kind: End,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 13,
end: 14,
),
kind: Whitespace,
),
],
),
expected: For,
),
]
65 changes: 65 additions & 0 deletions crates/fuse-parser/tests/cases/panic/impl-statement-01/tokens.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
source: crates/fuse-parser/tests/cases/mod.rs
description: "impl Test\nend\n"
expression: tokens
input_file: crates/fuse-parser/tests/cases/pass/impl-statement-01/case.fuse
---
[
TokenReference(
token: Token(
span: Span(
start: 0,
end: 4,
),
kind: Impl,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 4,
end: 5,
),
kind: Whitespace,
),
],
),
TokenReference(
token: Token(
span: Span(
start: 5,
end: 9,
),
kind: Identifier,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 9,
end: 10,
),
kind: Whitespace,
),
],
),
TokenReference(
token: Token(
span: Span(
start: 10,
end: 13,
),
kind: End,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 13,
end: 14,
),
kind: Whitespace,
),
],
),
]
20 changes: 14 additions & 6 deletions crates/fuse-parser/tests/cases/pass/impl-statement-01/ast.snap
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
---
source: crates/fuse-parser/tests/cases/mod.rs
description: "impl Test\nend\n"
description: "impl Trait for Test\nend\n"
expression: parsed.chunk
input_file: crates/fuse-parser/tests/cases/pass/impl-statement-01/case.fuse
---
Some(Chunk(
span: Span(
start: 0,
end: 14,
end: 24,
),
body: Block(
statements: [
ImplStatement(ImplStatement(
span: Span(
start: 0,
end: 13,
end: 23,
),
target: TypeAnnotation(
identifier: Identifier(
span: Span(
start: 5,
end: 9,
start: 15,
end: 19,
),
name: Atom("Test"),
),
),
trait: None,
trait: TypeAnnotation(
identifier: Identifier(
span: Span(
start: 5,
end: 10,
),
name: Atom("Trait"),
),
),
methods: [],
)),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
impl Test
impl Trait for Test
end
56 changes: 47 additions & 9 deletions crates/fuse-parser/tests/cases/pass/impl-statement-01/tokens.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
source: crates/fuse-parser/tests/cases/mod.rs
description: "impl Test\nend\n"
description: "impl Trait for Test\nend\n"
expression: tokens
input_file: crates/fuse-parser/tests/cases/pass/impl-statement-01/case.fuse
input_file: crates/fuse-parser/tests/cases/pass/impl-statement-02/case.fuse
---
[
TokenReference(
Expand All @@ -28,16 +28,16 @@ input_file: crates/fuse-parser/tests/cases/pass/impl-statement-01/case.fuse
token: Token(
span: Span(
start: 5,
end: 9,
end: 10,
),
kind: Identifier,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 9,
end: 10,
start: 10,
end: 11,
),
kind: Whitespace,
),
Expand All @@ -46,17 +46,55 @@ input_file: crates/fuse-parser/tests/cases/pass/impl-statement-01/case.fuse
TokenReference(
token: Token(
span: Span(
start: 10,
end: 13,
start: 11,
end: 14,
),
kind: For,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 14,
end: 15,
),
kind: Whitespace,
),
],
),
TokenReference(
token: Token(
span: Span(
start: 15,
end: 19,
),
kind: Identifier,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 19,
end: 20,
),
kind: Whitespace,
),
],
),
TokenReference(
token: Token(
span: Span(
start: 20,
end: 23,
),
kind: End,
),
leading_trivia: [],
trailing_trivia: [
Token(
span: Span(
start: 13,
end: 14,
start: 23,
end: 24,
),
kind: Whitespace,
),
Expand Down
Loading

0 comments on commit 9ae541d

Please sign in to comment.