Skip to content

Commit

Permalink
Allow brackets and braces in inline_macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbens-starkware committed Jun 28, 2023
1 parent d80d29b commit 5d4e832
Show file tree
Hide file tree
Showing 17 changed files with 435 additions and 79 deletions.
16 changes: 12 additions & 4 deletions crates/cairo-lang-formatter/src/node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ impl SyntaxNodeFormat for SyntaxNode {
| SyntaxKind::ExprParenthesized
| SyntaxKind::ExprPath
| SyntaxKind::ExprStructCtorCall
| SyntaxKind::ExprTuple
| SyntaxKind::ExprListParenthesized
| SyntaxKind::ExprListBraced
| SyntaxKind::ExprListBracketed
| SyntaxKind::ExprUnary => Some(2),
SyntaxKind::ElseClause => Some(3),
_ => None,
Expand All @@ -214,7 +216,9 @@ impl SyntaxNodeFormat for SyntaxNode {
| SyntaxKind::ExprParenthesized
| SyntaxKind::ExprPath
| SyntaxKind::ExprStructCtorCall
| SyntaxKind::ExprTuple
| SyntaxKind::ExprListParenthesized
| SyntaxKind::ExprListBraced
| SyntaxKind::ExprListBracketed
| SyntaxKind::ExprUnary => Some(10),
_ => None,
},
Expand All @@ -231,7 +235,9 @@ impl SyntaxNodeFormat for SyntaxNode {
| SyntaxKind::ExprParenthesized
| SyntaxKind::ExprPath
| SyntaxKind::ExprStructCtorCall
| SyntaxKind::ExprTuple
| SyntaxKind::ExprListParenthesized
| SyntaxKind::ExprListBraced
| SyntaxKind::ExprListBracketed
| SyntaxKind::ExprUnary => Some(1),
SyntaxKind::TerminalEq => Some(10),
SyntaxKind::PatternEnum | SyntaxKind::PatternTuple | SyntaxKind::PatternStruct => {
Expand All @@ -244,7 +250,9 @@ impl SyntaxNodeFormat for SyntaxNode {
SyntaxKind::ExprParenthesized
| SyntaxKind::ExprList
| SyntaxKind::ExprBlock
| SyntaxKind::ExprTuple
| SyntaxKind::ExprListParenthesized
| SyntaxKind::ExprListBraced
| SyntaxKind::ExprListBracketed
| SyntaxKind::PatternTuple
| SyntaxKind::ModuleBody
| SyntaxKind::MatchArms
Expand Down
75 changes: 68 additions & 7 deletions crates/cairo-lang-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,13 +927,63 @@ impl<'a> Parser<'a> {
ExprFunctionCall::new_green(self.db, func_name, parenthesized_args)
}

/// Assumes the current token is LParen.
/// Expected pattern: `<ArgListParenthesized>`
/// Assumes the current token is TerminalNot.
/// Expected pattern: `!<WrappedArgList>`
fn expect_macro_call(&mut self, path: ExprPathGreen) -> ExprInlineMacroGreen {
let bang = self.take::<TerminalNot>();
let macro_name = path;
let parenthesized_args = self.expect_parenthesized_argument_list();
ExprInlineMacro::new_green(self.db, macro_name, bang, parenthesized_args)
let wrapped_expr_list = self.parse_wrapped_expr_list();
ExprInlineMacro::new_green(self.db, macro_name, bang, wrapped_expr_list)
}

/// Returns a GreenId of a node with an ExprTuple|ExprListBracketed kind or None if such an
/// argument list can't be parsed.
fn parse_wrapped_expr_list(&mut self) -> WrappedExprListGreen {
let current_token = self.peek().kind;
match current_token {
SyntaxKind::TerminalLParen => self
.expect_wrapped_expr_list::<TerminalLParen, TerminalRParen, _, _>(
ExprListParenthesized::new_green,
)
.into(),
SyntaxKind::TerminalLBrack => self
.expect_wrapped_expr_list::<TerminalLBrack, TerminalRBrack, _, _>(
ExprListBracketed::new_green,
)
.into(),
SyntaxKind::TerminalLBrace => self
.expect_wrapped_expr_list::<TerminalLBrace, TerminalRBrace, _, _>(
ExprListBraced::new_green,
)
.into(),
_ => self.create_and_report_missing::<WrappedExprList>(
ParserDiagnosticKind::MissingExpression,
),
}
}

/// Assumes the current token is LTerminal.
/// Expected pattern: `[LTerminal](<expr>,)*<expr>?[RTerminal]`
/// Gets `new_green` a green id node builder for the list of the requested type, applies it to
/// the parsed list and returns the result.
fn expect_wrapped_expr_list<
LTerminal: syntax::node::Terminal,
RTerminal: syntax::node::Terminal,
ListGreen,
NewGreen: Fn(&dyn SyntaxGroup, LTerminal::Green, ExprListGreen, RTerminal::Green) -> ListGreen,
>(
&mut self,
new_green: NewGreen,
) -> ListGreen {
let l_term = self.take::<LTerminal>();
let exprs: Vec<ExprListElementOrSeparatorGreen> = self
.parse_separated_list::<Expr, TerminalComma, ExprListElementOrSeparatorGreen>(
Self::try_parse_expr,
is_of_kind!(rparen, rbrace, rbrack, block, top_level),
"expression",
);
let r_term: <RTerminal as TypedSyntaxNode>::Green = self.parse_token::<RTerminal>();
new_green(self.db, l_term, ExprList::new_green(self.db, exprs), r_term)
}

/// Assumes the current token is LParen.
Expand Down Expand Up @@ -1075,8 +1125,13 @@ impl<'a> Parser<'a> {
// We have exactly one item and no separator --> This is not a tuple.
ExprParenthesized::new_green(self.db, lparen, *expr, rparen).into()
} else {
ExprTuple::new_green(self.db, lparen, ExprList::new_green(self.db, exprs), rparen)
.into()
ExprListParenthesized::new_green(
self.db,
lparen,
ExprList::new_green(self.db, exprs),
rparen,
)
.into()
}
}

Expand All @@ -1099,7 +1154,13 @@ impl<'a> Parser<'a> {
span: TextSpan { start: self.offset, end: self.offset },
});
}
ExprTuple::new_green(self.db, lparen, ExprList::new_green(self.db, exprs), rparen).into()
ExprListParenthesized::new_green(
self.db,
lparen,
ExprList::new_green(self.db, exprs),
rparen,
)
.into()
}

