Skip to content

Commit

Permalink
type statement parser support (#6369)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomer-StarkWare authored Sep 9, 2024
1 parent f715fb0 commit 1eb938a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/cairo-lang-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,15 @@ impl<'a> Parser<'a> {
.into(),
)
.into()),
SyntaxKind::TerminalType => Ok(StatementItem::new_green(
self.db,
self.expect_item_type_alias(
attributes,
VisibilityDefault::new_green(self.db).into(),
)
.into(),
)
.into()),
_ => match self.try_parse_expr() {
Ok(expr) => {
let optional_semicolon = if self.peek().kind == SyntaxKind::TerminalSemicolon {
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-parser/src/parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ cairo_lang_test_utils::test_file_test!(
for_: "for",
range: "range",
use_: "use",
type_alias: "type_alias",
},
test_partial_parser_tree
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! > Test statement type alias.

//! > test_runner_name
test_partial_parser_tree(expect_diagnostics: false)

//! > cairo_code
fn foo() {
type R = u8;
}

//! > top_level_kind
ItemTypeAlias

//! > ignored_kinds

//! > expected_diagnostics

//! > expected_tree
└── Top level kind: ItemTypeAlias
├── attributes (kind: AttributeList) []
├── visibility (kind: VisibilityDefault) []
├── type_kw (kind: TokenType): 'type'
├── name (kind: TokenIdentifier): 'R'
├── generic_params (kind: OptionWrappedGenericParamListEmpty) []
├── eq (kind: TokenEq): '='
├── ty (kind: ExprPath)
│ └── item #0 (kind: PathSegmentSimple)
│ └── ident (kind: TokenIdentifier): 'u8'
└── semicolon (kind: TokenSemicolon): ';'

//! > ==========================================================================

//! > Test statement type alias with attributes.

//! > test_runner_name
test_partial_parser_tree(expect_diagnostics: false)

//! > cairo_code
fn foo() {
#[attribute]
type R = u8;
}

//! > top_level_kind
ItemTypeAlias

//! > ignored_kinds

//! > expected_diagnostics

//! > expected_tree
└── Top level kind: ItemTypeAlias
├── attributes (kind: AttributeList)
│ └── child #0 (kind: Attribute)
│ ├── hash (kind: TokenHash): '#'
│ ├── lbrack (kind: TokenLBrack): '['
│ ├── attr (kind: ExprPath)
│ │ └── item #0 (kind: PathSegmentSimple)
│ │ └── ident (kind: TokenIdentifier): 'attribute'
│ ├── arguments (kind: OptionArgListParenthesizedEmpty) []
│ └── rbrack (kind: TokenRBrack): ']'
├── visibility (kind: VisibilityDefault) []
├── type_kw (kind: TokenType): 'type'
├── name (kind: TokenIdentifier): 'R'
├── generic_params (kind: OptionWrappedGenericParamListEmpty) []
├── eq (kind: TokenEq): '='
├── ty (kind: ExprPath)
│ └── item #0 (kind: PathSegmentSimple)
│ └── ident (kind: TokenIdentifier): 'u8'
└── semicolon (kind: TokenSemicolon): ';'

0 comments on commit 1eb938a

Please sign in to comment.