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

implemented ? #7483

Open
wants to merge 1 commit into
base: high-level-inline-macros
Choose a base branch
from
Open
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 corelib/src/test/language_features/macro_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,37 @@ fn test_add_exprs() {
assert_eq!(add_exprs!(abc 1 2), 3);
assert_eq!(add_exprs!(0 2), 2);
}

macro optional_repetitions {
($x:ident) => {
$x
};

($x:ident {$y:ident }? {$z:ident }?) => {
2
};

($x:ident {$y:ident, {$z:ident }? }?) => {
3
};

($x:ident, $y:ident?) => {
OptionTrait::unwrap_or(y, $x)
};

($x:ident, $y:ident?, $z:ident?) => {
OptionTrait::unwrap_or(z, OptionTrait::unwrap_or(y, $x))
};
}
#[test]
fn test_macro_optional_repetitions() {
let x = 1;
assert_eq!(optional_repetitions!(x), 1);
let y: Option<felt252> = Some(2);
assert_eq!(optional_repetitions!(x, y), 2);
let z: Option<felt252> = Some(3);
assert_eq!(optional_repetitions!(x, y, z), 3);
assert_eq!(optional_repetitions!(x {y, {
z
}}), 3);
}
2 changes: 1 addition & 1 deletion crates/cairo-lang-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ impl<'a> Parser<'a> {
| SyntaxKind::TerminalLBrace
| SyntaxKind::TerminalLBrack => {
let subtree = self.parse_macro_elements();
Ok(MacroMatcherwrapper::new_green(self.db, subtree).into())
Ok(MacroMatcherWrapper::new_green(self.db, subtree).into())
}
_ => {
let token = self.parse_token_tree_leaf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ macro some_macro {
│ │ │ │ │ └── ident (kind: TokenIdentifier): 'ident'
│ │ │ │ ├── child #5 (kind: TokenTreeLeaf)
│ │ │ │ │ └── leaf (kind: TokenPlus): '+'
│ │ │ │ └── child #6 (kind: MacroMatcherwrapper)
│ │ │ │ └── child #6 (kind: MacroMatcherWrapper)
│ │ │ │ └── subtree (kind: ParenthesizedMacroMatcher)
│ │ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ │ ├── elements (kind: MacroRuleElements)
Expand All @@ -93,7 +93,7 @@ macro some_macro {
│ │ │ │ │ │ └── ident (kind: TokenIdentifier): 'ident'
│ │ │ │ │ ├── child #1 (kind: TokenTreeLeaf)
│ │ │ │ │ │ └── leaf (kind: TokenPlus): '+'
│ │ │ │ │ └── child #2 (kind: MacroMatcherwrapper)
│ │ │ │ │ └── child #2 (kind: MacroMatcherWrapper)
│ │ │ │ │ └── subtree (kind: ParenthesizedMacroMatcher)
│ │ │ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ │ │ ├── elements (kind: MacroRuleElements)
Expand All @@ -118,3 +118,155 @@ macro some_macro {
│ │ └── semicolon (kind: TokenSemicolon): ';'
│ └── rbrace (kind: TokenRBrace): '}'
└── eof (kind: TokenEndOfFile).

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

//! > Test a syntax tree with optional repetitions

//! > test_runner_name
test_partial_parser_tree(expect_diagnostics: false)

//! > cairo_code
macro some_macro {
($x:ident $y:ident? $z:expr?) => {
1
};
}

fn use_macro() {
some_macro!(foo);
some_macro!(foo bar);
some_macro!(foo bar 100);
}

//! > top_level_kind

//! > ignored_kinds

//! > expected_diagnostics

//! > expected_tree
└── root (kind: SyntaxFile)
├── items (kind: ModuleItemList)
│ ├── child #0 (kind: ItemMacroDeclaration)
│ │ ├── attributes (kind: AttributeList) []
│ │ ├── visibility (kind: VisibilityDefault) []
│ │ ├── macro_kw (kind: TokenMacro): 'macro'
│ │ ├── name (kind: TokenIdentifier): 'some_macro'
│ │ ├── lbrace (kind: TokenLBrace): '{'
│ │ ├── rules (kind: MacroRulesList)
│ │ │ └── child #0 (kind: MacroRule)
│ │ │ ├── lhs (kind: ParenthesizedMacroMatcher)
│ │ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ │ ├── elements (kind: MacroRuleElements)
│ │ │ │ │ ├── child #0 (kind: MacroRuleParam)
│ │ │ │ │ │ ├── dollar (kind: TokenDollar): '$'
│ │ │ │ │ │ ├── name (kind: TokenIdentifier): 'x'
│ │ │ │ │ │ ├── colon (kind: TokenColon): ':'
│ │ │ │ │ │ └── kind (kind: ParamIdent)
│ │ │ │ │ │ └── ident (kind: TokenIdentifier): 'ident'
│ │ │ │ │ ├── child #1 (kind: MacroRuleParam)
│ │ │ │ │ │ ├── dollar (kind: TokenDollar): '$'
│ │ │ │ │ │ ├── name (kind: TokenIdentifier): 'y'
│ │ │ │ │ │ ├── colon (kind: TokenColon): ':'
│ │ │ │ │ │ └── kind (kind: ParamIdent)
│ │ │ │ │ │ └── ident (kind: TokenIdentifier): 'ident'
│ │ │ │ │ ├── child #2 (kind: TokenTreeLeaf)
│ │ │ │ │ │ └── leaf (kind: TokenQuestionMark): '?'
│ │ │ │ │ ├── child #3 (kind: MacroRuleParam)
│ │ │ │ │ │ ├── dollar (kind: TokenDollar): '$'
│ │ │ │ │ │ ├── name (kind: TokenIdentifier): 'z'
│ │ │ │ │ │ ├── colon (kind: TokenColon): ':'
│ │ │ │ │ │ └── kind (kind: ParamExpr)
│ │ │ │ │ │ └── expr (kind: TokenIdentifier): 'expr'
│ │ │ │ │ └── child #4 (kind: TokenTreeLeaf)
│ │ │ │ │ └── leaf (kind: TokenQuestionMark): '?'
│ │ │ │ └── rparen (kind: TokenRParen): ')'
│ │ │ ├── fat_arrow (kind: TokenMatchArrow): '=>'
│ │ │ ├── rhs (kind: ExprBlock)
│ │ │ │ ├── lbrace (kind: TokenLBrace): '{'
│ │ │ │ ├── statements (kind: StatementList)
│ │ │ │ │ └── child #0 (kind: StatementExpr)
│ │ │ │ │ ├── attributes (kind: AttributeList) []
│ │ │ │ │ ├── expr (kind: TokenLiteralNumber): '1'
│ │ │ │ │ └── semicolon (kind: OptionTerminalSemicolonEmpty) []
│ │ │ │ └── rbrace (kind: TokenRBrace): '}'
│ │ │ └── semicolon (kind: TokenSemicolon): ';'
│ │ └── rbrace (kind: TokenRBrace): '}'
│ └── child #1 (kind: FunctionWithBody)
│ ├── attributes (kind: AttributeList) []
│ ├── visibility (kind: VisibilityDefault) []
│ ├── declaration (kind: FunctionDeclaration)
│ │ ├── optional_const (kind: OptionTerminalConstEmpty) []
│ │ ├── function_kw (kind: TokenFunction): 'fn'
│ │ ├── name (kind: TokenIdentifier): 'use_macro'
│ │ ├── generic_params (kind: OptionWrappedGenericParamListEmpty) []
│ │ └── signature (kind: FunctionSignature)
│ │ ├── lparen (kind: TokenLParen): '('
│ │ ├── parameters (kind: ParamList) []
│ │ ├── rparen (kind: TokenRParen): ')'
│ │ ├── ret_ty (kind: OptionReturnTypeClauseEmpty) []
│ │ ├── implicits_clause (kind: OptionImplicitsClauseEmpty) []
│ │ └── optional_no_panic (kind: OptionTerminalNoPanicEmpty) []
│ └── body (kind: ExprBlock)
│ ├── lbrace (kind: TokenLBrace): '{'
│ ├── statements (kind: StatementList)
│ │ ├── child #0 (kind: StatementExpr)
│ │ │ ├── attributes (kind: AttributeList) []
│ │ │ ├── expr (kind: ExprInlineMacro)
│ │ │ │ ├── path (kind: ExprPath)
│ │ │ │ │ ├── dollar (kind: OptionTerminalDollarEmpty) []
│ │ │ │ │ └── segments (kind: ExprPathInner)
│ │ │ │ │ └── item #0 (kind: PathSegmentSimple)
│ │ │ │ │ └── ident (kind: TokenIdentifier): 'some_macro'
│ │ │ │ ├── bang (kind: TokenNot): '!'
│ │ │ │ └── arguments (kind: TokenTreeNode)
│ │ │ │ └── subtree (kind: ParenthesizedTokenTree)
│ │ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ │ ├── tokens (kind: TokenList)
│ │ │ │ │ └── child #0 (kind: TokenTreeLeaf)
│ │ │ │ │ └── leaf (kind: TokenIdentifier): 'foo'
│ │ │ │ └── rparen (kind: TokenRParen): ')'
│ │ │ └── semicolon (kind: TokenSemicolon): ';'
│ │ ├── child #1 (kind: StatementExpr)
│ │ │ ├── attributes (kind: AttributeList) []
│ │ │ ├── expr (kind: ExprInlineMacro)
│ │ │ │ ├── path (kind: ExprPath)
│ │ │ │ │ ├── dollar (kind: OptionTerminalDollarEmpty) []
│ │ │ │ │ └── segments (kind: ExprPathInner)
│ │ │ │ │ └── item #0 (kind: PathSegmentSimple)
│ │ │ │ │ └── ident (kind: TokenIdentifier): 'some_macro'
│ │ │ │ ├── bang (kind: TokenNot): '!'
│ │ │ │ └── arguments (kind: TokenTreeNode)
│ │ │ │ └── subtree (kind: ParenthesizedTokenTree)
│ │ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ │ ├── tokens (kind: TokenList)
│ │ │ │ │ ├── child #0 (kind: TokenTreeLeaf)
│ │ │ │ │ │ └── leaf (kind: TokenIdentifier): 'foo'
│ │ │ │ │ └── child #1 (kind: TokenTreeLeaf)
│ │ │ │ │ └── leaf (kind: TokenIdentifier): 'bar'
│ │ │ │ └── rparen (kind: TokenRParen): ')'
│ │ │ └── semicolon (kind: TokenSemicolon): ';'
│ │ └── child #2 (kind: StatementExpr)
│ │ ├── attributes (kind: AttributeList) []
│ │ ├── expr (kind: ExprInlineMacro)
│ │ │ ├── path (kind: ExprPath)
│ │ │ │ ├── dollar (kind: OptionTerminalDollarEmpty) []
│ │ │ │ └── segments (kind: ExprPathInner)
│ │ │ │ └── item #0 (kind: PathSegmentSimple)
│ │ │ │ └── ident (kind: TokenIdentifier): 'some_macro'
│ │ │ ├── bang (kind: TokenNot): '!'
│ │ │ └── arguments (kind: TokenTreeNode)
│ │ │ └── subtree (kind: ParenthesizedTokenTree)
│ │ │ ├── lparen (kind: TokenLParen): '('
│ │ │ ├── tokens (kind: TokenList)
│ │ │ │ ├── child #0 (kind: TokenTreeLeaf)
│ │ │ │ │ └── leaf (kind: TokenIdentifier): 'foo'
│ │ │ │ ├── child #1 (kind: TokenTreeLeaf)
│ │ │ │ │ └── leaf (kind: TokenIdentifier): 'bar'
│ │ │ │ └── child #2 (kind: TokenTreeLeaf)
│ │ │ │ └── leaf (kind: TokenLiteralNumber): '100'
│ │ │ └── rparen (kind: TokenRParen): ')'
│ │ └── semicolon (kind: TokenSemicolon): ';'
│ └── rbrace (kind: TokenRBrace): '}'
└── eof (kind: TokenEndOfFile).
50 changes: 41 additions & 9 deletions crates/cairo-lang-semantic/src/items/macro_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,25 @@ fn is_macro_rule_match_ex(
for matcher_element in matcher_elements.elements(db.upcast()) {
match matcher_element {
ast::MacroRuleElement::Token(matcher_token) => {
let input_token = input_iter.next()?;
match input_token {
ast::TokenTree::Token(token_tree_leaf) => {
if matcher_token.as_syntax_node().get_text_without_trivia(db.upcast())
!= token_tree_leaf.as_syntax_node().get_text_without_trivia(db.upcast())
{
return None;
if matcher_token.as_syntax_node().get_text_without_trivia(db.upcast()) == "?" {
continue;
}
if let Some(input_token) = input_iter.next() {
match input_token {
ast::TokenTree::Token(token_tree_leaf) => {
if matcher_token.as_syntax_node().get_text_without_trivia(db.upcast())
!= token_tree_leaf
.as_syntax_node()
.get_text_without_trivia(db.upcast())
{
return None;
}
}
ast::TokenTree::Subtree(_) => return None,
ast::TokenTree::Missing(_) => unreachable!(),
}
ast::TokenTree::Subtree(_) => return None,
ast::TokenTree::Missing(_) => unreachable!(),
} else {
return None;
}
}
ast::MacroRuleElement::Param(param) => {
Expand Down Expand Up @@ -207,8 +215,32 @@ fn is_macro_rule_match_ex(
ast::TokenTree::Missing(_) => unreachable!(),
}
}
ast::MacroRuleElement::Repetition(repetition) => {
let rep_op = repetition.operator(db.upcast());
if rep_op.as_syntax_node().get_text_without_trivia(db.upcast()) == "?" {
if let Some(next_input_token) = input_iter.next() {
let mut temp_captures = OrderedHashMap::default();
if is_macro_rule_match_ex(
db,
repetition.subtree(db.upcast()),
match next_input_token {
ast::TokenTree::Subtree(subtree) => subtree,
_ => return None,
},
&mut temp_captures,
)
.is_some()
{
input_iter.next();
captures.extend(temp_captures);
}
}
continue;
}
}
}
}

if input_iter.next().is_some() {
return None;
}
Expand Down
13 changes: 11 additions & 2 deletions crates/cairo-lang-syntax-codegen/src/cairo_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,18 @@ pub fn get_spec() -> Vec<Node> {
.add_enum(EnumBuilder::new("MacroRuleElement")
.node_with_explicit_kind("Token", "TokenTreeLeaf")
.node_with_explicit_kind("Param", "MacroRuleParam")
.node_with_explicit_kind("Subtree", "MacroMatcherwrapper")
.node_with_explicit_kind("Subtree", "MacroMatcherWrapper")
.node_with_explicit_kind("Repetition", "MacroRepetitionWrapper")
)
.add_struct(StructBuilder::new("MacroMatcherwrapper")
.add_struct(StructBuilder::new("MacroRepetitionWrapper")
.node("subtree", "MacroMatcher")
.node("operator", "MacroRepetitionOperatorWrapper")
)
.add_enum(EnumBuilder::new("MacroRepetitionOperatorWrapper")
.node_with_explicit_kind("QuestionMark", "TerminalQuestionMark")
// TODO(Dean): add Star & Plus.
)
.add_struct(StructBuilder::new("MacroMatcherWrapper")
.node("subtree", "MacroMatcher")
)
.add_enum(EnumBuilder::new("MacroMatcher")
Expand Down
Loading
Loading