/// Assumes the current token is DotDot.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn bar() -> (felt252) {
├── rparen (kind: TokenRParen): ')'
├── ret_ty (kind: ReturnTypeClause)
│ ├── arrow (kind: TokenArrow): '->'
│ └── ty (kind: ExprTuple)
│ └── ty (kind: ExprListParenthesized)
│ ├── lparen (kind: TokenLParen): '('
│ ├── expressions (kind: ExprList)
│ │ └── item #0 (kind: ExprPath)
Expand Down
9 changes: 3 additions & 6 deletions crates/cairo-lang-parser/src/parser_test_data/inline_macro
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ ExprInlineMacro
│ └── item #0 (kind: PathSegmentSimple)
│ └── ident (kind: TokenIdentifier): 'println'
├── bang (kind: TokenNot): '!'
└── arguments (kind: ArgListParenthesized)
└── arguments (kind: ExprListParenthesized)
├── lparen (kind: TokenLParen): '('
├── args (kind: ArgList)
│ └── item #0 (kind: Arg)
│ ├── modifiers (kind: ModifierList) []
│ └── arg_clause (kind: ArgClauseUnnamed)
│ └── value (kind: TokenShortString): ''foo''
├── expressions (kind: ExprList)
│ └── item #0 (kind: TokenShortString): ''foo''
└── rparen (kind: TokenRParen): ')'
7 changes: 7 additions & 0 deletions crates/cairo-lang-parser/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ macro_rules! rparen {
}
pub(crate) use rparen;

macro_rules! rbrack {
() => {
SyntaxKind::TerminalRBrack
};
}
pub(crate) use rbrack;

