From 037e7317ef3acf882b8805ddd47155fba795da30 Mon Sep 17 00:00:00 2001 From: TomerC-StarkWare <167065153+Tomer-StarkWare@users.noreply.github.com> Date: Thu, 29 Aug 2024 14:06:51 +0300 Subject: [PATCH] use items to parser (#6304) --- crates/cairo-lang-parser/src/parser.rs | 6 ++ crates/cairo-lang-parser/src/parser_test.rs | 1 + .../src/parser_test_data/partial_trees/use | 74 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 crates/cairo-lang-parser/src/parser_test_data/partial_trees/use diff --git a/crates/cairo-lang-parser/src/parser.rs b/crates/cairo-lang-parser/src/parser.rs index 21b67d55f08..010424b1cb8 100644 --- a/crates/cairo-lang-parser/src/parser.rs +++ b/crates/cairo-lang-parser/src/parser.rs @@ -1996,6 +1996,12 @@ impl<'a> Parser<'a> { .into(), ) .into()), + SyntaxKind::TerminalUse => Ok(StatementItem::new_green( + self.db, + self.expect_item_use(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 { diff --git a/crates/cairo-lang-parser/src/parser_test.rs b/crates/cairo-lang-parser/src/parser_test.rs index 4027223c2e2..17bf1b53a07 100644 --- a/crates/cairo-lang-parser/src/parser_test.rs +++ b/crates/cairo-lang-parser/src/parser_test.rs @@ -191,6 +191,7 @@ cairo_lang_test_utils::test_file_test!( while_: "while", for_: "for", range: "range", + use_: "use", }, test_partial_parser_tree ); diff --git a/crates/cairo-lang-parser/src/parser_test_data/partial_trees/use b/crates/cairo-lang-parser/src/parser_test_data/partial_trees/use new file mode 100644 index 00000000000..a50854a6987 --- /dev/null +++ b/crates/cairo-lang-parser/src/parser_test_data/partial_trees/use @@ -0,0 +1,74 @@ +//! > Test use simple + +//! > test_runner_name +test_partial_parser_tree(expect_diagnostics: false) + +//! > cairo_code +fn foo() { + use X::Y; +} + +//! > top_level_kind +ItemUse + +//! > ignored_kinds + +//! > expected_diagnostics + +//! > expected_tree +└── Top level kind: ItemUse + ├── attributes (kind: AttributeList) [] + ├── visibility (kind: VisibilityDefault) [] + ├── use_kw (kind: TokenUse): 'use' + ├── use_path (kind: UsePathSingle) + │ ├── ident (kind: PathSegmentSimple) + │ │ └── ident (kind: TokenIdentifier): 'X' + │ ├── colon_colon (kind: TokenColonColon): '::' + │ └── use_path (kind: UsePathLeaf) + │ ├── ident (kind: PathSegmentSimple) + │ │ └── ident (kind: TokenIdentifier): 'Y' + │ └── alias_clause (kind: OptionAliasClauseEmpty) [] + └── semicolon (kind: TokenSemicolon): ';' + +//! > ========================================================================== + +//! > Test use attributes. + +//! > test_runner_name +test_partial_parser_tree(expect_diagnostics: false) + +//! > cairo_code +fn foo() { + #[attribute] + use X::Y; +} + +//! > top_level_kind +ItemUse + +//! > ignored_kinds + +//! > expected_diagnostics + +//! > expected_tree +└── Top level kind: ItemUse + ├── 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) [] + ├── use_kw (kind: TokenUse): 'use' + ├── use_path (kind: UsePathSingle) + │ ├── ident (kind: PathSegmentSimple) + │ │ └── ident (kind: TokenIdentifier): 'X' + │ ├── colon_colon (kind: TokenColonColon): '::' + │ └── use_path (kind: UsePathLeaf) + │ ├── ident (kind: PathSegmentSimple) + │ │ └── ident (kind: TokenIdentifier): 'Y' + │ └── alias_clause (kind: OptionAliasClauseEmpty) [] + └── semicolon (kind: TokenSemicolon): ';'