macro_rules! rangle {
() => {
SyntaxKind::TerminalGT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@
│ │ │ ├── name (kind: TokenIdentifier): 'member2'
│ │ │ └── type_clause (kind: TypeClause)
│ │ │ ├── colon (kind: TokenColon): ':'
│ │ │ └── ty (kind: ExprTuple)
│ │ │ └── ty (kind: ExprListParenthesized)
│ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ ├── expressions (kind: ExprList)
│ │ │ │ ├── item #0 (kind: ExprPath)
Expand All @@ -531,7 +531,7 @@
│ │ │ │ │ └── item #0 (kind: PathSegmentSimple)
│ │ │ │ │ └── ident (kind: TokenIdentifier): 'felt252'
│ │ │ │ ├── separator #1 (kind: TokenComma): ','
│ │ │ │ └── item #2 (kind: ExprTuple)
│ │ │ │ └── item #2 (kind: ExprListParenthesized)
│ │ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ │ ├── expressions (kind: ExprList) []
│ │ │ │ └── rparen (kind: TokenRParen): ')'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@
│ │ │ │ ├── token (kind: TokenColon): ':'
│ │ │ │ └── trailing_trivia (kind: Trivia)
│ │ │ │ └── child #0 (kind: TokenWhitespace).
│ │ │ └── ty (kind: ExprTuple)
│ │ │ └── ty (kind: ExprListParenthesized)
│ │ │ ├── lparen (kind: TerminalLParen)
│ │ │ │ ├── leading_trivia (kind: Trivia) []
│ │ │ │ ├── token (kind: TokenLParen): '('
Expand Down Expand Up @@ -1450,7 +1450,7 @@
│ │ │ │ │ ├── token (kind: TokenComma): ','
│ │ │ │ │ └── trailing_trivia (kind: Trivia)
│ │ │ │ │ └── child #0 (kind: TokenWhitespace).
│ │ │ │ └── item #2 (kind: ExprTuple)
│ │ │ │ └── item #2 (kind: ExprListParenthesized)
│ │ │ │ ├── lparen (kind: TerminalLParen)
│ │ │ │ │ ├── leading_trivia (kind: Trivia) []
│ │ │ │ │ ├── token (kind: TokenLParen): '('
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
│ │ │ │ │ │ │ ├── identifier (kind: TokenIdentifier): '_gg'
│ │ │ │ │ │ │ └── arg_expr (kind: StructArgExpr)
│ │ │ │ │ │ │ ├── colon (kind: TokenColon): ':'
│ │ │ │ │ │ │ └── expr (kind: ExprTuple)
│ │ │ │ │ │ │ └── expr (kind: ExprListParenthesized)
│ │ │ │ │ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ │ │ │ │ ├── expressions (kind: ExprList) []
│ │ │ │ │ │ │ └── rparen (kind: TokenRParen): ')'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
│ │ │ │ │ │ │ │ ├── token (kind: TokenColon): ':'
│ │ │ │ │ │ │ │ └── trailing_trivia (kind: Trivia)
│ │ │ │ │ │ │ │ └── child #0 (kind: TokenWhitespace).
│ │ │ │ │ │ │ └── expr (kind: ExprTuple)
│ │ │ │ │ │ │ └── expr (kind: ExprListParenthesized)
│ │ │ │ │ │ │ ├── lparen (kind: TerminalLParen)
│ │ │ │ │ │ │ │ ├── leading_trivia (kind: Trivia) []
│ │ │ │ │ │ │ │ ├── token (kind: TokenLParen): '('
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@
│ │ │ │ │ │ └── item #0 (kind: PathSegmentSimple)
│ │ │ │ │ │ └── ident (kind: TokenIdentifier): 'some_macro'
│ │ │ │ │ ├── bang (kind: TokenNot): '!'
│ │ │ │ │ └── arguments (kind: ArgListParenthesized)
│ │ │ │ │ └── arguments (kind: ExprListParenthesized)
│ │ │ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ │ │ ├── args (kind: ArgList) []
│ │ │ │ │ ├── expressions (kind: ExprList) []
│ │ │ │ │ └── rparen (kind: TokenRParen): ')'
│ │ │ │ └── semicolon (kind: TokenSemicolon): ';'
│ │ │ └── child #2 (kind: StatementExpr)
Expand Down Expand Up @@ -245,7 +245,7 @@
│ │ ├── rparen (kind: TokenRParen): ')'
│ │ ├── ret_ty (kind: ReturnTypeClause)
│ │ │ ├── arrow (kind: TokenArrow): '->'
│ │ │ └── ty (kind: ExprTuple)
│ │ │ └── ty (kind: ExprListParenthesized)
│ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ ├── expressions (kind: ExprList)
│ │ │ │ ├── item #0 (kind: ExprPath)
Expand Down Expand Up @@ -305,7 +305,7 @@
│ │ │ │ ├── item #0 (kind: MatchArm)
│ │ │ │ │ ├── pattern (kind: TokenLiteralNumber): '0'
│ │ │ │ │ ├── arrow (kind: TokenMatchArrow): '=>'
│ │ │ │ │ └── expression (kind: ExprTuple)
│ │ │ │ │ └── expression (kind: ExprListParenthesized)
│ │ │ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ │ │ ├── expressions (kind: ExprList)
│ │ │ │ │ │ ├── item #0 (kind: ExprFunctionCall)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@
│ │ │ │ │ │ ├── leading_trivia (kind: Trivia) []
│ │ │ │ │ │ ├── token (kind: TokenNot): '!'
│ │ │ │ │ │ └── trailing_trivia (kind: Trivia) []
│ │ │ │ │ └── arguments (kind: ArgListParenthesized)
│ │ │ │ │ └── arguments (kind: ExprListParenthesized)
│ │ │ │ │ ├── lparen (kind: TerminalLParen)
│ │ │ │ │ │ ├── leading_trivia (kind: Trivia) []
│ │ │ │ │ │ ├── token (kind: TokenLParen): '('
│ │ │ │ │ │ └── trailing_trivia (kind: Trivia) []
│ │ │ │ │ ├── args (kind: ArgList) []
│ │ │ │ │ ├── expressions (kind: ExprList) []
│ │ │ │ │ └── rparen (kind: TerminalRParen)
│ │ │ │ │ ├── leading_trivia (kind: Trivia) []
│ │ │ │ │ ├── token (kind: TokenRParen): ')'
Expand Down Expand Up @@ -593,7 +593,7 @@
│ │ │ │ ├── token (kind: TokenArrow): '->'
│ │ │ │ └── trailing_trivia (kind: Trivia)
│ │ │ │ └── child #0 (kind: TokenWhitespace).
│ │ │ └── ty (kind: ExprTuple)
│ │ │ └── ty (kind: ExprListParenthesized)
│ │ │ ├── lparen (kind: TerminalLParen)
│ │ │ │ ├── leading_trivia (kind: Trivia) []
│ │ │ │ ├── token (kind: TokenLParen): '('
Expand Down Expand Up @@ -737,7 +737,7 @@
│ │ │ │ │ │ ├── token (kind: TokenMatchArrow): '=>'
│ │ │ │ │ │ └── trailing_trivia (kind: Trivia)
│ │ │ │ │ │ └── child #0 (kind: TokenWhitespace).
│ │ │ │ │ └── expression (kind: ExprTuple)
│ │ │ │ │ └── expression (kind: ExprListParenthesized)
│ │ │ │ │ ├── lparen (kind: TerminalLParen)
│ │ │ │ │ │ ├── leading_trivia (kind: Trivia) []
│ │ │ │ │ │ ├── token (kind: TokenLParen): '('
Expand Down
24 changes: 12 additions & 12 deletions crates/cairo-lang-plugins/src/plugins/consteval_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,24 @@ fn extract_consteval_macro_expression(
macro_ast: &ast::ExprInlineMacro,
diagnostics: &mut Vec<PluginDiagnostic>,
) -> Option<ast::Expr> {
let args = macro_ast.arguments(db).args(db).elements(db);
if args.len() != 1 {
let wrapped_args = macro_ast.arguments(db);
let exprs = match wrapped_args {
ast::WrappedExprList::BracketedExprList(args_list) => {
args_list.expressions(db).elements(db)
}
ast::WrappedExprList::ParenthesizedExprList(args_list) => {
args_list.expressions(db).elements(db)
}
ast::WrappedExprList::BracedExprList(args_list) => args_list.expressions(db).elements(db),
};
if exprs.len() != 1 {
diagnostics.push(PluginDiagnostic {
stable_ptr: macro_ast.stable_ptr().untyped(),
message: "consteval_int macro must have a single unnamed argument.".to_string(),
});
return None;
}
match args[0].arg_clause(db) {
ast::ArgClause::Unnamed(arg) => Some(arg.value(db)),
_ => {
diagnostics.push(PluginDiagnostic {
stable_ptr: macro_ast.stable_ptr().untyped(),
message: "consteval_int macro must have a single unnamed argument.".to_string(),
});
None
}
}
Some(exprs[0].clone())
}

/// Compute the actual value of an integer expression, or fail with diagnostics.
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/expr/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ fn call_core_binary_op(

fn compute_expr_tuple_semantic(
ctx: &mut ComputationContext<'_>,
syntax: &ast::ExprTuple,
syntax: &ast::ExprListParenthesized,
) -> Maybe<Expr> {
let db = ctx.db;
let syntax_db = db.upcast();
Expand Down
Loading

0 comments on commit 5d4e832

Please sign in to comment.